C++ Extensions (cppx) Documentation Internal Version
Main | Namespaces | Classes | NamespaceMembers | ClassMembers | Files

stdxStrings.h Classes and Functions

Miscellaneous classes and functions for manipulating strings. Includes toString and fromString template functions, specializations for various types, and an efficient string tokenizing function.

Compounds

Functions

General String Functions
toString Conversion Functions
fromString Conversion Functions
String Split Functions

Detailed Documentation

Function Documentation

bool hasPrefix const std::string &    input,
const std::string &    prefix
 

This function returns true if the given input string has the supplied prefix string anchored at the beginning of the input string.

template<class T>
std::string toString const T &    in
 

General template function for converting any type to a string. Requires the insertion operator to be overloaded for the supplied template type.

template<class T>
int stdx::fromString const std::string &    in
 

General template function for converting any type into a string. The type must have overloaded the extraction operator and also have a default constructor. The function will throw a EInvalidStringConversions exception in two cases: (a) if after the extraction the stream is in an invalid state or (b) there is non-white space left in the string after extraction.

Include specializations several specializations. STL strings are handled efficiently, bools will correctly recognize "true", "on", "yes", and "1" for a true boolean value and "false", "off", "no", "0" for a false boolean value, and ints will correctly recognize the 0x prefix for hex numbers as well as the new 0b prefix for binary numbers.

template<class outItr>
void splitString outItr    out,
const std::string &    str,
const std::string &    delimiters = WHITESPACE
 

This function will split a given string into substrings based on the provided delimiters. Any one of the characters in the delimiters string will be counted as a break between tokens. The default delimiters are whitespace. This is faster than the boost::regex_split if you have a const string reference you want to parse. Each token is copied to the output iterator.

std::string removePrefix const std::string &    input,
const std::string &    prefix
 

Removes the given prefix if it exists. If not then the input is returned unmodified.

bool hasSuffix const std::string &    input,
const std::string &    suffix
 

This function returns true if the given input string has the supplied suffix string anchored at the end of the input string.

std::string removeSuffix const std::string &    input,
const std::string &    prefix
 

Removes the given suffix if it exists. If not then the input is returned unmodified.

std::string removeLeading const std::string &    input,
const std::string &    leadingChars = WHITESPACE
 

Removes any leading characters in the input which match the characters given in leadingChars.

std::string removeTrailing const std::string &    input,
const std::string &    trailingChars = WHITESPACE
 

Removes any trailing characters in the input which match the characters given in trailingChars.

std::string removeLeadingTrailing const std::string &    input,
const std::string &    leadingChars = WHITESPACE,
const std::string &    trailingChars = WHITESPACE
 

Removes any leading characters in the input which match the characters in leadingChars, and any trailing characters in the input which match the characters in trailingChars. Useful for removing leading/trailing whitespace or brackets. The default is to remove whitespace.

template<class T>
std::string toFixedWidthString const T &    in,
int    width,
char    fillchar = ' ',
StringAlignment    align = StringAlignment::left
 

std::string intToBinString int    in,
int    numBits,
int    grouping = 0
 

A function to convert an integer into a binary bit string. The binary string is prefixed with "0b". The numBits parameter specifies the number of bits that should be in the output. This enables the user to represent an 8 bit number using a normal int and then just display 8 bits when necessary. If there are significant bits beyond the given numBits they are ignored. The grouping parameter inserts an underscore every grouping bits. Examples follow:

 intToBinString(231,10)   == "0b0011100111"
 intToBinString(231,13,4) == "0b_0_0000_1110_0111"
 intToBinString(231,10,4) == "0b_00_1110_0111"
 intToBinString(231,8,4)  == "0b_1110_0111"

std::string intToHexString int    in,
int    numBits
 

A function to convert an integer into a hexadecimal string. The hex string is prefixed with "0x". The numBist parameter specifies the number of bits that should be in the output. This enables the user to represent an 8 bit number using a normal int and then just display the two hexadecimal digits when necessary. If there are more significant bits beyond the given numBits they are not ignored. If numBits is not divisible by four then the number of displayed digits is rounded up. All digits are lower case. Examples follow:

 intToHexString(4095,16) == "0x0fff"
 intToHexString(4095,17) == "0x00fff"
 intToHexString(4095,32) == "0x00000fff"
 intToHexString(-559038737,16) == "0xdeadbeef"
 intToHexString(-559038737,32) == "0xdeadbeef"

template<class outItr>
void splitString outItr    out,
const std::string &    str,
const std::string &    delimiters,
const std::string &    quote
 

This function is similar to splitString() except that it also allows the user to specify a set of quote characters. A quote character causes the function to ignore delimiters until it sees another quote character. For example, to split a string into whitespace delimited strings except for strings delimited with single quotes we could use the following:

 list<string> lst;
 splitString( back_inserter(lst), "This is 'a test'", " ", "'" );

This code will produce a list with the following three strings: "This", "is", "a test". Notice how "a test" is not split into two strings because it is quoted in the input string.

template<class T, class outItr>
void splitStringAndConvert outItr    out,
const std::string &    str,
const std::string &    delimiters = WHITESPACE
 

This function will split a given string similar to the splitString() function except that the T template type parameter is used to convert each split string into an object of type T using the fromString() function. Here's an example:

 string str = "{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }";
 
 vector<int> intVec;
 splitStringAndConvert<int>( back_inserter(intVec), str, "{}, " );

bool splitStringInTwo const std::string &    input,
std::string *    head,
std::string *    tail,
const std::string &    delimiters = WHITESPACE
 

This function will split an input string into two parts. It will split the input string at the first occurance of one of the characters in the supplied delimiters string. This function is a little simpler to use than splitString when you only want one split point. The function will return true if the input string was successfully split, otherwise it returns false.

Generated on Mon Aug 15 21:43:09 2005 by Doxygen 1.2.13-20020210 written by Dimitri van Heesch © 1997-2002