rcolyer.net
RC Lib  Version 202403231100
List of all members
RC::RStr Class Reference

A bounds-safe string class which provides an identical interface to std::string plus many convenience functions for string manipulation. More...

#include <RStr.h>

Constants

static const size_t npos = -1
 The largest possible value of size_t.
 
static const RStr Whitespace ()
 Provides all the standard whitespace characters.
 
static const RStr NewLine ()
 Provides the OS-specific newline string.
 

Wrapper methods for std::string

 RStr ()
 Default constructor, initializes to blank string.
 
 RStr (const RStr &other)
 Copy constructor.
 
 RStr (const std::string &str)
 Initializes to str.
 
 RStr (const RStr &s, size_t pos, size_t n=npos)
 Creates a new RStr from a segment of s starting at index pos with length n or until the end. More...
 
 RStr (const char *s, size_t n)
 Creates a new RStr from n characters at s, or until null-termination. More...
 
 RStr (const char *s)
 Creates a new RStr from s until null-termination, or empty string if s is NULL.
 
 RStr (size_t n, char c)
 Initializes to n copies of character c.
 
template<class InputIterator >
 RStr (InputIterator begin, InputIterator end)
 Initializes the string from the values in the iterator range.
 
 RStr (const std::initializer_list< char > characters)
 Initializes the string with the list of characters.
 
 RStr (RStr &&other) noexcept
 Moves other to this RStr.
 
RStroperator= (const RStr &s)
 Copies the contents of s to this RStr.
 
RStroperator= (const char *s)
 Copies the null-terminated characters at s to this RStr, or empties if s is NULL.
 
RStroperator= (char c)
 Sets the RStr to char c.
 
RStroperator= (std::initializer_list< char > characters)
 Sets the contents of the RStr to the initializer list.
 
RStroperator= (RStr &&other)
 Moves the contests of other to this RStr.
 
RStrIter begin ()
 Returns a bounds-safe iterator to the first element. More...
 
RStrIter end ()
 Returns a bounds-safe iterator which points past the last element. More...
 
size_t size () const
 Returns the length of the string.
 
size_t length () const
 Returns the length of the string.
 
size_t max_size () const
 Returns the maximum size of the string.
 
void resize (size_t n, char c)
 Resizes the string, filling with character c if enlarging.
 
void resize (size_t n)
 Resizes the string, filling with null characters if enlarging.
 
size_t capacity () const
 Returns the allocated storage space.
 
void reserve (size_t capsize=0)
 Request a storage capacity increase to capsize.
 
void clear ()
 Make the string empty.
 
bool empty () const
 True of the string is empty.
 
char & operator[] (size_t pos)
 Returns the char at index pos, or throws ErrorMsgBounds if out of bounds.
 
const char & operator[] (size_t pos) const
 Const version of operator[].
 
char & at (size_t pos)
 Identical to operator[].
 
const char & at (size_t pos) const
 Const version of at.
 
RStroperator+= (const RStr &s)
 Appends s to this RStr.
 
RStroperator+= (const char *s)
 Appends s to this RStr if s is non-NULL.
 
RStroperator+= (char c)
 Appends the char c to this RStr.
 
RStrappend (const RStr &s)
 Appends s to this RStr.
 
RStrappend (const RStr &s, size_t pos, size_t n)
 Appends n characters in s starting at pos to this RStr, or fewer characters if s is too short.
 
RStrappend (const char *s, size_t n)
 Appends n characters starting at s to this RStr, or fewer if null-termination is reached in s. More...
 
RStrappend (const char *s)
 Append s to this RStr if s is non-NULL.
 
RStrappend (size_t n, char c)
 Appends n copies of char c.
 
template<class InputIterator >
RStrappend (InputIterator first, InputIterator last)
 Appends from first to one before last, or until a null character.
 
RStrappend (std::initializer_list< char > characters)
 Append the initializer list of characters.
 
void push_back (char c)
 Appends character c.
 
RStrassign (const RStr &s)
 Copies s to this RStr.
 
RStrassign (const RStr &s, size_t pos, size_t n)
 Sets this RStr to n characters in s at index pos, or until the end.
 
RStrassign (const char *s, size_t n)
 Sets this RStr to n characters starting at s or until null-termination. More...
 
RStrassign (const char *s)
 Sets this RStr to the characters at s until null-termination. More...
 
RStrassign (size_t n, char c)
 Sets this RStr to n copies of character c.
 
template<class InputIterator >
RStrassign (InputIterator first, InputIterator last)
 Sets the RStr to characters from first to one before last, or until a null character.
 
RStrinsert (size_t pos_this, const RStr &s)
 Inserts s starting at index pos_this of this RStr, or throws ErrorMsgBounds.
 
RStrinsert (size_t pos_this, const RStr &s, size_t pos_s, size_t n)
 Inserts at index pos_this, n characters from s starting at pos_s. More...
 
