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
// Copyright 2017 The UNIC Project Developers.
//
// See the COPYRIGHT file at the top-level directory of this distribution.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.


use super::{bidi_class, BidiClass, is_explicit, is_rtl};


/// Methods for bidi properties of character types.
pub trait BidiChar {

    /// Get `BidiClass` of the character.
    fn bidi_class(self) -> BidiClass;

    /// Whether the character is an *explicit* bidi formatting character.
    fn is_explicit(self) -> bool;

    /// Whether the character has *left-to-right* (LTR) bidi directionality.
    fn is_ltr(self) -> bool;

    /// Whether the character has *right-to-left* (RTL) bidi directionality.
    fn is_rtl(self) -> bool;
}

impl BidiChar for char {
    #[inline]
    fn bidi_class(self) -> BidiClass {
        bidi_class(self)
    }

    #[inline]
    fn is_explicit(self) -> bool {
        is_explicit(bidi_class(self))
    }

    #[inline]
    fn is_ltr(self) -> bool {
        !is_rtl(bidi_class(self))
    }

    #[inline]
    fn is_rtl(self) -> bool {
        is_rtl(bidi_class(self))
    }
}


/// Methods for bidi properties of string types.
pub trait BidiStr {

    /// Whether the string has any *explicit* bidi formatting character.
    fn has_explicit(&self) -> bool;

    /// Whether the string has any character with *left-to-right* (LTR) bidi directionality.
    fn has_ltr(&self) -> bool;

    /// Whether the string has any character with *right-to-left* (RTL) bidi directionality.
    fn has_rtl(&self) -> bool;
}

impl BidiStr for str {
    #[inline]
    fn has_explicit(&self) -> bool {
        self.chars().any(|ch| ch.is_explicit())
    }

    #[inline]
    fn has_ltr(&self) -> bool {
        self.chars().any(|ch| ch.is_rtl())
    }

    #[inline]
    fn has_rtl(&self) -> bool {
        self.chars().any(|ch| ch.is_rtl())
    }
}


#[cfg(test)]
mod tests {
    #[test]
    fn test_bidi_char() {
        use super::{BidiChar, BidiClass};

        let ch = '\u{0041}'; // U+0041 LATIN CAPITAL LETTER A "A"
        assert_eq!(ch.bidi_class(), BidiClass::L);
        assert!(ch.is_ltr());
        assert!(!ch.is_rtl());

        let ch = '\u{05D0}'; // U+05D0 HEBREW LETTER ALEF "א"
        assert_eq!(ch.bidi_class(), BidiClass::R);
        assert!(!ch.is_ltr());
        assert!(ch.is_rtl());

        let ch = '\u{0627}'; // U+0627 ARABIC LETTER ALEF "ا"
        assert_eq!(ch.bidi_class(), BidiClass::AL);
        assert!(!ch.is_ltr());
        assert!(ch.is_rtl());
    }

    #[test]
    fn test_bidi_str() {
        use super::BidiStr;

        let text = "";
        assert!(!text.has_explicit());
        assert!(!text.has_ltr());
        assert!(!text.has_rtl());

        let text = "\u{0041}\u{05D0}\u{0627}";
        assert!(!text.has_explicit());
        assert!(text.has_ltr());
        assert!(text.has_rtl());
    }
}