WString.h
Go to the documentation of this file.
22 * The standard String object default constructor creates an empty string, which requires a heap allocation of 1 byte.
23 * I changed this behaviour to default to a null string (invalid) to avoid this (usually) un-necessary allocation.
24 * If the value of the string hasn't actually been assigned yet then an 'invalid' (or null) string is the more logical choice.
25 * Additional changes ensure that the content of such a string are equivalent to an empty string "".
29 * The intent of the Wiring authors seems to be that an expression producing a String object will fail and produce
30 * an 'invalid' String (that evaluates to False) if any of the allocations within that expression fail. This could
37 * If a heap allocation fails on either "A" or "B" the the result should be a null string. However, this is not actually
38 * the case. In practice, if "A" fails but "B" does not then the result will be "B", while if "A" succeeds but "B" fails
39 * then the result will be 'invalid'. This would appear to be an oversight in the Wiring library (last updated July 2016).
41 * I made a decision with these changes that heap allocation errors are a rare enough occurrence that attempting to deal with
44 * These changes have a knock-on effect in that if any of the allocations in an expression fail, then the result, tmp,
50 * An empty String object now consumes 12 bytes (from 8) but provides an SSO capacity of 11 characters.
113 #define F(string_literal) String(FPSTR(PSTR_COUNTED(string_literal)), sizeof(string_literal) - 1)
194 explicit String(unsigned char, unsigned char base = 10, unsigned char width = 0, char pad = '0');
199 explicit String(unsigned int num, unsigned char base = 10, unsigned char width = 0, char pad = '0')
205 explicit String(unsigned long, unsigned char base = 10, unsigned char width = 0, char pad = '0');
206 explicit String(unsigned long long, unsigned char base = 10, unsigned char width = 0, char pad = '0');
334 bool concat(unsigned char num, unsigned char base = 10, unsigned char width = 0, char pad = '0');
345 bool concat(unsigned long num, unsigned char base = 10, unsigned char width = 0, char pad = '0');
346 bool concat(unsigned long long num, unsigned char base = 10, unsigned char width = 0, char pad = '0');
351 constexpr typename std::enable_if<std::is_enum<E>::value && !std::is_convertible<E, int>::value, bool>::type
734 bool replace(const char* find_buf, size_t find_len, const char* replace_buf, size_t replace_len);
std::enable_if< std::is_integral< T >::value, String >::type toString(T value)
Definition: BitSet.h:481
const __FlashStringHelper * flash_string_t
Provides a strongly-typed pointer to allow safe implicit operation using String class methods.
Definition: WString.h:96
bool equalsIgnoreCase(const char *cstr, size_t len) const
Definition: String.hpp:202
bool equals(const char *cstr, size_t len, bool ignoreCase=false) const
Check for equality with a C-string.
Definition: WString.h:892
void setString(flash_string_t pstr)
String(unsigned long, unsigned char base=10, unsigned char width=0, char pad='0')
const char * c_str() const
Get a constant (un-modifiable) pointer to String content.
Definition: WString.h:616
friend StringSumHelper & operator+(const StringSumHelper &lhs, int num)
String(long long, unsigned char base=10, unsigned char width=0, char pad='0')
friend StringSumHelper & operator+(const StringSumHelper &lhs, char c)
friend StringSumHelper & operator+(const StringSumHelper &lhs, unsigned long long num)
String(char c)
friend StringSumHelper & operator+(const StringSumHelper &lhs, unsigned int num)
void setString(const char *cstr, size_t length)
bool startsWith(const String &prefix) const
Compare the start of a String Comparison is case-sensitive, must match exactly.
Definition: WString.h:531
int lastIndexOf(const String &s2, size_t fromIndex) const
bool concat(long num, unsigned char base=10, unsigned char width=0, char pad='0')
char & operator[](size_t index)
long toInt(void) const
friend StringSumHelper & operator+(const StringSumHelper &lhs, const String &rhs)
bool concat(double num)
bool concat(unsigned long long num, unsigned char base=10, unsigned char width=0, char pad='0')
String & padLeft(uint16_t minWidth, char c=' ')
Insert padding at start of string if length is less than given width.
Definition: WString.h:788
void replace(char find, char replace)
void setString(flash_string_t pstr, size_t length)
void move(String &rhs)
bool concat(unsigned char num, unsigned char base=10, unsigned char width=0, char pad='0')
friend StringSumHelper & operator+(const StringSumHelper &lhs, long num)
bool startsWith(const String &prefix, size_t offset) const
Compare a string portion.
bool equalsIgnoreCase(const FlashString &fstr) const
Definition: WString.h:519
static const String nullstr
A null string evaluates to false.
Definition: WString.h:148
String & padRight(uint16_t minWidth, char c=' ')
Insert padding at end of string if length is less than given width.
Definition: WString.h:796
int compareTo(const char *cstr, size_t length) const
bool setBuffer(const Buffer &buffer)
Set String content using move semantics from external memory buffer.
String(float, unsigned char decimalPlaces=2)
friend StringSumHelper & operator+(const StringSumHelper &lhs, unsigned long num)
bool setLength(size_t length)
set the string length accordingly, expanding if necessary
int lastIndexOf(char ch) const
String(StringSumHelper &&rval) noexcept
size_t getBytes(unsigned char *buf, size_t bufsize, size_t index=0) const
Read contents of a String into a buffer.
int lastIndexOf(const char *s2_buf, size_t fromIndex, size_t s2_len) const
static constexpr size_t SSO_CAPACITY
Max chars. (excluding NUL terminator) we can store in SSO mode.
Definition: WString.h:814
bool concat(const FlashString &fstr)
String & copy(flash_string_t pstr, size_t length)
bool concat(float num)
void toCharArray(char *buf, size_t bufsize, size_t index=0) const
Read contents of String into a buffer.
Definition: WString.h:607
String(unsigned int num, unsigned char base=10, unsigned char width=0, char pad='0')
Definition: WString.h:199
void setString(const char *cstr)
String(double, unsigned char decimalPlaces=2)
bool replace(const char *find_buf, size_t find_len, const char *replace_buf, size_t replace_len)
friend StringSumHelper & operator+(const StringSumHelper &lhs, unsigned char num)
String(unsigned long long, unsigned char base=10, unsigned char width=0, char pad='0')
char charAt(size_t index) const
Obtain the character at the given index.
Definition: WString.h:569
int lastIndexOf(char ch, size_t fromIndex) const
char operator[](size_t index) const
int indexOf(char ch, size_t fromIndex=0) const
friend StringSumHelper & operator+(const StringSumHelper &lhs, float num)
bool concat(unsigned int num, unsigned char base=10, unsigned char width=0, char pad='0')
Definition: WString.h:339
void invalidate(void)
friend StringSumHelper & operator+(const StringSumHelper &lhs, const char *cstr)
bool equalsIgnoreCase(const char *cstr) const
String & operator=(StringSumHelper &&rval) noexcept
bool replace(const String &find, const String &replace)
String & operator=(const char *cstr)
int indexOf(const char *s2_buf, size_t fromIndex, size_t s2_len) const
String(const char *cstr)
bool equals(const char *cstr) const
bool equalsIgnoreCase(const char *cstr, size_t length) const
String substring(size_t from, size_t to) const
bool concat(int num, unsigned char base=10, unsigned char width=0, char pad='0')
Definition: WString.h:335
bool concat(long long num, unsigned char base=10, unsigned char width=0, char pad='0')
void trim(const char *set=" \t\n\v\f\r")
Remove all leading and trailing characters from the String.
float toFloat(void) const
char * end()
Get a modifiable pointer to one-past the end of the String.
Definition: WString.h:635
void remove(size_t index, size_t count)
bool concat(unsigned long num, unsigned char base=10, unsigned char width=0, char pad='0')
constexpr std::enable_if< std::is_enum< E >::value &&!std::is_convertible< E, int >::value, bool >::type concat(E value)
Definition: WString.h:352
friend StringSumHelper & operator+(const StringSumHelper &lhs, double num)
bool concat(const char *cstr, size_t length)
String & copy(const char *cstr, size_t length)
String(int num, unsigned char base=10, unsigned char width=0, char pad='0')
Definition: WString.h:195
bool concat(const char *cstr)
String & operator=(const String &rhs)
bool equals(const char *cstr, size_t length) const
String(unsigned char, unsigned char base=10, unsigned char width=0, char pad='0')
String(long, unsigned char base=10, unsigned char width=0, char pad='0')
size_t length(void) const
Obtain the String length in characters, excluding NUL terminator.
Definition: WString.h:243
int lastIndexOf(const String &s2) const
Definition: WString.h:824