var::String Class Reference

Documentation

This is an embedded friendly string class. It is similar to the C++ string type but is built on var::Data and cstring functions. The naming convention follows includes many std::string methods.

#include <sapi/var.hpp>
String s1;
String s2;
s1 = "This is my string";
s1 << " " << s1.capacity() << "\n";
printf("%s", s1.c_str());
//Strings can be compared
s2 = "This is another string";
if( s1 == s2 ){
printf("The strings are the same!\n");
} else {
printf("The strings are different\n");
}

The above code outputs:

This is my string 64
The strings are different

Public Member Functions

void append (const char *a)
 
void append (char c)
 
void assign (const char *a, u32 subpos, u32 sublen)
 
void assign (const char *a, u32 n)
 
void assign (const char *a)
 
char at (u32 pos) const
 
float atoff () const
 
int atoi () const
 
const char * c_str () const
 
int calc_delimited_data_size (char sep= ',', char term= '\n')
 
u32 capacity () const
 
int compare (const String &str) const
 
int compare (u32 pos, u32 len, const String &str) const
 
int compare (u32 pos, u32 len, const String &str, u32 subpos, u32 sublen) const
 
int compare (const char *s) const
 
int compare (u32 pos, u32 len, const char *s)
 
int compare (u32 pos, u32 len, const char *s, u32 n) const
 
u32 copy (char *s, u32 len, u32 pos=0) const
 
u32 copy (String &s, u32 n, u32 pos=0) const
 
Stringerase (u32 pos, u32 len=-1)
 
u32 find (const String &str, u32 pos=0) const
 
u32 find (const char *str, u32 pos=0) const
 
u32 find (const char c, u32 pos=0) const
 
u32 find (const char *s, u32 pos, u32 n) const
 
bool get_delimited_data (String &dest, int n, char sep= ',', char term= '\n')
 
Stringinsert (u32 pos, const char *s)
 
bool is_empty () const
 
u32 length () const
 
 operator const char * () const
 
bool operator!= (const char *cmp) const
 
Stringoperator<< (const char *a)
 
Stringoperator<< (char c)
 
Stringoperator= (const char *a)
 
Stringoperator= (String &a)
 
bool operator== (const char *cmp) const
 
int printf ()
 
void push_back (char c)
 
u32 rfind (const String &str, u32 pos=0) const
 
u32 rfind (const char *str, u32 pos=0) const
 
u32 rfind (const char c, u32 pos=0) const
 
u32 rfind (const char *s, u32 pos, u32 n) const
 
int set_capacity (u32 s)
 
u32 size () const
 
int sprintf (const char *format,...)
 
 String ()
 
 String (u32 capacity)
 
 String (const char *s)
 
 String (const char *s, u32 len)
 
 String (char *mem, u32 capacity, bool readonly=false)
 
String substr (u32 pos=0, u32 len=npos) const
 
void tolower ()
 
void toupper ()
 
- Public Member Functions inherited from var::Data
int alloc (u32 size, bool resize=false)
 
virtual u32 calc_size () const
 
u32 capacity () const
 
char * cdata () const
 
const char * cdata_const () const
 
void clear ()
 
 Data ()
 
 Data (void *mem, u32 size, bool readonly=false)
 
 Data (u32 size)
 
void * data () const
 
const void * data_const () const
 
void fill (unsigned char d)
 
int free ()
 
int resize (u32 size)
 
void set (void *mem, u32 size, bool readonly=false)
 
int set_capacity (u32 s)
 
virtual ~Data ()
 

Additional Inherited Members

- Static Public Member Functions inherited from var::Data
static u32 minimum_size ()
 

Member Enumeration Documentation

anonymous enum
Enumerator
npos 

Defines an invalid string length and position

Constructor & Destructor Documentation

var::String::String ( )

Declares an empty string.

var::String::String ( u32  capacity)

Declares an empty string of a specified capacity.

var::String::String ( const char *  s)

Declares a string and initialize to s.

var::String::String ( const char *  s,
u32  len 
)

Declares a string and initialize to s.

var::String::String ( char *  mem,
u32  capacity,
bool  readonly = false 
)

Constructs a string using statically allocated memory.

Parameters
memA pointer to memory to use
capacityThe number of bytes available
readonlyTrue if the mem is in readonly memory

Member Function Documentation

void var::String::append ( const char *  a)

Appends a (zero terminated) to string.

void var::String::append ( char  c)

Appends c to string.

void var::String::assign ( const char *  a,
u32  subpos,
u32  sublen 
)
inline

Assigns a substring of a to string.

void var::String::assign ( const char *  a,
u32  n 
)

Assigns a maximum of n characters of a to string.

void var::String::assign ( const char *  a)

Assigns a (zero terminated) to string.

char var::String::at ( u32  pos) const

Returns character at pos.

float var::String::atoff ( ) const

Converts to a float.

int var::String::atoi ( ) const
inline

Converts to an integer.

String x = "10";
printf("X is %d\n", x.atoi());
const char* var::String::c_str ( ) const
inline

