json_ld_syntax_next/
utils.rs

1use std::{
2	cmp::Ordering,
3	hash::{Hash, Hasher},
4};
5
6pub fn into_smallcase(c: u8) -> u8 {
7	if c.is_ascii_uppercase() {
8		c + 0x20
9	} else {
10		c
11	}
12}
13
14pub fn case_insensitive_eq(a: &[u8], b: &[u8]) -> bool {
15	if a.len() == b.len() {
16		for i in 0..a.len() {
17			if into_smallcase(a[i]) != into_smallcase(b[i]) {
18				return false;
19			}
20		}
21
22		true
23	} else {
24		false
25	}
26}
27
28pub fn case_insensitive_hash<H: Hasher>(bytes: &[u8], hasher: &mut H) {
29	for b in bytes {
30		into_smallcase(*b).hash(hasher)
31	}
32}
33
34pub fn case_insensitive_cmp(a: &[u8], b: &[u8]) -> Ordering {
35	let mut i = 0;
36
37	loop {
38		if a.len() <= i {
39			if b.len() <= i {
40				return Ordering::Equal;
41			}
42
43			return Ordering::Greater;
44		} else if b.len() <= i {
45			return Ordering::Less;
46		} else {
47			match into_smallcase(a[i]).cmp(&into_smallcase(b[i])) {
48				Ordering::Equal => i += 1,
49				ord => return ord,
50			}
51		}
52	}
53}