SwfStr

Struct SwfStr 

Source
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::encoding or SwfStr::encoding_for_version to 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

Source

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!");
Source

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());
Source

pub const fn from_utf8_str(string: &str) -> &Self

Creates a new UTF-8 SwfStr from a Rust str.

§Examples
use swf::SwfStr;

let s = SwfStr::from_utf8_str("Hello, 🌏!");
Source

pub fn from_utf8_str_null_terminated(string: &str) -> Option<&Self>

Creates a new UTF-8 SwfStr from a Rust str.

§Examples
use swf::SwfStr;

let s = SwfStr::from_utf8_str_null_terminated("I'm null-terminated!\0");
assert!(s.is_some());

let s = SwfStr::from_utf8_str_null_terminated("I'm not terminated...");
assert!(s.is_none());
Source

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());
Source

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);
Source

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]);
Source

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());
Source

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);
Source

pub fn to_str_lossy(&self, encoding: &'static Encoding) -> Cow<'_, str>

Decodes the string into a Rust UTF-8 str.

The UTF-8 replacement character will be used for any invalid data.

§Examples
use swf::SwfStr;
use encoding_rs::UTF_8;

let s = SwfStr::from_bytes(&[0xF0, 0x9F, 0x92, 0x96]);
assert_eq!(s.to_str_lossy(UTF_8), "💖");
Source

pub fn to_string_lossy(&self, encoding: &'static Encoding) -> String

Decodes the string into a Rust UTF-8 String.

The UTF-8 replacement character will be used for any invalid data.

§Examples
use swf::SwfStr;
use encoding_rs::UTF_8;

let s = SwfStr::from_bytes(&[0xF0, 0x9F, 0x92, 0x96]);
assert_eq!(s.to_string_lossy(UTF_8), "💖");

Trait Implementations§

Source§

impl Debug for SwfStr

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the SwfStr using the given formatter.

Non-ASCII characters will be formatted in hexadecimal form (\xNN).

Source§

impl<'a> Default for &'a SwfStr

Source§

fn default() -> &'a SwfStr

Returns the “default value” for a type. Read more
Source§

impl<'a> From<&'a str> for &'a SwfStr

Source§

fn from(s: &'a str) -> &'a SwfStr

Converts to this type from the input type.
Source§

impl<T: ?Sized + AsRef<str>> PartialEq<T> for SwfStr

Source§

fn eq(&self, other: &T) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialEq for SwfStr

Source§

fn eq(&self, other: &SwfStr) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Eq for SwfStr

Source§

impl StructuralPartialEq for SwfStr

Auto Trait Implementations§

§

impl Freeze for SwfStr

§

impl RefUnwindSafe for SwfStr

§

impl Send for SwfStr

§

impl !Sized for SwfStr

§

impl Sync for SwfStr

§

impl Unpin for SwfStr

§

impl UnwindSafe for SwfStr

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more