Debian Codegraph @

debian/rust-vendor/tinystr/src/lib.rs

raw
1 // This file is part of ICU4X. For terms of use, please see the file
2 // called LICENSE at the top level of the ICU4X source tree
3 // (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).
4
5 // https://github.com/unicode-org/icu4x/blob/main/documents/process/boilerplate.md#library-annotations
6 #![cfg_attr(not(any(test, doc)), no_std)]
7 #![cfg_attr(
8 not(test),
9 deny(
10 clippy::indexing_slicing,
11 clippy::unwrap_used,
12 clippy::expect_used,
13 clippy::panic,
14 )
15 )]
16 // #![warn(missing_docs)]
17
18 //! `tinystr` is a utility crate of the [`ICU4X`] project.
19 //!
20 //! It includes [`TinyAsciiStr`], a core API for representing small ASCII-only bounded length strings.
21 //!
22 //! It is optimized for operations on strings of size 8 or smaller. When use cases involve comparison
23 //! and conversion of strings for lowercase/uppercase/titlecase, or checking
24 //! numeric/alphabetic/alphanumeric, `TinyAsciiStr` is the edge performance library.
25 //!
26 //! # Examples
27 //!
28 //! ```rust
29 //! use tinystr::TinyAsciiStr;
30 //!
31 //! let s1: TinyAsciiStr<4> = "tEsT".parse().expect("Failed to parse.");
32 //!
33 //! assert_eq!(s1, "tEsT");
34 //! assert_eq!(s1.to_ascii_uppercase(), "TEST");
35 //! assert_eq!(s1.to_ascii_lowercase(), "test");
36 //! assert_eq!(s1.to_ascii_titlecase(), "Test");
37 //! assert!(s1.is_ascii_alphanumeric());
38 //! assert!(!s1.is_ascii_numeric());
39 //!
40 //! let s2 = TinyAsciiStr::<8>::try_from_raw(*b"New York")
41 //! .expect("Failed to parse.");
42 //!
43 //! assert_eq!(s2, "New York");
44 //! assert_eq!(s2.to_ascii_uppercase(), "NEW YORK");
45 //! assert_eq!(s2.to_ascii_lowercase(), "new york");
46 //! assert_eq!(s2.to_ascii_titlecase(), "New york");
47 //! assert!(!s2.is_ascii_alphanumeric());
48 //! ```
49 //!
50 //! # Details
51 //!
52 //! When strings are of size 8 or smaller, the struct transforms the strings as `u32`/`u64` and uses
53 //! bitmasking to provide basic string manipulation operations:
54 //! * `is_ascii_numeric`
55 //! * `is_ascii_alphabetic`
56 //! * `is_ascii_alphanumeric`
57 //! * `to_ascii_lowercase`
58 //! * `to_ascii_uppercase`
59 //! * `to_ascii_titlecase`
60 //! * `PartialEq`
61 //!
62 //! `TinyAsciiStr` will fall back to `u8` character manipulation for strings of length greater than 8.
63
64 //!
65 //! [`ICU4X`]: ../icu/index.html
66
67 mod macros;
68
69 mod ascii;
70 mod asciibyte;
71 mod error;
72 mod int_ops;
73 mod unvalidated;
74
75 #[cfg(feature = "serde")]
76 mod serde;
77
78 #[cfg(feature = "databake")]
79 mod databake;
80
81 #[cfg(feature = "zerovec")]
82 mod ule;
83
84 #[cfg(feature = "alloc")]
85 extern crate alloc;
86
87 pub use ascii::TinyAsciiStr;
88 pub use error::ParseError;
89 pub use unvalidated::UnvalidatedTinyAsciiStr;
90
91 /// These are temporary compatability reexports that will be removed
92 /// in a future version.
93 pub type TinyStr4 = TinyAsciiStr<4>;
94 /// These are temporary compatability reexports that will be removed
95 /// in a future version.
96 pub type TinyStr8 = TinyAsciiStr<8>;
97 /// These are temporary compatability reexports that will be removed
98 /// in a future version.
99 pub type TinyStr16 = TinyAsciiStr<16>;
100
101 #[test]
102 fn test_size() {
103 assert_eq!(size_of::<TinyStr4>(), size_of::<Option<TinyStr4>>());
104 assert_eq!(size_of::<TinyStr8>(), size_of::<Option<TinyStr8>>());
105 }