OLD | NEW |
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 library dart2js.util; | 5 library dart2js.util; |
6 | 6 |
7 import 'util_implementation.dart'; | 7 import 'util_implementation.dart'; |
8 import 'characters.dart'; | 8 import 'characters.dart'; |
9 | 9 |
10 export 'setlet.dart'; | 10 export 'setlet.dart'; |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
109 buffer.write(r'\t'); | 109 buffer.write(r'\t'); |
110 } else if (code == $LF) { | 110 } else if (code == $LF) { |
111 buffer.write(r'\n'); | 111 buffer.write(r'\n'); |
112 } else if (code == $CR) { | 112 } else if (code == $CR) { |
113 buffer.write(r'\r'); | 113 buffer.write(r'\r'); |
114 } else if (code == $DEL) { | 114 } else if (code == $DEL) { |
115 addCodeUnitEscaped(buffer, $DEL); | 115 addCodeUnitEscaped(buffer, $DEL); |
116 } else if (code == $LS) { | 116 } else if (code == $LS) { |
117 // This Unicode line terminator and $PS are invalid in JS string | 117 // This Unicode line terminator and $PS are invalid in JS string |
118 // literals. | 118 // literals. |
119 addCodeUnitEscaped(buffer, $LS); // 0x2028. | 119 addCodeUnitEscaped(buffer, $LS); // 0x2028. |
120 } else if (code == $PS) { | 120 } else if (code == $PS) { |
121 addCodeUnitEscaped(buffer, $PS); // 0x2029. | 121 addCodeUnitEscaped(buffer, $PS); // 0x2029. |
122 } else if (code == $BACKSLASH) { | 122 } else if (code == $BACKSLASH) { |
123 buffer.write(r'\\'); | 123 buffer.write(r'\\'); |
124 } else { | 124 } else { |
125 if (code < 0x20) { | 125 if (code < 0x20) { |
126 addCodeUnitEscaped(buffer, code); | 126 addCodeUnitEscaped(buffer, code); |
127 // We emit DEL (ASCII 0x7f) as an escape because it would be confusing | 127 // We emit DEL (ASCII 0x7f) as an escape because it would be confusing |
128 // to have it unescaped in a string literal. We also escape | 128 // to have it unescaped in a string literal. We also escape |
129 // everything above 0x7f because that means we don't have to worry | 129 // everything above 0x7f because that means we don't have to worry |
130 // about whether the web server serves it up as Latin1 or UTF-8. | 130 // about whether the web server serves it up as Latin1 or UTF-8. |
131 } else if (code < 0x7f) { | 131 } else if (code < 0x7f) { |
132 buffer.writeCharCode(code); | 132 buffer.writeCharCode(code); |
133 } else { | 133 } else { |
134 // This will output surrogate pairs in the form \udxxx\udyyy, rather | 134 // This will output surrogate pairs in the form \udxxx\udyyy, rather |
135 // than the more logical \u{zzzzzz}. This should work in JavaScript | 135 // than the more logical \u{zzzzzz}. This should work in JavaScript |
136 // (especially old UCS-2 based implementations) and is the only | 136 // (especially old UCS-2 based implementations) and is the only |
137 // format that is allowed in JSON. | 137 // format that is allowed in JSON. |
138 addCodeUnitEscaped(buffer, code); | 138 addCodeUnitEscaped(buffer, code); |
139 } | 139 } |
140 } | 140 } |
141 } | 141 } |
142 } | 142 } |
143 | 143 |
144 for (int i = 0; i < string.length; i++) { | 144 for (int i = 0; i < string.length; i++) { |
145 int code = string.codeUnitAt(i); | 145 int code = string.codeUnitAt(i); |
146 if (code < 0x20 || code == $DEL || code == $DQ || code == $LS || | 146 if (code < 0x20 || |
147 code == $PS || code == $BACKSLASH || code >= 0x80) { | 147 code == $DEL || |
| 148 code == $DQ || |
| 149 code == $LS || |
| 150 code == $PS || |
| 151 code == $BACKSLASH || |
| 152 code >= 0x80) { |
148 writeEscapedOn(string, buffer); | 153 writeEscapedOn(string, buffer); |
149 return; | 154 return; |
150 } | 155 } |
151 } | 156 } |
152 buffer.write(string); | 157 buffer.write(string); |
153 } | 158 } |
154 | 159 |
155 int computeHashCode(part1, [part2, part3, part4, part5]) { | 160 int computeHashCode(part1, [part2, part3, part4, part5]) { |
156 return (part1.hashCode | 161 return (part1.hashCode ^ |
157 ^ part2.hashCode | 162 part2.hashCode ^ |
158 ^ part3.hashCode | 163 part3.hashCode ^ |
159 ^ part4.hashCode | 164 part4.hashCode ^ |
160 ^ part5.hashCode) & 0x3fffffff; | 165 part5.hashCode) & |
| 166 0x3fffffff; |
161 } | 167 } |
162 | 168 |
163 String modifiersToString({bool isStatic: false, | 169 String modifiersToString( |
164 bool isAbstract: false, | 170 {bool isStatic: false, |
165 bool isFinal: false, | 171 bool isAbstract: false, |
166 bool isVar: false, | 172 bool isFinal: false, |
167 bool isConst: false, | 173 bool isVar: false, |
168 bool isFactory: false, | 174 bool isConst: false, |
169 bool isExternal: false}) { | 175 bool isFactory: false, |
| 176 bool isExternal: false}) { |
170 LinkBuilder<String> builder = new LinkBuilder<String>(); | 177 LinkBuilder<String> builder = new LinkBuilder<String>(); |
171 if (isStatic) builder.addLast('static'); | 178 if (isStatic) builder.addLast('static'); |
172 if (isAbstract) builder.addLast('abstract'); | 179 if (isAbstract) builder.addLast('abstract'); |
173 if (isFinal) builder.addLast('final'); | 180 if (isFinal) builder.addLast('final'); |
174 if (isVar) builder.addLast('var'); | 181 if (isVar) builder.addLast('var'); |
175 if (isConst) builder.addLast('const'); | 182 if (isConst) builder.addLast('const'); |
176 if (isFactory) builder.addLast('factory'); | 183 if (isFactory) builder.addLast('factory'); |
177 if (isExternal) builder.addLast('external'); | 184 if (isExternal) builder.addLast('external'); |
178 StringBuffer buffer = new StringBuffer(); | 185 StringBuffer buffer = new StringBuffer(); |
179 builder.toLink().printOn(buffer, ', '); | 186 builder.toLink().printOn(buffer, ', '); |
(...skipping 10 matching lines...) Expand all Loading... |
190 | 197 |
191 bool operator ==(var other) { | 198 bool operator ==(var other) { |
192 if (identical(this, other)) return true; | 199 if (identical(this, other)) return true; |
193 if (other is! Pair) return false; | 200 if (other is! Pair) return false; |
194 return a == other.a && b == other.b; | 201 return a == other.a && b == other.b; |
195 } | 202 } |
196 | 203 |
197 String toString() => '($a,$b)'; | 204 String toString() => '($a,$b)'; |
198 } | 205 } |
199 | 206 |
200 | |
201 int longestCommonPrefixLength(List a, List b) { | 207 int longestCommonPrefixLength(List a, List b) { |
202 int index = 0; | 208 int index = 0; |
203 for ( ; index < a.length && index < b.length; index++) { | 209 for (; index < a.length && index < b.length; index++) { |
204 if (a[index] != b[index]) { | 210 if (a[index] != b[index]) { |
205 break; | 211 break; |
206 } | 212 } |
207 } | 213 } |
208 return index; | 214 return index; |
209 } | 215 } |
210 | 216 |
211 /// Returns [suggestedName] if it is not in [usedNames]. Otherwise concatenates | 217 /// Returns [suggestedName] if it is not in [usedNames]. Otherwise concatenates |
212 /// the smallest number that makes it not appear in [usedNames]. | 218 /// the smallest number that makes it not appear in [usedNames]. |
213 /// | 219 /// |
214 /// Adds the result to [usedNames]. | 220 /// Adds the result to [usedNames]. |
215 String makeUnique(String suggestedName, Set<String> usedNames) { | 221 String makeUnique(String suggestedName, Set<String> usedNames) { |
216 String result = suggestedName; | 222 String result = suggestedName; |
217 if (usedNames.contains(suggestedName)) { | 223 if (usedNames.contains(suggestedName)) { |
218 int counter = 0; | 224 int counter = 0; |
219 while (usedNames.contains(result)) { | 225 while (usedNames.contains(result)) { |
220 counter++; | 226 counter++; |
221 result = "$suggestedName$counter"; | 227 result = "$suggestedName$counter"; |
222 } | 228 } |
223 } | 229 } |
224 usedNames.add(result); | 230 usedNames.add(result); |
225 return result; | 231 return result; |
226 } | 232 } |
OLD | NEW |