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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
#![doc = include_str!("../README.md")]

pub use self::apf_block::*;
pub use self::hebrew_block::*;
pub use self::script::*;

pub mod script {
    use crate::is_hebrew_apf_block;
    use crate::is_hebrew_block;

    /// Checks if the given character belongs to the unicode script 'Hebrew'.
    ///
    /// # Example
    ///
    /// ```
    /// use hebrew_unicode_script::is_hebrew_script;
    /// let hebrew_ch = 'מ';
    /// assert!(is_hebrew_script(hebrew_ch));
    ///
    /// let non_hebrew_ch = 'д';
    /// assert!(!is_hebrew_script(non_hebrew_ch));
    /// ```
    pub fn is_hebrew_script(c: char) -> bool {
        match c {
            c if is_hebrew_block(c) => true,
            c if is_hebrew_apf_block(c) => true,
            _ => false,
        }
    }
}

pub mod hebrew_block {
    /// Checks if the given character belongs to the unicode block 'Hebrew'.
    ///
    /// # Examples
    /// ```
    /// use hebrew_unicode_script::is_hebrew_block;
    ///
    /// let test_str = "וַֽיְחִי־שֵׁ֗םאַֽחֲרֵי֙הוֹלִיד֣וֹאֶת־אַרְפַּכְשָׁ֔דחֲמֵ֥שׁמֵא֖וֹתשָׁנָ֑הוַיּ֥וֹלֶדבָּנִ֖יםוּבָנֽוֹת׃";
    /// for c in test_str.chars() {
    ///    assert!(is_hebrew_block(c));
    /// }
    /// ```
    pub fn is_hebrew_block(c: char) -> bool {
        match c {
            c if is_accent(c) => true,
            c if is_mark(c) => true,
            c if is_vowel_point(c) => true,
            c if is_punctuation(c) => true,
            c if is_letter(c) => true,
            c if is_yod_triangle(c) => true,
            c if is_ligature_yiddish(c) => true,
            _ => false,
        }
    }

    /// Checks if the given character is a Hebrew acccent.
    ///
    /// # Example
    ///
    /// ```
    /// use hebrew_unicode_script::is_accent;
    /// use is_even::IsEven;
    ///
    /// let test_str = "ב֑";
    /// for (position, c) in test_str.chars().enumerate() {
    ///   let position_u8 = u8::try_from(position).unwrap();
    ///   if position_u8.is_even() {
    ///     // even position is a normal letter (non-accent)
    ///     assert!(!is_accent(c));
    ///   } else {
    ///     assert!(is_accent(c));
    ///   }
    /// }
    /// ```
    pub fn is_accent(c: char) -> bool {
        matches!(c, '\u{0591}'..='\u{05AE}')
    }

