StringRepeat¶
Status: Stable
documented, exercised by the test suite and/or worked examples, with no known limitations recorded.
Description¶
StringRepeat["string", n]
Gives a string with "string" repeated n times.
StringRepeat["string", n, max]
Gives up to n copies of "string", truncated to a total length of at most max characters.
Examples (10)¶
Every input below was run against the current Mathilda build and its output recorded.
Basic examples (3)¶
In[1]:= StringRepeat["a", 50]
Out[1]= "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
In[2]:= StringRepeat["abc", 10]
Out[2]= "abcabcabcabcabcabcabcabcabcabc"
In[3]:= StringRepeat["ab", 10, 19]
Out[3]= "abababababababababa"
Applications (7)¶
Four copies
A rule of ten dashes
Capped at 7 characters, so the last copy is partial
A cap larger than the result has no effect
The cap counts characters, so no character is cut in half
Zero copies is the empty string
A negative count: StringRepeat::intp, left unevaluated
Algorithm¶
stringrepeat.c - StringRepeat builtin for Mathilda
StringRepeat["str", n] - "str" concatenated n times.
StringRepeat["str", n, max] - up to n copies of "str", truncated so the
total length is at most max (a partial final
copy is allowed).
Lengths count characters (Unicode code points): StringRepeat["a:00e9", 2, 3] is "a:00e9a". Invalid arguments emit Mathematica's StringRepeat::string / StringRepeat::intp and leave the call unevaluated.
Implementation notes¶
Algorithm. builtin_stringrepeat takes 2–3 arguments and gives StringRepeat::argt otherwise. It works out how many whole copies of the base fit, which is n, or fewer when a maximum length max caps the result. Each whole copy is one memcpy. When the cap cuts the last copy, only that cut needs a character offset (u8_offset). The buffer is adopted into the result string (expr_new_string_adopt) without a second copy.
Data structures. One output buffer of exactly the final byte length. Lengths count Unicode code points (u8_length), so StringRepeat["a\:00e9", 2, 3] is "aéa" and not a string cut in the middle of a character.
Complexity / limits. O(output). n == 0, an empty base string, or max == 0 gives "". A non-string base gives StringRepeat::string, and a negative or non-integer count or max gives StringRepeat::intp. Both route through mth_message, so Quiet/Check see them, and leave the call unevaluated. A result length that would overflow size_t also stays unevaluated.
Attributes: Protected.
References¶
- Source:
src/strings/stringrepeat.c - Specification:
docs/spec/builtins/string-operations.md - Tests:
tests/test_string_review.c - Tests:
tests/test_stringrepeat.c
Notes & additional examples¶
Notes¶
StringRepeat[s, n]concatenatesncopies ofs.StringRepeat[s, n, max]stops atmaxcharacters, keeping a partial final copy.- Since v0.456 lengths count Unicode code points.
- A non-string first argument gives
StringRepeat::string, and a negative or non-integer count or cap givesStringRepeat::intp. Both leave the call unevaluated. - Whole copies are
memcpy'd and the buffer is adopted into the result without a second copy. - See also:
StringJoin,StringPadRight,Table.