diff --git a/Cargo.lock b/Cargo.lock index 9c832425a..4f287da4d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2995,6 +2995,7 @@ dependencies = [ "rand", "regress", "ruffle_macros", + "ruffle_wstr", "serde", "serde_json", "smallvec", @@ -3150,6 +3151,13 @@ dependencies = [ "web-sys", ] +[[package]] +name = "ruffle_wstr" +version = "0.1.0" +dependencies = [ + "gc-arena", +] + [[package]] name = "rustc-hash" version = "1.1.0" diff --git a/Cargo.toml b/Cargo.toml index a1af8d351..4e848960a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,6 +5,7 @@ members = [ "desktop", "swf", "web", + "wstr", "scanner", "exporter", diff --git a/core/Cargo.toml b/core/Cargo.toml index 42f5b55bb..09b71606b 100644 --- a/core/Cargo.toml +++ b/core/Cargo.toml @@ -20,6 +20,7 @@ log = "0.4" minimp3 = { version = "0.5.1", optional = true } png = { version = "0.17.5" } ruffle_macros = { path = "macros" } +ruffle_wstr = { path = "../wstr" } swf = { path = "../swf" } bitflags = "1.3.2" smallvec = { version = "1.8.0", features = ["union"] } diff --git a/core/src/string.rs b/core/src/string.rs index 3d1af0d83..3f37f1891 100644 --- a/core/src/string.rs +++ b/core/src/string.rs @@ -1,41 +1 @@ -//! Provides UCS2 string types for usage in AVM1 and AVM2. -//! -//! Internally, these types are represeted by a sequence of 1-byte or 2-bytes (wide) code units, -//! that may contains null bytes or unpaired surrogates. -//! -//! To match Flash behavior, the string length is limited to 2³¹-1 code units; -//! any attempt to create a longer string will panic. - -#[macro_use] -mod common; - -mod avm; -mod buf; -mod ops; -mod parse; -mod pattern; -mod ptr; -mod tables; -pub mod utils; - -#[cfg(test)] -mod tests; - -pub use ptr::{WStr, MAX_STRING_LEN}; - -pub use avm::AvmString; -pub use buf::WString; -pub use common::Units; -pub use ops::{CharIndices, Chars, Iter, Split, WStrToUtf8}; -pub use parse::{FromWStr, Integer}; -pub use pattern::Pattern; - -use std::borrow::Borrow; - -use common::panic_on_invalid_length; - -/// Flattens a slice of strings, placing `sep` as a separator between each. -#[inline] -pub fn join, S: Borrow>(elems: &[E], sep: &S) -> WString { - crate::string::ops::str_join(elems, sep.borrow()) -} +pub use ruffle_wstr::*; diff --git a/wstr/Cargo.toml b/wstr/Cargo.toml new file mode 100644 index 000000000..3a74f0d81 --- /dev/null +++ b/wstr/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "ruffle_wstr" +version = "0.1.0" +edition = "2021" +authors = ["Arthur Heuillard "] +license = "MIT OR Apache-2.0" + +[dependencies] +gc-arena = { git = "https://github.com/ruffle-rs/gc-arena" } diff --git a/core/src/string/avm.rs b/wstr/src/avm.rs similarity index 100% rename from core/src/string/avm.rs rename to wstr/src/avm.rs diff --git a/core/src/string/buf.rs b/wstr/src/buf.rs similarity index 100% rename from core/src/string/buf.rs rename to wstr/src/buf.rs diff --git a/core/src/string/common.rs b/wstr/src/common.rs similarity index 99% rename from core/src/string/common.rs rename to wstr/src/common.rs index b25aae195..9299e84c9 100644 --- a/core/src/string/common.rs +++ b/wstr/src/common.rs @@ -271,7 +271,7 @@ impl WStr { /// Iterates over the unicode characters of `self`, together with their indices. #[inline] - pub fn char_indices(&self) -> crate::string::ops::CharIndices<'_> { + pub fn char_indices(&self) -> crate::ops::CharIndices<'_> { super::ops::str_char_indices(self) } diff --git a/wstr/src/lib.rs b/wstr/src/lib.rs new file mode 100644 index 000000000..065d8aef6 --- /dev/null +++ b/wstr/src/lib.rs @@ -0,0 +1,40 @@ +//! Provides UCS2 string types for usage in AVM1 and AVM2. +//! +//! Internally, these types are represeted by a sequence of 1-byte or 2-bytes (wide) code units, +//! that may contains null bytes or unpaired surrogates. +//! +//! To match Flash behavior, the string length is limited to 2³¹-1 code units; +//! any attempt to create a longer string will panic. + +#[macro_use] +mod common; + +mod avm; +mod buf; +mod ops; +mod parse; +mod pattern; +mod ptr; +mod tables; +pub mod utils; + +#[cfg(test)] +mod tests; + +pub use avm::AvmString; +pub use buf::WString; +pub use common::Units; +pub use ops::{CharIndices, Chars, Iter, Split, WStrToUtf8}; +pub use parse::{FromWStr, Integer}; +pub use pattern::Pattern; +pub use ptr::{WStr, MAX_STRING_LEN}; + +use std::borrow::Borrow; + +use common::panic_on_invalid_length; + +/// Flattens a slice of strings, placing `sep` as a separator between each. +#[inline] +pub fn join, S: Borrow>(elems: &[E], sep: &S) -> WString { + crate::ops::str_join(elems, sep.borrow()) +} diff --git a/core/src/string/ops.rs b/wstr/src/ops.rs similarity index 100% rename from core/src/string/ops.rs rename to wstr/src/ops.rs diff --git a/core/src/string/parse.rs b/wstr/src/parse.rs similarity index 96% rename from core/src/string/parse.rs rename to wstr/src/parse.rs index 1d9617af2..a93ee14a5 100644 --- a/core/src/string/parse.rs +++ b/wstr/src/parse.rs @@ -1,3 +1,4 @@ +use std::fmt; use std::num::Wrapping; use super::WStr; @@ -10,10 +11,17 @@ pub trait FromWStr: Sized { } /// Error returned by [`Integer::from_str_radix`]. -#[derive(Debug, thiserror::Error)] -#[error("failed to parse integer")] +#[derive(Debug)] pub struct ParseNumError(()); +impl fmt::Display for ParseNumError { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "failed to parse integer") + } +} + +impl std::error::Error for ParseNumError {} + /// Trait implemented for all integer types that can be parsed from a [`WStr`]. pub trait Integer: FromWStr { fn from_wstr_radix(s: &WStr, radix: u32) -> Result; diff --git a/core/src/string/pattern.rs b/wstr/src/pattern.rs similarity index 100% rename from core/src/string/pattern.rs rename to wstr/src/pattern.rs diff --git a/core/src/string/ptr.rs b/wstr/src/ptr.rs similarity index 100% rename from core/src/string/ptr.rs rename to wstr/src/ptr.rs diff --git a/core/src/string/tables.rs b/wstr/src/tables.rs similarity index 100% rename from core/src/string/tables.rs rename to wstr/src/tables.rs diff --git a/core/src/string/tests.rs b/wstr/src/tests.rs similarity index 100% rename from core/src/string/tests.rs rename to wstr/src/tests.rs diff --git a/core/src/string/utils.rs b/wstr/src/utils.rs similarity index 100% rename from core/src/string/utils.rs rename to wstr/src/utils.rs