Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re^3: length() miscounting UTF8 characters?

by Jim (Curate)
on Apr 28, 2014 at 18:17 UTC ( [id://1084159]=note: print w/replies, xml ) Need Help??


in reply to Re^2: length() miscounting UTF8 characters?
in thread length() miscounting UTF8 characters?

In what sense is using length to count Unicode characters a bug waiting to happen, though?

It's a "bug waiting to happen" when you try to make meaningful inferences about Unicode text by computing the size in bytes of the text in a specific Unicode character encoding scheme (e.g., UTF-8). This is what another monk was hinting at doing earlier in this thread when he or she suggested "dividing by two." That's a bug waiting to happen.

In general, when dealing with Unicode text, you're much more likely to need to know the numbers of code points in a string, or the numbers of graphemes in it ("extended grapheme clusters" in Unicode standardese). However, there are situations in which you might need to know the length in bytes of a Unicode string in some specific encoding. An example of this is needing to store character data in a database column with a capacity measured in bytes rather than in Unicode code points or graphemes. If you have a character data type column with a capacity of, say, 255 bytes, then the number of UTF-8 encoded Chinese characters you can insert into the column is likely going to be a lot fewer than the number of UTF-8 encoded Latin characters you can insert into the same column. In this case, knowing the size of the string in code points or graphemes won't help you answer the question "Will it fit?" You need the size in bytes.

Replies are listed 'Best First'.
Re^4: length() miscounting UTF8 characters?
by AppleFritter (Vicar) on Apr 28, 2014 at 19:38 UTC
    OK, thanks again for the detailed reply! That really clears things up.
Re^4: length() miscounting UTF8 characters?
by wjw (Priest) on Apr 28, 2014 at 19:37 UTC
    Thanks for explaining that /2 issue! I had no idea.

    This thread has been most enlightening.

    ...the majority is always wrong, and always the last to know about it...
    Insanity: Doing the same thing over and over again and expecting different results...

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1084159]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (5)
As of 2024-03-28 16:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found