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 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
74 quoteLength += 1; | 74 quoteLength += 1; |
75 } | 75 } |
76 } else if (code == $LF) { | 76 } else if (code == $LF) { |
77 quoteLength += 1; | 77 quoteLength += 1; |
78 } | 78 } |
79 } | 79 } |
80 } | 80 } |
81 return StringQuoting.getQuoting(quoteChar, raw, quoteLength); | 81 return StringQuoting.getQuoting(quoteChar, raw, quoteLength); |
82 } | 82 } |
83 | 83 |
| 84 /** |
| 85 * Return the string [string] witout its [initial] first and [terminal] last |
| 86 * characters. This is intended to be used to remove quotes from string |
| 87 * literals (including an initial 'r' for raw strings). |
| 88 */ |
| 89 String copyWithoutQuotes(String string, int initial, int terminal) { |
| 90 assert(0 <= initial); |
| 91 assert(0 <= terminal); |
| 92 assert(initial + terminal <= string.length); |
| 93 return string.substring(initial, string.length - terminal); |
| 94 } |
| 95 |
84 void stringParseError(String message, Token token, int offset) { | 96 void stringParseError(String message, Token token, int offset) { |
85 listener.cancel("$message @ $offset", token : token); | 97 listener.cancel("$message @ $offset", token : token); |
86 } | 98 } |
87 | 99 |
88 /** | 100 /** |
89 * Validates the escape sequences and special characters of a string literal. | 101 * Validates the escape sequences and special characters of a string literal. |
90 * Returns a DartString if valid, and null if not. | 102 * Returns a DartString if valid, and null if not. |
91 */ | 103 */ |
92 DartString validateString(Token token, | 104 DartString validateString(Token token, |
93 int startOffset, | 105 int startOffset, |
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
205 return null; | 217 return null; |
206 } | 218 } |
207 // String literal successfully validated. | 219 // String literal successfully validated. |
208 if (quoting.raw || !containsEscape) { | 220 if (quoting.raw || !containsEscape) { |
209 // A string without escapes could just as well have been raw. | 221 // A string without escapes could just as well have been raw. |
210 return new DartString.rawString(string, length); | 222 return new DartString.rawString(string, length); |
211 } | 223 } |
212 return new DartString.escapedString(string, length); | 224 return new DartString.escapedString(string, length); |
213 } | 225 } |
214 } | 226 } |
OLD | NEW |