| OLD | NEW |
| 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2016, 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 fasta.quote; | 5 library fasta.quote; |
| 6 | 6 |
| 7 import 'errors.dart' show inputError, internalError; | 7 import 'errors.dart' show inputError, internalError; |
| 8 | 8 |
| 9 import 'scanner/characters.dart' | 9 import 'scanner/characters.dart' |
| 10 show | 10 show |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 61 if (code == $BACKSLASH) { | 61 if (code == $BACKSLASH) { |
| 62 i++; | 62 i++; |
| 63 if (i < codeUnits.length) { | 63 if (i < codeUnits.length) { |
| 64 code = codeUnits[i]; | 64 code = codeUnits[i]; |
| 65 } else { | 65 } else { |
| 66 break; | 66 break; |
| 67 } | 67 } |
| 68 } | 68 } |
| 69 if (code == $TAB || code == $SPACE) continue; | 69 if (code == $TAB || code == $SPACE) continue; |
| 70 if (code == $CR) { | 70 if (code == $CR) { |
| 71 if (i + 1 < codeUnits.length && codeUnits[i] == $LF) { | 71 if (i + 1 < codeUnits.length && codeUnits[i + 1] == $LF) { |
| 72 i++; | 72 i++; |
| 73 } | 73 } |
| 74 return i + 1; | 74 return i + 1; |
| 75 } | 75 } |
| 76 if (code == $LF) { | 76 if (code == $LF) { |
| 77 return i + 1; | 77 return i + 1; |
| 78 } | 78 } |
| 79 break; // Not a white-space character. | 79 break; // Not a white-space character. |
| 80 } | 80 } |
| 81 return start; | 81 return start; |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 133 return unescape( | 133 return unescape( |
| 134 string.substring(firstQuoteLength(string, quote), | 134 string.substring(firstQuoteLength(string, quote), |
| 135 string.length - lastQuoteLength(quote)), | 135 string.length - lastQuoteLength(quote)), |
| 136 quote); | 136 quote); |
| 137 } | 137 } |
| 138 | 138 |
| 139 String unescape(String string, Quote quote) { | 139 String unescape(String string, Quote quote) { |
| 140 switch (quote) { | 140 switch (quote) { |
| 141 case Quote.Single: | 141 case Quote.Single: |
| 142 case Quote.Double: | 142 case Quote.Double: |
| 143 return !string.contains("\\") |
| 144 ? string |
| 145 : unescapeCodeUnits(string.codeUnits, false); |
| 146 |
| 143 case Quote.MultiLineSingle: | 147 case Quote.MultiLineSingle: |
| 144 case Quote.MultiLineDouble: | 148 case Quote.MultiLineDouble: |
| 145 break; | 149 return !string.contains("\\") && !string.contains("\r") |
| 150 ? string |
| 151 : unescapeCodeUnits(string.codeUnits, false); |
| 146 | 152 |
| 147 case Quote.RawSingle: | 153 case Quote.RawSingle: |
| 148 case Quote.RawDouble: | 154 case Quote.RawDouble: |
| 155 return string; |
| 156 |
| 149 case Quote.RawMultiLineSingle: | 157 case Quote.RawMultiLineSingle: |
| 150 case Quote.RawMultiLineDouble: | 158 case Quote.RawMultiLineDouble: |
| 151 return string; | 159 return !string.contains("\r") |
| 160 ? string |
| 161 : unescapeCodeUnits(string.codeUnits, true); |
| 152 } | 162 } |
| 153 return !string.contains("\\") ? string : unescapeCodeUnits(string.codeUnits); | 163 return internalError("Unhandled string quote: $quote"); |
| 154 } | 164 } |
| 155 | 165 |
| 156 const String incompleteSequence = "Incomplete escape sequence."; | 166 const String incompleteSequence = "Incomplete escape sequence."; |
| 157 | 167 |
| 158 const String invalidCharacter = "Invalid character in escape sequence."; | 168 const String invalidCharacter = "Invalid character in escape sequence."; |
| 159 | 169 |
| 160 const String invalidCodePoint = "Invalid code point."; | 170 const String invalidCodePoint = "Invalid code point."; |
| 161 | 171 |
| 162 // Note: based on | 172 // Note: based on |
| 163 // [StringValidator.validateString](pkg/compiler/lib/src/string_validator.dart). | 173 // [StringValidator.validateString](pkg/compiler/lib/src/string_validator.dart). |
| 164 String unescapeCodeUnits(List<int> codeUnits) { | 174 String unescapeCodeUnits(List<int> codeUnits, bool isRaw) { |
| 165 // Can't use Uint8List or Uint16List here, the code units may be larger. | 175 // Can't use Uint8List or Uint16List here, the code units may be larger. |
| 166 List<int> result = new List<int>(codeUnits.length); | 176 List<int> result = new List<int>(codeUnits.length); |
| 167 int resultOffset = 0; | 177 int resultOffset = 0; |
| 168 error(int offset, String message) { | 178 error(int offset, String message) { |
| 169 inputError(null, null, message); | 179 inputError(null, null, message); |
| 170 } | 180 } |
| 171 | 181 |
| 172 for (int i = 0; i < codeUnits.length; i++) { | 182 for (int i = 0; i < codeUnits.length; i++) { |
| 173 int code = codeUnits[i]; | 183 int code = codeUnits[i]; |
| 174 if (code == $BACKSLASH) { | 184 if (code == $CR) { |
| 185 if (i + 1 < codeUnits.length && codeUnits[i + 1] == $LF) { |
| 186 i++; |
| 187 } |
| 188 code = $LF; |
| 189 } else if (!isRaw && code == $BACKSLASH) { |
| 175 if (codeUnits.length == ++i) return error(i, incompleteSequence); | 190 if (codeUnits.length == ++i) return error(i, incompleteSequence); |
| 176 code = codeUnits[i]; | 191 code = codeUnits[i]; |
| 177 | 192 |
| 178 /// `\n` for newline, equivalent to `\x0A`. | 193 /// `\n` for newline, equivalent to `\x0A`. |
| 179 /// `\r` for carriage return, equivalent to `\x0D`. | 194 /// `\r` for carriage return, equivalent to `\x0D`. |
| 180 /// `\f` for form feed, equivalent to `\x0C`. | 195 /// `\f` for form feed, equivalent to `\x0C`. |
| 181 /// `\b` for backspace, equivalent to `\x08`. | 196 /// `\b` for backspace, equivalent to `\x08`. |
| 182 /// `\t` for tab, equivalent to `\x09`. | 197 /// `\t` for tab, equivalent to `\x09`. |
| 183 /// `\v` for vertical tab, equivalent to `\x0B`. | 198 /// `\v` for vertical tab, equivalent to `\x0B`. |
| 184 /// `\xXX` for hex escape. | 199 /// `\xXX` for hex escape. |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 230 } | 245 } |
| 231 } else { | 246 } else { |
| 232 // Nothing, escaped character is passed through; | 247 // Nothing, escaped character is passed through; |
| 233 } | 248 } |
| 234 if (code > 0x10FFFF) return error(i, invalidCodePoint); | 249 if (code > 0x10FFFF) return error(i, invalidCodePoint); |
| 235 } | 250 } |
| 236 result[resultOffset++] = code; | 251 result[resultOffset++] = code; |
| 237 } | 252 } |
| 238 return new String.fromCharCodes(result, 0, resultOffset); | 253 return new String.fromCharCodes(result, 0, resultOffset); |
| 239 } | 254 } |
| OLD | NEW |