string_more
string_more extends String and str with two utility traits:
StringExtfor in-place editing onStringStrExtfor the corresponding immutable operations on string slices
The crate focuses on small, allocation-aware helpers for padding, editing, boundary handling, and simple text analysis.
Installation
or add it manually:
[]
= "0.4"
See CHANGELOG.md for release notes and breaking changes.
Quick Start
use BTreeMap;
use ;
let centered = "rust".center;
assert_eq!;
let mut value = Stringfrom;
value.expand_tabs_in_place;
value.trim_in_place;
assert_eq!;
assert_eq!;
assert_eq!;
let frequencies = "Hello".;
assert_eq!;
API Notes
StringExtmutatesStringin place.StrExtleaves the original value unchanged. Most transformation methods return a newString, while some analysis and slicing helpers return borrowed&strresults.- Most text operations are based on Unicode scalar values (
char), not grapheme clusters. - Methods that accept indices use byte offsets and require valid char boundaries.
- Most text-like inputs accepted by the API can be passed as
char,&str, orStringthrough the sealedEncodeUtf8trait.
Frequency Example
char_frequencies counts Unicode scalar values, while byte_frequencies counts UTF-8 bytes:
use BTreeMap;
use StrExt;
let s = "·x·";
let char_frequencies = s.;
assert_eq!;
assert_eq!;
let byte_frequencies = s.;
assert_eq!;
assert_eq!;
assert_eq!;
Overview
In-place editing with StringExt
trim_start_in_place,trim_end_in_place,trim_in_placefill_start_in_place,fill_end_in_place,center_in_placepad_start_to_in_place,pad_end_to_in_place,center_to_in_placeenclose_in_placeexpand_tabs_in_placestrip_prefix_in_place,strip_suffix_in_placetruncate_to_chars_in_placeshift_in_placereplace_in_place
use StringExt;
let mut s = Stringfrom;
s.expand_tabs_in_place;
s.trim_in_place;
s.strip_prefix_in_place;
s.enclose_in_place;
assert_eq!;
Immutable helpers with StrExt
fill_start,fill_end,centerpad_start_to,pad_end_to,center_toencloseexpand_tabstruncate_to_charsshiftcommon_prefix,common_suffixlongest_common_substringlevenshtein_distance,hamming_distancechar_frequencies,byte_frequenciesnext_char_boundary,previous_char_boundaryjoin
use StrExt;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
Selected Methods
Padding and enclosure
use ;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
let mut s = Stringfrom;
s.enclose_in_place;
assert_eq!;
Truncation and prefix/suffix helpers
use ;
assert_eq!;
assert_eq!;
assert_eq!;
let mut s = Stringfrom;
s.strip_prefix_in_place;
assert_eq!;
Boundary-aware indexing
use ;
assert_eq!;
assert_eq!;
let mut s = Stringfrom;
s.shift_in_place;
assert_eq!;
Distances and substring search
use StrExt;
assert_eq!;
assert_eq!;
assert_eq!;
Safety and Coverage
This crate contains a small amount of unsafe code in the in-place mutation layer.
The public API is covered by unit tests and doctests.
Contributing
Contributions are welcome! Feel free to open an issue or submit a pull request.
License
This crate is licensed under the MIT License. See LICENSE for more details.