Conversation

Imagine you have a string in UTF8 that you want to spindle in some way involving a handful of punctuation -- let's say space, comma, double-quote, and a pair of brackets).

It's _guaranteed_ that the string is in UTF8, but it _may_ not have any characters 128 and over in it.

Do you _need_ to scan through the UTF8 byte sequences to ensure you can't fuck up, or can you just shrug and go char by char, since the _important_ characters you're looking for are all <128?

4
0
0

@kawa iirc any codepoint in utf8 that's larger than 127 will be encoded using only bytes larger than 127, there's, like, a 0b11?????? byte indicating the start of a multi-byte sequence and then one or more 0b10?????? ones continuing it

UPD: looked it up, got the byte sequences mixed up, but the point still stands

1
0
0

@rnd I think you forgot to *make* the point tbh. Like, you're not wrong but you also didn't actually answer the question. I can only assume your next words could've been "so going char by char should be safe" :3

0
0
0

Alice Averlong🏳️‍⚧️

@kawa if it doesn't have any characters 128 and over in it, doesn't that mean it's effectively in ASCII, and thus fine to do char by char?

1
0
0

@foone The input string is *guaranteed* to be UTF8, and *may effectively* be ASCII. But it can and might have characters 128 and over.

1
0
0

Charlotte lotteheartplural/Cinny cinny_heart_plural thetadelta ursaminor treblesand

Edited 4 days ago
@kawa all bytes <128 in a UTF-8 string match what they mean in ASCII, and will not appear in multi-byte sequences. utf-8 is also stateless between codepoints, so a lot of bytewise operations are valid in the spot like substitution, insertion, deletion, and string slicing (that starts or ends at position p or p+1)
0
0
1

Alice Averlong🏳️‍⚧️

Edited 4 days ago

@kawa okay I misread, sorry.

but yes. you should be fine. if your code is just doing like:

char *p=&str[0];
while(*p!=0){
if(*p==':'){
// do something with the char or the offset
}
p++;
}

there won't be any weird utf-8 characters that "look like" a specific <128 character. utf-8 doesn't work like that.

0
0
0

Full disclosure, I already knew this is perfectly safe and already wrote the string spindler. I'm just curious to see how everyone gets to their conclusions.

0
0
0