| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 // Check the validity of string literals. | 5 // Check the validity of string literals. |
| 6 | 6 |
| 7 library stringvalidator; | 7 library stringvalidator; |
| 8 | 8 |
| 9 import "dart2jslib.dart"; | 9 import "dart2jslib.dart"; |
| 10 import "tree/tree.dart"; | 10 import "tree/tree.dart"; |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 89 DartString validateString(Token token, | 89 DartString validateString(Token token, |
| 90 int startOffset, | 90 int startOffset, |
| 91 SourceString string, | 91 SourceString string, |
| 92 StringQuoting quoting) { | 92 StringQuoting quoting) { |
| 93 // We only need to check for invalid x and u escapes, for line | 93 // We only need to check for invalid x and u escapes, for line |
| 94 // terminators in non-multiline strings, and for invalid Unicode | 94 // terminators in non-multiline strings, and for invalid Unicode |
| 95 // scalar values (either directly or as u-escape values). | 95 // scalar values (either directly or as u-escape values). |
| 96 int length = 0; | 96 int length = 0; |
| 97 int index = startOffset; | 97 int index = startOffset; |
| 98 bool containsEscape = false; | 98 bool containsEscape = false; |
| 99 bool previousWasLeadSurrogate = false; |
| 100 bool invalidUtf16 = false; |
| 99 for(Iterator<int> iter = string.iterator(); iter.hasNext; length++) { | 101 for(Iterator<int> iter = string.iterator(); iter.hasNext; length++) { |
| 100 index++; | 102 index++; |
| 101 int code = iter.next(); | 103 int code = iter.next(); |
| 102 if (code == $BACKSLASH) { | 104 if (code == $BACKSLASH) { |
| 103 if (quoting.raw) continue; | 105 if (quoting.raw) continue; |
| 104 containsEscape = true; | 106 containsEscape = true; |
| 105 if (!iter.hasNext) { | 107 if (!iter.hasNext) { |
| 106 stringParseError("Incomplete escape sequence",token, index); | 108 stringParseError("Incomplete escape sequence",token, index); |
| 107 return null; | 109 return null; |
| 108 } | 110 } |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 161 stringParseError("Invalid character in escape sequence", | 163 stringParseError("Invalid character in escape sequence", |
| 162 token, index); | 164 token, index); |
| 163 return null; | 165 return null; |
| 164 } | 166 } |
| 165 value = value * 16 + hexDigitValue(code); | 167 value = value * 16 + hexDigitValue(code); |
| 166 } | 168 } |
| 167 } | 169 } |
| 168 code = value; | 170 code = value; |
| 169 } | 171 } |
| 170 } | 172 } |
| 173 if (code >= 0x10000) length++; |
| 174 assert(code <= 0x10ffff); |
| 171 // This handles both unescaped characters and the value of unicode | 175 // This handles both unescaped characters and the value of unicode |
| 172 // escapes. | 176 // escapes. |
| 173 if (!isUnicodeScalarValue(code)) { | 177 if (previousWasLeadSurrogate) { |
| 174 stringParseError( | 178 if (!isUtf16TrailSurrogate(code)) { |
| 175 "Invalid Unicode scalar value U+${code.toRadixString(16)}", | 179 invalidUtf16 = true; |
| 176 token, index); | 180 break; |
| 177 return null; | 181 } |
| 182 previousWasLeadSurrogate = false; |
| 183 } else if (isUtf16LeadSurrogate(code)) { |
| 184 previousWasLeadSurrogate = true; |
| 185 } else if (!isUnicodeScalarValue(code)) { |
| 186 invalidUtf16 = true; |
| 187 break; |
| 178 } | 188 } |
| 179 } | 189 } |
| 190 if (previousWasLeadSurrogate || invalidUtf16) { |
| 191 stringParseError("Invalid Utf16 surrogate", token, index); |
| 192 return null; |
| 193 } |
| 180 // String literal successfully validated. | 194 // String literal successfully validated. |
| 181 if (quoting.raw || !containsEscape) { | 195 if (quoting.raw || !containsEscape) { |
| 182 // A string without escapes could just as well have been raw. | 196 // A string without escapes could just as well have been raw. |
| 183 return new DartString.rawString(string, length); | 197 return new DartString.rawString(string, length); |
| 184 } | 198 } |
| 185 return new DartString.escapedString(string, length); | 199 return new DartString.escapedString(string, length); |
| 186 } | 200 } |
| 187 } | 201 } |
| OLD | NEW |