StringRotateLeft¶
Status: Stable
documented, exercised by the test suite and/or worked examples, with no known limitations recorded.
Description¶
StringRotateLeft["string", n]
Cycles the characters of "string" n places to the left.
StringRotateLeft["string"]
Cycles one place to the left.
Examples (11)¶
Every input below was run against the current Mathilda build and its output recorded.
Basic examples (3)¶
In[1]:= StringRotateLeft["abcdef", 2]
Out[1]= "cdefab"
In[2]:= StringRotateRight["abcdef", 2]
Out[2]= "efabcd"
In[3]:= StringRotateLeft["abcdef", -8]
Out[3]= "efabcd"
Applications (8)¶
One place by default
A negative shift rotates right
Shifts wrap modulo the length
Positions count characters, not bytes
In[9]:= Table[StringRotateLeft["abcd", k], {k, 0, 4}]
Out[9]= {"abcd", "bcda", "cdab", "dabc", "abcd"}
In[10]:= StringRotateLeft["", 3]
Out[10]= ""
A symbolic shift: StringRotateLeft::int
Algorithm¶
stringrotate.c - StringRotateLeft / StringRotateRight.
StringRotateLeft[s, n] cycles the characters of s n places to the left
StringRotateLeft[s] one place
StringRotateRight[s, n] n places to the right (StringRotateLeft[s, -n])
n may be any integer (negative rotates the other way; |n| > length wraps). Positions count characters (Unicode code points). The result is two memcpys of the subject's bytes.
Implementation notes¶
Algorithm. builtin_stringrotateleft calls the shared kernel rotate(res, "StringRotateLeft", 1). It accepts one or two arguments: the subject must be a String (otherwise StringRotateLeft::string) and the optional shift a machine Integer (otherwise StringRotateLeft::int), defaulting to 1; both messages route through strings_arg_message / mth_message and leave the call unevaluated. The character count m comes from u8_length; the shift is converted to an equivalent left rotation k = n mod m, reduced into [0, m), and its byte offset found with u8_offset. The result is the tail from that offset followed by the head — two memcpys into one buffer adopted as the result String.
Data structures. Only the UTF-8 byte buffer of the subject; positions count Unicode code points, so multi-byte characters are never split.
Complexity / limits. O(B) in the byte length (one length scan, one offset scan, one copy). Any machine integer shift is accepted (it wraps modulo the length); a bignum shift is rejected with ::int. A list of strings is not threaded.
Attributes: Protected.
References¶
See also: StringRotateRight
- Source:
src/strings/stringrotate.c - Specification:
docs/spec/builtins/string-operations.md - Tests:
tests/test_string_review.c
Notes & additional examples¶
Notes¶
- The shift is reduced modulo the character count, the cut point located with
u8_offset, and the result assembled with twomemcpys of the UTF-8 bytes. - Takes a single string; a list of strings is not threaded (emits
::string), so useMap. A non-integer shift emits::int. - Attributes
{Protected}. - See also:
StringRotateRight,RotateLeft,StringTake.