    /// Checks if the given character is a Hebrew mark.
    ///
    /// # Example
    ///
    /// ```
    /// use hebrew_unicode_script::is_mark;
    /// use is_even::IsEven;
    ///
    /// let test_str = "ב֯";
    /// for (position, c) in test_str.chars().enumerate() {
    ///   let position_u8 = u8::try_from(position).unwrap();
    ///   if position_u8.is_even() {
    ///     // even position is a normal letter (non-mark)
    ///     assert!(!is_mark(c));
    ///   } else {
    ///     assert!(is_mark(c));
    ///   }
    /// }
    /// ```
    pub fn is_mark(c: char) -> bool {
        // 05AF + 05C4 + 05C5
        matches!(c, '\u{05AF}' | '\u{05C4}' | '\u{05C5}')
    }
    /// Checks if the given character is a Hebrew point (vowel)
    ///
    /// # Example
    ///
    /// ```
    /// use hebrew_unicode_script::is_vowel_point;
    /// use is_even::IsEven;
    ///
    /// let test_str = "מָ";
    /// for (position, c) in test_str.chars().enumerate() {
    ///   let position_u8 = u8::try_from(position).unwrap();
    ///   if position_u8.is_even() {
    ///     // even position is a normal letter (non-vowel-point)
    ///     assert!(!is_vowel_point(c));
    ///   } else {
    ///     assert!(is_vowel_point(c));
    ///   }
    /// }
    /// ```
    pub fn is_vowel_point(c: char) -> bool {
        // 05B0 .. 05BD + 05BF + 05C1 .. 05C2 + 05C7
        matches!(c,
            '\u{05B0}'..='\u{05BD}'
            | '\u{05BF}'
            | '\u{05C1}'..='\u{05C2}'
            | '\u{05C7}'
        )
    }
    /// Checks if the given character is a Hebrew punctuation.
    ///
    /// # Example
    ///
    /// ```
    /// use hebrew_unicode_script::is_mark;
    /// use is_even::IsEven;
    ///
    /// let test_str = "ב֯";
    /// for (position, c) in test_str.chars().enumerate() {
    ///   let position_u8 = u8::try_from(position).unwrap();
    ///   if position_u8.is_even() {
    ///     // even position is a normal letter (non_mark)
    ///     assert!(!is_mark(c));
    ///   } else {
    ///     assert!(is_mark(c));
    ///   }
    /// }
    /// ```
    pub fn is_punctuation(c: char) -> bool {
        // 05BE + 05C0 + 05C3 + 05C6 + 05F3 + 05F4
        matches!(
            c,
            '\u{05BE}' | '\u{05C0}' | '\u{05C3}' | '\u{05C6}' | '\u{05F3}'..='\u{05F4}'
        )
    }
    /// Checks if the given character is a Hebrew letter (normal).
    ///
    /// # Example
    ///
    /// ```
    /// use hebrew_unicode_script::is_letter_normal;
    /// use hebrew_unicode_script::is_letter;
    ///
    /// let test_str = "אבגדהוזחטיכלמנסעפצקרשת";
    /// for c in test_str.chars() {
    ///     assert!(is_letter_normal(c));
    ///     assert!(is_letter(c));
    /// }
    /// ```
    pub fn is_letter_normal(c: char) -> bool {
        // 05D0..05D9 + 05DB..05DC + 05DE + 05E0..05E2 + 05E4 + 05E6..05EA
        matches!(
            c,
            '\u{05D0}'..='\u{05D9}'
            | '\u{05DB}'..='\u{05DC}'
            | '\u{05DE}'
            | '\u{05E0}'..='\u{05E2}'
            | '\u{05E4}'
            | '\u{05E6}'..='\u{05EA}'
        )
    }
    /// Checks if the given character is a Hebrew letter (final).
    ///
    /// # Example
    ///
    /// ```
    /// use hebrew_unicode_script::is_letter_final;
    /// use hebrew_unicode_script::is_letter;
    ///
    /// let test_str = "ךםןףץ";
    /// for c in test_str.chars() {
    ///     assert!(is_letter_final(c));
    ///     assert!(is_letter(c));
    /// }
    /// ```
    pub fn is_letter_final(c: char) -> bool {
        // 05DA + 05DD + 05DF + 05E3 + 05E5
        matches!(c,'\u{05DA}' | '\u{05DD}' | '\u{05DF}' | '\u{05E3}' | '\u{05E5}')
    }
    /// Checks if the given character is a Hebrew letter (final OR normal)
    ///
    /// # Example
    ///
    /// ```
    /// use hebrew_unicode_script::is_letter;
    /// use is_even::IsEven;
    ///
    /// let test_str = "אבגדהוזחטיכךלמםנןסעפףצץקרשת";
    /// for c in test_str.chars() {
    ///   assert!(is_letter(c));
    /// }
    /// ```
    pub fn is_letter(c: char) -> bool {
        // 05D0 .. 05EA
        matches!(c, '\u{05D0}'..='\u{05EA}')
    }
    /// Checks if the given character is a Hebrew yod triangle.
    ///
    /// # Example
    ///
    /// ```
    /// use hebrew_unicode_script::is_yod_triangle;
    ///
    /// let yod_triangle = '\u{05EF}';
    /// assert!(is_yod_triangle(yod_triangle));
    /// ```
    pub fn is_yod_triangle(c: char) -> bool {
        matches!(c, '\u{05EF}')
    }
    /// Checks if the given character is a Yiddish ligature.
    ///
    /// # Example
    ///
    /// ```
    /// use hebrew_unicode_script::is_ligature_yiddish;
    ///
    /// let test_str = "װױײ";
    /// for c in test_str.chars() {
    ///   assert!(is_ligature_yiddish(c));
    /// }
    /// ```
    pub fn is_ligature_yiddish(c: char) -> bool {
        // 05F0 .. 05F2
        matches!(c, '\u{05F0}'..='\u{05F2}')
    }
}

