CUGL 2.0
Cornell University Game Library
Functions
cugl::strtool Namespace Reference

Functions

std::string to_string (Uint8 value)
 
std::string to_string (Sint16 value)
 
std::string to_string (Uint16 value)
 
std::string to_string (Sint32 value)
 
std::string to_string (Uint32 value)
 
std::string to_string (Sint64 value)
 
std::string to_string (Uint64 value)
 
std::string to_string (float value, int precision=-1)
 
std::string to_string (double value, int precision=-1)
 
std::string to_string (Uint8 *array, size_t length, size_t offset=0)
 
std::string to_string (Sint16 *array, size_t length, size_t offset=0)
 
std::string to_string (Uint16 *array, size_t length, size_t offset=0)
 
std::string to_string (Sint32 *array, size_t length, size_t offset=0)
 
std::string to_string (Uint32 *array, size_t length, size_t offset=0)
 
std::string to_string (Sint64 *array, size_t length, size_t offset=0)
 
std::string to_string (Uint64 *array, size_t length, size_t offset=0)
 
std::string to_string (float *array, size_t length, size_t offset=0, int precision=-1)
 
std::string to_string (double *array, size_t length, size_t offset=0, int precision=-1)
 
Uint8 stou8 (const std::string str, std::size_t *pos=0, int base=10)
 
Sint16 stos16 (const std::string str, std::size_t *pos=0, int base=10)
 
Uint16 stou16 (const std::string str, std::size_t *pos=0, int base=10)
 
Sint32 stos32 (const std::string str, std::size_t *pos=0, int base=10)
 
Uint32 stou32 (const std::string str, std::size_t *pos=0, int base=10)
 
Sint64 stos64 (const std::string str, std::size_t *pos=0, int base=10)
 
Uint64 stou64 (const std::string str, std::size_t *pos=0, int base=10)
 
float stof (const std::string str, std::size_t *pos=0)
 
double stod (const std::string str, std::size_t *pos=0)
 
bool isalpha (const std::string str)
 
bool isalphanum (const std::string str)
 
bool isnumeric (const std::string str)
 
bool isnumber (const std::string str)
 
int count (const std::string str, const std::string a)
 
bool starts_with (const std::string str, const std::string a)
 
bool ends_with (const std::string str, const std::string a)
 
bool islower (const std::string str)
 
bool isupper (const std::string str)
 
std::vector< std::string > split (const std::string str, const std::string sep)
 
std::vector< std::string > splitlines (const std::string str)
 
std::string join (const std::string sep, std::initializer_list< std::string > elts)
 
std::string join (const std::string sep, std::vector< std::string > elts)
 
std::string join (const std::string sep, const std::string *elts, size_t size)
 
std::string trim (const std::string str)
 
std::string ltrim (const std::string str)
 
std::string rtrim (const std::string str)
 
template<typename ... Args>
std::string format (const std::string format, Args ... args)
 
std::string tolower (std::string str)
 
std::string toupper (const std::string str)
 
std::string replace (const std::string str, const std::string a, const std::string b)
 
std::string replaceall (const std::string str, const std::string a, const std::string b)
 

Detailed Description

A platform independent module for generating strings.

The functions in this namespace are necessary because Android does not support a lot of the built-in string methods. Therefore, we need alternate definitions that are platform agnostic. Note that these functions have names that are very similar to those in the std namespace, but all live in the cugl::strtool namespace.

Note that this module does not refer to the integral types as short, int, long, etc. Those types are NOT cross-platform. For example, a long is 8 bytes on Unix/OS X, but 4 bytes on some Win32 platforms. Hence we use the SDL naming convention instead.

Function Documentation

◆ count()

int cugl::strtool::count ( const std::string  str,
const std::string  a 
)

Returns the number of times substring a appears in str.

Overlapping substring count. So count("aaa","aa") returns 2.

Parameters
strThe string to count from
aThe substring to count
Returns
the number of times substring a appears in str.

◆ ends_with()

bool cugl::strtool::ends_with ( const std::string  str,
const std::string  a 
)

Returns true if str ends with the substring a.

Parameters
strThe string to query
aThe substring to match
Returns
true if str ends with the substring a.

◆ format()

template<typename ... Args>
std::string cugl::strtool::format ( const std::string  format,
Args ...  args 
)

