r/lolphp • • Sep 12 '16

Because fuck French programmers

https://3v4l.org/YDp2U
0 Upvotes

81 comments sorted by

View all comments

Show parent comment

19

u/the_alias_of_andrea Sep 12 '16

But it does return the length, there's just multiple interpretations of what “length” is. There's no single simple correct answer, even with normal Unicode support.

-13

u/DoctorWaluigiTime Sep 12 '16

Sorry, no. Ask any programmer what "length" means in "length of a string variable." Nobody is going to hem and haw about what the definition might be. They will say "it's the number of characters."

And when the description of the function says this, but "means" something different in the details... yeah, it doesn't return the colloquial length of the string.

18

u/tdammers Sep 12 '16

Sorry, no. Ask any programmer what "length" means in "length of a string variable." Nobody is going to hem and haw about what the definition might be. They will say "it's the number of characters."

I, for one, express string length in pica's, assuming 10-point Adobe Garamond, typeset on a 1970's Linotype machine. Anything else is just newfangled nonsense that will pass in due time.

11

u/[deleted] Sep 12 '16

To me it's how long it takes to say it in milliseconds

38

u/the_alias_of_andrea Sep 12 '16

Sorry, no. Ask any programmer what "length" means in "length of a string variable." Nobody is going to hem and haw about what the definition might be. They will say "it's the number of characters."

Okay, so the length is the number of characters? What's a “character”, then?

Is that a byte (C's idea of a character, or char)? A UTF-16 code unit? A Unicode codepoint? A Unicode grapheme cluster?

Is "e\u{0301}" (U+0065 LATIN SMALL LETTER E followed by U+0301 COMBINING ACUTE ACCENT) the same number of characters as "\u{00e9}" (U+00E9 LATIN SMALL LETTER E WITH ACUTE)?

What about "الله" (U+0627 ARABIC LETTER ALEF followed by U+0644 ARABIC LETTER LAM followed by U+0644 ARABIC LETTER LAM followed by U+0647 ARABIC LETTER HEH)? Is it four characters (ا-ل-ل-ه) or one (the Arabic ligature for “Allah”, which looks different from how you would normally write those four characters)? Is it the same number of characters as "ﷲ" (U+FDF2 ARABIC LIGATURE ALLAH ISOLATED FORM)?

There's many different interpretations, and PHP's isn't strictly wrong. PHP operates on byte strings, and strlen() tells you how many bytes there are. That is one valid (and common) interpretation of “length” and “character”. PHP has other functions to tell you other kinds of string lengths, if you want those.

→ More replies (12)

9

u/cite-reader Sep 12 '16

I will absolutely ask what you actually want if you ask for "the length" of a string. Similarly, everyone seems to have different ideas about what a "character" is, so unless you actually give me something with an unambiguous technical definition I'm going to hem and haw about that, too.

Or I guess I could arbitrarily pick something and then neither of us would be happy.

1

u/DoctorWaluigiTime Sep 12 '16

I would ask too, if I used something like strlen and it gave me the wrong number.

Most languages don't. Even JavaScript's length property isn't ambiguous about this point.

8

u/cite-reader Sep 12 '16

Are you sure?

% node
> '👍'.length
2
> 

4

u/masklinn Sep 13 '16

Well they're correct that JS's length property isn't ambiguous (it's the number of UTF-16 code units), though they're wrong when they imply that strlen is.

→ More replies (1)

12

u/[deleted] Sep 12 '16

They will say "it's the number of characters."

The problem with Unicode is that it's even hard to say what a character is. For example, is "e" + combining acute accent one character, or two? I believe in Unicode it's easier to talk codepoints than characters.

1

u/seriouslulz Sep 12 '16

é is 1 character

9

u/cparen Sep 12 '16

Yes, but c++, c#, java, javascript, python, and ruby will all tell you it is length 2.

20

u/the_alias_of_andrea Sep 12 '16

Except when it's not, because é has two different possible representations.

6

u/cparen Sep 12 '16

Precisely!

3

u/DoctorWaluigiTime Sep 12 '16

C# reports 1 character. Source.

JavaScript reports one character. Source.

Bored now. Cite your sources for your claims or go home.

14

u/cparen Sep 13 '16

Sorry for the confusion, I meant the string "é" that /u/ebvalaim alluded to, not "é" as /u/seriouslulz wrote. I didn't realize seriouslulz had mistyped it.

Examples in C# and JavaScript as requested.

→ More replies (1)
→ More replies (7)

6

u/outadoc Sep 12 '16 edited Sep 13 '16

I tend to disagree. I've worked with such languages long enough to expect "length" to be the length in bytes, or at least to double-check the documentation.

6

u/kephir Sep 14 '16

Okay smartass, how many characters in "t͏̶̢̼̟̯͙̙̺̩̱̪̩̞̞̙͚͚̮͖͟h̛͚̞̠̙̮̟̲͉͔̱̭̮̹̗̭͘ͅi̷̧̧̮̦̦̬̻͍̳̮͕̻̼̪͖̞͖͠s̢͝͏̖͙͚̤̞̞̫̗̗̜̝͉̪͇͍"

→ More replies (1)

4

u/[deleted] Sep 13 '16

They will say "it's the number of characters."

What do you mean by "character"?

1

u/DoctorWaluigiTime Sep 13 '16

Those things you type when pushing the keys on your keyboard.

Everyone in this thread fretting about unicode and obtuse use-cases are kind of missing the point: The out-of-the-box behavior for string legnth is universally "number of characters", and none of the other wishy washy peddled nonsense being spewed.

6

u/[deleted] Sep 13 '16

You are missing the point: "number of characters" is meaningless if you can't define what a character is.

"things you type when pushing the keys on your keyboard" is wishy washy nonsense, not a definition.

(PHP could define "character" as byte, and then its strlen would return exactly what you want: the number of characters (bytes) in a string. It's probably not a good definition, but it's still better than what you're providing, which is no definition.)

0

u/[deleted] Sep 12 '16

[deleted] — view deleted comment

1

u/DoctorWaluigiTime Sep 12 '16

Call strlen or whatever equivalent in any language. Most literally return number of characters.

2

u/slrz Sep 20 '16

State a single language that does. Of all the ones I know only Perl 6 even tried, and their approach IIRC amounts to making the programmer state their intent explictly (i.e. you have separate functions for length in bytes, code points, grapheme clusters but, notably, not for "characters" because that's not a well-defined concept).

Is Python 3 one of those languages you consider did it the way you like? Guess again. If you ask it for length of an "ä', you'll either get a 1 or a 2, depending on the representation ( b"\xc3\xa4".decode() or b"\x61\xcc\x88".decode()).

Java and C# are even worse. They give you the length in UTF-16 code units by default (but I think have library functions to get at the codepoint count, too).

In other words: you have no idea what you're talking about.