| 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 "dart:collection"; | 9 import "dart:collection"; |
| 10 | 10 |
| (...skipping 30 matching lines...) Expand all Loading... |
| 41 int quoteChar = source.current; | 41 int quoteChar = source.current; |
| 42 if (quoteChar == $r) { | 42 if (quoteChar == $r) { |
| 43 raw = true; | 43 raw = true; |
| 44 source.moveNext(); | 44 source.moveNext(); |
| 45 quoteChar = source.current; | 45 quoteChar = source.current; |
| 46 } | 46 } |
| 47 assert(quoteChar == $SQ || quoteChar == $DQ); | 47 assert(quoteChar == $SQ || quoteChar == $DQ); |
| 48 // String has at least one quote. Check it if has three. | 48 // String has at least one quote. Check it if has three. |
| 49 // If it only has two, the string must be an empty string literal, | 49 // If it only has two, the string must be an empty string literal, |
| 50 // and end after the second quote. | 50 // and end after the second quote. |
| 51 bool multiline = false; | |
| 52 if (source.moveNext() && source.current == quoteChar && source.moveNext()) { | 51 if (source.moveNext() && source.current == quoteChar && source.moveNext()) { |
| 53 int code = source.current; | 52 int code = source.current; |
| 54 assert(code == quoteChar); // If not, there is a bug in the parser. | 53 assert(code == quoteChar); // If not, there is a bug in the parser. |
| 55 leftQuoteLength = 3; | 54 leftQuoteLength = 3; |
| 56 | 55 |
| 57 // Check if a multiline string starts with optional whitespace followed by | 56 // Check if a multiline string starts with optional whitespace followed by |
| 58 // a newline (CR, LF or CR+LF). | 57 // a newline (CR, LF or CR+LF). |
| 59 // We also accept if the these characters are escaped by a backslash. | 58 // We also accept if the these characters are escaped by a backslash. |
| 60 int newLineLength = 1; | 59 int newLineLength = 1; |
| 61 while (true) { | 60 while (true) { |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 148 code = iter.next(); | 147 code = iter.next(); |
| 149 if (!isHexDigit(code)) { | 148 if (!isHexDigit(code)) { |
| 150 stringParseError("Invalid character in escape sequence", | 149 stringParseError("Invalid character in escape sequence", |
| 151 token, index); | 150 token, index); |
| 152 return null; | 151 return null; |
| 153 } | 152 } |
| 154 } | 153 } |
| 155 // A two-byte hex escape can't generate an invalid value. | 154 // A two-byte hex escape can't generate an invalid value. |
| 156 continue; | 155 continue; |
| 157 } else if (code == $u) { | 156 } else if (code == $u) { |
| 158 int escapeStart = index - 1; | |
| 159 index++; | 157 index++; |
| 160 code = iter.hasNext ? iter.next() : 0; | 158 code = iter.hasNext ? iter.next() : 0; |
| 161 int value = 0; | 159 int value = 0; |
| 162 if (code == $OPEN_CURLY_BRACKET) { | 160 if (code == $OPEN_CURLY_BRACKET) { |
| 163 // expect 1-6 hex digits. | 161 // expect 1-6 hex digits. |
| 164 int count = 0; | 162 int count = 0; |
| 165 while (iter.hasNext) { | 163 while (iter.hasNext) { |
| 166 code = iter.next(); | 164 code = iter.next(); |
| 167 index++; | 165 index++; |
| 168 if (code == $CLOSE_CURLY_BRACKET) { | 166 if (code == $CLOSE_CURLY_BRACKET) { |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 226 return null; | 224 return null; |
| 227 } | 225 } |
| 228 // String literal successfully validated. | 226 // String literal successfully validated. |
| 229 if (quoting.raw || !containsEscape) { | 227 if (quoting.raw || !containsEscape) { |
| 230 // A string without escapes could just as well have been raw. | 228 // A string without escapes could just as well have been raw. |
| 231 return new DartString.rawString(string, length); | 229 return new DartString.rawString(string, length); |
| 232 } | 230 } |
| 233 return new DartString.escapedString(string, length); | 231 return new DartString.escapedString(string, length); |
| 234 } | 232 } |
| 235 } | 233 } |
| OLD | NEW |