Returns a new string from the given formatter and arguments

This funciton is similar to the C-function sprintf except that it supports C++ style strings.

Parameters
formatThe formatting string
argsThe argument list
Returns
a new string from the given formatter and arguments

◆ isalpha()

bool cugl::strtool::isalpha ( const std::string  str)

Returns true if the string only contains alphabetic characters.

This function uses the current C++ locale.

Parameters
strThe string to check
Returns
true if the string only contains alphabetic characters.

◆ isalphanum()

bool cugl::strtool::isalphanum ( const std::string  str)

Returns true if the string only contains alphabetic and numeric characters.

This function uses the current C++ locale.

Parameters
strThe string to check
Returns
true if the string only contains alphabetic and numeric characters.

◆ islower()

bool cugl::strtool::islower ( const std::string  str)

Returns true if the string is lower case

This method ignores any non-letter characters and returns true if str is the empty string. So the only way it can be false is if there is an upper case letter in the string.

This function uses the current C++ locale.

Parameters
strThe string to query
Returns
true if the string is lower case

◆ isnumber()

bool cugl::strtool::isnumber ( const std::string  str)

Returns true if the string can safely be converted to a number (double)

This function uses the current C++ locale.

Parameters
strThe string to check
Returns
true if the string can safely be converted to a number (double)

◆ isnumeric()

bool cugl::strtool::isnumeric ( const std::string  str)

Returns true if the string only contains numeric characters.

This function uses the current C++ locale.

Parameters
strThe string to check
Returns
true if the string only contains numeric characters.

◆ isupper()

bool cugl::strtool::isupper ( const std::string  str)

Returns true if the string is upper case

This method ignores any non-letter characters and returns true if str is the empty string. So the only way it can be false is if there is a lower case letter in the string.

This function uses the current C++ locale.

Parameters
strThe string to query
Returns
true if the string is upper case

◆ join() [1/3]

std::string cugl::strtool::join ( const std::string  sep,
const std::string *  elts,
size_t  size 
)

Returns a string that is the concatenation of elts.

The string sep is placed between each concatenated item of elts. If elts is one element or none, then sep is ignored.

Parameters
sepThe join separator
eltsThe strings to join
sizeThe number of items in elts.
Returns
a string that is the concatentation of elts.

◆ join() [2/3]

std::string cugl::strtool::join ( const std::string  sep,
std::initializer_list< std::string >  elts 
)

Returns a string that is the concatenation of elts.

The string sep is placed between each concatenated item of elts. If elts is one element or none, then sep is ignored.

Parameters
sepThe join separator
eltsThe strings to join
Returns
a string that is the concatentation of elts.

◆ join() [3/3]

std::string cugl::strtool::join ( const std::string  sep,
std::vector< std::string >  elts 
)

Returns a string that is the concatenation of elts.

The string sep is placed between each concatenated item of elts. If elts is one element or none, then sep is ignored.

Parameters
sepThe join separator
eltsThe strings to join
Returns
a string that is the concatentation of elts.

◆ ltrim()

std::string cugl::strtool::ltrim ( const std::string  str)

Returns a copy of str with any leading whitespace removed.

This function uses the current C++ locale.

Parameters
strThe string to trim
Returns
a copy of str with any leading whitespace removed.

◆ replace()

std::string cugl::strtool::replace ( const std::string  str,
const std::string  a,
const std::string  b 
)

Returns a copy of str with the first instance of a replaced by b.

If a is not a substring of str, the function returns an unmodified copy of str.

Parameters
strThe string to modify
aThe substring to replace
bThe substring to substitute
Returns
a copy of str with the first instance of a replaced by b.

◆ replaceall()

std::string cugl::strtool::replaceall ( const std::string  str,
const std::string  a,
const std::string  b 
)

Returns a copy of str with all instances of a replaced by b.

If a is not a substring of str, the function returns an unmodified copy of str.

Parameters
strThe string to modify
aThe substring to replace
bThe substring to substitute
Returns
a copy of str with all instances of a replaced by b.

◆ rtrim()

std::string cugl::strtool::rtrim ( const std::string  str)

Returns a copy of str with any trailing whitespace removed.

This function uses the current C++ locale.

