chickadee » srfi-130 » string-replicate

string-replicateprocedure

This is an "extended substring" procedure that implements replicated copying of a substring of some string.

s is a string; start and end are optional arguments that demarcate a substring of s, defaulting to 0 and the length of s (i.e., the whole string). Replicate this substring up and down index space, in both the positive and negative directions. For example, if s = "abcdefg", start = 3, and end = 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. Note that these arguments cannot be cursors. 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 indexes give a half-open range--the characters from index from up to, but not including, index to.
  • The from / to indexes are not in terms of the index space for string s. They are in terms of the replicated index space of the substring defined by s, start, and end.
  • It is an error if start = end--although this is allowed by special dispensation when from = to.

Compatibility note: string-replicate is identical to the xsubstring procedure of SRFI 13, except that the to argument is required.