add s:replace-all

FossilOrigin-Name: 91ca5b8716921386864322776e792ae318f8aa76425aba52e205184b139ad616
This commit is contained in:
crc 2019-06-13 14:33:03 +00:00
parent 36d102985c
commit 2d4bc0ef77
8 changed files with 588 additions and 517 deletions

View file

@ -1243,6 +1243,9 @@ Display a string.
s:replace D: sss-s A: - F: -
Replace the first instance of s2 in s1 with s3.
s:replace-all D: sss-s A: - F: -
Replace all instances of s2 in s1 with s3.
s:reverse D: s-s A: - F: -
Reverse the order of ASCII characters in a string.
@ -1433,7 +1436,7 @@ unix:sleep D: n- A: - F: -
Sleep for the specified number of seconds.
unix:slurp-pipe D: as-n A: - F: -
Open a pipe. Read all output from the pipe (until ASCII:NULL) is read into buffer. Return the number of bytes read.
Open a pipe. Read all output from the pipe (until ASCII:NULL) and store in the specified buffer. Return the number of bytes read.
unix:system D: s- A: - F: -
Runs another application using the system shell and returns after execution is completed.

View file

@ -413,6 +413,7 @@ s:map D: sq-s A: - F: -
s:prepend D: ss-s A: - F: -
s:put D: s- A: - F: -
s:replace D: sss-s A: - F: -
s:replace-all D: sss-s A: - F: -
s:reverse D: s-s A: - F: -
s:right D: sn-s A: - F: -
s:skip D: - A: - F: -

View file

@ -3593,6 +3593,14 @@
<p>Replace the first instance of s2 in s1 with s3.</p>
<p><b>Class:</b> class:word | <b>Namespace:</b> s | <b>Interface Layer:</b> all</p>
<hr/>
<h1>s:replace-all</h1>
<div style='margin-left: 1em;'><p><b>Data:</b> sss-s<br>
<b>Addr:</b> -<br>
<b>Float:</b> -</p>
</div>
<p>Replace all instances of s2 in s1 with s3.</p>
<p><b>Class:</b> class:word | <b>Namespace:</b> s | <b>Interface Layer:</b> all</p>
<hr/>
<h1>s:reverse</h1>
<div style='margin-left: 1em;'><p><b>Data:</b> s-s<br>
<b>Addr:</b> -<br>
@ -4157,7 +4165,7 @@
<b>Addr:</b> -<br>
<b>Float:</b> -</p>
</div>
<p>Open a pipe. Read all output from the pipe (until ASCII:NULL) is read into buffer. Return the number of bytes read.</p>
<p>Open a pipe. Read all output from the pipe (until ASCII:NULL) and store in the specified buffer. Return the number of bytes read.</p>
<p><b>Class:</b> class:word | <b>Namespace:</b> unix | <b>Interface Layer:</b> rre</p>
<hr/>
<h1>unix:system</h1>

View file

@ -4880,6 +4880,17 @@ Replace the first instance of s2 in s1 with s3.
Class: class:word | Namespace: s | Interface Layer: all
------------------------------------------------------------------------
s:replace-all
Data: sss-s
Addr: -
Float: -
Replace all instances of s2 in s1 with s3.
Class: class:word | Namespace: s | Interface Layer: all
------------------------------------------------------------------------
s:reverse
Data: s-s
@ -5646,7 +5657,7 @@ unix:slurp-pipe
Addr: -
Float: -
Open a pipe. Read all output from the pipe (until ASCII:NULL) is read into buffer. Return the number of bytes read.
Open a pipe. Read all output from the pipe (until ASCII:NULL) and store in the specified buffer. Return the number of bytes read.
Class: class:word | Namespace: unix | Interface Layer: rre
------------------------------------------------------------------------

BIN
ngaImage

Binary file not shown.

File diff suppressed because it is too large Load diff

View file