Parameters
strThe string to trim
Returns
a copy of str with any trailing whitespace removed.

◆ split()

std::vector<std::string> cugl::strtool::split ( const std::string  str,
const std::string  sep 
)

Returns a list of substrings separate by the given separator

The separator is interpretted exactly; no whitespace is removed around the separator. If the separator is the empty string, this function will return a list of the characters in str.

Parameters
strThe string to split
sepThe splitting delimeter
Returns
a list of substrings separate by the given separator

◆ splitlines()

std::vector<std::string> cugl::strtool::splitlines ( const std::string  str)

Returns a list of substrings separated by the line separator

This function treats both newlines and carriage returns as line separators. Windows-style line separators (CR+NL) do not produce an extra line in the middle.

Parameters
strThe string to split
Returns
a list of substrings separate by the line separator

◆ starts_with()

bool cugl::strtool::starts_with ( const std::string  str,
const std::string  a 
)

Returns true if str starts with the substring a.

Parameters
strThe string to query
aThe substring to match
Returns
true if str starts with the substring a.

◆ stod()

double cugl::strtool::stod ( const std::string  str,
std::size_t *  pos = 0 
)

Returns the double equivalent to the given string

This function discards any whitespace characters (as determined by std::isspace()) until first non-whitespace character is found. Then it takes as many characters as possible to form a valid floating point representation and converts them to a floating point value.

Parameters
strthe string to convert
posaddress of an integer to store the number of characters processed
Returns
the double equivalent to the given string

◆ stof()

float cugl::strtool::stof ( const std::string  str,
std::size_t *  pos = 0 
)

Returns the float equivalent to the given string

This function discards any whitespace characters (as determined by std::isspace()) until first non-whitespace character is found. Then it takes as many characters as possible to form a valid floating point representation and converts them to a floating point value.

Parameters
strthe string to convert
posaddress of an integer to store the number of characters processed
Returns
the float equivalent to the given string

◆ stos16()

Sint16 cugl::strtool::stos16 ( const std::string  str,
std::size_t *  pos = 0,
int  base = 10 
)

Returns the signed 16 bit integer equivalent to the given string

This function discards any whitespace characters (as identified by calling isspace()) until the first non-whitespace character is found, then takes as many characters as possible to form a valid base-n (where n=base) integer number representation and converts them to a long value.

Parameters
strthe string to convert
posaddress of an integer to store the number of characters processed
basethe number base
Returns
the signed 16 bit integer equivalent to the given string

◆ stos32()

Sint32 cugl::strtool::stos32 ( const std::string  str,
std::size_t *  pos = 0,
int  base = 10 
)

Returns the signed 32 bit integer equivalent to the given string

This function discards any whitespace characters (as identified by calling isspace()) until the first non-whitespace character is found, then takes as many characters as possible to form a valid base-n (where n=base) integer number representation and converts them to a long value.

Parameters
strthe string to convert
posaddress of an integer to store the number of characters processed
basethe number base
Returns
the signed 32 bit integer equivalent to the given string

◆ stos64()

Sint64 cugl::strtool::stos64 ( const std::string  str,
std::size_t *  pos = 0,
int  base = 10 
)

Returns the signed 64 bit integer equivalent to the given string

This function discards any whitespace characters (as identified by calling isspace()) until the first non-whitespace character is found, then takes as many characters as possible to form a valid base-n (where n=base) integer number representation and converts them to a long value.

Parameters
strthe string to convert
posaddress of an integer to store the number of characters processed
basethe number base
Returns
the signed 64 bit integer equivalent to the given string

◆ stou16()

Uint16 cugl::strtool::stou16 ( const std::string  str,
std::size_t *  pos = 0,
int  base = 10 
)

Returns the unsigned 16 bit integer equivalent to the given string

This function discards any whitespace characters (as identified by calling isspace()) until the first non-whitespace character is found, then takes as many characters as possible to form a valid base-n (where n=base) integer number representation and converts them to a long value.

Parameters
strthe string to convert
posaddress of an integer to store the number of characters processed
basethe number base
Returns
the unsigned 16 bit integer equivalent to the given string

◆ stou32()

Uint32 cugl::strtool::stou32 ( const std::string  str,
std::size_t *  pos = 0,
int  base = 10 
)

Returns the unsigned 32 bit integer equivalent to the given string

