CharacterRange¶
Status: Stable
documented, exercised by the test suite and/or worked examples, with no known limitations recorded.
Description¶
CharacterRange["c1", "c2"]
Gives the list of characters with codes from that of c1 to that of c2.
CharacterRange[n1, n2]
Gives the characters with codes n1 through n2.
Examples (11)¶
Every input below was run against the current Mathilda build and its output recorded.
Basic examples (3)¶
In[1]:= CharacterRange["a", "e"]
Out[1]= {"a", "b", "c", "d", "e"}
In[2]:= CharacterRange[65, 70]
Out[2]= {"A", "B", "C", "D", "E", "F"}
In[3]:= Length[Alphabet[]]
Out[3]= 26
Applications (8)¶
Any Unicode characters, by code point
Integer endpoints are code points
Circled digits
An empty range
As a string pattern it is a character class
Mixed endpoint kinds: CharacterRange::argtype
Implementation notes¶
Algorithm. builtin_characterrange requires two arguments and converts each with range_end: a machine Integer that is a valid code point, or a String that decodes (via u8_decode) to exactly one code point. If either end is invalid, or the two ends are of different kinds (one string, one integer), it emits CharacterRange::argtype through mth_message and stays unevaluated. Otherwise char_list(lo, hi) encodes each code point from lo to hi with u8_encode into a one-character String, giving {} when lo > hi. As a string pattern, CharacterRange["a", "z"] is not expanded at all: string_pattern.c emits the PCRE character class [a-z] (put_class_char).
Data structures. A List of n = hi - lo + 1 one-character strings built by strings_char_expr. A surrogate code point inside an integer range (which u8_encode refuses) is rendered as "?" rather than producing invalid UTF-8.
Complexity / limits. O(n) time and memory in the size of the range; there is no lazy form, so a range spanning the whole code space allocates a million-element list. Ranges are by raw code point, with no collation or script awareness.
Attributes: Protected.
References¶
See also: Alphabet
- Source:
src/strings/charcode.c - Specification:
docs/spec/builtins/string-operations.md - Tests:
tests/test_string_review.c - Tests:
tests/test_stringfns.c
Notes & additional examples¶
Notes¶
- The endpoints are both one-character strings or both non-negative integer codes;
the range is by code point, so
CharacterRange["ά", "ε"]also picks up the accented Greek letters lying between them. {}when the first code exceeds the second. Anything else emitsCharacterRange::argtypeand stays unevaluated.- Inside a string pattern (
StringCases,StringMatchQ, …) it is translated to a PCRE class[c1-c2]instead of being expanded. - Attributes
{Protected}. - See also:
Alphabet,FromCharacterCode,LetterCharacter.