Returns a c-style string pointer.

int var::String::calc_delimited_data_size ( char  sep = ',',
char  term = '\n' 
)

Returns the number of elements in the String.

u32 var::String::capacity ( ) const

Returns the capacity of the string.

The capacity is the current number of bytes allocated in memory for the string. set_capacity() will increase the capacity of the string. The append() method will also increase the capacity of the String if the appended string needs the extra space.

int var::String::compare ( const String str) const

Compares a the object to str.

int var::String::compare ( u32  pos,
u32  len,
const String str 
) const

Compares a the object to str.

int var::String::compare ( u32  pos,
u32  len,
const String str,
u32  subpos,
u32  sublen 
) const

Compares a the object to str.

int var::String::compare ( const char *  s) const

Compares a the object to s.

int var::String::compare ( u32  pos,
u32  len,
const char *  s 
)

Compares a the object to s.

int var::String::compare ( u32  pos,
u32  len,
const char *  s,
u32  n 
) const

Compares a the object to s.

u32 var::String::copy ( char *  s,
u32  len,
u32  pos = 0 
) const

Copies a portion of the string to s.

u32 var::String::copy ( String s,
u32  n,
u32  pos = 0 
) const
inline

Copies a portion of a string to s.

String& var::String::erase ( u32  pos,
u32  len = -1 
)

Erases a portion of the string starting with the character at pos.

u32 var::String::find ( const String str,
u32  pos = 0 
) const

Finds a var::String within the object.

Parameters
strThe String to find
posThe position to start searching
Returns
The position of the string or var::String::npos if the String was not found
u32 var::String::find ( const char *  str,
u32  pos = 0 
) const

Finds a c string within the object.

u32 var::String::find ( const char  c,
u32  pos = 0 
) const

Finds a character within the object.

u32 var::String::find ( const char *  s,
u32  pos,
u32  n 
) const

Finds a string within the object.

bool var::String::get_delimited_data ( String dest,
int  n,
char  sep = ',',
char  term = '\n' 
)

Copies the nth element (separated by sep) of the string to to dest.

String& var::String::insert ( u32  pos,
const char *  s 
)

Inserts s (zero terminated) into string at pos.

bool var::String::is_empty ( ) const
inline

Tests if string is empty.

u32 var::String::length ( ) const
inline

Returns the length of the string.

var::String::operator const char * ( ) const
inline

Returns a c-string.

This operator causes a String object to be converted to a c-style string if the compiler expects a c-style string.

Here is an example:

String str;
char buffer[16];
str.assign("Hello");
strcpy(buffer, str); //strcpy() expects a const char *, str is converted to one
bool var::String::operator!= ( const char *  cmp) const
inline

Compares to a c-string (inequality).

String& var::String::operator<< ( const char *  a)
inline

Appends a c style string go the string.

The string will be resized to accept the string if needed.

String& var::String::operator<< ( char  c)
inline

Appends a character to the string.

String& var::String::operator= ( const char *  a)
inline

Assigns a c-string to a String.

String& var::String::operator= ( String a)
inline

Assigns the value of a String to another String.

Parameters
aThe new String to assign
Returns
A reference to the destination String

This method will make a copy of the string in a new memory location rather than just using the data location of the source string.

String str1 = "hello";
String str2 = "goodbye";
str2 = str1; //both strings are now "hello"
bool var::String::operator== ( const char *  cmp) const
inline

Compares to a c-string.

int var::String::printf ( )
inline

Prints the string on the standard output.

Returns
The total number of characters written to the stdout
void var::String::push_back ( char  c)
inline

Appends c to string.

u32 var::String::rfind ( const String str,
u32  pos = 0 
) const

Finds a string within the string searching from right to left.

u32 var::String::rfind ( const char *  str,
u32  pos = 0 
) const

Finds a string within the string searching from right to left.

u32 var::String::rfind ( const char  c,
u32  pos = 0 
) const

Finds a character within the string searching from right to left.

u32 var::String::rfind ( const char *  s,
u32  pos,
u32  n 
) const

Finds a string within the string searching from right to left.

int var::String::set_capacity ( u32  s)
inline

Sets the capacity of the string.

Parameters
sThe number of bytes to reserve to string capacity.
Returns
Less than zero on an error

If a String uses dynamic memory allocation, this method will increase the capacity of the String. If s is smaller than capacity(), this function return without changing the capacity.

u32 var::String::size ( ) const
inline

Returns the length of the string.

int var::String::sprintf ( const char *  format,
  ... 
)

Prints a formatted string to this String.

Parameters
formatFormatted string
Returns
Total number of characters written to the string

If the formatted string exceeds the length of the string capacity, the formatted string will be truncated to fit.

String var::String::substr ( u32  pos = 0,
u32  len = npos 
) const

Gets a sub string of the string.

Parameters
posStarting position to look for the sub-string
lenThe number of bytes in the String to search
void var::String::tolower ( )

Converts to lower case.

void var::String::toupper ( )

Converts to upper case.