This function discards any whitespace characters (as identified by calling isspace()) until the first non-whitespace character is found, then takes as many characters as possible to form a valid base-n (where n=base) integer number representation and converts them to a long value.

Parameters
strthe string to convert
posaddress of an integer to store the number of characters processed
basethe number base
Returns
the unsigned 32 bit integer equivalent to the given string

◆ stou64()

Uint64 cugl::strtool::stou64 ( const std::string  str,
std::size_t *  pos = 0,
int  base = 10 
)

Returns the unsigned 64 bit integer equivalent to the given string

This function discards any whitespace characters (as identified by calling isspace()) until the first non-whitespace character is found, then takes as many characters as possible to form a valid base-n (where n=base) integer number representation and converts them to a long value.

Parameters
strthe string to convert
posaddress of an integer to store the number of characters processed
basethe number base
Returns
the unsigned 64 bit integer equivalent to the given string

◆ stou8()

Uint8 cugl::strtool::stou8 ( const std::string  str,
std::size_t *  pos = 0,
int  base = 10 
)

Returns the byte equivalent to the given string

This function discards any whitespace characters (as identified by calling isspace()) until the first non-whitespace character is found, then takes as many characters as possible to form a valid base-n (where n=base) integer number representation and converts them to an integer value.

Parameters
strthe string to convert
posaddress of an integer to store the number of characters processed
basethe number base
Returns
the byte equivalent to the given string

◆ to_string() [1/18]

std::string cugl::strtool::to_string ( double *  array,
size_t  length,
size_t  offset = 0,
int  precision = -1 
)

Returns a string equivalent to the given double array

The value is display as a python-style list in brackets.

As with to_string(double), this function allows us to specify a precision (the number of digits to display after the decimal point). If precision is negative, then maximum precision will be used.

Parameters
arraythe array to convert
lengththe array length
offsetthe starting position in the array
precisionthe number of digits to display after the decimal
Returns
a string equivalent to the given double array

◆ to_string() [2/18]

std::string cugl::strtool::to_string ( double  value,
int  precision = -1 
)

Returns a string equivalent to the given double value

This function differs from std::to_string(double) in that it allows us to specify a precision (the number of digits to display after the decimal point). If precision is negative, then maximum precision will be used.

Parameters
valuethe numeric value to convert
precisionthe number of digits to display after the decimal
Returns
a string equivalent to the given double value

◆ to_string() [3/18]

std::string cugl::strtool::to_string ( float *  array,
size_t  length,
size_t  offset = 0,
int  precision = -1 
)

Returns a string equivalent to the given float array

The value is display as a python-style list in brackets.

As with to_string(float), this function allows us to specify a precision (the number of digits to display after the decimal point). If precision is negative, then maximum precision will be used.

Parameters
arraythe array to convert
lengththe array length
offsetthe starting position in the array
precisionthe number of digits to display after the decimal
Returns
a string equivalent to the given float array

◆ to_string() [4/18]

std::string cugl::strtool::to_string ( float  value,
int  precision = -1 
)

Returns a string equivalent to the given float value

This function differs from std::to_string(float) in that it allows us to specify a precision (the number of digits to display after the decimal point). If precision is negative, then maximum precision will be used.

Parameters
valuethe numeric value to convert
precisionthe number of digits to display after the decimal
Returns
a string equivalent to the given float value

◆ to_string() [5/18]

std::string cugl::strtool::to_string ( Sint16 *  array,
size_t  length,
size_t  offset = 0 
)

Returns a string equivalent to the signed 16 bit integer array

The value is display as a python-style list in brackets.

Parameters
arraythe array to convert
lengththe array length
offsetthe starting position in the array
Returns
a string equivalent to the signed 16 bit integer array

◆ to_string() [6/18]

std::string cugl::strtool::to_string ( Sint16  value)

Returns a string equivalent to the given signed 16 bit integer

Parameters
valuethe numeric value to convert
Returns
a string equivalent to the given signed 16 bit integer

◆ to_string() [7/18]

std::string cugl::strtool::to_string ( Sint32 *  array,
size_t  length,
size_t  offset = 0 
)

Returns a string equivalent to the signed 32 bit integer array

The value is display as a python-style list in brackets.