@ -1138,7 +1138,7 @@ located.
`s:tokenize` takes a string and a character to use as a
separator. It splits the string into a set of substrings and
returns a set containing pointers to each of them.
returns an array containing pointers to each of them.
~~~
{{
@ -1155,27 +1155,6 @@ returns a set containing pointers to each of them.
}}
~~~
`s:tokenize-on-string` is like `s:tokenize`, but for strings.
~~~
{{
'Tokens var
'Needle var
:-match? (s-sf)
dup @Needle s:contains-string? ;
:save-token (s-s)
@Needle s:split-on-string s:keep buffer:add n:inc ;
:tokens-to-set (-a)
here @Tokens buffer:size dup , [ fetch-next , ] times drop ;
---reveal---
:s:tokenize-on-string (ss-a)
[ s:keep !Needle here #8192 + !Tokens
@Tokens buffer:set
[ repeat -match? 0; drop save-token again ] call
s:keep buffer:add tokens-to-set ] buffer:preserve ;
}}
~~~
Use `s:format` to construct a string from multiple items. This
can be illustrated with:
@ -1535,6 +1514,46 @@ and `a:middle`.
}}
~~~
## Strings
Returning to strings, leveraging the array words alongside them
I can implement `s:replace-all` and `s:tokenize-on-string`.
`s:tokenize-on-string` is like `s:tokenize`, but for strings.
~~~
{{
'Needle d:create #128 allot
'Needle<Len> var
'Tokens d:create #128 allot
'TP var
:save s:keep @TP &Tokens a:store &TP v:inc ;
:next [ @Needle<Len> + ] sip ;
:done s:length n:zero? ;
---reveal---
:s:tokenize-on-string (ss-s)
#0 !TP
[ dup &Needle s:copy s:append ] [ s:length !Needle<Len> ] bi
[ &Needle s:split-on-string save next done ] until
&Tokens @TP n:dec !Tokens ;
}}
~~~
~~~
{{
'Replacement d:create #128 allot
:extract &Replacement s:copy ;
:tokenize s:tokenize-on-string s:empty ;
:combine &Replacement s:append s:append ;
:merge swap [ combine ] a:for-each nip ;
:find-end dup s:length &Replacement s:length - over + ;
:clean find-end #0 swap store ;
---reveal---
:s:replace-all (sss-s)
&Heap [ extract tokenize merge clean s:temp ] v:preserve ;
}}
~~~
## Muri: an assembler
Muri is my minimalist assembler for Nga. This is an attempt to

View file

@ -413,6 +413,7 @@ s:map sq-s - - Execute the specified quote once for each character in the string
s:prepend ss-s - - Return a new string consisting of s2 followed by s1. class:word {n/a} {n/a} s all
s:put s- - - Display a string. class:word {n/a} {n/a} global all
s:replace sss-s - - Replace the first instance of s2 in s1 with s3. class:word {n/a} {n/a} s all
s:replace-all sss-s - - Replace all instances of s2 in s1 with s3. class:word {n/a} {n/a} s all
s:reverse s-s - - Reverse the order of ASCII characters in a string. class:word {n/a} {n/a} s all
s:right sn-s - - Return a new string containing the specified number of characters from the right side of the string. class:word {n/a} {n/a} s all
s:skip - - - Internal helper function used to skip over a string in a definition. class:word {n/a} {n/a} s all
@ -476,7 +477,7 @@ unix:pclose n- - - Close a pipe. class:word {n/a} {n/a} unix rre
unix:popen sn-n - - Open a pipe. Takes a command to run, and a file mode (`file:R` or `file:W`; `file:R+` may work on some systems). Returns a file ID usable with words in the `file:` namespace. class:word {n/a} {n/a} unix rre
unix:putenv s- - - Takes a string in the form `name=value` and sets an environment variable named `name` to `value`. class:word 'EDITOR=vi unix:putenv {n/a} unix rre
unix:sleep n- - - Sleep for the specified number of seconds. class:word {n/a} {n/a} unix rre
unix:slurp-pipe as-n - - Open a pipe. Read all output from the pipe (until ASCII:NULL) is read into buffer. Return the number of bytes read. class:word {n/a} {n/a} unix rre
unix:slurp-pipe as-n - - Open a pipe. Read all output from the pipe (until ASCII:NULL) and store in the specified buffer. Return the number of bytes read. class:word {n/a} {n/a} unix rre
unix:system s- - - Runs another application using the system shell and returns after execution is completed. class:word 'ls_-lh_/etc unix:system {n/a} unix rre
unix:time -n - - Return a timestamp. class:word {n/a} {n/a} unix rre
unix:wait -n - - Waits for a child process to complete. This maps to the wait() system call. class:word {n/a} {n/a} unix rre

Can't render this file because it contains an unexpected character in line 62 and column 55.