For any UTF8 string, we have four "lengths":
  1. the length in codepoints:
    perl -C63 -MDevel::Peek -Mutf8 -le '$_="(\x{5fcd} Guimarăes)"; Dump($_ +); print length($_); print' SV = PV(0x8154b00) at 0x8153bd4 REFCNT = 1 FLAGS = (POK,pPOK,UTF8) PV = 0x8170460 "(\345\277\215 Guimar\303\243es)"\0 [UTF8 "(\x{5fcd} +Guimar\x{e3}es)"] CUR = 16 LEN = 20 13
    (忍 Guimarăes)
  2. the length in graphemes (the "a" is one, the composing "~" is another):
    perl -C63 -MDevel::Peek -Mutf8 -MUnicode::Normalize -le '$_="(\x{5fcd} + Guimarăes)"; $_ = NFD $_; Dump($_); print length($_); print' SV = PV(0x8154b00) at 0x8153bd4 REFCNT = 1 FLAGS = (POK,pPOK,UTF8) PV = 0x816feb8 "(\345\277\215 Guimara\314\203es)"\0 [UTF8 "(\x{5fcd} + Guimara\x{303}es)"] CUR = 17 LEN = 20 14
    (忍 Guimarães)
  3. the length in columns of text used (the string has one wide character):
    perl -C63 -MDevel::Peek -Mutf8 -mText::CharWidth=mbswidth -le '$_="(\x +{5fcd} Guimarăes)"; Dump($_); print mbswidth($_); print' SV = PV(0x8154b00) at 0x8153bd4 REFCNT = 1 FLAGS = (POK,pPOK,UTF8) PV = 0x8170460 "(\345\277\215 Guimar\303\243es)"\0 [UTF8 "(\x{5fcd} +Guimar\x{e3}es)"] CUR = 16 LEN = 20 14
    (忍 Guimarăes)
  4. the length in bytes of the string (notice I didn't print the string after the encode):
    perl -C63 -MDevel::Peek -Mutf8 -mEncode=encode_utf8 -le '$_="(\x{5fcd} + Guimarăes)"; Dump($_); print length(encode_utf8 $_)' SV = PV(0x8154b00) at 0x8153bd4 REFCNT = 1 FLAGS = (POK,pPOK,UTF8) PV = 0x8170460 "(\345\277\215 Guimar\303\243es)"\0 [UTF8 "(\x{5fcd} +Guimar\x{e3}es)"] CUR = 16 LEN = 20 16
[]s, HTH, Massa (κς,πμ,πλ)

In reply to Re: The “real length" of UTF8 strings by massa
in thread The “real length" of UTF8 strings by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.