I propose to place the following strategy in the string library for StrategoXT:


/**
* Replaces one substring with another in a string.
*
* Example: <string-replace(|“foo”, “bar”)> “foobarfoo” gives “barbarbar”
*
* @param old - the old substring which is to be excised
* @param new - the new subtring which is to be inserted
* @type String -> String
*
* @author Karl Trygve Kalleberg karltk@stratego.org
*/
string-replace(|old, new) =
![ [], , old ]
; internal-string-replace(| old, <explode-string ; reverse> new)
; reverse
; implode-string

strategies

/** @internal */
internal-string-replace(|old, new) =
?[ pre, [ hd | tl ], cur ]
; (
!cur => [hd | ctl]
; ![ pre, tl, ctl ]
; internal-string-replace-found(|old, new)
; internal-string-replace(|old, new)
<+
![ [ hd | pre ], tl, cur ]
; internal-string-replace(|old, new)
)

/** @internal */
internal-string-replace(|old, new):
[ pre, [], _ ] -> pre

/** @internal */
internal-string-replace-found(|old, new) =
?[ pre, [ hd | tl ], cur ]
; !cur => [ hd | ctl ]
; ![ pre, tl, ctl ]
; internal-string-replace-found(|old, new)

/** @internal */
internal-string-replace-found(|old, new):
[ pre, rest, [] ] -> [ [ pre, new ], rest, old ]
—————————-

This is how it appears in the growing Curious library. I guess we should remove @author, and possibly rename it to string-replace. I can add it to the library myself, if there are no objections.

Submitted on 30 July 2005 at 04:21

On 1 August 2005 at 22:55 Jira commented:

STR-386, martin:
If it works and there is no existing string-replace-like strategy, then just add it :) . Please add some tests as well.

Btw, there is nothing wrong with mentioning an author: @author is not a copyright claim and it is useful if we can blame someone in case of bugs ;) .


On 19 October 2005 at 16:08 Jira commented:

STR-386, rbvermaa:
svn r. 13053

Log in to post comments