1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
|
// SPDX-License-Identifier: MPL-2.0
// (c) Hare authors <https://harelang.org>
use ascii;
use encoding::utf8;
use io;
use memio;
use strings;
fn is_safe(s: str) bool = {
const iter = strings::iter(s);
for (let rn => strings::next(&iter)) {
switch (rn) {
case '@', '%', '+', '=', ':', ',', '.', '/', '-' =>
void;
case =>
if (!ascii::isalnum(rn) || ascii::isspace(rn)) {
return false;
};
};
};
return true;
};
// Quotes a shell string and writes it to the provided I/O handle.
export fn quote(sink: io::handle, s: str) (size | io::error) = {
if (len(s) == 0) {
return io::writeall(sink, strings::toutf8(`''`))?;
};
if (is_safe(s)) {
return io::writeall(sink, strings::toutf8(s))?;
};
let z = io::writeall(sink, ['\''])?;
const iter = strings::iter(s);
for (let rn => strings::next(&iter)) {
if (rn == '\'') {
z += io::writeall(sink, strings::toutf8(`'"'"'`))?;
} else {
z += io::writeall(sink, utf8::encoderune(rn))?;
};
};
z += io::writeall(sink, ['\''])?;
return z;
};
// Quotes a shell string and returns a new string. The caller must free the
// return value.
export fn quotestr(s: str) str = {
const sink = memio::dynamic();
quote(&sink, s)!;
return memio::string(&sink)!;
};
|