cheatah
Module

string

Text operations plus Python's string constants, exposed as free functions so a .purr program writes string.upper("x").

import string

print(string.upper("purr"))            # PURR
print(string.split("a,b,c", ","))      # ['a', 'b', 'c']
print(string.join("-", ["a", "b"]))    # a-b

What's here

  • Constantsascii_lowercase, ascii_uppercase, ascii_letters, digits, hexdigits, octdigits, punctuation, whitespace.

  • Caseupper, lower, capitalize, title, swapcase.

  • Trimmingstrip, lstrip, rstrip.

  • Search / teststartswith, endswith, contains, find, rfind, count.

  • Transformreplace, split, splitlines, capwords, join.

  • Paddingljust, rjust, center, zfill.

  • Classificationisdigit, isalpha, isalnum, isspace, isupper, islower.

Functions returning std::string / std::vector<std::string> allocate their result on the heap; the predicate/index functions (bool/long) do not.

Per-function docs (parameters, runtime complexity, heap behavior) are in string.hpp. Tested in ../tests/string_test.cpp; ASan + Valgrind clean via the QA gate (security/run-valgrind.sh).

Functions

fn std::string upper(std::string_view s) source#

Uppercase.

Returns a new string with every ASCII lowercase letter mapped to uppercase via std::toupper; non-letters and bytes ≥ 0x80 are copied unchanged (ASCII-only).

Parameters
s

input.

Returns

s uppercased.

Complexity

O(n).

Allocation

allocates the result.

Compile-run testStringCompileRun.Upper
Performance42.47 ns/call in cheatah · 77.36 ns/call in CPython 3.12.3 · ≈1.9× faster
fn std::string lower(std::string_view s) source#

Lowercase.

Returns a new string with every ASCII uppercase letter mapped to lowercase via std::tolower; non-letters and bytes ≥ 0x80 are copied unchanged (ASCII-only).

Parameters
s

input.

Returns

s lowercased.

Complexity

O(n).

Allocation

allocates the result.

Compile-run testStringCompileRun.Lower
System testStdlibE2E.String
Performance42.41 ns/call in cheatah · 78.49 ns/call in CPython 3.12.3 · ≈1.8× faster
fn std::string capitalize(std::string_view s) source#

Capitalize: first char upper, rest lower.

Uppercases the first character and lowercases all remaining characters (ASCII-only); an empty input is returned unchanged.

Parameters
s

input.

Returns

capitalized s.

Complexity

O(n).

Allocation

allocates.

System testStdlibE2E.String
Performance49.90 ns/call in cheatah · 153 ns/call in CPython 3.12.3 · ≈3.1× faster
fn std::string title(std::string_view s) source#

Title-case each word.

Uppercases the first letter of every run of letters and lowercases the rest; any non-letter (digits, punctuation, whitespace) acts as a word boundary (ASCII-only).

Parameters
s

input.

Returns

title-cased s.

Complexity

O(n).

Allocation

allocates.

Compile-run testStringCompileRun.Title
System testStdlibE2E.String
Performance67.35 ns/call in cheatah · 165 ns/call in CPython 3.12.3 · ≈2.4× faster
fn std::string swapcase(std::string_view s) source#

Swap the case of each letter.

Returns a new string with each ASCII letter's case inverted; non-letters are left unchanged (ASCII-only).

Parameters
s

input.

Returns

case-swapped s.

Complexity

O(n).

Allocation

allocates.

System testStdlibE2E.String
Performance72.75 ns/call in cheatah · 165 ns/call in CPython 3.12.3 · ≈2.2× faster
fn std::string lstrip(std::string_view s, std::string_view chars=whitespace) source#

Strip leading chars.

Removes characters from the front only, as long as each is in the chars set (a bag of characters, not a substring); defaults to ASCII whitespace.

Parameters
s

input.

chars

cut set.

Returns

left-trimmed s.

Complexity

O(n·m) (m = size of the chars set; a constant for the default).

Allocation

allocates.

Compile-run testStringCompileRun.Lstrip
System testStdlibE2E.String
Performance14.61 ns/call in cheatah · 64.36 ns/call in CPython 3.12.3 · ≈4.5× faster
fn std::string rstrip(std::string_view s, std::string_view chars=whitespace) source#

Strip trailing chars.

