Expand description
§charmap
A Rust library for one-to-(none/one/many) character mapping. It’s main use-case is preprocessing, transliterating, and cleaning natural language text.
§Usage
To use charmap with libstd’s mapping types
(HashMap and
BTreeMap),
add the following to your Cargo.toml:
[dependencies]
charmap = "0.2"This should also allow you to use
rustc-hash’s
FxHashMap
since it is an instance of libstd’s
HashMap.
charmap also supports hashbrown’s
HashMap and
phf’s
Map and
OrderedMap types.
You can enable these by setting the "hashbrown" and "phf" features
respectively.
For example, to use charmap with phf,
add the following to your Cargo.toml:
[dependencies]
charmap = {version = "0.2", features = ["phf"]}You can also disable libstd support for no_std builds by setting
default-features = false. For example:
[dependencies]
charmap = {version = "0.2", default-features = false, features = ["phf"]}§Example
Below is an example of how to use charmap with libstd’s
HashMap:
use std::collections::HashMap;
use charmap::*;
// We first define our action map that will tell our CharMapper what to do
// when it sees a particular character.
let actions = HashMap::from([
('!', CharMapAction::Delete), // Delete instances of '!'
('l', CharMapAction::SubStr("LLL")), // Substitute instances of 'l' with 'LLL'
]);
// This is the string we want to charmap.
let start_str = "Hello, world!";
// Create a character mapper using the previously defined actions while
// allowing all other character to be output as they are.
let mapper = CharMapper::new(&actions, CharMapAction::Pass);
// Use mapper to charmap start_str
let mapped_str: String = start_str.map_chars(&mapper).collect();
// Output should be: HeLLLLLLo, worLLLd
println!("{}", mapped_str);We can also use a different default action to apply to characters not defined in our actions map:
use std::collections::HashMap;
use charmap::*;
// We first define our action map that will tell our CharMapper what to do
// when it sees a particular character.
let actions = HashMap::from([
('!', CharMapAction::Delete), // Delete instances of '!'
('l', CharMapAction::SubStr("LLL")), // Substitute instances of 'l' with 'LLL'
('o', CharMapAction::Pass), // Output instances of 'o' as is
]);
// This is the string we want to charmap.
let start_str = "Hello, world!";
// Create a character mapper using the previously defined actions while
// replacing all other characters with "-".
let mapper = CharMapper::new(&actions, CharMapAction::SubChar('-'));
// Use mapper to charmap start_str
let mapped_str: String = start_str.map_chars(&mapper).collect();
// Output should be: --LLLLLLo---o-LLL-
println!("{}", mapped_str);Structs§
- Char
Mapper - Primary struct used for character mapping.
- Mapped
Chars - Character iterator returned by
CharMapper::map_chars_iterandMapCharsIter::map_chars.
Enums§
- Char
MapAction - An enum representing an action to be taken by a
CharMapperwhen it sees a certainchar.
Traits§
- Action
Map - A trait that should be implemented by any struct providing char-to-action
mappings for
CharMapper. - MapChars
Iter - A trait providing a convenience method for
Iteratorsofcharto charmap their output.