diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 19:33:14 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 19:33:14 +0000 |
commit | 36d22d82aa202bb199967e9512281e9a53db42c9 (patch) | |
tree | 105e8c98ddea1c1e4784a60a5a6410fa416be2de /third_party/rust/tinystr/src/lib.rs | |
parent | Initial commit. (diff) | |
download | firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.tar.xz firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.zip |
Adding upstream version 115.7.0esr.upstream/115.7.0esr
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'third_party/rust/tinystr/src/lib.rs')
-rw-r--r-- | third_party/rust/tinystr/src/lib.rs | 116 |
1 files changed, 116 insertions, 0 deletions
diff --git a/third_party/rust/tinystr/src/lib.rs b/third_party/rust/tinystr/src/lib.rs new file mode 100644 index 0000000000..7745da0e54 --- /dev/null +++ b/third_party/rust/tinystr/src/lib.rs @@ -0,0 +1,116 @@ +// This file is part of ICU4X. For terms of use, please see the file +// called LICENSE at the top level of the ICU4X source tree +// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ). + +//! `tinystr` is a utility crate of the [`ICU4X`] project. +//! +//! It includes [`TinyAsciiStr`], a core API for representing small ASCII-only bounded length strings. +//! +//! It is optimized for operations on strings of size 8 or smaller. When use cases involve comparison +//! and conversion of strings for lowercase/uppercase/titlecase, or checking +//! numeric/alphabetic/alphanumeric, `TinyAsciiStr` is the edge performance library. +//! +//! # Examples +//! +//! ```rust +//! use tinystr::TinyAsciiStr; +//! +//! let s1: TinyAsciiStr<4> = "tEsT".parse().expect("Failed to parse."); +//! +//! assert_eq!(s1, "tEsT"); +//! assert_eq!(s1.to_ascii_uppercase(), "TEST"); +//! assert_eq!(s1.to_ascii_lowercase(), "test"); +//! assert_eq!(s1.to_ascii_titlecase(), "Test"); +//! assert!(s1.is_ascii_alphanumeric()); +//! assert!(!s1.is_ascii_numeric()); +//! +//! let s2 = TinyAsciiStr::<8>::try_from_raw(*b"New York") +//! .expect("Failed to parse."); +//! +//! assert_eq!(s2, "New York"); +//! assert_eq!(s2.to_ascii_uppercase(), "NEW YORK"); +//! assert_eq!(s2.to_ascii_lowercase(), "new york"); +//! assert_eq!(s2.to_ascii_titlecase(), "New york"); +//! assert!(!s2.is_ascii_alphanumeric()); +//! ``` +//! +//! # Details +//! +//! When strings are of size 8 or smaller, the struct transforms the strings as `u32`/`u64` and uses +//! bitmasking to provide basic string manipulation operations: +//! * `is_ascii_numeric` +//! * `is_ascii_alphabetic` +//! * `is_ascii_alphanumeric` +//! * `to_ascii_lowercase` +//! * `to_ascii_uppercase` +//! * `to_ascii_titlecase` +//! * `PartialEq` +//! +//! `TinyAsciiStr` will fall back to `u8` character manipulation for strings of length greater than 8. + +//! +//! [`ICU4X`]: ../icu/index.html + +// https://github.com/unicode-org/icu4x/blob/main/docs/process/boilerplate.md#library-annotations +#![cfg_attr(not(any(test, feature = "std")), no_std)] +#![cfg_attr( + not(test), + deny( + clippy::indexing_slicing, + clippy::unwrap_used, + clippy::expect_used, + clippy::panic, + clippy::exhaustive_structs, + clippy::exhaustive_enums, + missing_debug_implementations, + ) +)] + +mod macros; + +mod ascii; +mod asciibyte; +mod error; +mod int_ops; + +#[cfg(feature = "serde")] +mod serde; + +#[cfg(feature = "databake")] +mod databake; + +#[cfg(feature = "zerovec")] +mod ule; + +#[cfg(any(feature = "serde", feature = "alloc"))] +extern crate alloc; + +pub use ascii::TinyAsciiStr; +pub use error::TinyStrError; + +/// These are temporary compatability reexports that will be removed +/// in a future version. +pub type TinyStr4 = TinyAsciiStr<4>; +/// These are temporary compatability reexports that will be removed +/// in a future version. +pub type TinyStr8 = TinyAsciiStr<8>; +/// These are temporary compatability reexports that will be removed +/// in a future version. +pub type TinyStr16 = TinyAsciiStr<16>; + +#[test] +fn test_size() { + assert_eq!( + core::mem::size_of::<TinyStr4>(), + core::mem::size_of::<Option<TinyStr4>>() + ); + assert_eq!( + core::mem::size_of::<TinyStr8>(), + core::mem::size_of::<Option<TinyStr8>>() + ); +} +// /// Allows unit tests to use the macro +// #[cfg(test)] +// mod tinystr { +// pub use super::{TinyAsciiStr, TinyStrError}; +// } |