pub mod apf_block {
    /// Checks if the given character belongs to the unicode block 'Alphabetic Presentation Form'.
    ///
    /// # Example
    ///
    /// ```
    /// use hebrew_unicode_script::is_hebrew_apf_block;
    ///
    /// let apf_ch = '\u{FB22}'; // HEBREW LETTER WIDE DALET
    /// assert!(is_hebrew_apf_block(apf_ch));
    ///
    /// let non_apf_ch = '\u{FB13}'; // ARMENIAN SMALL LIGATURE MEN NOW
    /// assert!(!is_hebrew_apf_block(non_apf_ch));
    /// ```
    pub fn is_hebrew_apf_block(c: char) -> bool {
        match c {
            c if is_apf_point(c) => true,
            c if is_apf_letter(c) => true,
            c if is_apf_ligature_yiddisch(c) => true,
            c if is_apf_ligature(c) => true,
            _ => false,
        }
    }
    /// Checks if the given character is an AFP point.
    ///
    /// # Example
    ///
    /// ```
    /// use hebrew_unicode_script::is_apf_point;
    ///
    /// let ly = '\u{FB1E}';
    /// assert!(is_apf_point(ly));
    /// ```
    pub fn is_apf_point(c: char) -> bool {
        // U+FB1E
        matches!(c, '\u{FB1E}')
    }
    /// Checks if the given character is an AFP letter.
    ///
    /// # Example
    ///
    /// ```
    /// use hebrew_unicode_script::is_apf_letter;
    ///
    /// let afp_l = '\u{FB3E}';
    /// assert!(is_apf_letter(afp_l));
    ///
    /// let afp_l = 'p';
    /// assert!(!is_apf_letter(afp_l));
    /// ```
    pub fn is_apf_letter(c: char) -> bool {
        // U+FB1D + (U+FB20 .. U+FB36) + (U+FB38 .. U+FB3C) + U+FB3E + (U+FB40 .. U+FB41)
        // + (U+FB43 .. U+FB44) + (U+FB46 .. U+FB4E)
        matches!(
            c,
            '\u{FB1D}'
            | '\u{FB20}'..='\u{FB36}'
            | '\u{FB38}'..='\u{FB3C}'
            | '\u{FB3E}'
            | '\u{FB40}'..='\u{FB41}'
            | '\u{FB43}'..='\u{FB44}'
            | '\u{FB46}'..='\u{FB4E}'
        )
    }
    /// Checks if the given character is a AFP Yiddish ligature.
    ///
    /// # Example
    ///
    /// ```
    /// use hebrew_unicode_script::is_apf_ligature_yiddisch;
    ///
    /// // HEBREW LIGATURE YIDDISH YOD YOD PATAH
    /// let test_ch = '\u{FB1F}';
    /// assert!(is_apf_ligature_yiddisch(test_ch));
    ///
    /// // ARABIC LETTER TEHEH ISOLATED FORM
    /// let test_ch = '\u{FB62}';
    ///   assert!(!is_apf_ligature_yiddisch(test_ch));
    /// ```
    pub fn is_apf_ligature_yiddisch(c: char) -> bool {
        // U+FB1F
        matches!(c, '\u{FB1F}')
    }
    /// Checks if the given character is a Yiddish ligature.
    ///
    /// ```
    /// use hebrew_unicode_script::is_apf_ligature;
    ///
    /// // HEBREW LIGATURE ALEF LAMED
    /// let test_ch = '\u{FB4F}';
    /// assert!(is_apf_ligature(test_ch));
    ///
    /// // HEBREW LIGATURE YIDDISH YOD YOD PATAH
    /// let test_ch = '\u{FB1F}';
    /// assert!(!is_apf_ligature(test_ch));
    /// ```
    pub fn is_apf_ligature(c: char) -> bool {
        // U+FB4F
        matches!(c, '\u{FB4F}')
    }
}
#[cfg(test)]
mod tests {
    use super::*;
    use is_even::IsEven;

