Skip to content

Commit

Permalink
[dart2js] Migrate command line util
Browse files Browse the repository at this point in the history
Also makes the following minor improvements:
- Standardizes the file on single quotes to make the file more uniform and match elsewhere.
- Switches the ESCAPE_MAPPING declaration to const
- Corrects method spelling in doc comment

Change-Id: Ic271cd28d287f321b9b3719250b053df548ba194
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/202061
Reviewed-by: Stephen Adams <[email protected]>
Commit-Queue: Kevin Moore <[email protected]>
  • Loading branch information
parlough authored and [email protected] committed Jun 4, 2021
1 parent 51b7809 commit 47e4b4f
Showing 1 changed file with 22 additions and 19 deletions.
41 changes: 22 additions & 19 deletions pkg/compiler/lib/src/util/command_line.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,36 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

// @dart=2.12

library dart2js.util.command_line;

/// The accepted escapes in the input of the --batch processor.
///
/// Contrary to Dart strings it does not contain hex escapes (\u or \x).
Map<String, String> ESCAPE_MAPPING = const {
"n": "\n",
"r": "\r",
"t": "\t",
"b": "\b",
"f": "\f",
"v": "\v",
"\\": "\\",
const Map<String, String> ESCAPE_MAPPING = {
'n': '\n',
'r': '\r',
't': '\t',
'b': '\b',
'f': '\f',
'v': '\v',
'\\': '\\',
};

/// Splits the line similar to how a shell would split arguments. If [windows]
/// Splits the [line] similar to how a shell would split arguments. If [windows]
/// is `true` escapes will be handled like on the Windows command-line.
///
/// Example:
///
/// splitline("""--args "ab"c 'with " \'spaces'""").forEach(print);
/// splitLine("""--args "ab"c 'with " \'spaces'""").forEach(print);
/// // --args
/// // abc
/// // with " 'spaces
List<String> splitLine(String line, {bool windows: false}) {
List<String> splitLine(String line, {bool windows = false}) {
List<String> result = <String>[];
bool inQuotes = false;
String openingQuote;
String? openingQuote;
StringBuffer buffer = new StringBuffer();
for (int i = 0; i < line.length; i++) {
String c = line[i];
Expand All @@ -44,7 +46,7 @@ List<String> splitLine(String line, {bool windows: false}) {
}
if (c == '\\') {
if (i == line.length - 1) {
throw new FormatException("Unfinished escape: $line");
throw new FormatException('Unfinished escape: $line');
}
if (windows) {
String next = line[i + 1];
Expand All @@ -57,20 +59,21 @@ List<String> splitLine(String line, {bool windows: false}) {
i++;

c = line[i];
String mapped = ESCAPE_MAPPING[c];
if (mapped == null) mapped = c;
String mapped = ESCAPE_MAPPING[c] ?? c;
buffer.write(mapped);
continue;
}
}
if (!inQuotes && c == " ") {
if (buffer.isNotEmpty) result.add(buffer.toString());
buffer.clear();
if (!inQuotes && c == ' ') {
if (buffer.isNotEmpty) {
result.add(buffer.toString());
buffer.clear();
}
continue;
}
buffer.write(c);
}
if (inQuotes) throw new FormatException("Unclosed quotes: $line");
if (inQuotes) throw new FormatException('Unclosed quotes: $line');
if (buffer.isNotEmpty) result.add(buffer.toString());
return result;
}

0 comments on commit 47e4b4f

Please sign in to comment.