Parameters
arraythe array to convert
lengththe array length
offsetthe starting position in the array
Returns
a string equivalent to the signed 32 bit integer array

◆ to_string() [8/18]

std::string cugl::strtool::to_string ( Sint32  value)

Returns a string equivalent to the given signed 32 bit integer

Parameters
valuethe numeric value to convert
Returns
a string equivalent to the given signed 32 bit integer

◆ to_string() [9/18]

std::string cugl::strtool::to_string ( Sint64 *  array,
size_t  length,
size_t  offset = 0 
)

Returns a string equivalent to the signed 64 bit integer array

The value is display as a python-style list in brackets.

Parameters
arraythe array to convert
lengththe array length
offsetthe starting position in the array
Returns
a string equivalent to the signed 64 bit integer array

◆ to_string() [10/18]

std::string cugl::strtool::to_string ( Sint64  value)

Returns a string equivalent to the given signed 64 bit integer

Parameters
valuethe numeric value to convert
Returns
a string equivalent to the given signed 64 bit integer

◆ to_string() [11/18]

std::string cugl::strtool::to_string ( Uint16 *  array,
size_t  length,
size_t  offset = 0 
)

Returns a string equivalent to the unsigned 16 bit integer array

The value is display as a python-style list in brackets.

Parameters
arraythe array to convert
lengththe array length
offsetthe starting position in the array
Returns
a string equivalent to the unsigned 16 bit integer array

◆ to_string() [12/18]

std::string cugl::strtool::to_string ( Uint16  value)

Returns a string equivalent to the given unsigned 16 bit integer

Parameters
valuethe numeric value to convert
Returns
a string equivalent to the given unsigned 16 bit integer

◆ to_string() [13/18]

std::string cugl::strtool::to_string ( Uint32 *  array,
size_t  length,
size_t  offset = 0 
)

Returns a string equivalent to the unsigned 32 bit integer array

The value is display as a python-style list in brackets.

Parameters
arraythe array to convert
lengththe array length
offsetthe starting position in the array
Returns
a string equivalent to the unsigned 32 bit integer array

◆ to_string() [14/18]

std::string cugl::strtool::to_string ( Uint32  value)

Returns a string equivalent to the given unsigned 32 bit integer

Parameters
valuethe numeric value to convert
Returns
a string equivalent to the given unsigned 32 bit integer

◆ to_string() [15/18]

std::string cugl::strtool::to_string ( Uint64 *  array,
size_t  length,
size_t  offset = 0 
)

Returns a string equivalent to the unsigned 64 bit integer array

The value is display as a python-style list in brackets.

Parameters
arraythe array to convert
lengththe array length
offsetthe starting position in the array
Returns
a string equivalent to the unsigned 64 bit integer array

◆ to_string() [16/18]

std::string cugl::strtool::to_string ( Uint64  value)

Returns a string equivalent to the given unsigned 64 bit integer

Parameters
valuethe numeric value to convert
Returns
a string equivalent to the given unsigned 64 bit integer

◆ to_string() [17/18]

std::string cugl::strtool::to_string ( Uint8 *  array,
size_t  length,
size_t  offset = 0 
)

Returns a string equivalent to the given byte array

The value is display as a python-style list in brackets.

Parameters
arraythe array to convert
lengththe array length
offsetthe starting position in the array
Returns
a string equivalent to the given byte array

◆ to_string() [18/18]

std::string cugl::strtool::to_string ( Uint8  value)

Returns a string equivalent to the given byte

The value is displayed as a number, not a character.

Parameters
valuethe numeric value to convert
Returns
a string equivalent to the given byte

◆ tolower()

std::string cugl::strtool::tolower ( std::string  str)

Returns a lower case copy of str.

This function uses the current C++ locale.

Parameters
strThe string to convert
Returns
a lower case copy of str.

◆ toupper()

std::string cugl::strtool::toupper ( const std::string  str)

Returns an upper case copy of str.

This function uses the current C++ locale.

Parameters
strThe string to convert
Returns
an upper case copy of str.

◆ trim()

std::string cugl::strtool::trim ( const std::string  str)

Returns a copy of str with any leading and trailing whitespace removed.

This function uses the current C++ locale.

Parameters
strThe string to trim
Returns
a copy of str with any leading and trailing whitespace removed.