    #[test]
    fn test_hebrew_script() {
        let letter_a = '\u{0041}'; // A
        assert!(!is_hebrew_script(letter_a));

        let letter_aleph = '\u{05D0}'; // א HEBREW LETTER ALEF
        assert!(is_hebrew_script(letter_aleph));
        
        let letter_aleph = '\u{FB1E}'; // HEBREW POINT JUDEO-SPANISH VARIKA
        assert!(is_hebrew_script(letter_aleph));
    }
    #[test]
    fn test_hebrew_accent() {
        let test_str1 = "ב֑ב֒ב֓ב֔ב֕ב֖ב֗ב֘ב֙ב֚";
        let test_str2 = "ב֛ב֜ב֝ב֞ב֟ב֠ב֡ב֢ב֣ב֤";
        let test_str3 = "ב֥ב֦ב֧ב֨ב֩ב֪ב֫ב֬ב֭ב֮";
        check_accents(&test_str1);
        check_accents(&test_str2);
        check_accents(&test_str3);
    }
    #[test]
    fn test_hebrew_mark() {
        // 05AF + 05C4 + 05C5
        let test_str = "ב֯בׄבׅ";
        check_marks(&test_str);
    }
    #[test]
    fn test_hebrew_vowel_point() {
        let test_str = "בְבֱבֲבֳבִבֵבֶבַבָבֹבֺבֻבּבֽבֿבׁבׂבׇ";
        check_vowel_points(&test_str);
    }
    #[test]
    fn test_hebrew_puncuation() {
        let test_str = "ב־ב׀ב׃ב׆ב׳נ״";
        check_punctuations(&test_str);
    }
    #[test]
    fn test_hebrew_letter() {
        let test_str = "אבגדהוזחטיכךלמםנןסעפףצץקרשת";
        for c in test_str.chars() {
            assert!(is_letter(c));
        }
    }
    #[test]
    fn test_hebrew_letter_not_final() {
        let test_str = "אבגדהוזחטיכלמנסעפצקרשת";
        for c in test_str.chars() {
            assert!(is_letter_normal(c));
            assert!(is_letter(c));
        }
    }
    #[test]
    fn test_hebrew_letter_final() {
        let test_str = "ךםןףץ";
        for c in test_str.chars() {
            assert!(is_letter_final(c));
            assert!(is_letter(c));
        }
    }
    #[test]
    fn test_hebrew_yiddish_chars() {
        let test_str = "װױײ";
        for c in test_str.chars() {
            assert!(is_ligature_yiddish(c));
        }
    }
    #[test]
    fn test_hebrew_yod_triangle() {
        let yod_triangle = '\u{05EF}';
        assert!(is_yod_triangle(yod_triangle));
    }

    #[test]
    fn test_apf_point() {
        let test_ch = '\u{FB1E}'; // HEBREW POINT JUDEO-SPANISH VARIKA

        // unicode script 'Hebrew'
        assert_eq!(is_hebrew_script(test_ch), true);

        // unicode block Hebrew
        assert_eq!(is_hebrew_block(test_ch), false);
        assert_eq!(is_accent(test_ch), false);
        assert_eq!(is_mark(test_ch), false);
        assert_eq!(is_vowel_point(test_ch), false);
        assert_eq!(is_punctuation(test_ch), false);
        assert_eq!(is_letter_normal(test_ch), false);
        assert_eq!(is_letter(test_ch), false);
        assert_eq!(is_yod_triangle(test_ch), false);
        assert_eq!(is_ligature_yiddish(test_ch), false);

        // unicode block 'Alphabetic Presentation Form'
        assert_eq!(is_hebrew_apf_block(test_ch), true);
        assert_eq!(is_apf_point(test_ch), true);
        assert_eq!(is_apf_letter(test_ch), false);
        assert_eq!(is_apf_ligature_yiddisch(test_ch), false);
        assert_eq!(is_apf_ligature(test_ch), false);
    }

    #[test]
    fn test_apf_letter() {
        let test_ch = '\u{FB1D}'; // HEBREW LETTER YOD WITH HIRIQ

        // unicode script 'Hebrew'
        assert_eq!(is_hebrew_script(test_ch), true);

        // unicode block Hebrew
        assert_eq!(is_hebrew_block(test_ch), false);
        assert_eq!(is_accent(test_ch), false);
        assert_eq!(is_mark(test_ch), false);
        assert_eq!(is_vowel_point(test_ch), false);
        assert_eq!(is_punctuation(test_ch), false);
        assert_eq!(is_letter_normal(test_ch), false);
        assert_eq!(is_letter(test_ch), false);
        assert_eq!(is_yod_triangle(test_ch), false);
        assert_eq!(is_ligature_yiddish(test_ch), false);

        // unicode block 'Alphabetic Presentation Form'
        assert_eq!(is_hebrew_apf_block(test_ch), true);
        assert_eq!(is_apf_point(test_ch), false);
        assert_eq!(is_apf_letter(test_ch), true);
        assert_eq!(is_apf_ligature_yiddisch(test_ch), false);
        assert_eq!(is_apf_ligature(test_ch), false);
    }

