pub struct CharacterStringRef<'a> { /* private fields */ }Expand description
A CharacterString’s contents, borrowed from the octets they were decoded from: the character-set octet, then the string in that character set (Clause 20.2.9).
decode checks the contents’ structure as every decoder
of a CharacterString does (#1778): a character set the clause defines,
valid UTF-8, a DBCS string’s two-octet code page, and whole UCS-2 and
UCS-4 characters that are Unicode scalar values. So
as_str borrows UTF-8 contents as a &str; contents in
another character set are carried as they are, for the caller to convert.
use bacnet_encoding::primitives::{charset, CharacterStringRef};
let text = CharacterStringRef::decode(&[charset::UTF8, b'A', b'V'])?;
assert_eq!((text.charset(), text.as_str()), (charset::UTF8, Some("AV")));
let latin1 = CharacterStringRef::decode(&[charset::ISO_8859_1, 0xE9])?;
assert_eq!((latin1.as_str(), latin1.bytes()), (None, &[0xE9][..]));Implementations§
Source§impl<'a> CharacterStringRef<'a>
impl<'a> CharacterStringRef<'a>
Sourcepub fn decode(contents: &'a [u8]) -> Result<Self, Error>
pub fn decode(contents: &'a [u8]) -> Result<Self, Error>
Read CharacterString contents, refusing the malformed structures the
type’s docs list, each DecodingKind::InvalidEncoding at an offset
into contents: no or a reserved character-set octet, invalid UTF-8,
a DBCS string without its code page, and broken UCS-2 or UCS-4.
Sourcepub fn bytes(&self) -> &'a [u8] ⓘ
pub fn bytes(&self) -> &'a [u8] ⓘ
The string’s octets in its character set, after the character-set octet.