File size: 8,419 Bytes
9425aed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/// Unicode Engine — deterministic Unicode normalization and analysis
/// Provides NFC/NFKC normalization, code point analysis, bidirectional detection,
/// and roundtrip verification with zero external state.

use wasm_bindgen::prelude::*;
use unicode_normalization::UnicodeNormalization;
#[allow(unused_imports)]
use serde_json::{json, Value};

/// Normalize text to specified Unicode form (NFC or NFKC)
///
/// # Arguments
/// * `input` - The text to normalize
/// * `form` - "NFC" or "NFKC" (case-insensitive)
///
/// # Returns
/// Normalized UTF-8 string
#[wasm_bindgen]
pub fn normalize(input: &str, form: &str) -> String {
    match form.to_uppercase().as_str() {
        "NFC" => input.nfc().collect::<String>(),
        "NFKC" => input.nfkc().collect::<String>(),
        _ => input.to_string(), // Default: return as-is
    }
}

/// Encode text to comprehensive Unicode representation
///
/// # Returns
/// JSON object with structure:
/// {
///   "normalized": "NFC-normalized text",
///   "codePoints": [array of decimal code points],
///   "utf8Bytes": [array of decimal byte values],
///   "length": number of Unicode scalar values,
///   "byteLength": byte count of UTF-8 encoding
/// }
#[wasm_bindgen]
pub fn encode(input: &str) -> String {
    let normalized = input.nfc().collect::<String>();
    let code_points: Vec<u32> = input.chars().map(|c| c as u32).collect();
    let utf8_bytes: Vec<u8> = input.as_bytes().to_vec();
    let length = input.chars().count();
    let byte_length = utf8_bytes.len();

    let result = json!({
        "normalized": normalized,
        "codePoints": code_points,
        "utf8Bytes": utf8_bytes,
        "length": length,
        "byteLength": byte_length
    });

    result.to_string()
}

/// Detect whether input contains astral plane characters (code points > 0xFFFF)
///
/// Astral characters require surrogate pairs in UTF-16 and special handling in JS
#[wasm_bindgen]
pub fn has_astral_characters(input: &str) -> bool {
    input.chars().any(|c| (c as u32) > 0xFFFF)
}

/// Detect combining marks (Unicode block U+0300–U+036F, also U+1AB0–U+1AFF, etc.)
///
/// Returns true if input contains any combining diacritical marks
#[wasm_bindgen]
pub fn has_combining_marks(input: &str) -> bool {
    input.chars().any(|c| {
        let code = c as u32;
        // Combining Diacritical Marks: U+0300–U+036F
        (code >= 0x0300 && code <= 0x036F) ||
        // Combining Diacritical Marks Extended: U+1AB0–U+1AFF
        (code >= 0x1AB0 && code <= 0x1AFF) ||
        // Combining Diacritical Marks Supplement: U+1DC0–U+1DFF
        (code >= 0x1DC0 && code <= 0x1DFF) ||
        // Combining Half Marks: U+FE20–U+FE2F
        (code >= 0xFE20 && code <= 0xFE2F)
    })
}

/// Detect bidirectional text direction
///
/// Returns one of: "LTR" (left-to-right), "RTL" (right-to-left), "NEUTRAL"
#[wasm_bindgen]
pub fn detect_bidi_level(input: &str) -> String {
    let mut has_rtl = false;
    let mut has_ltr = false;

    for c in input.chars() {
        let code = c as u32;

        // RTL ranges: Hebrew, Arabic, Syriac, Thaana, Devanagari (some), etc.
        if (code >= 0x0590 && code <= 0x08FF) ||  // Hebrew + Arabic
           (code >= 0xFB1D && code <= 0xFB4F) ||  // Hebrew presentation forms
           (code >= 0xFB50 && code <= 0xFDFF) ||  // Arabic presentation forms A
           (code >= 0xFE70 && code <= 0xFEFF) {   // Arabic presentation forms B
            has_rtl = true;
        }

        // LTR ranges: Latin, Greek, Cyrillic, etc.
        if (code >= 0x0041 && code <= 0x005A) ||  // A-Z
           (code >= 0x0061 && code <= 0x007A) ||  // a-z
           (code >= 0x0391 && code <= 0x03C9) ||  // Greek
           (code >= 0x0410 && code <= 0x044F) {   // Cyrillic
            has_ltr = true;
        }
    }

    match (has_rtl, has_ltr) {
        (true, false) => "RTL".to_string(),
        (false, true) => "LTR".to_string(),
        (true, true) => "MIXED".to_string(),
        (false, false) => "NEUTRAL".to_string(),
    }
}

