4 ms·
I'll paypal $20 to anyone who can name a situation where string length (in number of visible characters) is actually required by any reasonable algorithm. (Hig
by sillysaurusx 5y ago
I'll paypal $20 to anyone who can name a situation where string length (in number of visible characters) is actually required by any reasonable algorithm.
(High roller, I know.)
No games, though. If you say "An algorithm to compute the length of the string in number of visible characters," that obviously is designed to pass the test rather than to do anything useful.
Maybe framing it as a bet will break the illusion that string length is ever required.
- latch 5y agoI'm not sure I understand what you're getting at. But if changing the case didn't alter the length, you could do it in-place, without having to allocate memory. I always thought this was the point?
- toast0 5y agoVisible character count isn't necessarily related to storage size. If uppercase requires a diacritic, but lower case didn't that doesn't (usually) increase the visible character count, but it likely increases storage space, depending on encodings and normalization forms. Thinking about i to I with dot (which I can't currently type), which would need more storage in utf-8 or as I + dot.
- deleted 5y ago[deleted]
- yrral 5y agoWhen you have fixed width font and need to size columns appropriately for the data displayed in eg: a table.
- lofties 5y agoOh that's a great one! I have a thermal receipt printer (can recommend anyone to buy one, they're amazing) that is 80 characters wide. Would definitely screw up the formatting if it ever encountered this situation. (and no further validation was performed on the uppercase'd string)
- BlueTemplar 5y agoThose clacking mechanical train/plane schedule displays came to mind, though I assume they are only rarely used these days ?
- sillysaurusx 5y agoI guess I'll admit that fixed width fonts are a valid use case, but I'd argue that that defeats the point of displaying language to humans. There are so many cases this doesn't apply: RTL languages, kanji, pretty much anything that's not eurocentric. My original response was going to be that you don't actually care about "foo".length in that context, either. You care whether one string is larger than the other, i.e. the actual value doesn't matter -- all that matters is that if a string X is longer than string Y, then the length should increase. But mathematically, that's just a precise definition of requiring the string length, so that logic doesn't work out. If you email me your paypal/venmo/whatever, you can collect the $20, since you were the first to point out monospaced font. I still feel that monospaced fonts are sidestepping the whole point, but I won't be pedantic about it. (https://news.ycombinator.com/item?id=29303325 https://news.ycombinator.com/item?id=29303325 points out why this isn't a sensible operation, if you were curious about why I feel inclined not to concede the bet.)
- yrral 5y agoIf you feel inclined, feel free to donate the $20 to a charity of your choice. Thanks! Just for more context, I was thinking data tables: like this. https://images.app.goo.gl/wWJgx9sntUAhXzQ66 https://images.app.goo.gl/wWJgx9sntUAhXzQ66
- sillysaurusx 5y agoActually, I've been meaning to ask: what's a good charity to donate to? I haven't had much money until recently, so I've never sat down and figured out a charity. There are lots to choose from, and I suppose I don't really care about making a perfect choice. I'm mostly just curious what other people donate to. Re: the data tables, it's tempting to think that you need the string length for that case. But what you really care about is whether the strings start at the same place, and whether the string fits in a column -- both of which are questions best answered by a font subsystem rather than relying on string length. But the data table case is so common that I agree it fulfills the terms of the bet, in the strictest sense. But e.g. that Competition / Name column will break for various non-English languages if you try to base the formatting on "number of visible characters."
- deathanatos 5y agoWidth of text is a discreet thing from string "length"… or, well, string "length" is particularly vague. But lets presume the OP meant length in unicode code points or grapheme clusters. (Neither of these will be width under a fixed width font. There's also length in bytes, which ofc. is also not width in a fixed width font.) E.g., most Asian characters & emoji are 2 cell widths in a fixed with font for a single character. The character "e" is 1 byte in UTF-8, "é" is >1. The "family of four" emoji can be up to 11 code points, 41 bytes (in utf-8), and 2 cell widths (still just an emoji) and is, I presume, a single grapheme cluster.
- detaro 5y agoTo not have users complain you're withholding characters when you implement artificial input lengths? ;) (although e.g. Twitter does not accurately count visible characters, despite the character limit being a prominent feature, suggesting its not that important) "What size does this UI element have to fit this text in our specific mono-spaced font"?
- furstenheim 5y agoSize of a tweet?
- DenseComet 5y agoThe 280 character limit in a tweet isn't equal to the number of glyphs in a tweet. https://developer.twitter.com/en/docs/counting-characters https://developer.twitter.com/en/docs/counting-characters
- garmaine 5y agoBut the point is it ought to be. Tell a user they have "a 280 character limit" and the intuitive interpretation for 98% of the world population that doesn't know the intricacies of Unicode is "a limit of 280 visible characters." That's what Twitter's tweet limit ought to be, and if there was an easy, culturally neutral algorithm for it I'm sure that's what they would have done.
- detaro 5y agoTwitter clearly has chosen to intentionally deviate from it. E.g. what they specify on that page is not "unicode code points" either, but indeed their own mapping of "this character is worth that many points".
- garmaine 5y agoFrom what that page describes, it appears Twitter's algorithm is in fact "number of unicode code points" except for specific carve-out cases which are so obscenely unfair by that metric as to justify counting differently. E.g. counting Unihan characters as two makes the 280 character limit in these languages be approximately the same limit on information as western languages, since they are about twice as compact in terms of information density. And emojis are an insane amount of unicode code points, particularly when modifiers are applied. Etc.
- 5y ago
- Retr0id 5y agoYou're programming in C, and you need to know the length of the buffer you're about to memcpy.
- detaro 5y agoThen you don't care about visible characters.
- sillysaurusx 5y agoYou'd just use the byte length.
- rtpg 5y agoimplementing tab-to-spacification in a way that requires tabs to work "as tabs" (i.e. you get your alignment along 4-space lines) with a mono-spaced font. I suppose some terminal rendering (where you're taking in chunks and you might want to be a bit clever about jumping to spaces to figure out where breaks should go). There is a notion of UTF codepoints that you can index into (see how Python does string indexing), though I generally think that people who whine a lot about string behavior for UTF tend to just not be reaching for the right tools.
- naniwaduni 5y agoIt turns out this isn't actually a sensible thing to do in the presence of East Asian fullwidth characters.
- deleted 5y ago[deleted]
- JoshTriplett 5y agoShowing a column number in a text editor, or in a compiler error message that lists line:column so that an editor can jump to the error.
- kaetemi 5y agoAt the very least least you need to know which code point clusters are one visual character for stepping the cursor left and right through text in a text editing control. Which is 99% of the work to also calculate the length in those terms.
- forgotmypw17 5y agoAlso, for doing `substr()` to limit the length of a title, for example. I'm still struggling with this in one place, where, for some reason, Cyrillic characters get cut in half.
- AndriyKunitsyn 5y agoIf your substr() cuts characters in half, then the reason is simple - it's either not aware of multi-byte encodings, so it cuts by bytes instead of cutting by characters, or the encoding it uses is different from the encoding of your string, so substr() cuts it wrong. You may visualize all that by converting your strings to lists of bytes.
- kaetemi 5y agoThat's the point. You can lose accents, or other parts of a character, even if you are aware of the encoding. You need to be aware of the modifiers as well.
- ordiel 5y agoSet top boxes (the things you use to decode paid tv signal and watch "the [shows] programing guide" ) uses a format called EIT (Event Information Table) which is transfered as an encoded binary file, for which MANY of the titles and tv shows info is capitalized (mostly* due to readability, you know so an L does not look like a 1) I worked on a proyect for such file generation and it is HIGHLY sensitive to the lengt of the data (since it will be encoded to a binary and if you add a single bite to the length of such file the rest of the info turns into garbage) ... Now that I think about it I may probably drop one of those guys still there a call to let them know the shows in german may have a bug... Can I get my 20 now?
- sillysaurusx 5y agoI'll send you $20 for the interesting reply and reporting unicode bugs, if you want! (Feel free to email me your info.) But in the situation you mention, you care about the byte length, not the string length. Byte length is a totally valid thing to care about, but string length isn't needed.
- ordiel 5y agoBite lenght will be affected by the length of whatever is it that you are encoding, in this case an additional character would shift them by 8 I guess what I amsaying is:where are you marking the diffefence? any string (and all data) IS at the a series of encoded bites
- dotancohen 5y ago> I'll paypal $20 to anyone who can name a situation where string length (in number of visible characters) is actually required by any reasonable algorithm. I've already described above where I've had this exact requirement. The client demands that "50 letters" be allowed in a title field for a website element. He means letters: spaces (and presumably newlines) don't count, neither do punctuation. Emojis were not a concern at the time. I wrote Javascript and PHP validators for client- and server-side validation and stored the string in a VARCHAR(127) to be safe. And this is completely reasonable. He is a human, not "a computer person" (his terminology) and pays me to abstract from him those "computer things" which he has no desire to understand.
- BlueTemplar 5y ago...as long as he doesn't go international ! (A website pretty much counts already.) Then he might have to learn it's very much a "human", not "computer" thing...
- dotancohen 5y agoThis is already in a language that is not represented by ASCII.
- 3np 5y agoInput validation. “Full name” and “username” must both have a minimum length of 1.