PARSE: INSERT and REMOVE can be handy
Over the years, perhaps one of the most requested enhancements was to allow insert and remove (and also change) directly in the parse dialect.
You will find the first two in A83. Check them out. For example:
parse d: "ac" [to "c" insert "b"]
probe d
"abc"
parse d [to "b" remove 1]
"ac"
Note that the input is properly adjusted for the action. It advances past the insert:
parse d: "ac" [to "c" insert "b" "c"]
true
and stays at the beginning of the remove:
parse d [to "b" remove 1 "c"]
true
Note that with REMOVE you can:
- use a positive integer to remove forward
- use a negative integer to remove backward
- use an index variable to remove to a specific location
Here's an index variable example:
parse "abxyzcd" [thru "b" m: to "c" remove m]
"abcd"
Also, with insert you can use the optional keyword ONLY to insert blocks-as-a-whole.
Note that the speed of insert and remove will depend on the size of the string (binary, block) being parsed. If you expect a large number of inserts or removes, it may be better to use an output string, and not make the inserts or removes at all.
But, for shorter strings...
dt [loop 1000000 [parse copy http://www.rebol/ [thru "rebol" insert ".com"]]]
0:00:01.031
So, about a million parse calls per second -- including entry, recovery overhead, and Unicode internals. Also, that number includes copying the original URL string, not part of the parse itself.
19 Comments
|