#case-insensitive #hash-map #string-key #case-folding #api-key #unicase

case_insensitive_hashmap

A HashMap that uses case-insensitive strings as keys

4 releases (2 stable)

1.0.1 Jul 5, 2024
1.0.0 Jun 5, 2020
0.9.1 Jun 5, 2020
0.9.0 Jun 5, 2020

#216 in Text processing

Download history 275/week @ 2024-08-26 212/week @ 2024-09-02 254/week @ 2024-09-09 157/week @ 2024-09-16 166/week @ 2024-09-23 329/week @ 2024-09-30 423/week @ 2024-10-07 254/week @ 2024-10-14 304/week @ 2024-10-21 265/week @ 2024-10-28 215/week @ 2024-11-04 169/week @ 2024-11-11 210/week @ 2024-11-18 313/week @ 2024-11-25 313/week @ 2024-12-02 352/week @ 2024-12-09

1,193 downloads per month
Used in 3 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 uses 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.

Release Notes

  • 1.0.1 - bumped version of UniCase to 2.7.0.

Dependencies

~91KB