Expand description
Library for strings of fixed maximum lengths that can be copied and stack-allocated using const generics.
The structures provided by this crate are fstr, zstr, Flexstr and tstr. However, tstr is not exported and can only be used through the type aliases str4, str8, str16, … str256, as well as indirectly with Flexstr.
The size of (std::mem::size_of) types str8 and zstr<8> are 8 bytes, compared to 16 bytes for &str (on 64bit systems), providing more efficient ways of representing very small strings. Unicode is supported.
The four versions of strings implemented are as follows.
- A fstr<N> stores a string of up to N bytes. It is represented underneath using a [u8;N] array and a separate usize variable holding the length.
- A zstr<N> stores a zero-terminated string, without a separate length variable, and can hold strings of up to N-1 bytes.
- The types str4, str8 through str256 are aliases for internal type
tstr<4> through tstr<256> respectively. These strings are stored
in an array of u8 bytes with the first byte holding the length of the
string. Each tstr<N> can store strings of up to N-1 bytes, with
maximum N=256. tstr
combines the best of fstr and zstr in terms of speed
and memory efficiency. However, because Rust does not currently provide
a way to specify conditions on const generics at compile time, such as
where N<=256, the tstr type is not exported and can only be used through the aliases. These strings implement the same functions and traits as fstr<N> so the documentation for fstr also apply to the alias types. - A Flexstr<N> uses an internal enum that is either a tstr<N> or an owned String, in case the length of the string exceeds N. This type is designed for situations where strings only occasionally exceed the limit of N-1 bytes.
Recent Updates:
Version 0.3.2 introduced the Flexstr type.
Version 0.3.1 implements Deref<Target=str> and removed
some redundant procedures. The functions to_ascii_lowercase
and to_ascii_uppercase has been renamed to to_ascii_lower and
to_ascii_upper, to avoid clash with those from the Deref trait.
Version 0.3.0 modified the implementations of the std::ops::Index traits to return &str slices (as opposed to &[u8] slices in pre-3.0 versions). In addition, IndexMut<usize> is separately implemented for the zstr type, allowing this type to represent more than just utf8 strings.
Version 0.2.12 includes contribution from
wallefan,
and added optional serde support for serialization.
This feature can be enabled by giving cargo the
--features serde option.
Version 0.2.11 impls std::fmt::Write, thereby enabling the write! macro. Also adds new macros str_format! and try_format!.
Version 0.2.10 allows str4-str128 strings to be concatenated with
the + operator, resulting in strings with twice the capacity,
str8-str256. This feature is only implemented for the strN types.
Version 0.2.6-0.2.8 impls AsRef<str> and AsMut<str> traits.
Functions try_make and reallocate
have been added that do not truncate strings. str4, str24 and
str48 were added. str4 can only hold three bytes but is good enough
for many types of abbreviations such as those for airports.
Examples
let a:fstr<8> = fstr::from("abcdefg"); //creates fstr from &str
let a1:fstr<8> = a; // copied, not moved
let a2:&str = a.to_str();
let a3:String = a.to_string();
assert_eq!(a.nth_ascii(2), 'c');
let ab = a.substr(1,5); // copies substring to new fstr
assert_eq!(ab,"bcde"); // can compare with &str
assert_eq!(&a[1..4],"bcd"); // implements Index
assert!(a<ab); // implements Ord (and Hash, Debug, Display, other traits)
let mut u:fstr<8> = fstr::from("aλb"); //unicode support
for x in u.nth(1) {assert_eq!(x,'λ');} // nth returns Option<char>
assert!(u.set(1,'μ')); // changes a character of the same character class
assert!(!u.set(1,'c')); // .set returns false on failure
assert!(u.set(2,'c'));
assert_eq!(u, "aμc");
assert_eq!(u.len(),4); // length in bytes
assert_eq!(u.charlen(),3); // length in chars
let mut ac:fstr<16> = a.resize(); // copies to larger capacity string
let remainder:&str = ac.push("hijklmnopqrst"); //appends string, returns left over
assert_eq!(ac.len(),16);
assert_eq!(remainder, "qrst");
ac.truncate(10); // shortens string in place
assert_eq!(&ac,"abcdefghij");
let (upper,lower) = (str8::make("ABC"), str8::make("abc"));
assert_eq!(upper, lower.to_ascii_uppercase()); // no owned String needed
let c1 = str8::from("abcdef"); // string concatenation with + for strN types
let c2 = str8::from("xyz123"); // this features is not available for fstr and tstr
let c3 = c1 + c2; // new in Version 0.2.10
assert_eq!(c3,"abcdefxyz123");
assert_eq!(c3.capacity(),15); // type of c3 is str16
// New in Version 0.2.11:
let c4 = str_format!(str16,"abc {}{}{}",1,2,3); // impls std::fmt::Write
assert_eq!(c4,"abc 123"); // str_format! truncates if capacity exceeded
let c5 = try_format!(str8,"abcdef{}","ghijklmn");
assert!(c5.is_none()); // try_format! returns None if capacity exceeded
// New in Version 0.3.0:
let mut s = <zstr<8>>::from("abcd");
s[0] = b'A'; // implements IndexMut<usize> (only for zstr)
assert_eq!(&s[0..3],"Abc");zstr and the type aliases str4…str256 implement the same functions and traits as fstr.
Re-exports
pub use zero_terminated::*;pub use flexible_string::*;
Modules
- This module implements zstr, which are zero-terminated strings of fixed maximum lengths. Compared to crate::fstr, these strings are more memory efficient but with some of the operations taking slightly longer. Type zstr<N> can store strings consisting of up to N-1 bytes whereas fstr<N> can store strings consisting of up to N bytes. Also, it is assumed that the zstr may carray non-textul data and therefore implements some of the traits differently.
Macros
- creates a formated string of given type (by implementing std::fmt::Write):
- version of str_format! that returns an Option of the given type.
Structs
Type Definitions
- strings of up to three 8-bit chars, good enough to represent abbreviations such as those for states and airports. Each str<4> is exactly 32 bits.
- Each type strN is represented underneath by a
[u8;N]with N<=256. The first byte of the array always holds the length of the string. Each such type can hold a string of up to N-1 bytes, with max size=255. These types represent the best combination of fstr and zstr in terms of speed and memory efficiency. Consult documentation for fstr or zstr for the same functions and traits.
In addition, the str4-str128 types implement std::ops::Add, allowing for string concatenation of strings of the same type. For example, two str8 strings will always concatenate to str16, and similarly for all other strN types up to str128.