pystring (C++)

group pystring

This is a set of functions matching the interface and behaviors of python string methods (as of python 2.3) using std::string.

Overlapping functionality ( such as index and slice/substr ) of std::string is included to match python interfaces.

Defines

MAX_32BIT_INT

Functions

std::string capitalize(std::string_view str)

Return a copy of the string with only its first character capitalized.

std::string center(std::string_view str, int width)

Return centered in a string of length width. Padding is done using spaces.

int count(std::string_view str, std::string_view substr, int start = 0, int end = MAX_32BIT_INT)

Return the number of occurrences of substring sub in string S[start:end]. Optional arguments start and end are interpreted as in slice notation.

bool endswith(std::string_view str, std::string_view suffix, int start = 0, int end = MAX_32BIT_INT)

Return True if the string ends with the specified suffix, otherwise return False. With optional start, test beginning at that position. With optional end, stop comparing at that position.

std::string expandtabs(std::string_view str, int tabsize = 8)

Return a copy of the string where all tab characters are expanded using spaces. If tabsize is not given, a tab size of 8 characters is assumed.

int find(std::string_view str, std::string_view sub, int start = 0, int end = MAX_32BIT_INT)

Return the lowest index in the string where substring sub is found, such that sub is contained in the range [start, end). Optional arguments start and end are interpreted as in slice notation. Return -1 if sub is not found.

int index(std::string_view str, std::string_view sub, int start = 0, int end = MAX_32BIT_INT)

Synonym of find right now. Python version throws exceptions. This one currently doesn’t.

bool isalnum(std::string_view str)

Return true if all characters in the string are alphanumeric and there is at least one character, false otherwise.

bool isalpha(std::string_view str)

Return true if all characters in the string are alphabetic and there is at least one character, false otherwise.

bool isdigit(std::string_view str)

Return true if all characters in the string are digits and there is at least one character, false otherwise.

bool islower(std::string_view str)

Return true if all cased characters in the string are lowercase and there is at least one cased character, false otherwise.

bool isspace(std::string_view str)

Return true if there are only whitespace characters in the string and there is at least one character, false otherwise.

bool istitle(std::string_view str)

Return true if the string is a titlecased string and there is at least one character, i.e. uppercase characters may only follow uncased characters and lowercase characters only cased ones. Return false otherwise.

bool isupper(std::string_view str)

Return true if all cased characters in the string are uppercase and there is at least one cased character, false otherwise.

std::string join(std::string_view str, std::initializer_list<std::string_view> seq)

Return a string which is the concatenation of the strings in the sequence seq. The separator between elements is the str argument.

std::string join(std::string_view str, const std::vector<std::string> &seq)
std::string join(std::string_view str, const std::vector<std::string_view> &seq)
std::string ljust(std::string_view str, int width)

Return the string left justified in a string of length width. Padding is done using spaces. The original string is returned if width is less than str.size().

std::string lower(std::string_view str)

Return a copy of the string converted to lowercase.

std::string lstrip(std::string_view str, std::string_view chars = "")

Return a copy of the string with leading characters removed. If chars is omitted or None, whitespace characters are removed. If given and not “”, chars must be a string; the characters in the string will be stripped from the beginning of the string this method is called on (argument “str” ).

std::string_view lstrip_view(std::string_view str, std::string_view chars = "")
std::string mul(std::string_view str, int n)

Return a copy of the string, concatenated N times, together. Corresponds to the __mul__ operator.

void partition(std::string_view str, std::string_view sep, std::vector<std::string> &result)

Split the string around first occurance of sep. Three strings will always placed into result. If sep is found, the strings will be the text before sep, sep itself, and the remaining text. If sep is not found, the original string will be returned with two empty strings.

void partition(std::string_view str, std::string_view sep, std::vector<std::string_view> &result)
std::vector<std::string> partition(std::string_view str, std::string_view sep)
std::vector<std::string_view> partition_view(std::string_view str, std::string_view sep)
std::string replace(std::string_view str, std::string_view oldstr, std::string_view newstr, int count = -1)

Return a copy of the string with all occurrences of substring old replaced by new. If the optional argument count is given, only the first count occurrences are replaced.

int rfind(std::string_view str, std::string_view sub, int start = 0, int end = MAX_32BIT_INT)

Return the highest index in the string where substring sub is found, such that sub is contained within s[start,end]. Optional arguments start and end are interpreted as in slice notation. Return -1 on failure.

