@@ -6,29 +6,29 @@ enum type { None, Alphabetic, Numeric, AlphaNumeric }
6
6
7
7
class EmailValidator {
8
8
static int _index = 0 ;
9
- static final _atomCharacters = "!#\$ %&'*+-/=?^_`{|}~" ;
9
+ static const String _atomCharacters = "!#\$ %&'*+-/=?^_`{|}~" ;
10
10
static type _domainType = type.None ;
11
11
12
- static bool _isDigit (c) {
13
- return ( c.codeUnitAt (0 ) >= 48 && c.codeUnitAt (0 ) <= 57 ) ;
12
+ static bool _isDigit (String c) {
13
+ return c.codeUnitAt (0 ) >= 48 && c.codeUnitAt (0 ) <= 57 ;
14
14
}
15
15
16
- static bool _isLetter (c) {
16
+ static bool _isLetter (String c) {
17
17
return (c.codeUnitAt (0 ) >= 65 && c.codeUnitAt (0 ) <= 90 ) ||
18
18
(c.codeUnitAt (0 ) >= 97 && c.codeUnitAt (0 ) <= 122 );
19
19
}
20
20
21
- static bool _isLetterOrDigit (c) {
21
+ static bool _isLetterOrDigit (String c) {
22
22
return _isLetter (c) || _isDigit (c);
23
23
}
24
24
25
- static bool _isAtom (c, bool allowInternational) {
25
+ static bool _isAtom (String c, bool allowInternational) {
26
26
return c.codeUnitAt (0 ) < 128
27
- ? _isLetterOrDigit (c) || _atomCharacters.indexOf (c) != - 1
27
+ ? _isLetterOrDigit (c) || _atomCharacters.contains (c)
28
28
: allowInternational;
29
29
}
30
30
31
- static bool _isDomain (c, bool allowInternational) {
31
+ static bool _isDomain (String c, bool allowInternational) {
32
32
if (c.codeUnitAt (0 ) < 128 ) {
33
33
if (_isLetter (c) || c == '-' ) {
34
34
_domainType = type.Alphabetic ;
@@ -51,7 +51,7 @@ class EmailValidator {
51
51
return false ;
52
52
}
53
53
54
- static bool _isDomainStart (c, bool allowInternational) {
54
+ static bool _isDomainStart (String c, bool allowInternational) {
55
55
if (c.codeUnitAt (0 ) < 128 ) {
56
56
if (_isLetter (c)) {
57
57
_domainType = type.Alphabetic ;
@@ -79,7 +79,7 @@ class EmailValidator {
79
79
}
80
80
81
81
static bool _skipAtom (String text, bool allowInternational) {
82
- int startIndex = _index;
82
+ final int startIndex = _index;
83
83
84
84
while (_index < text.length && _isAtom (text[_index], allowInternational))
85
85
_index++ ;
@@ -88,7 +88,7 @@ class EmailValidator {
88
88
}
89
89
90
90
static bool _skipSubDomain (String text, bool allowInternational) {
91
- int startIndex = _index;
91
+ final int startIndex = _index;
92
92
93
93
if (! _isDomainStart (text[_index], allowInternational)) return false ;
94
94
@@ -102,22 +102,26 @@ class EmailValidator {
102
102
103
103
static bool _skipDomain (
104
104
String text, bool allowTopLevelDomains, bool allowInternational) {
105
- if (! _skipSubDomain (text, allowInternational)) return false ;
105
+ if (! _skipSubDomain (text, allowInternational))
106
+ return false ;
106
107
107
108
if (_index < text.length && text[_index] == '.' ) {
108
109
do {
109
110
_index++ ;
110
111
111
- if (_index == text.length) return false ;
112
+ if (_index == text.length)
113
+ return false ;
112
114
113
- if (! _skipSubDomain (text, allowInternational)) return false ;
115
+ if (! _skipSubDomain (text, allowInternational))
116
+ return false ;
114
117
} while (_index < text.length && text[_index] == '.' );
115
118
} else if (! allowTopLevelDomains) {
116
119
return false ;
117
120
}
118
121
119
122
// Note: by allowing AlphaNumeric, we get away with not having to support punycode.
120
- if (_domainType == type.Numeric ) return false ;
123
+ if (_domainType == type.Numeric )
124
+ return false ;
121
125
122
126
return true ;
123
127
}
@@ -135,15 +139,17 @@ class EmailValidator {
135
139
if (text[_index] == '\\ ' ) {
136
140
escaped = ! escaped;
137
141
} else if (! escaped) {
138
- if (text[_index] == '"' ) break ;
142
+ if (text[_index] == '"' )
143
+ break ;
139
144
} else {
140
145
escaped = false ;
141
146
}
142
147
143
148
_index++ ;
144
149
}
145
150
146
- if (_index >= text.length || text[_index] != '"' ) return false ;
151
+ if (_index >= text.length || text[_index] != '"' )
152
+ return false ;
147
153
148
154
_index++ ;
149
155
@@ -154,7 +160,7 @@ class EmailValidator {
154
160
int groups = 0 ;
155
161
156
162
while (_index < text.length && groups < 4 ) {
157
- int startIndex = _index;
163
+ final int startIndex = _index;
158
164
int value = 0 ;
159
165
160
166
while (_index < text.length &&
@@ -175,8 +181,8 @@ class EmailValidator {
175
181
return groups == 4 ;
176
182
}
177
183
178
- static bool _isHexDigit (str) {
179
- var c = str.codeUnitAt (0 );
184
+ static bool _isHexDigit (String str) {
185
+ final int c = str.codeUnitAt (0 );
180
186
return (c >= 65 && c <= 70 ) ||
181
187
(c >= 97 && c <= 102 ) ||
182
188
(c >= 48 && c <= 57 );
@@ -204,32 +210,40 @@ class EmailValidator {
204
210
while (_index < text.length) {
205
211
int startIndex = _index;
206
212
207
- while (_index < text.length && _isHexDigit (text[_index])) _index++ ;
213
+ while (_index < text.length && _isHexDigit (text[_index]))
214
+ _index++ ;
208
215
209
- if (_index >= text.length) break ;
216
+ if (_index >= text.length)
217
+ break ;
210
218
211
219
if (_index > startIndex && colons > 2 && text[_index] == '.' ) {
212
220
// IPv6v4
213
221
_index = startIndex;
214
222
215
- if (! _skipIPv4Literal (text)) return false ;
223
+ if (! _skipIPv4Literal (text))
224
+ return false ;
216
225
217
226
return compact ? colons < 6 : colons == 6 ;
218
227
}
219
228
220
229
int count = _index - startIndex;
221
- if (count > 4 ) return false ;
230
+ if (count > 4 )
231
+ return false ;
222
232
223
- if (text[_index] != ':' ) break ;
233
+ if (text[_index] != ':' )
234
+ break ;
224
235
225
236
startIndex = _index;
226
- while (_index < text.length && text[_index] == ':' ) _index++ ;
237
+ while (_index < text.length && text[_index] == ':' )
238
+ _index++ ;
227
239
228
240
count = _index - startIndex;
229
- if (count > 2 ) return false ;
241
+ if (count > 2 )
242
+ return false ;
230
243
231
244
if (count == 2 ) {
232
- if (compact) return false ;
245
+ if (compact)
246
+ return false ;
233
247
234
248
compact = true ;
235
249
colons += 2 ;
@@ -238,7 +252,8 @@ class EmailValidator {
238
252
}
239
253
}
240
254
241
- if (colons < 2 ) return false ;
255
+ if (colons < 2 )
256
+ return false ;
242
257
243
258
return compact ? colons < 7 : colons == 7 ;
244
259
}
@@ -248,9 +263,11 @@ class EmailValidator {
248
263
[bool allowTopLevelDomains = false , bool allowInternational = false ]) {
249
264
_index = 0 ;
250
265
251
- if (email == null ) throw new ArgumentError ("email" );
266
+ if (email == null )
267
+ throw new ArgumentError ('email' );
252
268
253
- if (email.length == 0 || email.length >= 255 ) return false ;
269
+ if (email.isEmpty || email.length >= 255 )
270
+ return false ;
254
271
255
272
// Local-part = Dot-string / Quoted-string
256
273
// ; MAY be case-sensitive
@@ -268,11 +285,14 @@ class EmailValidator {
268
285
while (email[_index] == '.' ) {
269
286
_index++ ;
270
287
271
- if (_index >= email.length) return false ;
288
+ if (_index >= email.length)
289
+ return false ;
272
290
273
- if (! _skipAtom (email, allowInternational)) return false ;
291
+ if (! _skipAtom (email, allowInternational))
292
+ return false ;
274
293
275
- if (_index >= email.length) return false ;
294
+ if (_index >= email.length)
295
+ return false ;
276
296
}
277
297
}
278
298
@@ -291,17 +311,22 @@ class EmailValidator {
291
311
_index++ ;
292
312
293
313
// we need at least 8 more characters
294
- if (_index + 8 >= email.length) return false ;
314
+ if (_index + 8 >= email.length)
315
+ return false ;
316
+
317
+ final String ipv6 = email.substring (_index - 1 ).toLowerCase ();
295
318
296
- var ipv6 = email. substring (_index - 1 ). toLowerCase ();
297
- if (ipv6. contains ( "ipv6:" )) {
298
- _index += "IPv6:" .length;
299
- if ( ! _skipIPv6Literal (email)) return false ;
319
+ if ( ipv6. contains ( 'ipv6:' )) {
320
+ _index += 'IPv6:' .length;
321
+ if ( ! _skipIPv6Literal (email))
322
+ return false ;
300
323
} else {
301
- if (! _skipIPv4Literal (email)) return false ;
324
+ if (! _skipIPv4Literal (email))
325
+ return false ;
302
326
}
303
327
304
- if (_index >= email.length || email[_index++ ] != ']' ) return false ;
328
+ if (_index >= email.length || email[_index++ ] != ']' )
329
+ return false ;
305
330
306
331
return _index == email.length;
307
332
}
0 commit comments