RStrinsert (size_t pos_this, const char *s, size_t n)
 Inserts at index pos_this, n characters from s or until NULL-termination. More...
 
RStrinsert (size_t pos_this, const char *s)
 Inserts s at index pos_this until null-termination, or nothing if s is NULL.
 
RStrinsert (size_t pos_this, size_t n, char c)
 Inserts n copies of char c at pos_this.
 
RStrIter insert (RStrIter p, char c)
 Inserts char c at iterator position p.
 
void insert (RStrIter p, size_t n, char c)
 Inserts n copies of char c at iterator position p.
 
template<class InputIterator >
void insert (RStrIter p, InputIterator first, InputIterator last)
 At iterator position p, inserts characters from first through one before last or until null-termination.
 
RStrinsert (const RStrIter p, std::initializer_list< char > characters)
 At iterator position p, inserts list of characters.
 
RStrerase (size_t pos=0, size_t n=npos)
 Erases n characters at index pos, or until the end.
 
RStrIter erase (const RStrIter pos)
 Erases one character at index pos.
 
RStrIter erase (const RStrIter first, const RStrIter last)
 Erases from index first through one before index last.
 
RStrreplace (size_t pos_this, size_t n_this, const RStr &s)
 Replace n_this characters at pos_this with all of s.
 
RStrreplace (RStrIter first, RStrIter last, const RStr &s)
 Replace from first to one before last with all of s.
 
RStrreplace (size_t pos_this, size_t n_this, const RStr &s, size_t pos_s, size_t n_s)
 Replace n_this characters at pos_this with n_s characters from s starting at pos_s.
 
RStrreplace (size_t pos_this, size_t n_this, const char *s, size_t n_s)
 Replace n_this characters at pos_this with n_s characters from s, or until s's null-termination.
 
RStrreplace (RStrIter first, RStrIter last, const char *s, size_t n_s)
 Replace from first through one before last with n_s characters from s, or until s's null-termination.
 
RStrreplace (size_t pos_this, size_t n_this, const char *s)
 Replace n_this characters at pos_this with all of s, or nothing if s is NULL.
 
RStrreplace (RStrIter first, RStrIter last, const char *s)
 Replace from first to one before last with all of s, or nothing if s is NULL.
 
RStrreplace (size_t pos_this, size_t n_this, size_t n_c, char c)
 Replace n_this characters at pos_this with n_c copies of c.
 
RStrreplace (RStrIter first, RStrIter last, size_t n_c, char c)
 Replace from first through one before last with n_c copies of c.
 
template<class InputIterator >
RStrreplace (RStrIter first, RStrIter last, InputIterator in_first, InputIterator in_last)
 Replace from first through one before lsat with in1 through one before in2, or until null-termination.
 
void swap (RStr &s)
 Swap contents with s.
 
const char * c_str () const
 Provides a null-terminated C style string corresponding to RStr.
 
const char * data () const
 Identical to c_str.
 
std::allocator< char > get_allocator () const
 Get the allocator used for string storage.
 
size_t copy (char *s, size_t n, size_t pos=0) const
 Copies n characters to s, starting from pos_this. More...
 
size_t find (const RStr &s, size_t pos=0) const
 Returns the index at pos or later which matches string s, or npos if no match.
 
size_t find (const char *s, size_t pos, size_t n) const
 Returns the index at pos or later which matches n characters of s, or npos if no match.
 
size_t find (const char *s, size_t pos=0) const
 Returns the index at pos or later which matches s until null-termination, or npos if no match.
 
size_t find (char c, size_t pos=0) const
 Returns the index at pos or later which matches character c.
 
size_t rfind (const RStr &s, size_t pos=npos) const
 Returns the index at pos or before which starts a match of string s, or npos if no match.
 
size_t rfind (const char *s, size_t pos, size_t n) const
 Returns the index at pos or before which matches n characters of s, or npos if no match.
 
size_t rfind (const char *s, size_t pos=npos) const
 Returns the index at pos or before which matches s until null-termination, or npos if no match.
 
size_t rfind (char c, size_t pos=npos) const
 Returns the index at pos or before which matches character c.
 
size_t find_first_of (const RStr &s, size_t pos=0) const
 Returns the first index at pos or later which matches a character in s, or npos if none match.
 
size_t find_first_of (const char *s, size_t pos, size_t n) const
 Returns the first index at pos or later which matches one of the first n characters in s, or npos if none match.
 
size_t find_first_of (const char *s, size_t pos=0) const
 Returns the first index at pos or later which matches a character in s, or npos if none match.
 
size_t find_first_of (char c, size_t pos=0) const
 Returns the index at pos or later which matches character c.
 
size_t find_last_of (const RStr &s, size_t pos=npos) const
 Returns the highest index at pos or before which matches a character in s, or npos if none match.
 
size_t find_last_of (const char *s, size_t pos, size_t n) const
 Returns the highest index at pos or before which matches the first n characters in s, or npos if none match.
 
