pub struct SwfStr { /* private fields */ }Expand description
A bstr-like string type analogous to str that’s returned by SWF parsing functions:
- The encoding depends on the SWF version (UTF-8 for SWF6 and higher).
Use
Reader::encodingorSwfStr::encoding_for_versionto get the proper encoding. - Invalid data for any particular encoding is allowed; any conversions to std::String will be lossy for invalid data.
To convert this to a standard Rust string, use SwfStr::to_str_lossy.
Implementations§
Source§impl SwfStr
impl SwfStr
Sourcepub const fn from_bytes(string: &[u8]) -> &Self
pub const fn from_bytes(string: &[u8]) -> &Self
Creates a new SwfStr from a byte slice.
The data is not required to be valid for the given encoding.
§Examples
use swf::SwfStr;
let s = SwfStr::from_bytes(b"Hello, World!");Sourcepub fn from_bytes_null_terminated(string: &[u8]) -> Option<&Self>
pub fn from_bytes_null_terminated(string: &[u8]) -> Option<&Self>
Creates a SwfStr from a byte slice by reading until a NULL byte (0) is encountered.
Returns None if no NULL byte was found.
§Examples
use swf::SwfStr;
let s = SwfStr::from_bytes_null_terminated(b"I'm null-terminated!\0");
assert!(s.is_some());
let s = SwfStr::from_bytes_null_terminated(b"I'm not terminated...");
assert!(s.is_none());Sourcepub const fn from_utf8_str(string: &str) -> &Self
pub const fn from_utf8_str(string: &str) -> &Self
Sourcepub fn from_utf8_str_null_terminated(string: &str) -> Option<&Self>
pub fn from_utf8_str_null_terminated(string: &str) -> Option<&Self>
Sourcepub fn from_str_with_encoding<'a>(
string: &'a str,
encoding: &'static Encoding,
) -> Option<&'a Self>
pub fn from_str_with_encoding<'a>( string: &'a str, encoding: &'static Encoding, ) -> Option<&'a Self>
Creates a new SwfStr with the given encoding from a Rust str.
Returns None if the encoding is not lossless.
The string will be re-encoded with the given encoding.
The string will be truncated if a NULL byte (0) is encountered.
Intended for tests.
§Examples
use swf::SwfStr;
use encoding_rs::WINDOWS_1252;
let s = SwfStr::from_str_with_encoding("Hello, World!", WINDOWS_1252);
assert!(s.is_some());Sourcepub fn encoding_for_version(swf_version: u8) -> &'static Encoding
pub fn encoding_for_version(swf_version: u8) -> &'static Encoding
Returns the suggested string encoding for the given SWF version.
For SWF version 6 and higher, this is always UTF-8. For SWF version 5 and lower, this is locale-dependent, and we default to WINDOWS-1252.
§Examples
use swf::SwfStr;
use encoding_rs::{UTF_8, WINDOWS_1252};
assert_eq!(SwfStr::encoding_for_version(9), UTF_8);
assert_eq!(SwfStr::encoding_for_version(3), WINDOWS_1252);Sourcepub const fn as_bytes(&self) -> &[u8] ⓘ
pub const fn as_bytes(&self) -> &[u8] ⓘ
Returns the byte slice of this string.
§Examples
use swf::SwfStr;
let s = SwfStr::from_utf8_str("💖");
assert_eq!(s.as_bytes(), [0xF0, 0x9F, 0x92, 0x96]);Sourcepub const fn is_empty(&self) -> bool
pub const fn is_empty(&self) -> bool
Returns true if the string has a length of zero, and false otherwise.
§Examples
use swf::SwfStr;
let s = SwfStr::from_bytes(&[]);
assert!(s.is_empty());
let s = SwfStr::from_utf8_str("💖");
assert!(!s.is_empty());Sourcepub const fn len(&self) -> usize
pub const fn len(&self) -> usize
Returns the length of the string in bytes.
§Examples
use swf::SwfStr;
let s = SwfStr::from_utf8_str("");
assert_eq!(s.len(), 0);
let s = SwfStr::from_utf8_str("Hi!");
assert_eq!(s.len(), 3);
let s = SwfStr::from_utf8_str("💖");
assert_eq!(s.len(), 4);