Utf8
Description
Container class containing UTF-8 encoding.
@see Class Comments in <EsUnicodeEncoding> for usage examples.
Class Methods
asUnicodeString:repair:
  Answer a new unicode string instance using utf8 bytes from @aByteObject
     If @repair is true, then invalid utf8 will be replaced with the unicode replacement character U+FFFD.
     
     Examples:
        'Grinning Face (U+1F600)'.
        self assert: [(Utf8 asUnicodeString: #[240 159 152 128] repair: false) first name = 'GRINNING FACE'].
        
        'Invalid UTF8-encoding'.
        self assert: [[(Utf8 asUnicodeString: #[233]) repair: false] on: Exception do: [:ex | ex exitWith: true]].
        
        'Repaired invalid UTF8-encoding'.
        self assert: [(Utf8 asUnicodeString: #[233] repair: true) = UnicodeScalar replacementCharacter asUnicodeString].
    
     Arguments:
        aByteObject - <ByteArray | Utf8>
        repair - <Boolean>        
     Answers:
        <UnicodeString>
     Raises:
        <Exception> EsPrimErrValueOutOfRange if @anObject contains invalid bytes and @repair is false
Instance Methods
asByteArray
  Answer the receiver as a <ByteArray>
     
     Examples:
        self assert: [(Utf8 with: 16rF0 with: 16r9F with: 16r98 with: 16r80) asByteArray = #[240 159 152 128]]
     
     Answers:
        <ByteArray>
     Raises:
        <Exception> receiver with invalid content cannot be converted to unicode string
isValid
  Answer true if the content of the container represents valid UTF-8 data.
     
     Examples:
        'Grinning Face (U+1F600)'.
        self assert: [(Utf8 with: 16rF0 with: 16r9F with: 16r98 with: 16r80) isValid].
        
        'Invalid UTF8-encoding'.
        self assert: [(Utf8 with: 233) isValid not]
     
     Answers:
        <Boolean> true if valid, false if invalid
Last modified date: 01/06/2026