Skip to content

Commit

Permalink
feat: remove charcode dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
alextekartik committed Nov 25, 2023
1 parent 316cc71 commit 160a50d
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 21 deletions.
28 changes: 28 additions & 0 deletions packages/process_run/lib/src/common/ascii_charcodes.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/// "Horizontal Tab" control character, common name.
const int charcodeTab = 0x09;

/// "Line feed" control character.
const int charcodeLf = 0x0A;

// Visible characters.

/// Space character.
const int charcodeSpace = 0x20;

/// Character `"`.
const int charcodeDoubleQuote = 0x22;

/// Character `#`.
const int charcodeHash = 0x23;

/// Character `$`.
const int charcodeDollar = 0x24;

/// Character `'`.
const int charcodeSingleQuote = 0x27;

/// Character `\`.
const int charcodeBackslash = 0x5C;

/// Character `` ` ``.
const int charcodeBackquote = 0x60;
41 changes: 21 additions & 20 deletions packages/process_run/lib/src/io/shell_words.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@

// ignore_for_file: comment_references

import 'package:charcode/charcode.dart';
import 'package:string_scanner/string_scanner.dart';

import 'package:process_run/src/common/ascii_charcodes.dart';

/// Splits [command] into tokens according to [the POSIX shell
/// specification][spec].
///
Expand Down Expand Up @@ -34,33 +35,33 @@ List<String> shellSplit(String command) {
while (!scanner.isDone) {
final next = scanner.readChar();
switch (next) {
case $backslash:
case charcodeBackslash:
// Section 2.2.1: A <backslash> that is not quoted shall preserve the
// literal value of the following character, with the exception of a
// <newline>. If a <newline> follows the <backslash>, the shell shall
// interpret this as line continuation. The <backslash> and <newline>
// shall be removed before splitting the input into tokens. Since the
// escaped <newline> is removed entirely from the input and is not
// replaced by any white space, it cannot serve as a token separator.
if (scanner.scanChar($lf)) break;
if (scanner.scanChar(charcodeLf)) break;

hasToken = true;
token.writeCharCode(scanner.readChar());
break;

case $single_quote:
case charcodeSingleQuote:
hasToken = true;
// Section 2.2.2: Enclosing characters in single-quotes ( '' ) shall
// preserve the literal value of each character within the
// single-quotes. A single-quote cannot occur within single-quotes.
final firstQuote = scanner.position - 1;
while (!scanner.scanChar($single_quote)) {
while (!scanner.scanChar(charcodeSingleQuote)) {
_checkUnmatchedQuote(scanner, firstQuote);
token.writeCharCode(scanner.readChar());
}
break;

case $double_quote:
case charcodeDoubleQuote:
hasToken = true;
// Section 2.2.3: Enclosing characters in double-quotes ( "" ) shall
// preserve the literal value of all characters within the
Expand All @@ -71,10 +72,10 @@ List<String> shellSplit(String command) {
// or dollar sign within double quotes, since those are dynamic
// features.)
final firstQuote = scanner.position - 1;
while (!scanner.scanChar($double_quote)) {
while (!scanner.scanChar(charcodeDoubleQuote)) {
_checkUnmatchedQuote(scanner, firstQuote);

if (scanner.scanChar($backslash)) {
if (scanner.scanChar(charcodeBackslash)) {
_checkUnmatchedQuote(scanner, firstQuote);

// The <backslash> shall retain its special meaning as an escape
Expand All @@ -83,15 +84,15 @@ List<String> shellSplit(String command) {
//
// $ ` " \ <newline>
final next = scanner.readChar();
if (next == $lf) continue;
if (next == $dollar ||
next == $backquote ||
next == $double_quote ||
next == $backslash) {
if (next == charcodeLf) continue;
if (next == charcodeDollar ||
next == charcodeBackquote ||
next == charcodeDoubleQuote ||
next == charcodeBackslash) {
token.writeCharCode(next);
} else {
token
..writeCharCode($backslash)
..writeCharCode(charcodeBackslash)
..writeCharCode(next);
}
} else {
Expand All @@ -100,25 +101,25 @@ List<String> shellSplit(String command) {
}
break;

case $hash:
case charcodeHash:
// Section 2.3: If the current character is a '#' [and the previous
// characters was not part of a word], it and all subsequent characters
// up to, but excluding, the next <newline> shall be discarded as a
// comment. The <newline> that ends the line is not considered part of
// the comment.
if (hasToken) {
token.writeCharCode($hash);
token.writeCharCode(charcodeHash);
break;
}

while (!scanner.isDone && scanner.peekChar() != $lf) {
while (!scanner.isDone && scanner.peekChar() != charcodeLf) {
scanner.readChar();
}
break;

case $space:
case $tab:
case $lf:
case charcodeSpace:
case charcodeTab:
case charcodeLf:
// ignore: invariant_booleans
if (hasToken) results.add(token.toString());
hasToken = false;
Expand Down
1 change: 0 additions & 1 deletion packages/process_run/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ environment:
dependencies:
path: '>=1.8.0 <3.0.0'
collection: '>=1.15.0 <3.0.0'
charcode: '>=1.2.0 <3.0.0'
string_scanner: '>=1.1.0 <3.0.0'
yaml: '>=3.0.0 <5.0.0'
meta: '>=1.3.0 <3.0.0'
Expand Down

0 comments on commit 160a50d

Please sign in to comment.