oni_comb_parser_rs/core/
element.rs

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
use std::fmt::Debug;

/// A Element.<br/>
/// 要素。
pub trait Element: Debug {
  /// 文字に変換する。
  fn to_char(self) -> char;
  /// 空白かどうか。
  fn is_ascii_space(&self) -> bool;
  /// 改行を含む空白かどうか。
  fn is_ascii_multi_space(&self) -> bool;
  /// 空白かどうか。
  fn is_ascii_whitespace(&self) -> bool;

  /// アスキー文字かどうか。
  fn is_ascii(&self) -> bool;
  /// アルファベットの大文字かどうか。
  fn is_ascii_alpha_uppercase(&self) -> bool;
  /// アルファベットの小文字かどうか。
  fn is_ascii_alpha_lowercase(&self) -> bool;

  /// アルファベットかどうか。
  fn is_ascii_alpha(&self) -> bool;

  /// 数字かどうか。
  fn is_ascii_digit(&self) -> bool;
  /// 数字のゼロかどうか。
  fn is_ascii_digit_zero(&self) -> bool;
  /// 数字の非ゼロかどうか。
  fn is_ascii_digit_non_zero(&self) -> bool;
  /// 英数文字かどうか。
  fn is_ascii_alpha_digit(&self) -> bool;

  fn is_ascii_hex_digit(&self) -> bool;
  fn is_ascii_oct_digit(&self) -> bool;

  fn is_ascii_punctuation(&self) -> bool;
  fn is_ascii_graphic(&self) -> bool;
  fn is_ascii_control(&self) -> bool;
}

impl Element for u8 {
  fn to_char(self) -> char {
    char::from(self)
  }

  fn is_ascii_space(&self) -> bool {
    matches!(*self, b' ' | b'\t')
  }

  fn is_ascii_multi_space(&self) -> bool {
    self.is_ascii_space() || matches!(*self, b'\n' | b'\r')
  }

  fn is_ascii_whitespace(&self) -> bool {
    self.is_ascii_multi_space() || *self == b'\x0C'
  }

  fn is_ascii(&self) -> bool {
    *self & 128 == 0
  }

  fn is_ascii_alpha_uppercase(&self) -> bool {
    matches!(*self, b'A'..=b'Z')
  }

  fn is_ascii_alpha_lowercase(&self) -> bool {
    matches!(*self, b'a'..=b'z')
  }

  fn is_ascii_alpha(&self) -> bool {
    matches!(*self, b'A'..=b'Z' | b'a'..=b'z')
  }

  fn is_ascii_digit(&self) -> bool {
    matches!(*self, b'0'..=b'9')
  }

  fn is_ascii_digit_zero(&self) -> bool {
    *self == b'0'
  }

  fn is_ascii_digit_non_zero(&self) -> bool {
    !self.is_ascii_digit_zero()
  }

  fn is_ascii_alpha_digit(&self) -> bool {
    matches!(*self, b'0'..=b'9' | b'A'..=b'Z' | b'a'..=b'z')
  }

  fn is_ascii_hex_digit(&self) -> bool {
    matches!(*self, b'0'..=b'9' | b'A'..=b'F' | b'a'..=b'f')
  }

  fn is_ascii_oct_digit(&self) -> bool {
    matches!(*self, b'0'..=b'8')
  }

  fn is_ascii_punctuation(&self) -> bool {
    matches!(*self, b'!'..=b'/' | b':'..=b'@' | b'['..=b'`' | b'{'..=b'~')
  }

  fn is_ascii_graphic(&self) -> bool {
    matches!(*self, b'!'..=b'~')
  }

  fn is_ascii_control(&self) -> bool {
    matches!(*self, b'\0'..=b'\x1F' | b'\x7F')
  }
}

impl Element for char {
  fn to_char(self) -> char {
    self
  }

  fn is_ascii_space(&self) -> bool {
    matches!(*self, ' ' | '\t')
  }

  fn is_ascii_multi_space(&self) -> bool {
    self.is_ascii_space() || matches!(*self, '\n' | '\r')
  }

  fn is_ascii_whitespace(&self) -> bool {
    self.is_ascii_multi_space() || *self == '\x0C'
  }

  fn is_ascii(&self) -> bool {
    *self as u32 <= 0x7F
  }

  fn is_ascii_alpha_uppercase(&self) -> bool {
    matches!(*self, 'A'..='Z')
  }

  fn is_ascii_alpha_lowercase(&self) -> bool {
    matches!(*self, 'a'..='z')
  }

  fn is_ascii_alpha(&self) -> bool {
    matches!(*self, 'A'..='Z' | 'a'..='z')
  }

  fn is_ascii_digit(&self) -> bool {
    matches!(*self, '0'..='9')
  }

  fn is_ascii_digit_zero(&self) -> bool {
    *self == '0'
  }

  fn is_ascii_digit_non_zero(&self) -> bool {
    !self.is_ascii_digit_zero()
  }

  fn is_ascii_alpha_digit(&self) -> bool {
    matches!(*self, '0'..='9' | 'A'..='Z' | 'a'..='z')
  }

  fn is_ascii_hex_digit(&self) -> bool {
    matches!(*self, '0'..='9' | 'A'..='F' | 'a'..='f')
  }

  fn is_ascii_oct_digit(&self) -> bool {
    matches!(*self, '0'..='8')
  }

  fn is_ascii_punctuation(&self) -> bool {
    matches!(*self, '!'..='/' | ':'..='@' | '['..='`' | '{'..='~')
  }

  fn is_ascii_graphic(&self) -> bool {
    matches!(*self, '!'..='~')
  }

  fn is_ascii_control(&self) -> bool {
    matches!(*self, '\0'..='\x1F' | '\x7F')
  }
}