News for hackers. Stuff that matters.
Re:Show HN: High-speed UTF-8 validation in Rust by tialaramex on Wednesday April 21, 2021 @07:42AM (#26888281)
One flavour I would expect to be valuable that isn't present here is this:

Process the input (as quickly as possible) and never fail, but replace each invalid sequence of bytes with U+FFFD (bytes 0xEF 0xBF 0xBD).

Re:Show HN: High-speed UTF-8 validation in Rust by chrismorgan on Wednesday April 21, 2021 @09:46AM (#26889602)
One major problem with this: you can’t do it in place. Invalid byte sequences could be 1–4 bytes long, but U+FFFD is exactly three bytes long.
Re:Show HN: High-speed UTF-8 validation in Rust by AjtevhihBudhirv on Wednesday April 21, 2021 @10:38AM (#26890229)
There're still faster approaches than naïve and probably common "copy valid byte sequences one by one into a resizable result buffer". For instance, scan through the input bytes all at once, keeping track of position and length of valid sequences, then memcopy each valid sequence into a preallocated buffer.

Edit: Although, it looks like Rust's std already does this, except for preallocating an exactly correct size result buffer: https://doc.rust-lang.org/src/alloc/string.rs.html#538

Re:Show HN: High-speed UTF-8 validation in Rust by duckerude on Wednesday April 21, 2021 @11:13AM (#26890723)
> preallocating an exactly correct size result buffer

Looks like it just uses the size of the original slice. If the average broken chunk is less than three bytes (maybe quite common?) then it'll have to grow the buffer, at least doubling it.

  >> let bytestring = b"foobar\xcc";
  >> bytestring.len()
  7
  >> let cleaned = String::from_utf8_lossy(bytestring).into_owned();
  >> cleaned.len()
  9
  >> cleaned.capacity()
  14
Re:Show HN: High-speed UTF-8 validation in Rust by cryptonector on Wednesday April 21, 2021 @04:46PM (#26895222)
Use an ASCII character like `?` or, even better, ASCII SUB (0x1A, substitute), which is specifically intended for this sort of thing. Failing that, there are a number of other unused ASCII control characters like VT (vertical tab), FS (file separator), GS (group separator), US (unit separator), CAN (cancel), etc.
Re:Show HN: High-speed UTF-8 validation in Rust by tialaramex on Wednesday April 21, 2021 @05:15PM (#26895541)
No. Don't do any of these things. The reason U+FFFD exists, even though ASCII has any number of fun things you can scribble in one byte is that U+FFFD specifically isn't any of the things your program probably didn't expect to appear unexpectedly after unrelated processing.

It isn't a letter, or a digit, or whitespace, or punctuation, or a word separator, or a control character, it is neither uppercase nor lowercase, it doesn't have any canonical equivalents - it's just a codepoint that exists specifically for this purpose.

As a result it's much less likely that if gibberish sneaks into your system somehow and gets turned into U+FFFD this causes something important to break elsewhere.

And when sooner or later a human is shown this text, it's very obvious that U+FFFD isn't what they expected, whether that was E-acute, a Euro currency symbol, a cat emoji or whatever else, and the human will know something went wrong and can decide if they care about that.

Re:Show HN: High-speed UTF-8 validation in Rust by cryptonector on Wednesday April 21, 2021 @06:01PM (#26895992)
This is about what can be done in SIMD. See GP and GGP. I think using ASCII SUB is a perfectly reasonable thing to do in this context. You can always further post-process the result to turn ASCII SUB into U+FFFD.
Re:Show HN: High-speed UTF-8 validation in Rust by nezirus on Wednesday April 21, 2021 @08:59AM (#26889057)
Yeah, I'd like to see that as well, something like to_string_lossy(), but really fast, so I can call it whenever I expect occasional broken UTF-8 string (and don't care about few replaced characters).
Re:Show HN: High-speed UTF-8 validation in Rust by nerdponx on Wednesday April 21, 2021 @08:06AM (#26888485)
I don't know, you are throwing away the length of the invalid sequence that way. Maybe useful in certain situations, but probably not in most.
Re:Show HN: High-speed UTF-8 validation in Rust by loa_in_ on Wednesday April 21, 2021 @07:52AM (#26888384)
I bet one could modify this to save indexes of invalid chars and do what you say with just one additional copy