size_t find_last_of (const char *s, size_t pos=npos) const
 Returns the highest index at pos or before which matches a character in s, or npos if none match.
 
size_t find_last_of (char c, size_t pos=npos) const
 Returns the highest index at pos or before which matches character c, or npos if none match.
 
size_t find_first_not_of (const RStr &s, size_t pos=0) const
 Returns the first index at pos or later which does not match a character in s, or npos if all match.
 
size_t find_first_not_of (const char *s, size_t pos, size_t n) const
 Returns the first index at pos or later which does not match the first n characters in s, or npos if all match.
 
size_t find_first_not_of (const char *s, size_t pos=0) const
 Returns the first index at pos or later which does not match a character in s, or npos if all match.
 
size_t find_first_not_of (char c, size_t pos=0) const
 Returns the first index at pos or later which does not match character c, or npos if all match.
 
size_t find_last_not_of (const RStr &s, size_t pos=npos) const
 Returns the highest index at pos or before which does not match a character in s, or npos if all match.
 
size_t find_last_not_of (const char *s, size_t pos, size_t n) const
 Returns the highest index at pos or before which does not match the first n characters in s, or npos if all match.
 
size_t find_last_not_of (const char *s, size_t pos=npos) const
 Returns the highest index at pos or before which does not match a character in s, or npos if all match.
 
size_t find_last_not_of (char c, size_t pos=npos) const
 Returns the highest index at pos or before which does not match character c, or npos if all match.
 
RStr substr (size_t pos=0, size_t n=npos) const
 Creates a substring from n characters starting at position pos, or until the end of the string.
 
int compare (const RStr &s) const
 Returns negative, 0, or positive if this string is lesser, equal, or greater than s.
 
int compare (const char *s) const
 Returns negative, 0, or positive if this string is lesser, equal, or greater than s.
 
int compare (size_t pos_this, size_t n_this, const RStr &s) const
 Returns negative, 0, or positive if the n_this characters at pos_this are lesser, equal, or greater than s.
 
int compare (size_t pos_this, size_t n_this, const char *s) const
 Returns negative, 0, or positive if the n_this characters at pos_this are lesser, equal, or greater than s.
 
int compare (size_t pos_this, size_t n_this, const RStr &s, size_t pos_s, size_t n_s) const
 Returns negative, 0, or positive if the n_this characters at pos_this are lesser, equal, or greater than the n_s characters at pos_s in s.
 
int compare (size_t pos_this, size_t n_this, const char *s, size_t n_s) const
 Returns negative, 0, or positive if the n_this characters at pos_this are lesser, equal, or greater than the n_s characters in s.
 

Convert to and from other supported types

 RStr (char x)
 The default constructor for a char, treating it as a character. More...
 
 RStr (char x, RStr_IntStyle style, i32 precision=-1)
 Formats x as a string in the given style, and with at least precision 0-padded digits. More...
 
 RStr (u8 x, RStr_IntStyle style=DEC, i32 precision=-1)
 Formats x as a string in the given style, and with at least precision 0-padded digits.
 
 RStr (i8 x, RStr_IntStyle style=DEC, i32 precision=-1)
 Formats x as a string in the given style, and with at least precision 0-padded digits.
 
 RStr (u16 x, RStr_IntStyle style=DEC, i32 precision=-1)
 Formats x as a string in the given style, and with at least precision 0-padded digits.
 
 RStr (i16 x, RStr_IntStyle style=DEC, i32 precision=-1)
 Formats x as a string in the given style, and with at least precision 0-padded digits.
 
 RStr (u32 x, RStr_IntStyle style=DEC, i32 precision=-1)
 Formats x as a string in the given style, and with at least precision 0-padded digits.
 
 RStr (i32 x, RStr_IntStyle style=DEC, i32 precision=-1)
 Formats x as a string in the given style, and with at least precision 0-padded digits.
 
 RStr (u64 x, RStr_IntStyle style=DEC, i32 precision=-1)
 Formats x as a string in the given style, and with at least precision 0-padded digits.
 
 RStr (i64 x, RStr_IntStyle style=DEC, i32 precision=-1)
 Formats x as a string in the given style, and with at least precision 0-padded digits.
 
 RStr (f32 x, RStr_FloatStyle style=AUTO, u32 precision=std::numeric_limits< f32 >::digits10)
 Formats x as a string in the given style, and with it rounded to precision digits. More...
 
 RStr (f64 x, RStr_FloatStyle style=AUTO, u32 precision=std::numeric_limits< f64 >::digits10)
 Formats x as a string in the given style, and with it rounded to precision digits. More...
 
 RStr (f80 x, RStr_FloatStyle style=AUTO, u32 precision=std::numeric_limits< f80 >::digits10)
 Formats x as a string in the given style, and with it rounded to precision digits. More...
 
 RStr (bool b)
 "true" if b is true, or "false" if b is false.
 
 RStr (bool pad0s, void *ptr, bool use0x=true)
 Constructor for displaying a pointer, with 0's prepended if pad0s is true, and 0x prepended if use0x is true.
 
