class::var::String

class var::String
  : public var::Data
  : public var::ConstString

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.

This class is for manipulating and modifying strings. The var::ConstString class is for static strings (basically just a const char * wrapper). This class inherits var::ConstString which contains methods for getting the length, comparing, finding strings within the string. This class adds the ability to append strings, create sub-strings, add strings, insert, erase, and convert the case.

#include <sapi/var.hpp>

String s1;
String s2;
s1 = "This is my string";
printf("%s", s1.cstring());
 //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

A note about copy initialization:

String hello("hello"); //OK: const char[6] converted from ConstString
String hello = "hello"; //Error: no conversion from const char [6] to String (in copy-initialization)

String hello; hello = "hello"; //OK const char[6] converted from ConstString for assignment

Summary

String()

[String](#classvar_1_1_string_1a0468033e702be4fc19dc7c172827a1d8)(  

const String & a
)

[String](#classvar_1_1_string_1a8717eb25b2a10a65941b93ce8bb033f0)(  

String && a
)

String(

const ConstString & s
)

String(

const ConstString & s,
const arg::Length length
)

explicit [String](#classvar_1_1_string_1a4e5f44ca51182d603da771e7a4476db8)(  

const Data & data
)

[~String](#classvar_1_1_string_1af5c481ea174a2a6d3b628751f9a6cce0)()

int append(

const ConstString & a
)

int append(

char c
)

int assign(

const ConstString & a,
const arg::Position position,
const arg::Length length
)

int assign(

const ConstString & a,
const arg::Length length
)

int assign(

const ConstString & a
)

char at(

const arg::ImplicitPosition position
) const

u32 capacity() const

String & erase(

const arg::Position position,
const arg::Length length
)

String & erase(

const arg::StringToErase string_to_erase,
const arg::Position position,
const arg::Length length
)

String & format(

const char * format,

)

String & insert(

const arg::Position position,
const arg::StringToInsert string_to_insert
)

String operator+(

const ConstString & a
)

String & operator+=(

const ConstString & a
)

String & operator+=(

char a
)

String & operator<<(

const ConstString & a
)

String & operator<<(

char c
)

String & operator=(

const String & a
)

String & operator=(

String && a
)

String & operator=(

const ConstString & a
)

void push_back(

char c
)

String & replace(

const arg::StringToErase old_string,
const arg::StringToInsert new_string,
const arg::Position position,
const arg::Length length
)

int resize(

const arg::ImplicitSize size
)

int sprintf(

const char * format,

)

String substr(

const arg::Position position,
const arg::Length length
) const

float to_float()

String & to_lower()

String & to_upper()

String & tolower()

String & toupper()

int vformat(

const char * fmt,
va_list list
)

enum @26

Members

String()

Constructs an empty string.

This constructor does not use any dyncamic memory allocation.

The str() method will return a valid zero-length, null-terminated string.

If the string is modified using methods herein, the memory will be automatically allocated as needed.


String(

const String & a
)

Contructs a string as a copy of the string specified.

Parameters

  • a A reference to the string to copy

String(

String && a
)


String(

const ConstString & s
)

Declares a string and initialize to s.


String(

const ConstString & s,
const arg::Length length
)

Declares a string and initialize to s.


explicit String(

const Data & data
)


~String()

int append(

const ConstString & a
)

Appends a to this String.


int append(

char c
)

Appends c to this String.


int assign(

const ConstString & a,
const arg::Position position,
const arg::Length length
)

Assigns a substring of a to this String.


int assign(

const ConstString & a,
const arg::Length length
)

Assigns a maximum of n characters of a to this String.


int assign(

const ConstString & a
)

Assigns a to this String.


char at(

const arg::ImplicitPosition position
) const


u32 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. Appending to the String will increate the capacity as necessary.


String & erase(

const arg::Position position,
const arg::Length length
)

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

Parameters

  • pos The position to start erasing

  • len The number of characters to erase

Returns

A reference to this string.


String & erase(

const arg::StringToErase string_to_erase,
const arg::Position position,
const arg::Length length
)


String & format(

const char * format,

)

Prints a formatted string to this String.

Parameters

  • format Formatted string (same as printf())

Returns

A reference to this String

If the formatted string exceeds the length of the string capacity, the string will be resized to accomate the full formatted string.

#include <sapi/var.hpp>
String serial_number;
u32 serial_number_value[2] = { 0xAAAABBBB, 0xCCCCEEEE };

//prints the formatted serial number to the string
serial_number.format("%08lX%08lX",
  serial_number_value[1],
  serial_number_value[0]);

//append to the serial number
serial_number << " <- hex dec -> " << String().format("%ld, %ld",
  serial_number_value[1],
  serial_number_value[0]) << " shown in decimal too."

String & insert(

const arg::Position position,
const arg::StringToInsert string_to_insert
)

Inserts s (zero terminated) into string at pos.

Parameters

  • pos Where to insert the string in this object (zero to insert at beginning)

  • s Zero terminated string to insert

If pos is greater than length(), error_number() is set to EINVAL.


String operator+(

const ConstString & a
)

Appends a string to this string and returns a new string.


String & operator+=(

const ConstString & a
)

Appends a string to this string.


String & operator+=(

char a
)

Appends a character to this string.


String & operator<<(

const ConstString & a
)

Appends a c style string go the string.

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

#include <sapi/var.hpp>
String str0;
String str1;

str0 = "hello";  //str0 is "hello"
str1 << "hello"; //str1 is "hello"

//combining = and << can create unwanted effects
str0 = "Hello";
str1 = str0 << "World"; //str1 == str0 == "HelloWorld"
//in the above, << operates on str0 then str1 is assiged to str0

//to append numbers (or any printf() compatible formatting)
str1.clear();
str1 << "String data is at 0x" << String().format("%p", str1.data());
u32 value = 0xaa55;

str1.clear();
str1 << "Value is HEX 0x" << String().format("%08lX", value); //with leading zero, uppercase hex
str1 << " hex: 0x" << String().format("%lx", value); //no leading zeros, lower-case hex
str1 << " or decimal: " << String().format("%ld", value); //unsigned value

String & operator<<(

char c
)

Appends a character to the string.


String & operator=(

const String & a
)

Assigns a to this string.


String & operator=(

String && a
)

Assigns a (as an rvalue) to this string.

The memory allocated to a is moved to this object.


String & operator=(

const ConstString & a
)

Assigns the value of a String to another String.

Parameters

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"

void push_back(

char c
)

Appends c to this String


String & replace(

const arg::StringToErase old_string,
const arg::StringToInsert new_string,
const arg::Position position,
const arg::Length length
)

Replaces one or more instances of a string with another string

Parameters

  • old_string The string to search for and replace

  • new_string The string that will be inserted in place of the old string

Returns

A reference to this string


int resize(

const arg::ImplicitSize size
)

Sets the number of bytes allocated for the string.

Parameters

  • s The number of bytes to reserve to string capacity (plus a byte for a zero terminator).

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 returns without changing the capacity.

The size() method will return the number of bytes availabe. length() returns the length of the string.


int sprintf(

const char * format,

)


String substr(

const arg::Position position,
const arg::Length length
) const

Gets a sub string of the string.

Parameters

  • pos Starting position to look for the sub-string

  • len The number of bytes in the String to search

Returns

A new string object containing the sub string specified


float to_float()

Converts to a float.

ConstString pi = "3.14";
printf("pi is %0.2f\n", pi.to_float());

String & to_lower()

Converts the string lower case.

This method functions like to_upper() but converts to lower case instead of upper case.

See to_upper() for code examples.


String & to_upper()

Converts the string to upper case.

This method operates on the current string (rather than returning a copy.

#include <sapi/var.hpp>

String hello("hello");

hello.to_upper(); //hello is now "HELLO"
hello.to_lower(); //hellos is now "hello"

//making an uppercase copy of a string

String hello_to_upper = hello.to_upper(); //both hello and hello_to_upper are "HELLO"

String world = String(hello).to_upper(); //world is "HELLO", hello is still "hello"

String & tolower()

String & toupper()

int vformat(

const char * fmt,
va_list list
)


enum @26
  • npos Defines an invalid string length and position

X

Thanks for Coming!

Subscribe to news and updates