    #[test]
    fn test_apf_ligature_yiddisch() {
        let test_ch = '\u{FB1F}'; // HEBREW LIGATURE YIDDISH YOD YOD PATAH

        // unicode script 'Hebrew'
        assert_eq!(is_hebrew_script(test_ch), true);

        // unicode block Hebrew
        assert_eq!(is_hebrew_block(test_ch), false);
        assert_eq!(is_accent(test_ch), false);
        assert_eq!(is_mark(test_ch), false);
        assert_eq!(is_vowel_point(test_ch), false);
        assert_eq!(is_punctuation(test_ch), false);
        assert_eq!(is_letter_normal(test_ch), false);
        assert_eq!(is_letter(test_ch), false);
        assert_eq!(is_yod_triangle(test_ch), false);
        assert_eq!(is_ligature_yiddish(test_ch), false);

        // unicode block 'Alphabetic Presentation Form'
        assert_eq!(is_hebrew_apf_block(test_ch), true);
        assert_eq!(is_apf_point(test_ch), false);
        assert_eq!(is_apf_letter(test_ch), false);
        assert_eq!(is_apf_ligature_yiddisch(test_ch), true);
        assert_eq!(is_apf_ligature(test_ch), false);
    }

    #[test]
    fn test_apf_ligature() {
        let test_ch = '\u{FB4F}'; // HEBREW LETTER WIDE DALET

        // unicode script 'Hebrew'
        assert_eq!(is_hebrew_script(test_ch), true);

        // unicode block Hebrew
        assert_eq!(is_hebrew_block(test_ch), false);
        assert_eq!(is_accent(test_ch), false);
        assert_eq!(is_mark(test_ch), false);
        assert_eq!(is_vowel_point(test_ch), false);
        assert_eq!(is_punctuation(test_ch), false);
        assert_eq!(is_letter_normal(test_ch), false);
        assert_eq!(is_letter(test_ch), false);
        assert_eq!(is_yod_triangle(test_ch), false);
        assert_eq!(is_ligature_yiddish(test_ch), false);

        // unicode block 'Alphabetic Presentation Form'
        assert_eq!(is_hebrew_apf_block(test_ch), true);
        assert_eq!(is_apf_point(test_ch), false);
        assert_eq!(is_apf_letter(test_ch), false);
        assert_eq!(is_apf_ligature_yiddisch(test_ch), false);
        assert_eq!(is_apf_ligature(test_ch), true);
    }

    fn check_accents(str: &str) {
        // str is an alternation of consonant-accent-consonat-accent ...
        for (position, c) in str.chars().enumerate() {
            let position_u8 = u8::try_from(position).unwrap();
            if position_u8.is_even() {
                //println!("fn check_accents::at even position {} is: {}", pos_u8,c.escape_default());
                assert!(is_letter(c));
            } else {
                //println!("fn check_accents::at odd position {} is: {}", pos_u8, c.escape_default());
                assert!(is_accent(c));
            }
        }
    }
    fn check_marks(str: &str) {
        for (pos, c) in str.chars().enumerate() {
            let pos_u8 = u8::try_from(pos).unwrap();
            if pos_u8.is_even() {
                //println!("fn check_marks::at even position {} is: {}", pos_u8, c.escape_default());
                assert!(is_letter(c));
            } else {
                //println!("fn check_marks::at odd position {} is: {}", pos_u8, c.escape_default());
                assert!(is_mark(c));
            }
        }
    }
    fn check_vowel_points(str: &str) {
        for (pos, c) in str.chars().enumerate() {
            let pos_u8 = u8::try_from(pos).unwrap();
            if pos_u8.is_even() {
                //println!("fn check_vowel_points::char at even position {} is: {}", pos_u8, c.escape_default());
                assert!(is_letter(c));
            } else {
                //println!("fn check_vowel_points::char at odd position {} is: {}", pos_u8, c.escape_default());
                assert!(is_vowel_point(c));
            }
        }
    }
    fn check_punctuations(str: &str) {
        for (pos, c) in str.chars().enumerate() {
            let pos_u8 = u8::try_from(pos).unwrap();
            if pos_u8.is_even() {
                //println!("fn check_punctuations::char at even position {} is: {}", pos_u8, c.escape_default());
                assert!(is_letter(c));
            } else {
                //println!("fn check_punctuations::char at odd position {} is: {}", pos_u8, c.escape_default());
                assert!(is_punctuation(c));
            }
        }
    }
}