template<class T >
 RStr (const Data1D< T > &arr)
 Initialize the string with the bytes stored in Data1D treated as raw character data.
 
 RStr (const std::wstring &wstr)
 Convert a std::wstring to RStr, discarding the high bits.
 
 RStr (const QString &qstr)
 Convert a QString to RStr, discarding the high bits.
 
f32 Get_f32 () const
 Convert the beginning characters of this string to a float. More...
 
f64 Get_f64 () const
 Convert the beginning characters of this string to a float.
 
f80 Get_f80 () const
 Convert the beginning characters of this string to a float.
 
u32 Get_u32 (int base=0) const
 Convert the beginning characters of this string to this integer type. More...
 
u64 Get_u64 (int base=0) const
 Convert the beginning characters of this string to this integer type.
 
i32 Get_i32 (int base=0) const
 Convert the beginning characters of this string to this integer type.
 
i64 Get_i64 (int base=0) const
 Convert the beginning characters of this string to this integer type.
 
u32 Get_hex32 () const
 Convert the beginning characters of this string as a hexadecimal to this integer type.
 
u64 Get_hex64 () const
 Convert the beginning characters of this string as a hexadecimal to this integer type.
 
bool Get_bool () const
 Returns true if case-insensitive "true", "T", or non-zero.
 
void Get (f32 &x) const
 Overloaded function to extract type f32 from this class. More...
 
void Get (f64 &x) const
 Overloaded function to extract type f64 from this class. More...
 
void Get (f80 &x) const
 Overloaded function to extract type f80 from this class. More...
 
void Get (u32 &x) const
 Overloaded function to extract type u32 from this class. More...
 
void Get (u64 &x) const
 Overloaded function to extract type u64 from this class. More...
 
void Get (i32 &x) const
 Overloaded function to extract type i32 from this class. More...
 
void Get (i64 &x) const
 Overloaded function to extract type i64 from this class. More...
 
void Get (bool &x) const
 Overloaded function to extract type bool from this class. More...
 
bool Is_f32 (bool strict=false) const
 True if the start of the string can be converted to an f32. More...
 
bool Is_f64 (bool strict=false) const
 True if the start of the string can be converted to an f64. More...
 
bool Is_f80 (bool strict=false) const
 True if the start of the string can be converted to an f80. More...
 
bool Is_u32 (int base=0, bool strict=false) const
 True if the start of the string can be converted to an u32. More...
 
bool Is_u64 (int base=0, bool strict=false) const
 True if the start of the can be converted to an u64. More...
 
bool Is_i32 (int base=0, bool strict=false) const
 True if the start of the string can be converted to an i32. More...
 
bool Is_i64 (int base=0, bool strict=false) const
 True if the start of the string can be converted to an i64. More...
 
bool Is_hex32 (bool strict=false) const
 True if the start of the string can be converted as hexadecimal to a u32. More...
 
bool Is_hex64 (bool strict=false) const
 True if the start of the string can be converted as hexadecimal to a u64. More...
 
bool Is_bool () const
 True if the string is a "0", "1", or case insensitive "true", "false", "T", or "F".
 
QString ToQString () const
 Convert the RStr to a QString.
 
std::wstring wstring ()
 Convert the RStr to a std::wstring.
 
const char * ToLPCSTR () const
 For Win32, provides an LPCSTR to the string.
 
HoldRelated< std::wstring, const wchar_t * > ToLPCWSTR ()
 For Win32, provides an LPCWSTR to the string. More...
 
const char * ToLPCTSTR ()
 For Win32, provides an LPCTSTR to the string. More...
 
std::string & Raw ()
 Provides raw access to the std::string this RStr wraps.
 
const std::string & Raw () const
 Provides const raw access to the std::string this RStr wraps.
 
Data1D< char > ToData () const
 Returns a Data1D<char> corresponding to the character data in the string.
 

Bounds-checking

bool Check (size_t pos) const
 True if the index pos is in bounds.
 
bool CheckPlus (size_t pos) const
 True if the index pos is in bounds, permitting the null.
 
void Assert (size_t pos) const
 Throws ErrorMsgBounds if the index pos is out of bounds.
 
void AssertPlus (size_t pos) const
 Throws ErrorMsgBounds if the index pos is out of bounds, permitting the null.
 
void Assert (const char *ptr) const
 Throws ErrorMsgNull if ptr is null.
 

Text alignment and arrangement functions

RStrPadLeft (const size_t pad_to, const char pad_with=' ')
 If the length is less than pad_to, add pad_width to the left until it reaches that size.
 
RStrPadRight (const size_t pad_to, const char pad_with=' ')
 If the length is less than pad_to, add pad_width to the right until it reaches that size.
 
RStrPadCenter (const size_t pad_to, const char pad_with=' ')
 If the length is less than pad_to, add pad_width evenly to the right and left until it reaches that size.
 
