#case-insensitive #hashmap #case-folding #unicase

case_insensitive_hashmap

A HashMap that uses case-insensitive strings as keys

3 releases (1 stable)

1.0.0 Jun 5, 2020
0.9.1 Jun 5, 2020
0.9.0 Jun 5, 2020

#343 in Text processing

Download history 18/week @ 2021-09-29 18/week @ 2021-10-06 12/week @ 2021-10-13 7/week @ 2021-10-20 13/week @ 2021-10-27 19/week @ 2021-11-03 13/week @ 2021-11-10 28/week @ 2021-11-17 49/week @ 2021-11-24 33/week @ 2021-12-01 33/week @ 2021-12-08 23/week @ 2021-12-15 15/week @ 2021-12-22 17/week @ 2021-12-29 20/week @ 2022-01-05 52/week @ 2022-01-12

109 downloads per month
Used in 2 crates

MIT license

23KB
484 lines

CaseInsensitiveHashMap

A wrapper around the std::collections::HashMap that uses case-insensitive Strings for keys.

Since this is a simple wrapper around the standard HashMap, please see its documentation for more information.

The key type of the CaseInsensitiveHashMap is always UniCase<String>. Most methods that have a key parameter have a constraint <K: Into<Key>>. This means that you can call them with a String, a &str or a UniCase<String> if you already have one. This make the API more ergonomic than the alternative of using UniCase<String> directly as a key type in your own std::collections::HashMap.

Examples

use unicase::UniCase;
use case_insensitive_hashmap::CaseInsensitiveHashMap;

let mut map = CaseInsensitiveHashMap::new();
map.insert("a", 20);
map.insert("B".to_string(), 40);

// All these are valid key forms.
assert!(map.contains_key("A"));
assert!(map.contains_key("A".to_string()));
let uc = UniCase::new("A".to_string());
assert!(map.contains_key(uc));

// Lookup of values is case-insensitive.
assert_eq!(map.get("a"), Some(&20));
assert_eq!(map.get("A"), Some(&20));

assert_eq!(map["a"], 20);
assert_eq!(map["A"], 20);

Implementation

This use the UniCase crate to handle the case-insensitivity. Strings that are used as keys are wrapped in UniCase objects so that they hash and compare for equality in a case-insensitive manner.

Dependencies

~95KB

{o^