| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | |
| 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. | |
| 4 | |
| 5 library dart2js.util.command_line; | |
| 6 | |
| 7 /// The accepted escapes in the input of the --batch processor. | |
| 8 /// | |
| 9 /// Contrary to Dart strings it does not contain hex escapes (\u or \x). | |
| 10 Map<String, String> ESCAPE_MAPPING = | |
| 11 const { | |
| 12 "n": "\n", | |
| 13 "r": "\r", | |
| 14 "t": "\t", | |
| 15 "b": "\b", | |
| 16 "f": "\f", | |
| 17 "v": "\v", | |
| 18 "\\": "\\", | |
| 19 }; | |
| 20 | |
| 21 /// Splits the line similar to how a shell would split arguments. If [windows] | |
| 22 /// is `true` escapes will be handled like on the Windows command-line. | |
| 23 /// | |
| 24 /// Example: | |
| 25 /// | |
| 26 /// splitline("""--args "ab"c 'with " \'spaces'""").forEach(print); | |
| 27 /// // --args | |
| 28 /// // abc | |
| 29 /// // with " 'spaces | |
| 30 List<String> splitLine(String line, {bool windows: false}) { | |
| 31 List<String> result = <String>[]; | |
| 32 bool inQuotes = false; | |
| 33 String openingQuote; | |
| 34 StringBuffer buffer = new StringBuffer(); | |
| 35 for (int i = 0; i < line.length; i++) { | |
| 36 String c = line[i]; | |
| 37 if (inQuotes && c == openingQuote) { | |
| 38 inQuotes = false; | |
| 39 continue; | |
| 40 } | |
| 41 if (!inQuotes && (c == '"' || (c == "'" && !windows))) { | |
| 42 inQuotes = true; | |
| 43 openingQuote = c; | |
| 44 continue; | |
| 45 } | |
| 46 if (c == '\\') { | |
| 47 if (i == line.length - 1) { | |
| 48 throw new FormatException("Unfinished escape: $line"); | |
| 49 } | |
| 50 if (windows) { | |
| 51 String next = line[i+1]; | |
| 52 if (next == '"' || next == r'\') { | |
| 53 buffer.write(next); | |
| 54 i++; | |
| 55 continue; | |
| 56 } | |
| 57 } else { | |
| 58 i++; | |
| 59 | |
| 60 c = line[i]; | |
| 61 String mapped = ESCAPE_MAPPING[c]; | |
| 62 if (mapped == null) mapped = c; | |
| 63 buffer.write(mapped); | |
| 64 continue; | |
| 65 } | |
| 66 } | |
| 67 if (!inQuotes && c == " ") { | |
| 68 if (buffer.isNotEmpty) result.add(buffer.toString()); | |
| 69 buffer.clear(); | |
| 70 continue; | |
| 71 } | |
| 72 buffer.write(c); | |
| 73 } | |
| 74 if (inQuotes) throw new FormatException("Unclosed quotes: $line"); | |
| 75 if (buffer.isNotEmpty) result.add(buffer.toString()); | |
| 76 return result; | |
| 77 } | |
| 78 | |
| OLD | NEW |