Data1D< RStrWrap (const size_t width)
 Wraps each line in this RStr to be no longer than width.
 
Data1D< RStrWordWrap (const size_t width)
 Wraps each line in this RStr to be no longer than width, but tries to keep whole words together.
 

Regular expression support

These use C++11 regex by default, but can fall back on boost regex if C++11 is missing and RC_HAVE_BOOST is defined in RCconfig.h.

bool Match (const RC_REGEX_NS::regex &reg)
 True if the regular expression reg matches this string.
 
bool Match (const RStr &regstr)
 True if the regular expression regstr matches this string.
 
bool Match (const RC_REGEX_NS::regex &reg, Data1D< RStr > &matches)
 True if the regular expression reg matches this string, and returns the matches in matches.
 
bool Match (const RStr &regstr, Data1D< RStr > &matches)
 True if the regular expression regstr matches this string, and returns the matches in matches.
 
void Subst (const RC_REGEX_NS::regex &reg, const RStr &subst)
 Substitutes regular expression reg with subst in this string.
 
void Subst (const RStr &regstr, const RStr &subst)
 Substitutes regular expression regstr with subst in this string.
 

Text manipulation routines

bool Contains (const RStr &s, size_t pos=0) const
 Returns true if pos or later which matches string s.
 
size_t After (const RStr &s, size_t pos=0) const
 Returns the index after the first instance of string s at pos or later, or npos if no match.
 
RStrChomp (const RStr &chomp_chars="\r\n")
 Remove all trailing newline characters, or provided char set.
 
RStrTrim (const RStr &trim_chars=" \t\r\n")
 Remove all leading and trailing whitespace, or provided char set.
 
RStrTruncate (const size_t max_size)
 Make sure the string is no longer than max_size.
 
RStrToLower ()
 Make this RStr lowercase.
 
RStrToUpper ()
 Make this RStr uppercase.
 
Data1D< RStrSplit (char c) const
 Return an array of strings split at each instance of c.
 
Data1D< RStrSplitFirst (const RStr &dividers) const
 Return an array of 2 strings split at the first character from dividers.
 
Data1D< RStrSplitLast (const RStr &dividers) const
 Return an array of 2 strings split at the last character from dividers.
 
Data1D< RStrSplitAny (const RStr &dividers) const
 Return an array of strings split by any characters in dividers. More...
 
Data1D< RStrSplitWords () const
 Return an array of one or more whitespace separated words.
 
template<class T >
static RStr Join (const Data1D< T > &str_arr, const RStr &spacer="", RStr(*func)(const T &)=nullptr)
 Takes a Data1D<T> array and joins them as one string with spacer between each element while applying the function to each element. More...
 

Metrics

size_t Distance (const RStr &other) const
 Compute the Levenshtein distance to other.
 
size_t Length8 () const
 Determine the length of the contents treated as a UTF-8 string. More...
 

Interacting with standard formats

Data1D< RStrSplitCSV (char divider=',') const
 Return comma-separated values with whitespace trimmed.
 
RStr ISOtoUTF8 () const
 Treat this string as ISO-8859-1 and return a UTF8 string. More...
 
Data1D< u32UTF8toUTF32 () const
 Treat the contents as a UTF-8 string, and return corresponding UTF-32 data. More...
 
template<class T >
static RStr MakeCSV (const Data1D< T > &arr, const RStr &divider=", ")
 Makes a comma-separated values string out of a Data1D<T> arr. More...
 
template<class T >
static RStr MakeCSV (const Data2D< T > &arr, const RStr &divider1=", ", const RStr &divider2="\n")
 Makes a comma-separated values string out of a Data2D<T> arr. More...
 
template<class T >
static RStr MakeCSV (const Data3D< T > &arr, const RStr &divider1=", ", const RStr &divider2="\n", const RStr &divider3="\n")
 Makes a comma-separated values string out of a Data3D<T> arr. More...
 
static RStr ToBase64 (const Data1D< u8 > &input, size_t wrap=0)
 Encodes input to standard base64 in compliance with RFC 4648.
 
static RStr ToBase64URL (const Data1D< u8 > &input, size_t wrap=0)
 Encodes input to base64url in compliance with RFC 4648, safe for url and filename use.
 
static Data1D< u8FromBase64 (const RStr &input)
 Decodes input from base64 in compliance with RFC 4648, standard or URL encoding.
 
static Data1D< u8FromHexStr (const RStr &hexstr)
 Converts hexstr to a byte array, like "a4c208" -> { 0xa4, 0xc2, 0x08 }.
 
template<class T >
static RStr ToHexStr (const Data1D< T > &rawdata)
 Converts rawdata to a hex string, like { 0xa4, 0xc2, 0x08 } -> "a4c208".
 
static Data1D< RStrArgs (int argc=0, char *argv[]=NULL)
 Converts standard arguments to "int main" into a Data1D<RStr>. More...
 
static RStr Errno (int err_num)
 Returns a thread-safe error string corresponding to err_num.
 
