PARSE: INSERT and REMOVE can be handy

Carl Sassenrath, CTO
REBOL Technologies
25-Sep-2009 23:17 GMT

Article #0254
Main page || Index || Prior Article [0253] || Next Article [0255] || 19 Comments || Send feedback

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
parse d [to "b" remove 1]

Note that the input is properly adjusted for the action. It advances past the insert:

parse d: "ac" [to "c" insert "b" "c"]

and stays at the beginning of the remove:

parse d [to "b" remove 1 "c"]

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]

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"]]]

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.


Updated 23-Mar-2017 - Edit - Copyright REBOL Technologies -