Removes characters from the end only, as long as each is in the chars set (a bag of characters, not a substring); defaults to ASCII whitespace.

Parameters
s

input.

chars

cut set.

Returns

right-trimmed s.

Complexity

O(n·m) (m = size of the chars set; a constant for the default).

Allocation

allocates.

Compile-run testStringCompileRun.Rstrip
System testStdlibE2E.String
Performance13.48 ns/call in cheatah · 65.57 ns/call in CPython 3.12.3 · ≈4.7× faster
fn std::string strip(std::string_view s, std::string_view chars=whitespace) source#

Strip leading+trailing chars.

Removes characters from both ends as long as each is present in the chars set (the set is a bag of characters, not a substring); defaults to ASCII whitespace. An empty chars set strips nothing.

Parameters
s

input.

chars

cut set.

Returns

trimmed s.

Complexity

O(n·m) (m = size of the chars set; a constant for the default).

Allocation

allocates the result plus lstrip's intermediate string.

Compile-run testStringCompileRun.Strip
System testStdlibE2E.String
Performance30.67 ns/call in cheatah · 65.15 ns/call in CPython 3.12.3 · ≈2.1× faster
fn bool startswith(std::string_view s, std::string_view prefix) source#

Prefix test.

Case-sensitive, byte-exact comparison; an empty prefix always matches.

Parameters
s

input.

prefix

sought prefix.

Returns

true iff s starts with prefix.

Complexity

O(n).

Allocation

none.

System testStdlibE2E.String
Performance7.79 ns/call in cheatah · 72.52 ns/call in CPython 3.12.3 · ≈9.5× faster
fn bool endswith(std::string_view s, std::string_view suffix) source#

Suffix test.

Case-sensitive, byte-exact comparison; an empty suffix always matches.

Parameters
s

input.

suffix

sought suffix.

Returns

true iff s ends with suffix.

Complexity

O(n).

Allocation

none.

System testStdlibE2E.String
Performance8.10 ns/call in cheatah · 70.68 ns/call in CPython 3.12.3 · ≈9× faster
fn contains · 2 overloads
bool contains(std::string_view s, std::string_view sub)source#
bool contains(std::string_view s, char c)source#

Substring test.

Case-sensitive search for sub anywhere in s; an empty sub is always considered present.

Parameters
s

input.

sub

needle.

Returns

true iff sub occurs in s.

Complexity

O(n·m).

Allocation

none.

System testStdlibE2E.String
Performance7.44 ns/call in cheatah · 46.75 ns/call in CPython 3.12.3 · ≈6.3× faster
fn find · 2 overloads
long find(std::string_view s, std::string_view sub)source#
long find(std::string_view s, std::string_view sub, long start)source#

First index of sub.

Returns the 0-based byte index of the first (leftmost) case-sensitive match, or -1 if not found; an empty sub returns 0.

Parameters
s

input.

sub

needle.

Returns

index, or -1.

Complexity

O(n·m).

Allocation

none.

Compile-run testStringCompileRun.Find
System testStdlibE2E.String
Performance8.11 ns/call in cheatah · 80.63 ns/call in CPython 3.12.3 · ≈9.7× faster
fn long rfind(std::string_view s, std::string_view sub) source#

Last index of sub.

Returns the 0-based byte index of the last (rightmost) case-sensitive match, or -1 if not found; an empty sub returns the length of s.

Parameters
s

input.

sub

needle.

Returns

index, or -1.

Complexity

O(n·m).

Allocation

none.

Compile-run testStringCompileRun.Rfind
System testStdlibE2E.String
Performance4.93 ns/call in cheatah · 75.49 ns/call in CPython 3.12.3 · ≈15.3× faster
fn long count(std::string_view s, std::string_view sub) source#

Count non-overlapping sub.

Counts left-to-right, non-overlapping case-sensitive matches; matching Python, an empty sub returns len(s) + 1.

Parameters
s

input.

sub

needle.

Returns

occurrence count.

Complexity

O(n·m).

Allocation

none.

Compile-run testStringCompileRun.Count
Performance21.20 ns/call in cheatah · 80.27 ns/call in CPython 3.12.3 · ≈3.8× faster
fn std::string replace(std::string_view s, std::string_view from, std::string_view to) source#

Replace all from with to.

