22// for details. All rights reserved. Use of this source code is governed by a
33// BSD-style license that can be found in the LICENSE file.
44
5- import "dart:_internal" show patch;
5+ import "dart:_internal" show patch, checkNotNullable ;
66
77@patch
88@pragma ("vm:entry-point" )
@@ -20,4 +20,48 @@ class bool {
2020 int get hashCode => this ? 1231 : 1237 ;
2121
2222 int get _identityHashCode => this ? 1231 : 1237 ;
23+
24+ @patch
25+ static bool parse (String source, {bool caseSensitive = true }) {
26+ checkNotNullable (source, "source" );
27+ // The caseSensitive defaults to true.
28+ if (caseSensitive == null || caseSensitive) {
29+ return source == "true" ||
30+ source != "false" &&
31+ (throw FormatException ("Not a valid boolean" , source));
32+ }
33+ // Ignore case-sensitive when caseSensitive is false.
34+ return _compareIgnoreCase (source, "true" ) ||
35+ ! _compareIgnoreCase (source, "false" ) &&
36+ (throw FormatException ("Not a valid boolean" , source));
37+ }
38+
39+ @patch
40+ static bool ? tryParse (String source, {bool ? caseSensitive}) {
41+ checkNotNullable (source, "source" );
42+ // The caseSensitive defaults to true.
43+ if (caseSensitive == null || caseSensitive) {
44+ return source == "true"
45+ ? true
46+ : source == "false"
47+ ? false
48+ : null ;
49+ }
50+ // Ignore case-sensitive when caseSensitive is false.
51+ return _compareIgnoreCase (source, "true" )
52+ ? true
53+ : _compareIgnoreCase (source, "false" )
54+ ? false
55+ : null ;
56+ }
57+
58+ static bool _compareIgnoreCase (String input, String lowerCaseTarget) {
59+ if (input.length != lowerCaseTarget.length) return false ;
60+ for (var i = 0 ; i < input.length; i++ ) {
61+ if (input.codeUnitAt (i) | 0x20 != lowerCaseTarget.codeUnitAt (i)) {
62+ return false ;
63+ }
64+ }
65+ return true ;
66+ }
2367}
0 commit comments