diff --git a/config/docs.cfg b/config/docs.cfg index b1e3e33b..07cf3f45 100644 --- a/config/docs.cfg +++ b/config/docs.cfg @@ -135,6 +135,8 @@ docargument [N] [the index of the word] [] [0]; docexample [echo (at "zero one two three" 2)] [output: two]; docident [break] [Aborts a loop created with a 'loop', 'looplist' or 'while' command.]; docexample [loop i 10 [ if (= $i 4) [ break ]; echo $i]] [output: 0 1 2 3]; +docident [breakparse] [Breaks out of a parsestring loop.]; +docremark [Important: This command should only be used within the 3rd argument (the cubescript to execute) of parsestring.]; docident [c] [Adds color to a string.]; docargument [N] [color id] [0 min, 9 max] [0]; docexample [echo (c 3)Hello (c 0)world!] [Output: a red "Hello" and a green "world!"]; @@ -309,6 +311,16 @@ docexample [echo (modf 7.5 12.5)] [Output: 7.5]; docexample [echo (modf 17.5 12.5)] [Output: 5.0]; docident [numargs] [The number of arguments passed to the current alias]; docargument [V] [numargs] [ read-only]; +docident [parsestring] [Loops through every character in the given string and executes the given block of cubescript on each iteration.]; +docargument [S] [string] [string to parse] [0]; +docargument [A] [string] [name of alias to use as iterator] [0]; +docargument [C] [string] [cubescript to execute on each iteration] [0]; +docargument [B] [integer] [non-zero to force backwards parse] [0]; +docremark [Important: A secondary iterator alias (prefixed with a double underscore "__") is automatically created before each iteration that contains the character position data.]; +docexample [parsestring "Hello world" iter [echo $iter]] [Uses echo on every character in the string: "Hello world"]; +docexample [parsestring "Hello world" iter [echo (concatword "Char #" $__iter ": " $iter)]] [Uses echo on every character in the string: "Hello world" --- Also outputs the position of each character in the string.]; +docexample [backwardsstring = []; parsestring "This will look interesting backwards." iter [backwardsstring = (concatword $backwardsstring $iter); if (= $__iter 0) [echo $backwardsstring]] 1] [Outputs: ".sdrawkcab gnitseretni kool lliw sihT"]; +docexample [parsestring "abcdefghijklmnopqrstuvwxyz" iter [if (> $__iter 4) breakparse [echo $iter]]] [Example usage of the breakparse command. Uses echo on characters a through e, then breaks out of the parse.]; docident [persistidents] [Controls whether aliases defined afterwards will be saved (1) or not (0). This option is useful to keep saved.cfg free from unneeded clutter.]; docargument [B] [control option] [1...persistent, 0...not persistent] [0]; docexample [persistidents 0; foo = [ echo "bar"]] [foo will not be saved and has to be redefined when restarting AC.]; @@ -397,6 +409,12 @@ docexample [echo (tolower HELLO)] [Output: hello]; docident [toupper] [Converts a string to all uppercase characters.]; docargument [S] [a string] [] [0]; docexample [echo (toupper hello)] [Output: HELLO]; +docident [trimAllUnnecessaryWhitespace] [Removes all unnecessary leading and trailing whitespace characters from the given string.]; +docargument [S] [string] [string to modify] [0]; +docexample [echo (trimAllUnnecessaryWhitespace " H e ll o w o r l d ")] [Outputs: "H e ll o w o r l d"]; +docident [trimAllWhitespace] [Removes all whitespace characters from the given string.]; +docargument [S] [string] [string to modify] [0]; +docexample [echo (trimAllWhitespace " H e ll o w o r l d ")] [Outputs: Helloworld]; docident [while] [Loops the specified body while the condition evaluates to true.]; docargument [cond] [the condition] [the code evaluated before each iteration] [0]; docargument [body] [the body to execute on each iteration] [] [0]; diff --git a/config/parsestring.cfg b/config/parsestring.cfg new file mode 100644 index 00000000..1d6b1890 --- /dev/null +++ b/config/parsestring.cfg @@ -0,0 +1,71 @@ +const breakparse [ parsebreak = 1 ] + +const doparse [ + parselen = (strlen $strtoparse) + loop dpl $parselen [ + if $parsebreak break [ + if $parsebackwards [ + $charcontainer = (substr $strtoparse (- (- $parselen $dpl) 1) 1) + (format __%1 $charcontainer) = (- (- $parselen $dpl) 1) + ] [ + $charcontainer = (substr $strtoparse $dpl 1) + (format __%1 $charcontainer) = $dpl + ] + execute $parsecode + ] + ] +] + +const parsestring [ + if (> $numargs 2) [ + strtoparse = $arg1 + charcontainer = $arg2 + parsecode = $arg3 + if (> $numargs 3) [ + parsebackwards = $arg4 + ] [ parsebackwards = 0 ] + parsebreak = 0 + doparse + ] +] + +// make helpful consts for use with (testchar) +const CHARACTERTESTS [ISDIGIT ISALPHA ISALNUM ISLOWERCASE ISUPPERCASE ISCHAR ISPUNCT ISWHITESPACE] +loop cel (listlen $CHARACTERTESTS) [ + const (at $CHARACTERTESTS $cel) $cel + addListOnQuit (at $CHARACTERTESTS $cel) +] + +// trimAllUnnecessaryWhitespace - Returns a modified string after removing any unnecessary leading and trailing whitespace from the original. +const trimAllUnnecessaryWhitespace [ + strtotrim = $arg1 + forceinit [charsbegin charsend] -1 + parsestring $strtotrim tch [ + if (! (testchar $tch $ISWHITESPACE)) [ + charsbegin = $__tch + breakparse + ] + ] + parsestring $strtotrim thc [ + if (! (testchar $thc $ISWHITESPACE)) [ + charsend = $__thc + breakparse + ] + ] 1 + if (&& (&& (!= $charsbegin -1) (!= $charsend -1)) (!= $charsbegin $charsend)) [ + return (substr $strtotrim $charsbegin (- (+ $charsend 1) $charsbegin)) + ] [ return $strtotrim ] +] +// trimAllWhitespace - Returns a modified string after removing any whitespace characters from the original. +const trimAllWhitespace [ + strtotrim = $arg1 + trimmedstr = [] + parsestring $strtotrim twh [ + if (! (testchar $twh $ISWHITESPACE)) [ + trimmedstr = (concatword $trimmedstr $twh) + ] + ] + return $trimmedstr +] + +addListOnQuit [parsebreak parselen strtoparse parsebackwards charcontainer dpl parsecode cel strtotrim charsbegin charsend tch __tch thc __thc twh trimmedstr] diff --git a/config/scripts.cfg b/config/scripts.cfg index 40854333..647a7c6d 100644 --- a/config/scripts.cfg +++ b/config/scripts.cfg @@ -477,6 +477,7 @@ const customclaimadminscript [ // Execute other .cfgs/scripts. run on_quit run mdarray +run parsestring run dyngamma run survival run autosave