Move core::string into separate ruffle_wstr crate.

This commit is contained in:
Moulins 2022-03-23 15:52:55 +01:00 committed by Mike Welsh
parent 9234336dfc
commit 1911aa9a6b
16 changed files with 71 additions and 44 deletions

8
Cargo.lock generated
View File

@ -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"

View File

@ -5,6 +5,7 @@ members = [
"desktop",
"swf",
"web",
"wstr",
"scanner",
"exporter",

View File

@ -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"] }

View File

@ -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<E: Borrow<WStr>, S: Borrow<WStr>>(elems: &[E], sep: &S) -> WString {
crate::string::ops::str_join(elems, sep.borrow())
}
pub use ruffle_wstr::*;

9
wstr/Cargo.toml Normal file
View File

@ -0,0 +1,9 @@
[package]
name = "ruffle_wstr"
version = "0.1.0"
edition = "2021"
authors = ["Arthur Heuillard <arthur.heuillard<orange.fr>"]
license = "MIT OR Apache-2.0"
[dependencies]
gc-arena = { git = "https://github.com/ruffle-rs/gc-arena" }

View File

@ -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)
}

40
wstr/src/lib.rs Normal file
View File

@ -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<E: Borrow<WStr>, S: Borrow<WStr>>(elems: &[E], sep: &S) -> WString {
crate::ops::str_join(elems, sep.borrow())
}

View File

@ -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<Err = ParseNumError> {
fn from_wstr_radix(s: &WStr, radix: u32) -> Result<Self, Self::Err>;