From e8cacb7277b40b4c34c68fa2a95bbbcd0984d511 Mon Sep 17 00:00:00 2001 From: "Adam D. Ruppe" Date: Mon, 3 Nov 2025 19:22:09 -0500 Subject: [PATCH] using more useful functions --- dub.json | 24 +++++++++++++++---- oauth.d | 3 ++- string.d | 73 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ web.d | 2 ++ 4 files changed, 97 insertions(+), 5 deletions(-) diff --git a/dub.json b/dub.json index aa89f3a..45e1016 100644 --- a/dub.json +++ b/dub.json @@ -11,10 +11,26 @@ "description": "Shared components across other arsd modules", "targetType": "library", "libs-windows": ["user32", "ws2_32"], - "dflags-dmd": ["-mv=arsd.core=$PACKAGE_DIR/core.d"], - "dflags-ldc": ["--mv=arsd.core=$PACKAGE_DIR/core.d"], - "dflags-gdc": ["-fmodule-file=arsd.core=$PACKAGE_DIR/core.d"], - "sourceFiles": ["core.d"] + "dflags-dmd": [ + "-mv=arsd.core=$PACKAGE_DIR/core.d", + "-mv=arsd.string=$PACKAGE_DIR/string.d", + "-mv=arsd.conv=$PACKAGE_DIR/conv.d" + ], + "dflags-ldc": [ + "--mv=arsd.core=$PACKAGE_DIR/core.d", + "--mv=arsd.string=$PACKAGE_DIR/string.d", + "--mv=arsd.conv=$PACKAGE_DIR/conv.d" + ], + "dflags-gdc": [ + "-fmodule-file=arsd.core=$PACKAGE_DIR/core.d", + "-fmodule-file=arsd.string=$PACKAGE_DIR/string.d", + "-fmodule-file=arsd.conv=$PACKAGE_DIR/conv.d" + ], + "sourceFiles": [ + "core.d", + "string.d", + "conv.d" + ] }, { "name": "simpledisplay", diff --git a/oauth.d b/oauth.d index 4e41174..5b3e2ef 100644 --- a/oauth.d +++ b/oauth.d @@ -2,7 +2,8 @@ module arsd.oauth; import arsd.curl; -import arsd.cgi; // for decodeVariables +import arsd.uri; +import arsd.cgi : Cgi; import std.array; static import std.uri; static import std.algorithm; diff --git a/string.d b/string.d index 277afc4..3d5881b 100644 --- a/string.d +++ b/string.d @@ -53,3 +53,76 @@ deprecated("D calls this `stripRight` instead") alias trimRight = stripRight; alias stringz = arsd.core.stringz; // CharzBuffer // WCharzBuffer + +// ********* UTILITIES ************** + +string[] split(string s, string onWhat) { + assert(onWhat.length); + string[] ret; + more: + auto idx = s.indexOf(onWhat); + if(idx == -1) { + ret ~= s; + return ret; + } + ret ~= s[0 .. idx]; + s = s[idx + onWhat.length .. $]; + goto more; +} + +unittest { + assert("foo.bar".split(".") == ["foo", "bar"]); +} + +ptrdiff_t lastIndexOf(string s, string what) { + assert(what.length); + if(s.length < what.length) + return -1; + ptrdiff_t checking = s.length - what.length; + while(checking >= 0) { + if(s[checking .. checking + what.length] == what) + return checking; + + checking--; + } + + return -1; +} + +unittest { + assert("31234".lastIndexOf("3") == 3); +} + +string join(string[] str, string w) { + string ret; + foreach(i, s; str) { + if(i) + ret ~= w; + ret ~= s; + } + return ret; +} + +unittest { + assert(["a", "b"].join(" ") == "a b"); +} + +string replace(string str, string find, string repacement) { + assert(find.length); + + string ret; + more: + auto idx = str.indexOf(find); + if(idx == -1) { + ret ~= str; + return ret; + } + ret ~= str[0 .. idx]; + ret ~= repacement; + str = str[idx + find.length .. $]; + goto more; +} + +unittest { + assert("foobarfoo".replace("foo", "bar") == "barbarbar"); +} diff --git a/web.d b/web.d index 936871b..3b46e95 100644 --- a/web.d +++ b/web.d @@ -11,6 +11,8 @@ +/ module arsd.web; +import arsd.uri : decodeVariablesSingle, encodeVariables; + static if(__VERSION__ <= 2076) { // compatibility shims with gdc enum JSONType {