Something went wrong. Try again.
🪄 dd's hex editor
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017/// Utilities./// /// Copyright: dd86k <dd@dax.moe>/// License: MIT/// Authors: $(LINK2 https://github.com/dd86k, dd86k)module utils;
import messages;
/// Template to get binary size in mebibytes (base-1024)/// Params: base = Base unit.template MiB(int base){ enum MiB = cast(long)base * 1024 * 1024;}/// Template to get binary size in kibibytes (base-1024)/// Params: base = Base unit.template KiB(int base){ enum KiB = cast(long)base * 1024;}
/// One argument out of arguments().////// Because ddhx deals with bytes, arguments are resolved to bytes, allowing/// weirder escapes and in general, byte arrays.////// No `alias this`, it'd collide with std.conv.text.struct Argument{ /// Argument bytes, quotes removed and escape sequences resolved. immutable(ubyte)[] data;
/// An argument out of raw text, for callers that are not the command line. /// /// Taken as typed. D source has already resolved its own escapes, so /// `Argument("utf8:\t")` holds a real tab and has nothing left to interpret. /// Params: text = Argument text. this(string text) { data = cast(immutable(ubyte)[])text; }
/// An argument out of bytes. /// Params: newdata = Argument bytes. this(immutable(ubyte)[] newdata) { data = newdata; }
/// Translate argument as text. This validates text. /// /// Returns: Argument text. /// Throws: Exception if the bytes are not valid UTF-8. string text() const { import std.utf : validate, UTFException; import std.conv : text;
string result = cast(string)data;
try validate(result); catch (UTFException ex) throw new Exception(text(MSG_ARGUMENT_NOT_TEXT, printable(data)));
return result; }}unittest{ assert(Argument("utf8:ab").data == cast(immutable(ubyte)[])"utf8:ab"); assert(Argument("utf8:ab").text == "utf8:ab"); assert(Argument("héllo").text == "héllo");
// Bytes that are not text are an argument all the same, they are just not // one that a command taking a file name can do anything with Argument arg = Argument(cast(immutable(ubyte)[])[ 0x41, 0xff ]); assert(arg.data == [ 0x41, 0xff ]); try { cast(void)arg.text; assert(false, "text() should have thrown"); } catch (Exception) {}}
/// Take a list of arguments as text.////// For the callers that only ever wanted text, such as passing a command line/// on to something that has no idea what quoting is./// Params: args = Arguments./// Returns: Argument texts./// Throws: Exception if an argument is not valid UTF-8.string[] texts(Argument[] args){ string[] result = new string[args.length]; foreach (i, Argument arg; args) result[i] = arg.text; return result;}
/// Render bytes for an error message, since the bytes in question are, by/// definition, the ones that cannot be printed as-is./// Params: data = Bytes./// Returns: Printable text.string printable(immutable(ubyte)[] data){ import std.ascii : isPrintable; import std.format : format;
string result; foreach (ubyte b; data) { if (isPrintable(b)) result ~= cast(char)b; else result ~= format("\\x%02x", b); } return result;}unittest{ assert(printable(cast(immutable(ubyte)[])"ab") == "ab"); assert(printable(cast(immutable(ubyte)[])[ 0x41, 0xff, 0x0a ]) == `A\xff\x0a`);}
// Interpret C string escapes ("\0", "\t", "\x1b", "\101", etc.) into raw bytes.//// Unknown sequences throw instead of silently dropping the backslash: a// backslash that quietly means itself is a gray area, and a needle that// matches the wrong bytes is worse than one that refuses to compile. A literal// backslash is "\\", and raw text is what single quotes are for.//// Used for double-quoted runs only, see arguments().privateubyte[] unescape(const(char)[] input){ import std.ascii : isHexDigit, isOctalDigit; import std.conv : text;
ubyte[] result;
for (size_t i; i < input.length; ++i) { char c = input[i];
if (c != '\\') { result ~= cast(ubyte)c; continue; }
if (++i >= input.length) throw new Exception(MSG_INCOMPLETE_ESCAPE);
char e = input[i]; switch (e) { case 'a': result ~= '\a'; continue; case 'b': result ~= '\b'; continue; case 'e': result ~= 0x1b; continue; // GNU extension, handy for terminal dumps case 'f': result ~= '\f'; continue; case 'n': result ~= '\n'; continue; case 'r': result ~= '\r'; continue; case 't': result ~= '\t'; continue; case 'v': result ~= '\v'; continue; case '\\': result ~= '\\'; continue; case '\'': result ~= '\''; continue; case '"': result ~= '"'; continue; case '?': result ~= '?'; continue; // Octal, one to three digits ("\0", "\12", "\377") case '0': .. case '7': uint v = e - '0'; for (int d = 1; d < 3 && i + 1 < input.length && isOctalDigit(input[i + 1]); ++d) v = (v * 8) + (input[++i] - '0'); if (v > 0xff) throw new Exception(text(MSG_ESCAPE_OUT_OF_RANGE, `\`, e)); result ~= cast(ubyte)v; continue; // Hexadecimal, one or two digits ("\x0", "\x1b"). C would keep eating // digits and overflow; a trailing hex digit here is ambiguous with // literal text ("\x1bfoo"), so it is rejected rather than guessed at. case 'x': if (i + 1 >= input.length || isHexDigit(input[i + 1]) == false) throw new Exception(text(MSG_UNKNOWN_ESCAPE, `\x`)); size_t esc = i - 1; // on '\\', for error reporting uint h; for (int d; d < 2 && i + 1 < input.length && isHexDigit(input[i + 1]); ++d) { char x = input[++i]; h = (h * 16) + (x <= '9' ? x - '0' : (x | 0x20) - 'a' + 10); } if (i + 1 < input.length && isHexDigit(input[i + 1])) throw new Exception(text(MSG_ESCAPE_TOO_MANY_DIGITS, input[esc..i + 2])); result ~= cast(ubyte)h; continue; default: throw new Exception(text(MSG_UNKNOWN_ESCAPE, `\`, e, MSG_ESCAPE_RAW_HINT)); } }
return result;}unittest{ assert(unescape("") == []); assert(unescape("test") == cast(ubyte[])"test");
// Single-character escapes assert(unescape(`\a`) == [ 0x07 ]); assert(unescape(`\b`) == [ 0x08 ]); assert(unescape(`\e`) == [ 0x1b ]); assert(unescape(`\f`) == [ 0x0c ]); assert(unescape(`\n`) == [ 0x0a ]); assert(unescape(`\r`) == [ 0x0d ]); assert(unescape(`\t`) == [ 0x09 ]); assert(unescape(`\v`) == [ 0x0b ]); assert(unescape(`\\`) == [ '\\' ]); assert(unescape(`\'`) == [ '\'' ]); assert(unescape(`\"`) == [ '"' ]); assert(unescape(`\?`) == [ '?' ]);
// Octal assert(unescape(`\0`) == [ 0 ]); assert(unescape(`\00`) == [ 0 ]); assert(unescape(`\000`) == [ 0 ]); assert(unescape(`\7`) == [ 7 ]); assert(unescape(`\12`) == [ 0x0a ]); assert(unescape(`\101`) == [ 'A' ]); assert(unescape(`\377`) == [ 0xff ]); assert(unescape(`\0008`) == [ 0, '8' ]); // stops after three digits assert(unescape(`\08`) == [ 0, '8' ]); // '8' is not octal
// Hexadecimal assert(unescape(`\x0`) == [ 0 ]); assert(unescape(`\x00`) == [ 0 ]); assert(unescape(`\x1b`) == [ 0x1b ]); assert(unescape(`\xFF`) == [ 0xff ]); assert(unescape(`\x41z`) == [ 'A', 'z' ]); assert(unescape(`\x0z`) == [ 0, 'z' ]);
// Mixed assert(unescape(`a\tb`) == [ 'a', 0x09, 'b' ]); assert(unescape(`\r\n`) == [ 0x0d, 0x0a ]); assert(unescape(`C:\\Users`) == cast(ubyte[])`C:\Users`);
void test_throw(string input) { try { cast(void)unescape(input); } catch (Exception) { return; }
import std.stdio : stderr; stderr.writeln("Failed to throw with: ", input); assert(false, "test_throw test failed"); } test_throw(`\`); // dangling backslash test_throw(`a\`); test_throw(`\z`); // unknown escape test_throw(`\U0001`); // universal character names unsupported test_throw(`\x`); // missing hex digits test_throw(`\xzz`); test_throw(`\xdead`); // ambiguous with literal text, use "\xde ad" or x:dead test_throw(`\x1bfoo`); test_throw(`\400`); // over 0xff test_throw(`\777`);}
/// Split arguments while accounting for quotes.////// Word splitting, quoting, and escapes, in one pass, out the other end as/// bytes. Quoting style selects how a run of text is read, the same way it does/// in a programming language, and the two quotes buy two independent things:////// ---/// protects whitespace escape sequences/// utf8:text no no/// utf8:'text' yes no/// utf8:"text" yes yes/// ---////// So `utf8:C:\Users` and `utf8:'C:\Program Files'` are paths as typed, while/// `utf8:"\x1b[0m"` is an escape sequence:////// $(UL/// $(LI Unquoted and `'single quoted'` text is raw. Every byte up to the end of/// the run is taken verbatim, backslashes included, and there are no/// escapes at all. A `'` therefore cannot appear inside single quotes.)/// $(LI `"double quoted"` text is a C string literal and is resolved here, see/// unescape. An unknown escape is an error rather than a backslash that/// means itself, so `"C:\dir"` is `'C:\dir'` or `"C:\\dir"`.)/// )////// Adjacent runs concatenate into one argument, each read its own way, so/// `utf8:'C:\Users'"\0"` is a raw path followed by a NUL. That is a plain/// concatenation of literals, as in any language that has more than one kind.////// A quote is written by using the other kind (`"'"`, `'"'`), which is the only/// way, there being no escapes outside of double quotes.////// The result is bytes because an escape can produce one that is not text: it/// is up to the command to say whether it wanted text, see Argument.text.////// Uses the GC to append to the new array./// Params: buffer = Shell-like input./// Returns: Arguments./// Throws: Exception on an unterminated quote or an invalid escape sequence.Argument[] arguments(const(char)[] buffer){ import std.string : strip; import std.ascii : isControl, isWhite; import std.conv : text; buffer = strip(buffer); if (buffer.length == 0) return []; Argument[] results; immutable(ubyte)[] data; // Bytes of the argument being read // Empty quotes are not an empty argument, they are no argument void flush() { if (data.length == 0) return;
results ~= Argument(data); data = null; }
for (size_t i; i < buffer.length; ++i) { char c = buffer[i];
// Whitespace outside of a quote ends the argument if (isControl(c) || isWhite(c)) { flush(); continue; }
// Single quotes are literal to the byte. They end at the next quote and // nothing in between is syntax, not even a backslash, which is what // makes them the form for data carrying its own if (c == '\'') { size_t start = ++i; while (i < buffer.length && buffer[i] != '\'') ++i; if (i >= buffer.length) throw new Exception(text(MSG_UNTERMINATED_QUOTE, c)); data ~= cast(const(ubyte)[])buffer[start..i]; continue; }
// Double quotes are a C string. The end of the run is found first, with // a backslash only recognized as covering the next character, so a pair // cannot swallow the closing quote and `"C:\\"` terminates if (c == '"') { size_t start = ++i; for (; i < buffer.length; ++i) { if (buffer[i] == '\\') { ++i; continue; } if (buffer[i] == '"') break; } if (i >= buffer.length) throw new Exception(text(MSG_UNTERMINATED_QUOTE, c)); data ~= unescape(buffer[start..i]); continue; }
// Unquoted text is raw and runs until whitespace or a quote size_t start = i; while (i < buffer.length) { char u = buffer[i]; if (isControl(u) || isWhite(u) || u == '\'' || u == '"') break; ++i; } data ~= cast(const(ubyte)[])buffer[start..i]; --i; // Whatever stopped the run has not been read yet }
flush();
return results;}@system unittest{ void test_throw(string input) { Argument[] r; try { r = arguments(input); } catch (Exception) { return; }
import std.stdio : stderr; stderr.writeln("Failed to throw with: ", input, " it produced: ", r); assert(false, "test_throw test failed"); }
assert(texts(arguments("")) == []); assert(texts(arguments("\n")) == []); assert(texts(arguments("a")) == [ "a" ]); assert(texts(arguments("simple")) == [ "simple" ]); assert(texts(arguments("simple a b c")) == [ "simple", "a", "b", "c" ]); assert(texts(arguments("simple test\n")) == [ "simple", "test" ]); assert(texts(arguments("simple test\r\n")) == [ "simple", "test" ]); assert(texts(arguments("/simple/ /test/")) == [ "/simple/", "/test/" ]); assert(texts(arguments(`simple 'test extreme'`)) == [ "simple", "test extreme" ]); assert(texts(arguments(`simple "test extreme"`)) == [ "simple", "test extreme" ]); assert(texts(arguments(`simple ' hehe '`)) == [ "simple", " hehe " ]); assert(texts(arguments(`simple " hehe "`)) == [ "simple", " hehe " ]); assert(texts(arguments(`a 'b c' d`)) == [ "a", "b c", "d" ]); assert(texts(arguments(`a "b c" d`)) == [ "a", "b c", "d" ]); assert(texts(arguments(`/type 'yes string'`)) == [ "/type", "yes string" ]); assert(texts(arguments(`/type "yes string"`)) == [ "/type", "yes string" ]); assert(texts(arguments(`A B`)) == [ "A", "B" ]);
// Double quotes are the C string literal, so that is the one place a // backslash is syntax. Everywhere else it is data and costs nothing assert(texts(arguments(`find utf8:a\tb`)) == [ "find", `utf8:a\tb` ]); assert(texts(arguments(`find "a\tb"`)) == [ "find", "a\tb" ]); assert(texts(arguments(`find utf8:C:\\Users`)) == [ "find", `utf8:C:\\Users` ]); assert(texts(arguments(`find "utf8:C:\\Users"`)) == [ "find", `utf8:C:\Users` ]); assert(texts(arguments(`test\\ value`)) == [ `test\\`, "value" ]); assert(texts(arguments(`test\\value`)) == [ `test\\value` ]); assert(texts(arguments(`"a\\ b"`)) == [ `a\ b` ]); assert(texts(arguments(`a "b \"c\" d"`)) == [ "a", `b "c" d` ]); assert(texts(arguments(`open C:\dir`)) == [ "open", `C:\dir` ]);
// An unknown escape is an error, not a backslash that means itself, so a // Windows path in double quotes has to say which it is. Bash would take // `"C:\dir"` as written and `"C:\take"` as a path with a tab in it test_throw(`open "C:\dir"`); assert(texts(arguments(`open 'C:\dir'`)) == [ "open", `C:\dir` ]); assert(texts(arguments(`open "C:\\dir"`)) == [ "open", `C:\dir` ]);
// Single quotes are literal: not even a backslash is syntax inside assert(texts(arguments(`find 'a\tb'`)) == [ "find", `a\tb` ]); assert(texts(arguments(`find 'utf8:C:\\Users'`)) == [ "find", `utf8:C:\\Users` ]); assert(texts(arguments(`open 'C:\dir'`)) == [ "open", `C:\dir` ]); assert(texts(arguments(`open 'C:\'`)) == [ "open", `C:\` ]); assert(texts(arguments(`open C:\`)) == [ "open", `C:\` ]); assert(texts(arguments(`'a "b" c'`)) == [ `a "b" c` ]); assert(texts(arguments(`'a \"b\" c'`)) == [ `a \"b\" c` ]);
// A quote is written with the other kind of quote, there being nothing else // to write it with. The POSIX `'\''` idiom does not apply here: outside of // double quotes a backslash is data, so it leaves a dangling quote assert(texts(arguments(`'it'"'"'s'`)) == [ `it's` ]); assert(texts(arguments(`"it"'"'"s"`)) == [ `it"s` ]); assert(texts(arguments(`say'"'hi'"'`)) == [ `say"hi"` ]); test_throw(`'it'\''s'`); test_throw(`\"a\"`);
// Adjacent spans concatenate into one argument, so a quote can cover part // of an argument and the prefix can sit inside or outside it assert(texts(arguments(`utf8:'a''b'`)) == [ `utf8:ab` ]); assert(texts(arguments(`utf8:'a'"b"`)) == [ `utf8:ab` ]); assert(texts(arguments(`'utf8:'abc`)) == [ `utf8:abc` ]); assert(texts(arguments(`a'b'c`)) == [ `abc` ]);
// Nested/mixed quotes assert(texts(arguments(`a "b 'c' d" e`)) == [ "a", `b 'c' d`, "e" ]); assert(texts(arguments(`a 'b "c" d' e`)) == [ "a", `b "c" d`, "e" ]);
// Empty quotes are not an empty argument, they are no argument: there is // no command here that distinguishes "" from absent, and dropping keeps // 'find ""' an obvious "Need search" rather than an obscure empty needle assert(texts(arguments(`find ''`)) == [ "find" ]); assert(texts(arguments(`find ""`)) == [ "find" ]); assert(texts(arguments(`find '' x:00`)) == [ "find", "x:00" ]);
// Unterminated quotes are an error, not a line that ends where it stopped test_throw(`find 'abc`); test_throw(`find "abc`); test_throw(`'`); test_throw(`"`); // A backslash covers the next character, so it cannot swallow the closing // quote and a C string can end on one assert(texts(arguments(`"test\\"`)) == [ `test\` ]); assert(texts(arguments(`a "b\\" c`)) == [ "a", `b\`, "c" ]); assert(texts(arguments(`"C:\\"`)) == [ `C:\` ]); assert(texts(arguments(`"a\\\"b"`)) == [ `a\"b` ]); test_throw(`"abc\"`);
// Quoting is what selects how a run is read, and unquoted and single quoted // are the same raw thing: single quotes only hold an argument together assert(arguments(`a`) == [ Argument(`a`) ]); assert(arguments(`'a'`) == [ Argument(`a`) ]); assert(arguments(`"a"`) == [ Argument(`a`) ]); assert(arguments(`utf8:"a"`) == [ Argument(`utf8:a`) ]); assert(arguments(`"utf8:a"`) == [ Argument(`utf8:a`) ]); assert(arguments(`a "b" c`) == [ Argument(`a`), Argument(`b`), Argument(`c`) ]);
// Mixing the two forms is a concatenation of literals, as in any language // that has more than one kind, so each part keeps its own meaning: a raw // path with a NUL stuck on the end assert(arguments(`utf8:'C:\dir'"\0"`) == [ Argument(cast(immutable(ubyte)[])"utf8:C:\\dir\0") ]); assert(arguments(`utf8:"a"b`) == [ Argument(`utf8:ab`) ]); assert(arguments(`utf8:C:\dir"a b"`) == [ Argument(`utf8:C:\dir` ~ `a b`) ]); assert(arguments(`utf8:'a'b"c""d"`) == [ Argument(`utf8:abcd`) ]);
// Bytes that no encoding claims are still an argument. That is the whole // point of resolving escapes here: `"\xff"` used to be untypable outside // of a pattern, and text was the wrong type for it everywhere assert(arguments(`"\xff"`) == [ Argument(cast(immutable(ubyte)[])[ 0xff ]) ]); assert(arguments(`"a\0b"`) == [ Argument(cast(immutable(ubyte)[])"a\0b") ]); assert(arguments(`"\xff"a`) == [ Argument(cast(immutable(ubyte)[])[ 0xff, 'a' ]) ]); // ...and only a command that wanted text says so try { cast(void)texts(arguments(`open "\xff"`)); assert(false, "text() should have thrown"); } catch (Exception) {}
assert(texts(arguments(`find utf8:WARNING: %s`)) == [ "find", `utf8:WARNING:`, `%s` ]); assert(texts(arguments(`find utf8:"WARNING: %s"`)) == [ "find", `utf8:WARNING: %s` ]); assert(texts(arguments(`find "WARNING: %s"`)) == [ "find", `WARNING: %s` ]);}
/// Arguments of a command line being typed, for completion.////// Same as arguments(), except the last argument may be empty or missing its/// closing quote. It is always present, empty when the line ends on a space./// Params:/// buffer = Shell-like input, up to the caret./// start = Set to the byte offset where the last argument begins./// Returns: Arguments, never empty./// Throws: Exception on an invalid escape sequence.Argument[] partialArguments(const(char)[] buffer, out size_t start){ import std.ascii : isControl, isWhite;
char quote = 0; // Quote left open, or 0 (char.init is 0xff) for (size_t i; i < buffer.length; ++i) { char c = buffer[i]; if (quote) { if (c == quote) quote = 0; else if (quote == '"' && c == '\\') ++i; continue; } if (c == '\'' || c == '"') quote = c; else if (isControl(c) || isWhite(c)) start = i + 1; }
Argument[] results = arguments(buffer[0..start]); const(char)[] last = buffer[start..$]; if (quote) last ~= quote; Argument[] tail = arguments(last); results ~= tail.length ? tail[0] : Argument.init; return results;}unittest{ size_t start; assert(texts(partialArguments("", start)) == [ "" ] && start == 0); assert(texts(partialArguments("op", start)) == [ "op" ] && start == 0); assert(texts(partialArguments("open ", start)) == [ "open", "" ] && start == 5); assert(texts(partialArguments("open sr", start)) == [ "open", "sr" ] && start == 5); assert(texts(partialArguments("open 'my d", start)) == [ "open", "my d" ] && start == 5); assert(texts(partialArguments(`open "a\"b c`, start)) == [ "open", `a"b c` ] && start == 5); assert(texts(partialArguments("open 'my dir/'s", start)) == [ "open", "my dir/s" ] && start == 5); assert(texts(partialArguments("set data x", start)) == [ "set", "data", "x" ] && start == 10);}
/// Quote text so arguments() reads it back as one argument, or as-is when/// nothing in it needs quoting./// Params: text = Argument text./// Returns: Text fit for a command line.string quoteArgument(string text){ import std.ascii : isControl, isWhite; import std.format : format;
bool plain = true; bool squote; // has a single quote bool control; // has a byte that needs an escape foreach (char c; text) { if (isControl(c) || isWhite(c) || c == '\'' || c == '"') plain = false; if (c == '\'') squote = true; if (isControl(c)) control = true; } if (plain) return text; if (squote == false && control == false) return "'" ~ text ~ "'";
string result = `"`; foreach (char c; text) { if (c == '\\' || c == '"') result ~= '\\'; if (isControl(c)) result ~= format("\\%03o", c); // \x rejects a hex digit after it else result ~= c; } return result ~ `"`;}unittest{ assert(quoteArgument("") == ""); assert(quoteArgument("src/") == "src/"); assert(quoteArgument(`C:\dir`) == `C:\dir`); assert(quoteArgument("my dir/") == "'my dir/'"); assert(quoteArgument(`a"b`) == `'a"b'`); assert(quoteArgument(`it's \x`) == `"it's \\x"`); assert(quoteArgument("a\tb") == `"a\011b"`);
foreach (string s; [ "my dir/", `a"b`, `it's \x`, "a\tb", `C:\a b` ]) assert(texts(arguments(quoteArgument(s))) == [ s ], s);}
/// Parse string as hexadecimal, decimal, or octal./// Params: input = String input./// Returns: Parsed value./// Throws: Exception when empty, ConvException on anything but one number.long scan(scope string input){ import std.conv : parse, ConvException; import std.string : strip, startsWith; // Imitate stroll a little by ignoring whitespace input = strip(input); if (input.length == 0) throw new Exception("Empty scan"); string digits = input; uint radix = 10; // Hex: stroll supports "0X" but don't see the appeal yet if (startsWith(digits, "0x")) { digits = digits[2..$]; radix = 16; } // Binary: C23 supports "0b" and "0B" for binary numbers else if (startsWith(digits, "0b")) { digits = digits[2..$]; radix = 2; } // Octal else if (digits.length > 2 && digits[0] == '0') { digits = digits[1..$]; radix = 8; } // NOTE: Decimal is the only base where negative sign is allowed long value = parse!long(digits, radix); if (digits.length) // parse stops at the first character it can't take throw new ConvException(MSG_INVALID_NUMBER ~ input); return value;}@system unittest{ import std.conv : octal; // decimal assert(scan("0") == 0); assert(scan("1") == 1); assert(scan("2") == 2); assert(scan("10") == 10); assert(scan("-10") == -10); // hex assert(scan("0x0") == 0); assert(scan("0x1") == 0x1); assert(scan("0x2") == 0x2); assert(scan("0x10") == 0x10); // binary assert(scan("0b0") == 0); assert(scan("0b1") == 0b1); assert(scan("0b10") == 0b10); assert(scan("0b11") == 0b11); // octal assert(scan("00") == 0); assert(scan("01") == octal!"1"); assert(scan("02") == octal!"2"); assert(scan("010") == octal!"10"); // whitespace around is fine, anything else after the number is not assert(scan(" 0x10 ") == 0x10); import std.conv : ConvException; foreach (string s; [ "0x1000..0x33fff", "0x10zz", "12ab", "0b102", "019", "1 2", "0x" ]) { try { cast(void)scan(s); assert(false, s); } catch (ConvException) {} }}
/// Parse as a binary number with optional suffix up to gigabytes.////// For example, "32K" translates to 32768 (Bytes, 32 * 1024)./// Params: input = String input./// Returns: Byte count./// Throws: Exception or ConvException on error.ulong parsebin(scope string input){ import ddhx.platform : assertion; import std.conv : parse; assertion(input, "input is NULL"); assertion(input.length, "input is EMPTY"); ulong mult = 1; if (input.length > 1) { switch (input[$-1]) { case 'k', 'K': input = input[0..$-1]; mult = 1024; break; case 'm', 'M': input = input[0..$-1]; mult = 1024 * 1024; break; case 'g', 'G': input = input[0..$-1]; mult = 1024 * 1024 * 1024; break; default: } } return parse!ulong(input) * mult;}@system unittest{ assert(parsebin("0") == 0); assert(parsebin("1") == 1); assert(parsebin("10") == 10); assert(parsebin("8086") == 8086); assert(parsebin("1k") == 1024); assert(parsebin("1K") == 1024); assert(parsebin("2K") == 2 * 1024); assert(parsebin("1024K") == 1024 * 1024); assert(parsebin("1m") == 1024 * 1024); assert(parsebin("1M") == 1024 * 1024); assert(parsebin("2M") == 2 * 1024 * 1024); assert(parsebin("1g") == 1024 * 1024 * 1024); assert(parsebin("1G") == 1024 * 1024 * 1024); assert(parsebin("2G") == 2L * 1024 * 1024 * 1024); try { parsebin(null); // @suppress(dscanner.unused_result) assert(false); // Needs to throw } catch (Exception) {} try { parsebin(""); // @suppress(dscanner.unused_result) assert(false); // Needs to throw } catch (Exception) {} try { parsebin("-"); // @suppress(dscanner.unused_result) assert(false); // Needs to throw } catch (Exception) {} try { parsebin("-1"); // @suppress(dscanner.unused_result) assert(false); // Needs to throw } catch (Exception) {}}
/// Align a value downwards./// Params:/// v = Value./// alignment = Alignment value./// Returns: Aligned value.long align64down(long v, size_t alignment){ long mask = alignment - 1; return v & ~mask;}unittest{ assert(align64down( 0, 16) == 0); assert(align64down( 1, 16) == 0); assert(align64down( 2, 16) == 0); assert(align64down(15, 16) == 0); assert(align64down(16, 16) == 16); assert(align64down(17, 16) == 16); assert(align64down(31, 16) == 16); assert(align64down(32, 16) == 32); assert(align64down(33, 16) == 32);}
/// Align a value upwards./// Params:/// v = Value./// alignment = Alignment value./// Returns: Aligned value.long align64up(long v, size_t alignment){ long mask = alignment - 1; return (v+mask) & ~mask;}unittest{ assert(align64up( 0, 16) == 0); assert(align64up( 1, 16) == 16); assert(align64up( 2, 16) == 16); assert(align64up(15, 16) == 16); assert(align64up(16, 16) == 16); assert(align64up(17, 16) == 32); assert(align64up(31, 16) == 32); assert(align64up(32, 16) == 32); assert(align64up(33, 16) == 48);}
/// Divides an integer by a whole percentage./// Params:/// a = Number/// per = Percent (0-100)/// Returns: Number. Value of 1000 with per=50(%) will give 500.long llpercentdiv(long a, int per){ return (a * per) / 100;}unittest{ assert(llpercentdiv(1000, 0) == 0); assert(llpercentdiv(1000, 50) == 500); assert(llpercentdiv(1000, 100) == 1000); assert(llpercentdiv( 64, 50) == 32);}
/// Divides an integer by a percentage./// Params:/// a = Number/// per = Percent (0-100)/// Returns: Number. Value of 1000 with per=50.0(%) will give 500.long llpercentdivf(long a, double per){ import std.math : round; return cast(long)round((cast(double)a * per) / 100.0);}unittest{ assert(llpercentdivf(1000, 0.0) == 0); assert(llpercentdivf(1000, 50.0) == 500); assert(llpercentdivf(1000, 55.5) == 555); assert(llpercentdivf(1000, 100.0) == 1000); assert(llpercentdivf( 64, 50.0) == 32);}
/// Simple buffered writer structure with custom flush function/// Params:/// FLUSHER = Flush function./// SIZE = Size of the buffer.struct BufferedWriter(void function(void*,size_t) FLUSHER, size_t SIZE = 2048){ private ubyte[SIZE] buffer; private size_t index; private void function(void*,size_t) flusher = FLUSHER; /// Append data to the buffer /// Automatically flushes if buffer would overflow void put(scope const(ubyte)[] data) { if (data.length > SIZE) { flush(); flusher(cast(void*)data.ptr, data.length); return; } if (data.length + index > SIZE) { size_t avail = available(); buffer[index .. index + avail] = data[0 .. avail]; index += avail; flush(); data = data[avail .. $]; } buffer[index .. index + data.length] = data[]; index += data.length; } /// Append a string to the buffer void put(scope const(char)[] str) { put(cast(const(ubyte)[])str); } /// Put a single character void put(char c) { if (1 + index > SIZE) flush(); buffer[index] = cast(ubyte)c; index++; } void repeat(char c, size_t count) { if (count == 0) return; // Handle large counts that exceed buffer if (count > SIZE) { flush(); buffer[] = c; while (count > SIZE) { flusher(buffer.ptr, SIZE); count -= SIZE; } if (count > 0) { flusher(buffer.ptr, count); } return; } // Normal case - fits in buffer (possibly after flush) if (count + index > SIZE) { flush(); } buffer[index .. index + count] = c; index += count; } /// Write buffered data to stdout void flush() { if (index > 0) { size_t len = index; index = 0; flusher(buffer.ptr, len); } } /// Clear buffer without flushing void reset() { index = 0; } /// Returns number of bytes currently in buffer. /// Returns: Size in bytes. size_t length() const { return index; } /// Returns remaining space in buffer. /// Returns: Size in bytes. size_t available() const { return SIZE - index; } /// Returns true if buffer is empty /// Returns: True if empty. bool empty() const { return index == 0; }}unittest{ BufferedWriter!((void *data, size_t size) { assert(data); assert(size == 3); }, 16) bufwriter; bufwriter.put("gay"); assert(bufwriter.index == 3); assert(bufwriter.buffer[0..3] == "gay"); bufwriter.flush();}unittest{ // Incoming data too large to hold into buffer string str2 = "very long string that flushes once"; // 34 BufferedWriter!((void *data, size_t size) { assert(data); assert(size == 34); }, 16) bufwriter; bufwriter.put(str2); assert(bufwriter.index == 0); // flushed}unittest{ // BufferedWriter!((void *data, size_t size) { assert(data); assert(size == 16); }, 16) bufwriter; bufwriter.put("1234567890"); bufwriter.put("1234567890"); assert(bufwriter.index == 4);}