static RStr Errno ()
 Returns a thread-safe error string corresponding to errno.
 

Stream interfacing

bool GetLine (std::istream &in=std::cin, char delim='\n')
 Set the string equal to the next line from istream in, up to character delim. More...
 
std::istream & operator>> (std::istream &in, RStr &rstr)
 Input text from a std::istream (e.g., std::cin) into an RStr.
 
std::ostream & operator<< (std::ostream &out, const RStr &rstr)
 Output text from an RStr to a std::ostream (e.g., std::cout).
 

Detailed Description

A bounds-safe string class which provides an identical interface to std::string plus many convenience functions for string manipulation.

This class checks boundaries on access and sanitizes inputs whenever possible, throwing ErrorMsgBounds or ErrorMsgNull when a usage problem arises. Functions are provided for converting to and from numerical types with formatting control, as well as high-level functions for splitting, joining, regular expressions, word-wrapping and alignment, and working with base64, UTF-8, and CSV.

Usage example:

u64 val = 0x12ab34cd5;
// 5011360981 in hex is 0x000000012ab34cd5, or 12ab34cd5
cout << val << " in hex is " << RStr(val, HEX0x)
<< ", or " << RStr(val, HEX, 1) << endl;
// Its reciprocal is 1.9955e-10
RStr message = "Its reciprocal is " + RStr(1.0/val, SCI, 5) + "\n";
cout << message;
// Pi dollars would be $3.14
cout << "Pi dollars would be $" << RStr(3.14159, FIXED, 2) << endl;
// foo, bar, baz
cout << RStr::Join(RStr("foo:bar:baz").Split(':'), ", ") << endl;
uint64_t u64
64-bit unsigned integer.
Definition: Types.h:31
Data1D< RStr > Split(char c) const
Return an array of strings split at each instance of c.
Definition: RStr.h:1640
RStr()
Default constructor, initializes to blank string.
Definition: RStr.h:143
static RStr Join(const Data1D< T > &str_arr, const RStr &spacer="", RStr(*func)(const T &)=nullptr)
Takes a Data1D<T> array and joins them as one string with spacer between each element while applying ...
Definition: RStr.h:1742

Constructor & Destructor Documentation

◆ RStr() [1/7]

RC::RStr::RStr ( const RStr s,
size_t  pos,
size_t  n = npos 
)
inline

Creates a new RStr from a segment of s starting at index pos with length n or until the end.

Throws RC::ErrorMsgBounds if pos is out of bounds.

◆ RStr() [2/7]

RC::RStr::RStr ( const char *  s,
size_t  n 
)
inline

Creates a new RStr from n characters at s, or until null-termination.

Creates an empty string if s is NULL.

◆ RStr() [3/7]

RC::RStr::RStr ( char  x)
inline

The default constructor for a char, treating it as a character.

To override, cast to another type or add an RStr_IntStyle parameter.

◆ RStr() [4/7]

RC::RStr::RStr ( char  x,
RStr_IntStyle  style,
i32  precision = -1 
)
inline

Formats x as a string in the given style, and with at least precision 0-padded digits.

Note, the default style for a char is as a character.

◆ RStr() [5/7]

RC::RStr::RStr ( f32  x,
RStr_FloatStyle  style = AUTO,
u32  precision = std::numeric_limits< f32 >::digits10 
)
inline

Formats x as a string in the given style, and with it rounded to precision digits.

For SCI precision is the significant digits to show, for AUTO it is the most signicant digits to show before removing trailing zeroes, and for FIXED, precision is digits after the decimal. AUTO has no exponent for numbers in the range (1e-5, 10^Precision).

◆ RStr() [6/7]

RC::RStr::RStr ( f64  x,
RStr_FloatStyle  style = AUTO,
u32  precision = std::numeric_limits< f64 >::digits10 
)
inline

Formats x as a string in the given style, and with it rounded to precision digits.

For SCI precision is the significant digits to show, for AUTO it is the most signicant digits to show before removing trailing zeroes, and for FIXED, precision is digits after the decimal. AUTO has no exponent for numbers in the range (1e-5, 10^Precision).

◆ RStr() [7/7]

RC::RStr::RStr ( f80  x,
RStr_FloatStyle  style = AUTO,
u32  precision = std::numeric_limits< f80 >::digits10 
)
inline

Formats x as a string in the given style, and with it rounded to precision digits.

For SCI precision is the significant digits to show, for AUTO it is the most signicant digits to show before removing trailing zeroes, and for FIXED, precision is digits after the decimal. AUTO has no exponent for numbers in the range (1e-5, 10^Precision).

Member Function Documentation

◆ append()

RStr& RC::RStr::append ( const char *  s,
size_t  n 
)
inline

Appends n characters starting at s to this RStr, or fewer if null-termination is reached in s.

Appends nothing if s is NULL.

◆ Args()

static Data1D<RStr> RC::RStr::Args ( int  argc = 0,
char *  argv[] = NULL 
)
inlinestatic

