There are currently two split-strategies for strings:
name: split-at-space
type: String -> List(String)
name: split-at-dot
type: String -> (String,String)
The first one splits a string at every occurrence of a space, the second one splits the string on the first occurrence of a dot. Since the names are almost the same this difference in behavior seems strange.

Therefore I would propose to name the first strategy something like ‘split-to-list-on-space’. The ‘split-at-space’-strategy should still be kept to stay BC, but it could just call the new strategy to make it easier to understand the difference.

Furthermore, I wanted to split on a different character so I took the current implementation and generalized it a bit:

/**
* Splits a string at every occurrence of a given character.
*
* @param chr Number of the character to split at
* @type String -> List(String)
*/
split-at-char(|chr) =
explode-string
; CharSplitInit(|chr)
; rec x( CharSplitExit <+ CharSplitNext ; x)

rules
CharSplitInit(|chr) :
x -> ([], [], x, chr)
CharSplitExit :
(xs, cs, [], _) -> [<reverse; implode-string> cs|xs]
CharSplitNext :
(xs, cs, [y|ys], chr) -> result
where if (y,chr)
then result := ([<reverse; implode-string> cs | xs], [], ys, chr)
else result := (xs, [y|cs], ys, chr)
end

Might not be the nicest implementation, but I think it would be nice to have this in the Stratego-libraries.

Submitted on 23 March 2007 at 10:09

On 28 January 2013 at 14:47 Eelco Visser removed tag 0.18

On 28 January 2013 at 14:47 Eelco Visser tagged interesting

Log in to post comments