Welcome in this lecture, you're going to learn how to decode the rules in a UTF eight encoded string
value manually, but why would you want to do so?
Well, by manually decoding Groomes, you can gain a better understanding of how the decoding really
works.
And sometimes it is the best way.
All right, let's get started.
Remember this, it contains multiple bathrooms, as you also learned before, it isn't easy to work
with UTF eight encoded string values.
I mean, you cannot index or slice them without knowing where their rooms start and end.
In this lecture, you're going to learn a new way to find out where the rooms in a string start and
end.
Now I'm going to declare a string variable and I'm going to assign it the Turkish translation of the
opening lines of a sci fi book, The Hitchhiker's Guide to the Galaxy.
Now I'm going to print its bite by bite to the console using a loop LACHSA.
Look at the output.
It doesn't look good.
Right here it looks fine, but when I printed, it becomes messy.
As you know, this is because some rooms are sponte into multiple bytes.
So you cannot print the string value that contains multiple bedrooms by Drak looping over its bytes.
You need to print the rooms instead of the bytes.
They want to print the room using bytes.
It is called decoding.
So first I'm going to put the next five into a room variable LACHSA.
As you can see, it doesn't work yet it still prints gibberish unnecessarily, making a bad value bigger
by converting it to a room.
Remember, the room type is for whites.
So it's clear that converting a buyer to a room doesn't magically solve decoding problem.
So why should I do?
Well, as I said, I need to find out where everyone starts and ends.
So how can I do that?
Actually, there is a function that can do this.
Let me show it to you.
As you can see, it searches for the first room and returns it, I'll talk about the second result,
value the size later.
By the way, the court room accepts a bit less.
If I had a bite slice, I would be using this function.
But I have a street value instead, so I'm going to use it.
Sister, the court room in shrink.
As you can see, instead of a bite slice, it accepts a street value, there are almost the same.
Always remember this in gold standard library.
Then you see a function that expects a wide slice.
There may be another function that expects a string value as a.
OK, let me run it.
What's going on here?
It prints the same character over and over again, but why the problem happens because I'm giving the
court room in street function always the same street value.
So it looks to the same string.
And all this finds the first rule again and again.
For example, here, if I had given the function this string value, it would always return me the first
room when the court room in stream function finds the room.
The next time I call it, I should give it the next part of the string like so so that it will be able
to find the second rule instead.
It's because now the first room is the second room of the original string.
So it goes like this.
Until the spring Antz.
So how can I know which part of the string that I should give to the court room in string function?
Remember, the court ruling in string also returns the size of the first room.
So it practically returns where the first rule ends like so.
So the next bite is where the next room starts, right.
So by using the size of the first room, you can easily jump to the next room one by one.
OK, first, I'm going to save the size of the first room, then I'm going to jump to the next room
like so I also need to slice the shrink Lexar so that it will return the remaining part of the string.
OK, there are still problems, but we are getting close.
It looks like it jumps too fast because I jump twice here and here.
So I need to remove this part because the size parameter already tells me where the next room starts.
Perfect.
Now it works.
Let's take a look at this card for the last time is because now I'm going to remove almost everything
from this card.
This because you all learned how the room decoding works behind the scenes as your witness.
It's tedious to the court rooms manually.
So it's time to switch to the automatic mode.
So Gore offers us the full range loop as a way for decoding the rules automatically.
Let me convert this into a full range loop.
Perfect.
So as you learned before, for range loops automatically decode the rules in a string value.
However, sometimes decoding manual, it may still be useful.
Let's take a look at another example here.
I have a Turkish word in a watchlist.
Let me print the bytes in Hex.
Let's say you want to make the first one uppercase, how can you do that?
The first two words belong to the first room, but normally you cannot know that without analyzing the
words.
So first you need to find the bite size of the first room.
You can do that by using a four arranged loop like so.
However, this loop returns the bytes, not the rooms, but you need to get the rooms.
So first you need to convert the words to a string like so.
So the range loop will return the rooms.
However, remember that converting a slice to a string is a costly operation.
So this is not efficient for now.
Let's keep it as it is.
OK, next you need to get the size of the first room like so here I'm getting the index of the second
room.
Then I quit in a vault, encoded you to have a drink, value the second room.
She'll start from where the first rule.
And so by using this knowledge here, I find the bite size of the first room.
Lastly, I'm going to convert the first room to uppercase by using the bytes packages to upper function.
Now I'm going to copy our the first room.
I also need to convert the words last was to printed.
As you can see, the first leather is in upper case, and you can see that only the first two bites
of the world's last has changed because only the first two bodies belong to the first group.
However, using a full range loopier is kind of cumbersome and not efficient.
Fortunately, you can do the same thing using the UTF aid package more efficiently.
I just need to call the decoder room function to get the size of the first room like so now I can comment
out the for loop eyeball.
As you can see, I don't need to convert the bytes to a string value anymore.
This code is the same as the above for loop, so it is efficient and it contains lescott cool.
As you can see, sometimes it is way easier to use the court room function than a full range loop.
You can find more functions like the room function in the UTF eight and Unicode packages.
Please investigate the.
All right, that's all for now, seeing the next picture by.