News for hackers. Stuff that matters.
Re:It’s Not Wrong that “ ”.length == 7 by josephg on Friday June 02, 2023 @10:24AM (#36165486)
Yeah; I've recently noticed that almost every time I use string.length in javascript, its wrong and going to break something as soon as emoji appears. In my code, I always want to deal with either the number of codepoints or the number of UTF8 bytes. String.length gives you neither, but unfortunately it looks correct until you test with non-ASCII strings.
Re:It’s Not Wrong that “ ”.length == 7 by sroussey on Friday June 02, 2023 @05:36PM (#36171074)
At least the “of” loop returns what is selectable via a cursor in the browser. I think…
Re:It’s Not Wrong that “ ”.length == 7 by josephg on Friday June 02, 2023 @08:31PM (#36172531)
Yeah; its really confusing but javascript - for legacy reasons - treats strings as "arrays of UCS2 items". But javascript also implements iterator on strings which iterate through strings in unicode codepoints. Thats why "of" loops work differently from "in" loops. (for-of in javascript uses Symbol.iterator). That also means you can pull a string apart into an array of unicode codepoints using [...somestring].