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