res_regex/
unicode.rs

1use crate::unicode_tables::{general_category::GC, script_values::SCRIPT, GC_AND_BP};
2
3/// Validate a `LoneUnicodePropertyNameOrValue`
4/// is a valid name or value
5///
6/// ex:
7/// ```js
8/// let re = /\p{White_Space}\p{Alphabetic}/;
9/// ```
10///
11/// This function will first search the General_Category
12/// names and aliases and then the Binary Property
13/// names and aliases
14pub fn validate_name_or_value(name: &str) -> bool {
15    GC_AND_BP.binary_search(&name).is_ok()
16}
17/// Validate a `UnicodePropertyName` and `UnicodePropertyValue`
18/// are correct
19///
20///
21/// ex:
22/// ```js
23/// let re = /\p{Script=Greek}\p{gc=Lm}/
24/// ```
25///
26/// valid names include `General_Category`, `gc`, `Script`,
27/// `Script_Extensions`, `sc` and `scx`
28///  any other names will return false
29pub fn validate_name_and_value(name: &str, value: &str) -> bool {
30    if let Some(set) = validate_name(name) {
31        set.binary_search(&value).is_ok()
32    } else {
33        false
34    }
35}
36
37/// Validate a name is `General_Category`, `gc`, `Script`,
38/// `Script_Extensions`, `sc` or `scx`. This will return
39/// Some with the correct list of possible values
40/// None, otherwise
41pub fn validate_name(name: &str) -> Option<&[&str]> {
42    if name == "General_Category" || name == "gc" {
43        Some(GC)
44    } else if name == "Script" || name == "sc" || name == "Script_Extensions" || name == "scx" {
45        Some(SCRIPT)
46    } else {
47        None
48    }
49}
50
51#[cfg(test)]
52mod test {
53    use super::*;
54    #[test]
55    fn name_and_value() {
56        for value in GC {
57            assert!(validate_name_and_value("General_Category", value));
58            assert!(validate_name_and_value("gc", value));
59        }
60        assert!(!validate_name_and_value("General_Category", "junk"));
61        assert!(!validate_name_and_value("gc", "junk"));
62        for value in SCRIPT {
63            assert!(validate_name_and_value("Script", value));
64            assert!(validate_name_and_value("Script_Extensions", value));
65            assert!(validate_name_and_value("sc", value));
66            assert!(validate_name_and_value("scx", value));
67        }
68        assert!(!validate_name_and_value("Script", "junk"));
69        assert!(!validate_name_and_value("Script_Extensions", "junk"));
70        assert!(!validate_name_and_value("sc", "junk"));
71        assert!(!validate_name_and_value("scx", "junk"));
72    }
73    #[test]
74    fn name_or_value() {
75        for value in GC_AND_BP {
76            assert!(validate_name_or_value(value));
77        }
78        assert!(!validate_name_or_value("junk"));
79    }
80}