/// Verify encode→decode roundtrip consistency
///
/// Encodes text, extracts code points, reconstructs, and checks equality
/// Returns true if roundtrip preserves original string
#[wasm_bindgen]
pub fn verify_roundtrip(input: &str) -> bool {
    // Method 1: Direct encode/decode via code points
    let encoded: String = input
        .chars()
        .filter_map(|c| char::from_u32(c as u32))
        .collect();

    if encoded != input {
        return false;
    }

    // Method 2: NFC roundtrip
    let nfc_form: String = input.nfc().collect();
    let nfc_again: String = nfc_form.nfc().collect();

    if nfc_form != nfc_again {
        return false;
    }

    // Method 3: UTF-8 byte roundtrip
    let bytes = input.as_bytes();
    if let Ok(reconstructed) = std::str::from_utf8(bytes) {
        reconstructed == input
    } else {
        false
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_normalize_nfc() {
        // é can be represented as single character or e + combining acute
        let decomposed = "e\u{0301}"; // e + combining acute
        let normalized = normalize(decomposed, "NFC");
        let expected = "é"; // single character
        assert_eq!(normalized, expected);
    }

    #[test]
    fn test_normalize_nfkc() {
        let input = "fi"; // fi ligature U+FB01
        let normalized = normalize(input, "NFKC");
        assert_eq!(normalized, "fi");
    }

    #[test]
    fn test_encode_ascii() {
        let input = "hello";
        let result = encode(input);
        let parsed: Value = serde_json::from_str(&result).unwrap();

        assert_eq!(parsed["normalized"], "hello");
        assert_eq!(parsed["length"], 5);
        assert_eq!(parsed["byteLength"], 5);
        assert_eq!(
            parsed["codePoints"].as_array().unwrap().len(),
            5
        );
    }

    #[test]
    fn test_encode_emoji() {
        let input = "👋"; // waving hand emoji
        let result = encode(input);
        let parsed: Value = serde_json::from_str(&result).unwrap();

        assert_eq!(parsed["length"], 1);
        assert_eq!(parsed["byteLength"], 4); // UTF-8 encoding of U+1F44B
    }

    #[test]
    fn test_has_astral_characters_true() {
        assert!(has_astral_characters("👋")); // Emoji is astral
        assert!(has_astral_characters("𝕳𝖊𝖑𝖑𝖔")); // Mathematical alphanumeric symbols
    }

    #[test]
    fn test_has_astral_characters_false() {
        assert!(!has_astral_characters("hello"));
        assert!(!has_astral_characters("café"));
        assert!(!has_astral_characters("日本語")); // CJK is within BMP
    }

    #[test]
    fn test_has_combining_marks_true() {
        assert!(has_combining_marks("e\u{0301}")); // Explicit combining mark (e + combining acute)
        assert!(has_combining_marks("a\u{0308}")); // a with combining diaeresis
    }

    #[test]
    fn test_has_combining_marks_false() {
        assert!(!has_combining_marks("hello"));
        assert!(!has_combining_marks("é")); // Precomposed form (NFC) has no combining marks
    }

    #[test]
    fn test_detect_bidi_ltr() {
        let level = detect_bidi_level("Hello World");
        assert_eq!(level, "LTR");
    }

    #[test]
    fn test_detect_bidi_rtl() {
        let level = detect_bidi_level("שלום עולם"); // Hebrew
        assert_eq!(level, "RTL");
    }

    #[test]
    fn test_detect_bidi_mixed() {
        let level = detect_bidi_level("Hello שלום");
        assert_eq!(level, "MIXED");
    }

    #[test]
    fn test_detect_bidi_neutral() {
        let level = detect_bidi_level("123 !@#");
        assert_eq!(level, "NEUTRAL");
    }

    #[test]
    fn test_verify_roundtrip_ascii() {
        assert!(verify_roundtrip("hello"));
        assert!(verify_roundtrip("The quick brown fox jumps over the lazy dog"));
    }

    #[test]
    fn test_verify_roundtrip_unicode() {
        assert!(verify_roundtrip("café"));
        assert!(verify_roundtrip("日本語"));
        assert!(verify_roundtrip("👋🌍"));
    }

    #[test]
    fn test_verify_roundtrip_empty() {
        assert!(verify_roundtrip(""));
    }

    #[test]
    fn test_verify_roundtrip_complex() {
        // Complex string with multiple scripts and astral characters
        let complex = "Hello مرحبا שלום 日本語 👋";
        assert!(verify_roundtrip(complex));
    }
}