Converts standard arguments to "int main" into a Data1D<RStr>.

Index 0 contains the program name, corresponding to argv[0], and index 1 on up are the parameters. Thus size()-1 of the returned value is the number of command-line parameters received. If called with no arguments, the previous arglist is returned.

◆ assign() [1/2]

RStr& RC::RStr::assign ( const char *  s)
inline

Sets this RStr to the characters at s until null-termination.

The string is cleared if s is NULL.

◆ assign() [2/2]

RStr& RC::RStr::assign ( const char *  s,
size_t  n 
)
inline

Sets this RStr to n characters starting at s or until null-termination.

The string is cleared if s is NULL.

◆ begin()

RStrIter RC::RStr::begin ( )
inline

Returns a bounds-safe iterator to the first element.

The iterator throws ErrorMsgNull if it is accessed after this RStr is destructed. The element pointed to by this respects the offset with SetOffset.

◆ copy()

size_t RC::RStr::copy ( char *  s,
size_t  n,
size_t  pos = 0 
) const
inline

Copies n characters to s, starting from pos_this.

Warning: If n is greater than the length of s, this routine can write past the end of s. Consider instead obtaining a managed Data1D<char> via ToData(), using Data1D::CopyAt, and accessing the char* with Data1D::Raw for interfacing with C routines.

◆ end()

RStrIter RC::RStr::end ( )
inline

Returns a bounds-safe iterator which points past the last element.

Accessing this iterator before decrementing it will trigger an ErrorMsgBounds. Accessing a decremented iterator after this RStr is destructed will throw ErrorMsgNull.

◆ Get() [1/8]

void RC::RStr::Get ( bool &  x) const
inline

Overloaded function to extract type bool from this class.

Parameters
xThe reference to which the value will be assigned.

◆ Get() [2/8]

void RC::RStr::Get ( f32 x) const
inline

Overloaded function to extract type f32 from this class.

Parameters
xThe reference to which the value will be assigned.

◆ Get() [3/8]

void RC::RStr::Get ( f64 x) const
inline

Overloaded function to extract type f64 from this class.

Parameters
xThe reference to which the value will be assigned.

◆ Get() [4/8]

void RC::RStr::Get ( f80 x) const
inline

Overloaded function to extract type f80 from this class.

Parameters
xThe reference to which the value will be assigned.

◆ Get() [5/8]

void RC::RStr::Get ( i32 x) const
inline

Overloaded function to extract type i32 from this class.

Parameters
xThe reference to which the value will be assigned.

◆ Get() [6/8]

void RC::RStr::Get ( i64 x) const
inline

Overloaded function to extract type i64 from this class.

Parameters
xThe reference to which the value will be assigned.

◆ Get() [7/8]

void RC::RStr::Get ( u32 x) const
inline

Overloaded function to extract type u32 from this class.

Parameters
xThe reference to which the value will be assigned.

◆ Get() [8/8]

void RC::RStr::Get ( u64 x) const
inline

Overloaded function to extract type u64 from this class.

Parameters
xThe reference to which the value will be assigned.

◆ Get_f32()

f32 RC::RStr::Get_f32 ( ) const
inline

Convert the beginning characters of this string to a float.

Note, these detect hexadecimal float numbers formatted like like 0xF.8 = 15.5, but they do not process octal.

◆ Get_u32()

u32 RC::RStr::Get_u32 ( int  base = 0) const
inline

Convert the beginning characters of this string to this integer type.

Note, the default parameter for base autodetects according to decimal, unless 0-leading octal as 013 = 11, or 0x-leading hexadecimal as 0xF = 15.

◆ GetLine()

bool RC::RStr::GetLine ( std::istream &  in = std::cin,
char  delim = '\n' 
)
inline

Set the string equal to the next line from istream in, up to character delim.

Usage example: RStr line; while(line.GetLine()) { cout << line << endl; } Note: Due to buffering, it can be more efficient to use: FileRead fr(stdin); while(fr.Get(line)) { ... } But GetLine is the better choice to avoid reading ahead, or for interactive input.

Returns
True if input was received.

◆ insert() [1/2]

RStr& RC::RStr::insert ( size_t  pos_this,
const char *  s,
size_t  n 
)
inline

Inserts at index pos_this, n characters from s or until NULL-termination.

If s is NULL, nothing is inserted.

◆ insert() [2/2]

RStr& RC::RStr::insert ( size_t  pos_this,
const RStr s,
size_t  pos_s,
size_t  n 
)
inline

Inserts at index pos_this, n characters from s starting at pos_s.

Throws ErrorMsgBounds if pos_this or pos_s are out of their respective bounds. Inserts fewer characters if n is greater than the remaining characters.

◆ Is_f32()

bool RC::RStr::Is_f32 ( bool  strict = false) const
inline

True if the start of the string can be converted to an f32.

Parameters
strictReturn true only if the full string converts.

◆ Is_f64()