Replaces every non-overlapping, case-sensitive occurrence of from with to; an empty from leaves s unchanged (unlike Python).

Parameters
s

input.

from, to

needle/replacement.

Returns

new string.

Complexity

O(n·m + result length).

Allocation

allocates.

Compile-run testStringCompileRun.Replace
System testStdlibE2E.String
Performance54.09 ns/call in cheatah · 91.18 ns/call in CPython 3.12.3 · ≈1.7× faster
fn split · 2 overloads
std::vector< std::string > split(std::string_view s, std::string_view sep)source#
std::vector< std::string > split(std::string_view s)source#

Split on sep.

Splits at each non-overlapping occurrence of sep, keeping empty fields (e.g. "a,,b" yields three parts, leading/trailing separators yield empty strings); the result always has at least one element.

Parameters
s

input.

sep

separator (empty → the whole string as one part).

Returns

the parts.

Complexity

O(n·m).

Allocation

allocates a vector of strings.

Compile-run testStringCompileRun.Split
System testStdlibE2E.String
Performance124 ns/call in cheatah · 127 ns/call in CPython 3.12.3 · ≈1× faster
fn std::vector< std::string > splitlines(std::string_view s) source#

Split into lines.

Breaks on \n, \r, and \r\n (treated as a single break) with the line terminators removed; a trailing newline does not produce a final empty line, and an empty input yields an empty vector.

Parameters
s

input.

Returns

the lines (newlines removed).

Complexity

O(n).

Allocation

allocates a vector of strings.

Performance34.93 ns/call in cheatah · 89.79 ns/call in CPython 3.12.3 · ≈2.6× faster
fn std::string capwords(std::string_view s) source#

Python string.capwords: split on whitespace, capitalize, re-join with spaces.

Capitalizes each whitespace-delimited word (first letter upper, rest lower) and re-joins with single spaces, so all runs of original whitespace collapse and leading/trailing whitespace is dropped.

Parameters
s

input.

Returns

the result.

Complexity

O(n).

Allocation

allocates a vector of words plus the result.

System testStdlibE2E.String
Performance214 ns/call in cheatah · 500 ns/call in CPython 3.12.3 · ≈2.3× faster
fn std::string ljust(std::string_view s, std::size_t width, std::string_view fill=" ") source#

Left-justify to width.

Pads s on the right with the fill character up to width; if s is already at least width long it is returned unchanged. Only the first character of fill is used (an empty fill defaults to a space).

Parameters
s

input.

width

target.

fill

pad char.

Returns

padded s (or s if already ≥ width).

Complexity

O(n + width).

Allocation

allocates.

Compile-run testStringCompileRun.Ljust
Performance27.50 ns/call in cheatah · 80.63 ns/call in CPython 3.12.3 · ≈3× faster
fn std::string rjust(std::string_view s, std::size_t width, std::string_view fill=" ") source#

Right-justify to width.

Pads s on the left with the fill character up to width; if s is already at least width long it is returned unchanged. Only the first character of fill is used (an empty fill defaults to a space).

Parameters
s

input.

width

target.

fill

pad char.

Returns

padded s.

Complexity

O(n + width).

Allocation

allocates the result plus concatenation temporaries.

Compile-run testStringCompileRun.Rjust
Performance40.88 ns/call in cheatah · 79.92 ns/call in CPython 3.12.3 · ≈2× faster
fn std::string center(std::string_view s, std::size_t width, std::string_view fill=" ") source#

Center within width.

Pads both sides with the fill character; when the padding is odd the extra character goes on the right. Returns s unchanged if it is already at least width long, and only the first character of fill is used (empty → space).

Parameters
s

input.

width

target.

fill

pad char.

Returns

padded s.

Complexity

O(n + width).

Allocation

allocates the result plus concatenation temporaries.

Compile-run testStringCompileRun.Center
Performance58.91 ns/call in cheatah · 84.22 ns/call in CPython 3.12.3 · ≈1.5× faster
fn std::string zfill(std::string_view s, std::size_t width) source#

Zero-fill on the left to width.

Left-pads with '0' to width; if s begins with a '+' or '-' sign the zeros are inserted after the sign. Returns s unchanged if already at least width long.

Parameters
s

input.

width

target.

Returns

'0'-padded s.

Complexity

O(n + width).

Allocation

allocates the result plus concatenation temporaries.

