Skip to main content

validate_utf8

Function validate_utf8 

Source
pub fn validate_utf8(bytes: &[u8]) -> Result<&str>
Expand description

Validate that a byte slice is valid UTF-8

Returns the validated string slice on success, or a DxError::Utf8Error with the byte offset of the first invalid sequence on failure.

ยงExamples

use serializer::utf8::validate_utf8;

// Valid UTF-8
let valid = b"Hello, World!";
assert!(validate_utf8(valid).is_ok());

// Invalid UTF-8 (invalid continuation byte)
let invalid = &[0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x80]; // "Hello" + invalid byte
let err = validate_utf8(invalid).unwrap_err();
// Error contains offset 5 (position of invalid byte)