bool RC::RStr::Is_f64 ( bool  strict = false) const
inline

True if the start of the string can be converted to an f64.

Parameters
strictReturn true only if the full string converts.

◆ Is_f80()

bool RC::RStr::Is_f80 ( bool  strict = false) const
inline

True if the start of the string can be converted to an f80.

Parameters
strictReturn true only if the full string converts.

◆ Is_hex32()

bool RC::RStr::Is_hex32 ( bool  strict = false) const
inline

True if the start of the string can be converted as hexadecimal to a u32.

Parameters
strictReturn true only if the full string converts.

◆ Is_hex64()

bool RC::RStr::Is_hex64 ( bool  strict = false) const
inline

True if the start of the string can be converted as hexadecimal to a u64.

Parameters
strictReturn true only if the full string converts.

◆ Is_i32()

bool RC::RStr::Is_i32 ( int  base = 0,
bool  strict = false 
) const
inline

True if the start of the string can be converted to an i32.

Parameters
baseThe conversion base to test (0 is automatic).
strictReturn true only if the full string converts.

◆ Is_i64()

bool RC::RStr::Is_i64 ( int  base = 0,
bool  strict = false 
) const
inline

True if the start of the string can be converted to an i64.

Parameters
baseThe conversion base to test (0 is automatic).
strictReturn true only if the full string converts.

◆ Is_u32()

bool RC::RStr::Is_u32 ( int  base = 0,
bool  strict = false 
) const
inline

True if the start of the string can be converted to an u32.

Parameters
baseThe conversion base to test (0 is automatic).
strictReturn true only if the full string converts.

◆ Is_u64()

bool RC::RStr::Is_u64 ( int  base = 0,
bool  strict = false 
) const
inline

True if the start of the can be converted to an u64.

Parameters
baseThe conversion base to test (0 is automatic).
strictReturn true only if the full string converts.

◆ ISOtoUTF8()

RStr RC::RStr::ISOtoUTF8 ( ) const
inline

Treat this string as ISO-8859-1 and return a UTF8 string.

This supports the windows-1252 extension as required by HTML5.

◆ Join()

template<class T >
static RStr RC::RStr::Join ( const Data1D< T > &  str_arr,
const RStr spacer = "",
RStr(*)(const T &)  func = nullptr 
)
inlinestatic

Takes a Data1D<T> array and joins them as one string with spacer between each element while applying the function to each element.

Note: The default func converts each element to a string with its RStr(T) constructor, and thus only works on types for which a constructor exists.

◆ Length8()

size_t RC::RStr::Length8 ( ) const
inline

Determine the length of the contents treated as a UTF-8 string.

Throws ErrorMsg "Invalid UTF-8" if the string cannot be parsed as a UTF-8 string.

◆ MakeCSV() [1/3]

template<class T >
static RStr RC::RStr::MakeCSV ( const Data1D< T > &  arr,
const RStr divider = ", " 
)
inlinestatic

Makes a comma-separated values string out of a Data1D<T> arr.

The type must have an RStr(T) constructor. The elements are separated by ", ".

◆ MakeCSV() [2/3]

template<class T >
static RStr RC::RStr::MakeCSV ( const Data2D< T > &  arr,
const RStr divider1 = ", ",
const RStr divider2 = "\n" 
)
inlinestatic

Makes a comma-separated values string out of a Data2D<T> arr.

The elements are separated by ", " in the first dimension, and followed by "\n" in the second dimension.

See also
MakeCSV

◆ MakeCSV() [3/3]

template<class T >
static RStr RC::RStr::MakeCSV ( const Data3D< T > &  arr,
const RStr divider1 = ", ",
const RStr divider2 = "\n",
const RStr divider3 = "\n" 
)
inlinestatic

Makes a comma-separated values string out of a Data3D<T> arr.

The elements are separated by ", " in the first dimension, followed by "\n" in the second dimension, and separated by an additional "\n" in the third dimension.

See also
MakeCSV

◆ SplitAny()

Data1D<RStr> RC::RStr::SplitAny ( const RStr dividers) const
inline

Return an array of strings split by any characters in dividers.

Always returns at least one element.

◆ ToLPCTSTR()

const char* RC::RStr::ToLPCTSTR ( )
inline

For Win32, provides an LPCTSTR to the string.

See ToLPCWSTR() for the return type if UNICODE is defined.

◆ ToLPCWSTR()

HoldRelated<std::wstring, const wchar_t*> RC::RStr::ToLPCWSTR ( )
inline

For Win32, provides an LPCWSTR to the string.

Return type implicitly casts to const whcar_t*, or with Get().

◆ UTF8toUTF32()

Data1D<u32> RC::RStr::UTF8toUTF32 ( ) const
inline

Treat the contents as a UTF-8 string, and return corresponding UTF-32 data.

Throws ErrorMsg "Invalid UTF-8" if the string cannot be parsed as a UTF-8 string.


The documentation for this class was generated from the following file:
email address
— (c) 2015