Compile-run testStringCompileRun.Zfill
System testStdlibE2E.String
Performance40.49 ns/call in cheatah · 83.61 ns/call in CPython 3.12.3 · ≈2× faster
fn bool isdigit(std::string_view s) source#

All digits?

True only if s is non-empty and every character is an ASCII decimal digit; the empty string returns false (matching Python).

Parameters
s

input.

Returns

true iff non-empty and all 09.

Complexity

O(n).

Allocation

none.

Compile-run testStringCompileRun.Isdigit
System testStdlibE2E.String
Performance1.35 ns/call in cheatah · 45.60 ns/call in CPython 3.12.3 · ≈34.2× faster
fn bool isalpha(std::string_view s) source#

All letters?

True only if s is non-empty and every character is an ASCII letter (std::isalpha); the empty string returns false.

Parameters
s

input.

Returns

true iff non-empty and all alphabetic.

Complexity

O(n).

Allocation

none.

Compile-run testStringCompileRun.Isalpha
System testStdlibE2E.String
Performance7.62 ns/call in cheatah · 49.95 ns/call in CPython 3.12.3 · ≈6.6× faster
fn bool isalnum(std::string_view s) source#

All alphanumeric?

True only if s is non-empty and every character is an ASCII letter or digit (std::isalnum); the empty string returns false.

Parameters
s

input.

Returns

true iff non-empty and all letters/digits.

Complexity

O(n).

Allocation

none.

Compile-run testStringCompileRun.Isalnum
System testStdlibE2E.String
Performance6.37 ns/call in cheatah · 55.37 ns/call in CPython 3.12.3 · ≈8.9× faster
fn bool isspace(std::string_view s) source#

All whitespace?

True only if s is non-empty and every character is ASCII whitespace (std::isspace: space, tab, newline, CR, form-feed, vertical tab); the empty string returns false.

Parameters
s

input.

Returns

true iff non-empty and all whitespace.

Complexity

O(n).

Allocation

none.

Compile-run testStringCompileRun.Isspace
System testStdlibE2E.String
Performance2.12 ns/call in cheatah · 43.35 ns/call in CPython 3.12.3 · ≈20.8× faster
fn bool isupper(std::string_view s) source#

All uppercase?

True iff s contains at least one ASCII uppercase letter and no lowercase letters; non-letter characters are ignored, so e.g. "ABC123" is uppercase but "123" and the empty string are not.

Parameters
s

input.

Returns

true iff s has ≥ 1 uppercase letter and no lowercase.

Complexity

O(n).

Allocation

none.

Compile-run testStringCompileRun.Isupper
System testStdlibE2E.String
Performance3.67 ns/call in cheatah · 46.73 ns/call in CPython 3.12.3 · ≈12.9× faster
fn bool islower(std::string_view s) source#

All lowercase?

True iff s contains at least one ASCII lowercase letter and no uppercase letters; non-letter characters are ignored, so e.g. "abc123" is lowercase but "123" and the empty string are not.

Parameters
s

input.

Returns

true iff s has ≥ 1 lowercase letter and no uppercase.

Complexity

O(n).

Allocation

none.

Compile-run testStringCompileRun.Islower
System testStdlibE2E.String
Performance3.95 ns/call in cheatah · 43.96 ns/call in CPython 3.12.3 · ≈11.7× faster
fn std::string join(std::string_view sep, const Range &parts) source#

Join parts with sep.

Concatenates each element of parts with sep inserted only between elements (no leading or trailing separator); an empty range yields an empty string.

Parameters
sep

separator.

parts

any range of string-like values.

Returns

the joined string.

Complexity

O(total length).

Allocation

allocates the result.

Compile-run testStringCompileRun.Join
System testStdlibE2E.String
Performance37.03 ns/call in cheatah · 95.24 ns/call in CPython 3.12.3 · ≈2.6× faster

Constants & variables

var std::string_view ascii_lowercase source#

a–z.

var std::string_view ascii_uppercase source#

A–Z.

var std::string_view ascii_letters source#

a–zA–Z.

var std::string_view digits source#

09.

var std::string_view hexdigits source#

hex digits.

var std::string_view octdigits source#

octal digits.

var std::string_view punctuation source#

ASCII punctuation.

var std::string_view whitespace source#

ASCII whitespace.