chickadee » srfi-152 » string-replicate

(string-replicate string from to [start end]) → stringprocedure

This is an "extended substring" procedure that implements replicated copying of a substring. This substring is conceptually replicated both up and down the index space, in both the positive and negative directions.

For example, if string is "abcdefg", start is 3, and end is 6, then we have the conceptual bidirectionally-infinite string

…  d  e  f  d  e  f  d  e  f  d  e  f  d  e  f  d  e  f  d …
  -9 -8 -7 -6 -5 -4 -3 -2 -1  0 +1 +2 +3 +4 +5 +6 +7 +8 +9

string-replicate returns the substring of this string beginning at index from, and ending at to.

It is an error if from is greater than to.

You can use string-replicate to perform a variety of tasks:

  • To rotate a string left: (string-replicate "abcdef" 2 8) ⇒ "cdefab"
  • To rotate a string right: (string-replicate "abcdef" -2 4) ⇒ "efabcd"
  • To replicate a string: (string-replicate "abc" 0 7) ⇒ "abcabca"

Note that

  • The from / to arguments give a half-open range containing the characters from index from up to, but not including, index to.
  • The from / to indexes are not expressed in the index space of string. They refer instead to the replicated index space of the substring defined by string, start, and end.

It is an error if start = end, unless from = to, which is allowed as a special case.

Compatibility note: In SRFI 13, this procedure is called xsubstring.