int rindex(std::string_view str, std::string_view sub, int start = 0, int end = MAX_32BIT_INT)

Currently a synonym of rfind. The python version raises exceptions. This one currently does not.

std::string rjust(std::string_view str, int width)

Return the string right justified in a string of length width. Padding is done using spaces. The original string is returned if width is less than str.size().

void rpartition(std::string_view str, std::string_view sep, std::vector<std::string> &result)

Split the string around last occurance of sep. Three strings will always placed into result. If sep is found, the strings will be the text before sep, sep itself, and the remaining text. If sep is not found, the original string will be returned with two empty strings.

void rpartition(std::string_view str, std::string_view sep, std::vector<std::string_view> &result)
std::vector<std::string> rpartition(std::string_view str, std::string_view sep)
std::vector<std::string_view> rpartition_view(std::string_view str, std::string_view sep)
std::string rstrip(std::string_view str, std::string_view chars = "")

Return a copy of the string with trailing characters removed. If chars is “”, whitespace characters are removed. If not “”, the characters in the string will be stripped from the end of the string this method is called on.

std::string_view rstrip_view(std::string_view str, std::string_view chars = "")
void split(std::string_view str, std::vector<std::string> &result, std::string_view sep = "", int maxsplit = -1)

Fills the “result” list with the words in the string, using sep as the delimiter string. If maxsplit is > -1, at most maxsplit splits are done. If sep is “”, any whitespace string is a separator.

void split(std::string_view str, std::vector<std::string_view> &result, std::string_view sep = "", int maxsplit = -1)
std::vector<std::string> split(std::string_view str, std::string_view sep = "", int maxsplit = -1)
std::vector<std::string_view> split_view(std::string_view str, std::string_view sep = "", int maxsplit = -1)
void rsplit(std::string_view str, std::vector<std::string> &result, std::string_view sep = "", int maxsplit = -1)

Fills the “result” list with the words in the string, using sep as the delimiter string. Does a number of splits starting at the end of the string, the result still has the split strings in their original order. If maxsplit is > -1, at most maxsplit splits are done. If sep is “”, any whitespace string is a separator.

void rsplit(std::string_view str, std::vector<std::string_view> &result, std::string_view sep = "", int maxsplit = -1)
std::vector<std::string> rsplit(std::string_view str, std::string_view sep = "", int maxsplit = -1)
std::vector<std::string_view> rsplit_view(std::string_view str, std::string_view sep = "", int maxsplit = -1)
void splitlines(std::string_view str, std::vector<std::string> &result, bool keepends = false)

Return a list of the lines in the string, breaking at line boundaries. Line breaks are not included in the resulting list unless keepends is given and true.

void splitlines(std::string_view str, std::vector<std::string_view> &result, bool keepends = false)
std::vector<std::string> splitlines(std::string_view str, bool keepends = false)
std::vector<std::string_view> splitlines_view(std::string_view str, bool keepends = false)
bool startswith(std::string_view str, std::string_view prefix, int start = 0, int end = MAX_32BIT_INT)

Return True if string starts with the prefix, otherwise return False. With optional start, test string beginning at that position. With optional end, stop comparing string at that position.

std::string strip(std::string_view str, std::string_view chars = "")

Return a copy of the string with leading and trailing characters removed. If chars is “”, whitespace characters are removed. If given not “”, the characters in the string will be stripped from the both ends of the string this method is called on.

std::string_view strip_view(std::string_view str, std::string_view chars = "")
std::string swapcase(std::string_view str)

Return a copy of the string with uppercase characters converted to lowercase and vice versa.

std::string title(std::string_view str)

Return a titlecased version of the string: words start with uppercase characters, all remaining cased characters are lowercase.

std::string translate(std::string_view str, std::string_view table, std::string_view deletechars = "")

Return a copy of the string where all characters occurring in the optional argument deletechars are removed, and the remaining characters have been mapped through the given translation table, which must be a string of length 256.

std::string upper(std::string_view str)

Return a copy of the string converted to uppercase.

std::string zfill(std::string_view str, int width)

Return the numeric string left filled with zeros in a string of length width. The original string is returned if width is less than str.size().

std::string slice(std::string_view str, int start = 0, int end = MAX_32BIT_INT)

function matching python’s slice functionality.

std::string_view slice_view(std::string_view str, int start = 0, int end = MAX_32BIT_INT)