commit 0db89b74908cdd8ce36bc9b9e16c72f967be437c
parent 1fc77c61cc28b12d6ba48530ca2fbd5d1938f291
Author: Alexey Yerin <yyp@disroot.org>
Date: Sun, 5 Dec 2021 13:58:54 +0300
shlex: correctly handle leading whitespace
Prior to that, it was inserting an empty string as the first field.
Signed-off-by: Alexey Yerin <yyp@disroot.org>
Diffstat:
2 files changed, 15 insertions(+), 2 deletions(-)
diff --git a/shlex/+test.ha b/shlex/+test.ha
@@ -34,6 +34,12 @@ use strings;
assert(s[0] == "Empty");
assert(s[1] == "");
+ const s = split(" Leading spaces")!;
+ defer strings::freeall(s);
+ assert(len(s) == 2);
+ assert(s[0] == "Leading");
+ assert(s[1] == "spaces");
+
const s = split("with\\ backslashes 'single quoted' \"double quoted\"")!;
defer strings::freeall(s);
assert(len(s) == 3);
diff --git a/shlex/split.ha b/shlex/split.ha
@@ -12,6 +12,7 @@ export fn split(in: const str) ([]str | syntaxerr) = {
let s = strio::dynamic();
let slice: []str = [];
+ let first = true;
let dirty = false;
for (true) {
@@ -34,8 +35,10 @@ export fn split(in: const str) ([]str | syntaxerr) = {
case void =>
break;
};
- append(slice, strio::finish(s));
- s = strio::dynamic();
+ if (!first) {
+ append(slice, strio::finish(s));
+ s = strio::dynamic();
+ };
dirty = false;
case '\\' =>
scan_backslash(s, &iter)?;
@@ -46,6 +49,10 @@ export fn split(in: const str) ([]str | syntaxerr) = {
case =>
strio::appendrune(s, r)!;
};
+
+ if (first) {
+ first = false;
+ };
};
if (dirty) {