chickadee » srfi-135 » textual-replicate

(textual-replicate textual from to [start end]) → textprocedure

This is an "extended subtext" procedure that implements replicated copying of a subtext or substring.

textual is a text or string; start and end are optional arguments that specify a subtext of textual, defaulting to 0 and the length of textual. This subtext is conceptually replicated both up and down the index space, in both the positive and negative directions. For example, if textual is "abcdefg", start is 3, and end is 6, then we have the conceptual bidirectionally-infinite text

...  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

textual-replicate returns the subtext of this text beginning at index from, and ending at to. It is an error if from is greater than to.

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

  • To rotate a text left: (textual-replicate "abcdef" 2 8) ⇒ {{«cdefab»}}
  • To rotate a text right: (textual-replicate "abcdef" -2 4) ⇒ {{«efabcd»}}
  • To replicate a text: (textual-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 textual. They refer instead to the replicated index space of the subtext defined by textual, start, and end.

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