mirror of
https://github.com/arduino/Arduino.git
synced 2025-02-06 01:08:25 +01:00
Rewrite of the String class by Paul Stoffregen.
http://www.pjrc.com/teensy/string_class_experimental.html
This commit is contained in:
parent
6b890f8c7d
commit
1d9bbc01e3
@ -1,6 +1,8 @@
|
|||||||
/*
|
/*
|
||||||
WString.cpp - String library for Wiring & Arduino
|
WString.cpp - String library for Wiring & Arduino
|
||||||
|
...mostly rewritten by Paul Stoffregen...
|
||||||
Copyright (c) 2009-10 Hernando Barragan. All rights reserved.
|
Copyright (c) 2009-10 Hernando Barragan. All rights reserved.
|
||||||
|
Copyright 2011, Paul Stoffregen, paul@pjrc.com
|
||||||
|
|
||||||
This library is free software; you can redistribute it and/or
|
This library is free software; you can redistribute it and/or
|
||||||
modify it under the terms of the GNU Lesser General Public
|
modify it under the terms of the GNU Lesser General Public
|
||||||
@ -17,427 +19,680 @@
|
|||||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include "Arduino.h"
|
|
||||||
#include "WString.h"
|
#include "WString.h"
|
||||||
|
|
||||||
|
|
||||||
String::String( const char *value )
|
/*********************************************/
|
||||||
|
/* Constructors */
|
||||||
|
/*********************************************/
|
||||||
|
|
||||||
|
String::String(const char *cstr)
|
||||||
{
|
{
|
||||||
if ( value == NULL )
|
init();
|
||||||
value = "";
|
if (cstr) copy(cstr, strlen(cstr));
|
||||||
getBuffer( _length = strlen( value ) );
|
|
||||||
if ( _buffer != NULL )
|
|
||||||
strcpy( _buffer, value );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
String::String( const String &value )
|
String::String(const __FlashStringHelper *pgmstr)
|
||||||
{
|
{
|
||||||
getBuffer( _length = value._length );
|
init();
|
||||||
if ( _buffer != NULL )
|
*this = pgmstr;
|
||||||
strcpy( _buffer, value._buffer );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
String::String( const char value )
|
String::String(const String &value)
|
||||||
{
|
{
|
||||||
_length = 1;
|
init();
|
||||||
getBuffer(1);
|
*this = value;
|
||||||
if ( _buffer != NULL ) {
|
|
||||||
_buffer[0] = value;
|
|
||||||
_buffer[1] = 0;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
String::String( const unsigned char value )
|
#ifdef __GXX_EXPERIMENTAL_CXX0X__
|
||||||
|
String::String(String &&rval)
|
||||||
{
|
{
|
||||||
_length = 1;
|
init();
|
||||||
getBuffer(1);
|
move(rval);
|
||||||
if ( _buffer != NULL) {
|
}
|
||||||
_buffer[0] = value;
|
String::String(StringSumHelper &&rval)
|
||||||
_buffer[1] = 0;
|
{
|
||||||
}
|
init();
|
||||||
|
move(rval);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
String::String(char c)
|
||||||
|
{
|
||||||
|
init();
|
||||||
|
*this = c;
|
||||||
}
|
}
|
||||||
|
|
||||||
String::String( const int value, const int base )
|
String::String(unsigned char c)
|
||||||
{
|
{
|
||||||
char buf[33];
|
init();
|
||||||
itoa((signed long)value, buf, base);
|
*this = (char)c;
|
||||||
getBuffer( _length = strlen(buf) );
|
|
||||||
if ( _buffer != NULL )
|
|
||||||
strcpy( _buffer, buf );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
String::String( const unsigned int value, const int base )
|
String::String(const int value, unsigned char base)
|
||||||
{
|
{
|
||||||
char buf[33];
|
init();
|
||||||
ultoa((unsigned long)value, buf, base);
|
char buf[18];
|
||||||
getBuffer( _length = strlen(buf) );
|
itoa(value, buf, base);
|
||||||
if ( _buffer != NULL )
|
*this = buf;
|
||||||
strcpy( _buffer, buf );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
String::String( const long value, const int base )
|
String::String(unsigned int value, unsigned char base)
|
||||||
{
|
{
|
||||||
char buf[33];
|
init();
|
||||||
ltoa(value, buf, base);
|
char buf[17];
|
||||||
getBuffer( _length = strlen(buf) );
|
utoa(value, buf, base);
|
||||||
if ( _buffer != NULL )
|
*this = buf;
|
||||||
strcpy( _buffer, buf );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
String::String( const unsigned long value, const int base )
|
String::String(long value, unsigned char base)
|
||||||
{
|
{
|
||||||
char buf[33];
|
init();
|
||||||
ultoa(value, buf, 10);
|
char buf[34];
|
||||||
getBuffer( _length = strlen(buf) );
|
ltoa(value, buf, base);
|
||||||
if ( _buffer != NULL )
|
*this = buf;
|
||||||
strcpy( _buffer, buf );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
char String::charAt( unsigned int loc ) const
|
String::String(unsigned long value, unsigned char base)
|
||||||
{
|
{
|
||||||
return operator[]( loc );
|
init();
|
||||||
|
char buf[33];
|
||||||
|
ultoa(value, buf, base);
|
||||||
|
*this = buf;
|
||||||
}
|
}
|
||||||
|
|
||||||
void String::setCharAt( unsigned int loc, const char aChar )
|
String::~String()
|
||||||
{
|
{
|
||||||
if(_buffer == NULL) return;
|
free(buffer);
|
||||||
if(_length > loc) {
|
|
||||||
_buffer[loc] = aChar;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int String::compareTo( const String &s2 ) const
|
/*********************************************/
|
||||||
|
/* Memory Management */
|
||||||
|
/*********************************************/
|
||||||
|
|
||||||
|
inline void String::init(void)
|
||||||
{
|
{
|
||||||
return strcmp( _buffer, s2._buffer );
|
buffer = NULL;
|
||||||
|
capacity = 0;
|
||||||
|
len = 0;
|
||||||
|
flags = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
const String & String::concat( const String &s2 )
|
unsigned char String::reserve(unsigned int size)
|
||||||
{
|
{
|
||||||
return (*this) += s2;
|
if (capacity >= size) return 1;
|
||||||
|
if (changeBuffer(size)) {
|
||||||
|
if (len == 0) buffer[0] = 0;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
const String & String::operator=( const String &rhs )
|
unsigned char String::changeBuffer(unsigned int maxStrLen)
|
||||||
{
|
{
|
||||||
if ( this == &rhs )
|
char *newbuffer = (char *)realloc(buffer, maxStrLen + 1);
|
||||||
return *this;
|
if (newbuffer) {
|
||||||
|
buffer = newbuffer;
|
||||||
if ( rhs._length > _length )
|
capacity = maxStrLen;
|
||||||
{
|
return 1;
|
||||||
free(_buffer);
|
}
|
||||||
getBuffer( rhs._length );
|
return 0;
|
||||||
}
|
|
||||||
|
|
||||||
if ( _buffer != NULL ) {
|
|
||||||
_length = rhs._length;
|
|
||||||
strcpy( _buffer, rhs._buffer );
|
|
||||||
}
|
|
||||||
return *this;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//const String & String::operator+=( const char aChar )
|
/*********************************************/
|
||||||
//{
|
/* Copy and Move */
|
||||||
// if ( _length == _capacity )
|
/*********************************************/
|
||||||
// doubleBuffer();
|
|
||||||
//
|
|
||||||
// _buffer[ _length++ ] = aChar;
|
|
||||||
// _buffer[ _length ] = '\0';
|
|
||||||
// return *this;
|
|
||||||
//}
|
|
||||||
|
|
||||||
const String & String::operator+=( const String &other )
|
String & String::copy(const char *cstr, unsigned int length)
|
||||||
{
|
{
|
||||||
_length += other._length;
|
if (length == 0) {
|
||||||
if ( _length > _capacity )
|
if (buffer) buffer[0] = 0;
|
||||||
{
|
len = 0;
|
||||||
char *temp = (char *)realloc(_buffer, _length + 1);
|
return *this;
|
||||||
if ( temp != NULL ) {
|
}
|
||||||
_buffer = temp;
|
if (!reserve(length)) {
|
||||||
_capacity = _length;
|
if (buffer) {
|
||||||
} else {
|
free(buffer);
|
||||||
_length -= other._length;
|
buffer = NULL;
|
||||||
return *this;
|
}
|
||||||
}
|
len = capacity = 0;
|
||||||
}
|
return *this;
|
||||||
strcat( _buffer, other._buffer );
|
}
|
||||||
return *this;
|
len = length;
|
||||||
|
strcpy(buffer, cstr);
|
||||||
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
String & String::copy(const __FlashStringHelper *pgmstr)
|
||||||
int String::operator==( const String &rhs ) const
|
|
||||||
{
|
{
|
||||||
return ( _length == rhs._length && strcmp( _buffer, rhs._buffer ) == 0 );
|
unsigned int length = strlen_P((const prog_char *)pgmstr);
|
||||||
|
if (!reserve(length)) {
|
||||||
|
if (buffer) {
|
||||||
|
free(buffer);
|
||||||
|
buffer = NULL;
|
||||||
|
}
|
||||||
|
len = capacity = 0;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
len = length;
|
||||||
|
strcpy_P(buffer, (const prog_char *)pgmstr);
|
||||||
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
int String::operator!=( const String &rhs ) const
|
void String::move(String &rhs)
|
||||||
{
|
{
|
||||||
return ( _length != rhs.length() || strcmp( _buffer, rhs._buffer ) != 0 );
|
if (buffer) {
|
||||||
|
if (capacity >= rhs.len) {
|
||||||
|
strcpy(buffer, rhs.buffer);
|
||||||
|
len = rhs.len;
|
||||||
|
rhs.len = 0;
|
||||||
|
return;
|
||||||
|
} else {
|
||||||
|
free(buffer);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
buffer = rhs.buffer;
|
||||||
|
capacity = rhs.capacity;
|
||||||
|
len = rhs.len;
|
||||||
|
rhs.buffer = NULL;
|
||||||
|
rhs.capacity = 0;
|
||||||
|
rhs.len = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int String::operator<( const String &rhs ) const
|
String & String::operator = (const String &rhs)
|
||||||
{
|
{
|
||||||
return strcmp( _buffer, rhs._buffer ) < 0;
|
if (this == &rhs) return *this;
|
||||||
|
return copy(rhs.buffer, rhs.len);
|
||||||
}
|
}
|
||||||
|
|
||||||
int String::operator>( const String &rhs ) const
|
#ifdef __GXX_EXPERIMENTAL_CXX0X__
|
||||||
|
String & String::operator = (String &&rval)
|
||||||
{
|
{
|
||||||
return strcmp( _buffer, rhs._buffer ) > 0;
|
if (this != &rval) move(rval);
|
||||||
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
int String::operator<=( const String &rhs ) const
|
String & String::operator = (StringSumHelper &&rval)
|
||||||
{
|
{
|
||||||
return strcmp( _buffer, rhs._buffer ) <= 0;
|
if (this != &rval) move(rval);
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
String & String::operator = (const char *cstr)
|
||||||
|
{
|
||||||
|
if (cstr) {
|
||||||
|
copy(cstr, strlen(cstr));
|
||||||
|
} else {
|
||||||
|
len = 0;
|
||||||
|
}
|
||||||
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
int String::operator>=( const String & rhs ) const
|
String & String::operator = (const __FlashStringHelper *pgmstr)
|
||||||
{
|
{
|
||||||
return strcmp( _buffer, rhs._buffer ) >= 0;
|
copy(pgmstr);
|
||||||
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
char & String::operator[]( unsigned int index )
|
String & String::operator = (char c)
|
||||||
{
|
{
|
||||||
static char dummy_writable_char;
|
char buf[2];
|
||||||
if (index >= _length || !_buffer) {
|
buf[0] = c;
|
||||||
dummy_writable_char = 0;
|
buf[1] = 0;
|
||||||
return dummy_writable_char;
|
return copy(buf, 1);
|
||||||
}
|
}
|
||||||
return _buffer[ index ];
|
|
||||||
|
/*********************************************/
|
||||||
|
/* Append */
|
||||||
|
/*********************************************/
|
||||||
|
|
||||||
|
String & String::append(const String &s)
|
||||||
|
{
|
||||||
|
return append(s.buffer, s.len);
|
||||||
|
}
|
||||||
|
|
||||||
|
String & String::append(const char *cstr, unsigned int length)
|
||||||
|
{
|
||||||
|
unsigned int newlen = len + length;
|
||||||
|
if (length == 0 || !reserve(newlen)) return *this;
|
||||||
|
strcpy(buffer + len, cstr);
|
||||||
|
len = newlen;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
String & String::append(const char *cstr)
|
||||||
|
{
|
||||||
|
if (cstr) append(cstr, strlen(cstr));
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
String & String::append(const __FlashStringHelper *pgmstr)
|
||||||
|
{
|
||||||
|
unsigned int length = strlen_P((const prog_char *)pgmstr);
|
||||||
|
unsigned int newlen = len + length;
|
||||||
|
if (length == 0 || !reserve(newlen)) return *this;
|
||||||
|
strcpy_P(buffer + len, (const prog_char *)pgmstr);
|
||||||
|
len = newlen;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
String & String::append(char c)
|
||||||
|
{
|
||||||
|
char buf[2];
|
||||||
|
buf[0] = c;
|
||||||
|
buf[1] = 0;
|
||||||
|
append(buf, 1);
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
String & String::append(int num)
|
||||||
|
{
|
||||||
|
char buf[7];
|
||||||
|
itoa(num, buf, 10);
|
||||||
|
append(buf, strlen(buf));
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
String & String::append(unsigned int num)
|
||||||
|
{
|
||||||
|
char buf[6];
|
||||||
|
utoa(num, buf, 10);
|
||||||
|
append(buf, strlen(buf));
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
String & String::append(long num)
|
||||||
|
{
|
||||||
|
char buf[12];
|
||||||
|
ltoa(num, buf, 10);
|
||||||
|
append(buf, strlen(buf));
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
String & String::append(unsigned long num)
|
||||||
|
{
|
||||||
|
char buf[11];
|
||||||
|
ultoa(num, buf, 10);
|
||||||
|
append(buf, strlen(buf));
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*********************************************/
|
||||||
|
/* Concatenate */
|
||||||
|
/*********************************************/
|
||||||
|
|
||||||
|
StringSumHelper & operator + (const StringSumHelper &lhs, const String &rhs)
|
||||||
|
{
|
||||||
|
StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
|
||||||
|
a.append(rhs.buffer, rhs.len);
|
||||||
|
return a;
|
||||||
|
}
|
||||||
|
|
||||||
|
StringSumHelper & operator + (const StringSumHelper &lhs, const char *cstr)
|
||||||
|
{
|
||||||
|
StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
|
||||||
|
if (cstr) a.append(cstr, strlen(cstr));
|
||||||
|
return a;
|
||||||
|
}
|
||||||
|
|
||||||
|
StringSumHelper & operator + (const StringSumHelper &lhs, const __FlashStringHelper *pgmstr)
|
||||||
|
{
|
||||||
|
StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
|
||||||
|
a.append(pgmstr);
|
||||||
|
return a;
|
||||||
|
}
|
||||||
|
|
||||||
|
StringSumHelper & operator + (const StringSumHelper &lhs, char c)
|
||||||
|
{
|
||||||
|
StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
|
||||||
|
a.append(c);
|
||||||
|
return a;
|
||||||
|
}
|
||||||
|
|
||||||
|
StringSumHelper & operator + (const StringSumHelper &lhs, unsigned char c)
|
||||||
|
{
|
||||||
|
StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
|
||||||
|
a.append(c);
|
||||||
|
return a;
|
||||||
|
}
|
||||||
|
|
||||||
|
StringSumHelper & operator + (const StringSumHelper &lhs, int num)
|
||||||
|
{
|
||||||
|
StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
|
||||||
|
a.append(num);
|
||||||
|
return a;
|
||||||
|
}
|
||||||
|
|
||||||
|
StringSumHelper & operator + (const StringSumHelper &lhs, unsigned int num)
|
||||||
|
{
|
||||||
|
StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
|
||||||
|
a.append(num);
|
||||||
|
return a;
|
||||||
|
}
|
||||||
|
|
||||||
|
StringSumHelper & operator + (const StringSumHelper &lhs, long num)
|
||||||
|
{
|
||||||
|
StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
|
||||||
|
a.append(num);
|
||||||
|
return a;
|
||||||
|
}
|
||||||
|
|
||||||
|
StringSumHelper & operator + (const StringSumHelper &lhs, unsigned long num)
|
||||||
|
{
|
||||||
|
StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
|
||||||
|
a.append(num);
|
||||||
|
return a;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*********************************************/
|
||||||
|
/* Comparison */
|
||||||
|
/*********************************************/
|
||||||
|
|
||||||
|
int String::compareTo(const String &s) const
|
||||||
|
{
|
||||||
|
if (!buffer || !s.buffer) {
|
||||||
|
if (s.buffer && s.len > 0) return 0 - *(unsigned char *)s.buffer;
|
||||||
|
if (buffer && len > 0) return *(unsigned char *)buffer;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
return strcmp(buffer, s.buffer);
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned char String::equals(const String &s2) const
|
||||||
|
{
|
||||||
|
return (len == s2.len && compareTo(s2) == 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned char String::equals(const char *cstr) const
|
||||||
|
{
|
||||||
|
if (len == 0) return (cstr == NULL || *cstr == 0);
|
||||||
|
if (cstr == NULL) return buffer[0] == 0;
|
||||||
|
return strcmp(buffer, cstr) == 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned char String::equals(const __FlashStringHelper *pgmstr) const
|
||||||
|
{
|
||||||
|
if (len == 0) return pgm_read_byte(pgmstr) == 0;
|
||||||
|
return strcmp_P(buffer, (const prog_char *)pgmstr) == 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned char String::operator<(const String &rhs) const
|
||||||
|
{
|
||||||
|
return compareTo(rhs) < 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned char String::operator>(const String &rhs) const
|
||||||
|
{
|
||||||
|
return compareTo(rhs) > 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned char String::operator<=(const String &rhs) const
|
||||||
|
{
|
||||||
|
return compareTo(rhs) <= 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned char String::operator>=(const String &rhs) const
|
||||||
|
{
|
||||||
|
return compareTo(rhs) >= 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned char String::equalsIgnoreCase( const String &s2 ) const
|
||||||
|
{
|
||||||
|
if (this == &s2) return 1;
|
||||||
|
if (len != s2.len) return 0;
|
||||||
|
if (len == 0) return 1;
|
||||||
|
const char *p1 = buffer;
|
||||||
|
const char *p2 = s2.buffer;
|
||||||
|
while (*p1) {
|
||||||
|
if (tolower(*p1++) != tolower(*p2++)) return 0;
|
||||||
|
}
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned char String::startsWith( const String &s2 ) const
|
||||||
|
{
|
||||||
|
if (len < s2.len) return 0;
|
||||||
|
return startsWith(s2, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned char String::startsWith( const String &s2, unsigned int offset ) const
|
||||||
|
{
|
||||||
|
if (offset > len - s2.len || !buffer || !s2.buffer) return 0;
|
||||||
|
return strncmp( &buffer[offset], s2.buffer, s2.len ) == 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned char String::endsWith( const String &s2 ) const
|
||||||
|
{
|
||||||
|
if ( len < s2.len || !buffer || !s2.buffer) return 0;
|
||||||
|
return strcmp(&buffer[len - s2.len], s2.buffer) == 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*********************************************/
|
||||||
|
/* Character Access */
|
||||||
|
/*********************************************/
|
||||||
|
|
||||||
|
char String::charAt(unsigned int loc) const
|
||||||
|
{
|
||||||
|
return operator[](loc);
|
||||||
|
}
|
||||||
|
|
||||||
|
void String::setCharAt(unsigned int loc, char c)
|
||||||
|
{
|
||||||
|
if (loc < len) buffer[loc] = c;
|
||||||
|
}
|
||||||
|
|
||||||
|
char & String::operator[](unsigned int index)
|
||||||
|
{
|
||||||
|
static char dummy_writable_char;
|
||||||
|
if (index >= len || !buffer) {
|
||||||
|
dummy_writable_char = 0;
|
||||||
|
return dummy_writable_char;
|
||||||
|
}
|
||||||
|
return buffer[index];
|
||||||
}
|
}
|
||||||
|
|
||||||
char String::operator[]( unsigned int index ) const
|
char String::operator[]( unsigned int index ) const
|
||||||
{
|
{
|
||||||
// need to check for valid index, to do later
|
if (index >= len || !buffer) return 0;
|
||||||
return _buffer[ index ];
|
return buffer[index];
|
||||||
}
|
}
|
||||||
|
|
||||||
boolean String::endsWith( const String &s2 ) const
|
void String::getBytes(unsigned char *buf, unsigned int bufsize, unsigned int index) const
|
||||||
{
|
{
|
||||||
if ( _length < s2._length )
|
if (!bufsize || !buf) return;
|
||||||
return 0;
|
if (index >= len) {
|
||||||
|
buf[0] = 0;
|
||||||
return strcmp( &_buffer[ _length - s2._length], s2._buffer ) == 0;
|
return;
|
||||||
|
}
|
||||||
|
unsigned int n = bufsize - 1;
|
||||||
|
if (n > len - index) n = len - index;
|
||||||
|
strncpy((char *)buf, buffer + index, n);
|
||||||
|
buf[n] = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
boolean String::equals( const String &s2 ) const
|
/*********************************************/
|
||||||
|
/* Search */
|
||||||
|
/*********************************************/
|
||||||
|
|
||||||
|
int String::indexOf(char c) const
|
||||||
{
|
{
|
||||||
return ( _length == s2._length && strcmp( _buffer,s2._buffer ) == 0 );
|
return indexOf(c, 0);
|
||||||
}
|
|
||||||
|
|
||||||
boolean String::equalsIgnoreCase( const String &s2 ) const
|
|
||||||
{
|
|
||||||
if ( this == &s2 )
|
|
||||||
return true; //1;
|
|
||||||
else if ( _length != s2._length )
|
|
||||||
return false; //0;
|
|
||||||
|
|
||||||
return strcmp(toLowerCase()._buffer, s2.toLowerCase()._buffer) == 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
String String::replace( char findChar, char replaceChar )
|
|
||||||
{
|
|
||||||
if ( _buffer == NULL ) return *this;
|
|
||||||
String theReturn = _buffer;
|
|
||||||
char* temp = theReturn._buffer;
|
|
||||||
while( (temp = strchr( temp, findChar )) != 0 )
|
|
||||||
*temp = replaceChar;
|
|
||||||
|
|
||||||
return theReturn;
|
|
||||||
}
|
|
||||||
|
|
||||||
String String::replace( const String& match, const String& replace )
|
|
||||||
{
|
|
||||||
if ( _buffer == NULL ) return *this;
|
|
||||||
String temp = _buffer, newString;
|
|
||||||
|
|
||||||
int loc;
|
|
||||||
while ( (loc = temp.indexOf( match )) != -1 )
|
|
||||||
{
|
|
||||||
newString += temp.substring( 0, loc );
|
|
||||||
newString += replace;
|
|
||||||
temp = temp.substring( loc + match._length );
|
|
||||||
}
|
|
||||||
newString += temp;
|
|
||||||
return newString;
|
|
||||||
}
|
|
||||||
|
|
||||||
int String::indexOf( char temp ) const
|
|
||||||
{
|
|
||||||
return indexOf( temp, 0 );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int String::indexOf( char ch, unsigned int fromIndex ) const
|
int String::indexOf( char ch, unsigned int fromIndex ) const
|
||||||
{
|
{
|
||||||
if ( fromIndex >= _length )
|
if (fromIndex >= len) return -1;
|
||||||
return -1;
|
const char* temp = strchr(buffer + fromIndex, ch);
|
||||||
|
if (temp == NULL) return -1;
|
||||||
const char* temp = strchr( &_buffer[fromIndex], ch );
|
return temp - buffer;
|
||||||
if ( temp == NULL )
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
return temp - _buffer;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int String::indexOf( const String &s2 ) const
|
int String::indexOf(const String &s2) const
|
||||||
{
|
{
|
||||||
return indexOf( s2, 0 );
|
return indexOf(s2, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
int String::indexOf( const String &s2, unsigned int fromIndex ) const
|
int String::indexOf(const String &s2, unsigned int fromIndex) const
|
||||||
{
|
{
|
||||||
if ( fromIndex >= _length )
|
if (fromIndex >= len) return -1;
|
||||||
return -1;
|
const char *found = strstr(buffer + fromIndex, s2.buffer);
|
||||||
|
if (found == NULL) return -1;
|
||||||
const char *theFind = strstr( &_buffer[ fromIndex ], s2._buffer );
|
return found - buffer;
|
||||||
|
|
||||||
if ( theFind == NULL )
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
return theFind - _buffer; // pointer subtraction
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int String::lastIndexOf( char theChar ) const
|
int String::lastIndexOf( char theChar ) const
|
||||||
{
|
{
|
||||||
return lastIndexOf( theChar, _length - 1 );
|
return lastIndexOf(theChar, len - 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
int String::lastIndexOf( char ch, unsigned int fromIndex ) const
|
int String::lastIndexOf(char ch, int fromIndex) const
|
||||||
{
|
{
|
||||||
if ( fromIndex >= _length )
|
if (fromIndex >= len || fromIndex < 0) return -1;
|
||||||
return -1;
|
char tempchar = buffer[fromIndex + 1];
|
||||||
|
buffer[fromIndex + 1] = '\0';
|
||||||
char tempchar = _buffer[fromIndex + 1];
|
char* temp = strrchr( buffer, ch );
|
||||||
_buffer[fromIndex + 1] = '\0';
|
buffer[fromIndex + 1] = tempchar;
|
||||||
char* temp = strrchr( _buffer, ch );
|
if (temp == NULL) return -1;
|
||||||
_buffer[fromIndex + 1] = tempchar;
|
return temp - buffer;
|
||||||
|
|
||||||
if ( temp == NULL )
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
return temp - _buffer;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int String::lastIndexOf( const String &s2 ) const
|
int String::lastIndexOf(const String &s2) const
|
||||||
{
|
{
|
||||||
return lastIndexOf( s2, _length - s2._length );
|
return lastIndexOf(s2, len - s2.len);
|
||||||
}
|
}
|
||||||
|
|
||||||
int String::lastIndexOf( const String &s2, unsigned int fromIndex ) const
|
int String::lastIndexOf(const String &s2, int fromIndex) const
|
||||||
{
|
{
|
||||||
// check for empty strings
|
if (s2.len == 0 || len == 0 || s2.len > len || fromIndex < 0) return -1;
|
||||||
if ( s2._length == 0 || s2._length - 1 > fromIndex || fromIndex >= _length )
|
if (fromIndex >= len) fromIndex = len - 1;
|
||||||
return -1;
|
int found = -1;
|
||||||
|
for (char *p = buffer; p <= buffer + fromIndex; p++) {
|
||||||
// matching first character
|
p = strstr(p, s2.buffer);
|
||||||
char temp = s2[ 0 ];
|
if (!p) break;
|
||||||
|
if (p - buffer <= fromIndex) found = p - buffer;
|
||||||
for ( int i = fromIndex; i >= 0; i-- )
|
}
|
||||||
{
|
return found;
|
||||||
if ( _buffer[ i ] == temp && (*this).substring( i, i + s2._length ).equals( s2 ) )
|
|
||||||
return i;
|
|
||||||
}
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
boolean String::startsWith( const String &s2 ) const
|
|
||||||
{
|
|
||||||
if ( _length < s2._length )
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
return startsWith( s2, 0 );
|
|
||||||
}
|
|
||||||
|
|
||||||
boolean String::startsWith( const String &s2, unsigned int offset ) const
|
|
||||||
{
|
|
||||||
if ( offset > _length - s2._length )
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
return strncmp( &_buffer[offset], s2._buffer, s2._length ) == 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
String String::substring( unsigned int left ) const
|
String String::substring( unsigned int left ) const
|
||||||
{
|
{
|
||||||
return substring( left, _length );
|
return substring(left, len);
|
||||||
}
|
}
|
||||||
|
|
||||||
String String::substring( unsigned int left, unsigned int right ) const
|
String String::substring(unsigned int left, unsigned int right) const
|
||||||
{
|
{
|
||||||
if ( left > right )
|
if (left > right) {
|
||||||
{
|
unsigned int temp = right;
|
||||||
int temp = right;
|
right = left;
|
||||||
right = left;
|
left = temp;
|
||||||
left = temp;
|
}
|
||||||
}
|
String out;
|
||||||
|
if (left > len) return out;
|
||||||
if ( right > _length )
|
if (right > len) right = len;
|
||||||
{
|
char temp = buffer[right]; // save the replaced character
|
||||||
right = _length;
|
buffer[right] = '\0';
|
||||||
}
|
out = buffer + left; // pointer arithmetic
|
||||||
|
buffer[right] = temp; //restore character
|
||||||
char temp = _buffer[ right ]; // save the replaced character
|
return out;
|
||||||
_buffer[ right ] = '\0';
|
|
||||||
String outPut = ( _buffer + left ); // pointer arithmetic
|
|
||||||
_buffer[ right ] = temp; //restore character
|
|
||||||
return outPut;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
String String::toLowerCase() const
|
/*********************************************/
|
||||||
|
/* Modification */
|
||||||
|
/*********************************************/
|
||||||
|
|
||||||
|
String & String::replace(char find, char replace)
|
||||||
{
|
{
|
||||||
String temp = _buffer;
|
if (!buffer) return *this;
|
||||||
|
for (char *p = buffer; *p; p++) {
|
||||||
for ( unsigned int i = 0; i < _length; i++ )
|
if (*p == find) *p = replace;
|
||||||
temp._buffer[ i ] = (char)tolower( temp._buffer[ i ] );
|
}
|
||||||
return temp;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
String String::toUpperCase() const
|
String & String::replace(const String& find, const String& replace)
|
||||||
{
|
{
|
||||||
String temp = _buffer;
|
if (len == 0 || find.len == 0) return *this;
|
||||||
|
int diff = replace.len - find.len;
|
||||||
for ( unsigned int i = 0; i < _length; i++ )
|
char *readFrom = buffer;
|
||||||
temp._buffer[ i ] = (char)toupper( temp._buffer[ i ] );
|
char *foundAt;
|
||||||
return temp;
|
if (diff == 0) {
|
||||||
|
while ((foundAt = strstr(readFrom, find.buffer)) != NULL) {
|
||||||
|
memcpy(foundAt, replace.buffer, replace.len);
|
||||||
|
readFrom = foundAt + replace.len;
|
||||||
|
}
|
||||||
|
} else if (diff < 0) {
|
||||||
|
char *writeTo = buffer;
|
||||||
|
while ((foundAt = strstr(readFrom, find.buffer)) != NULL) {
|
||||||
|
unsigned int n = foundAt - readFrom;
|
||||||
|
memcpy(writeTo, readFrom, n);
|
||||||
|
writeTo += n;
|
||||||
|
memcpy(writeTo, replace.buffer, replace.len);
|
||||||
|
writeTo += replace.len;
|
||||||
|
readFrom = foundAt + find.len;
|
||||||
|
len += diff;
|
||||||
|
}
|
||||||
|
strcpy(writeTo, readFrom);
|
||||||
|
} else {
|
||||||
|
unsigned int size = len; // compute size needed for result
|
||||||
|
while ((foundAt = strstr(readFrom, find.buffer)) != NULL) {
|
||||||
|
readFrom = foundAt + find.len;
|
||||||
|
size += diff;
|
||||||
|
}
|
||||||
|
if (size == len) return *this;
|
||||||
|
if (size > capacity && !changeBuffer(size)) return *this;
|
||||||
|
int index = len - 1;
|
||||||
|
while ((index = lastIndexOf(find, index)) >= 0) {
|
||||||
|
readFrom = buffer + index + find.len;
|
||||||
|
memmove(readFrom + diff, readFrom, len - (readFrom - buffer));
|
||||||
|
len += diff;
|
||||||
|
buffer[len] = 0;
|
||||||
|
memcpy(buffer + index, replace.buffer, replace.len);
|
||||||
|
index--;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
String String::trim() const
|
String & String::toLowerCase(void)
|
||||||
{
|
{
|
||||||
if ( _buffer == NULL ) return *this;
|
if (!buffer) return *this;
|
||||||
String temp = _buffer;
|
for (char *p = buffer; *p; p++) {
|
||||||
unsigned int i,j;
|
*p = tolower(*p);
|
||||||
|
}
|
||||||
for ( i = 0; i < _length; i++ )
|
return *this;
|
||||||
{
|
|
||||||
if ( !isspace(_buffer[i]) )
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
for ( j = temp._length - 1; j > i; j-- )
|
|
||||||
{
|
|
||||||
if ( !isspace(_buffer[j]) )
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
return temp.substring( i, j + 1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void String::getBytes(unsigned char *buf, unsigned int bufsize)
|
String & String::toUpperCase(void)
|
||||||
{
|
{
|
||||||
if (!bufsize || !buf) return;
|
if (!buffer) return *this;
|
||||||
unsigned int len = bufsize - 1;
|
for (char *p = buffer; *p; p++) {
|
||||||
if (len > _length) len = _length;
|
*p = toupper(*p);
|
||||||
strncpy((char *)buf, _buffer, len);
|
}
|
||||||
buf[len] = 0;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
void String::toCharArray(char *buf, unsigned int bufsize)
|
String & String::trim(void)
|
||||||
{
|
{
|
||||||
if (!bufsize || !buf) return;
|
if (!buffer || len == 0) return *this;
|
||||||
unsigned int len = bufsize - 1;
|
char *begin = buffer;
|
||||||
if (len > _length) len = _length;
|
while (isspace(*begin)) begin++;
|
||||||
strncpy(buf, _buffer, len);
|
char *end = buffer + len - 1;
|
||||||
buf[len] = 0;
|
while (isspace(*end) && end >= begin) end--;
|
||||||
|
len = end + 1 - begin;
|
||||||
|
if (begin > buffer) memcpy(buffer, begin, len);
|
||||||
|
buffer[len] = 0;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*********************************************/
|
||||||
|
/* Parsing / Conversion */
|
||||||
|
/*********************************************/
|
||||||
|
|
||||||
|
long String::toInt(void) const
|
||||||
|
{
|
||||||
|
if (buffer) return atol(buffer);
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
long String::toInt() {
|
|
||||||
return atol(_buffer);
|
|
||||||
}
|
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
/*
|
/*
|
||||||
WString.h - String library for Wiring & Arduino
|
WString.h - String library for Wiring & Arduino
|
||||||
|
...mostly rewritten by Paul Stoffregen...
|
||||||
Copyright (c) 2009-10 Hernando Barragan. All right reserved.
|
Copyright (c) 2009-10 Hernando Barragan. All right reserved.
|
||||||
|
Copyright 2011, Paul Stoffregen, paul@pjrc.com
|
||||||
|
|
||||||
This library is free software; you can redistribute it and/or
|
This library is free software; you can redistribute it and/or
|
||||||
modify it under the terms of the GNU Lesser General Public
|
modify it under the terms of the GNU Lesser General Public
|
||||||
@ -17,96 +19,175 @@
|
|||||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#ifndef String_h
|
#ifndef String_class_h
|
||||||
#define String_h
|
#define String_class_h
|
||||||
|
#ifdef __cplusplus
|
||||||
|
|
||||||
//#include "Arduino.h"
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
|
#include <avr/pgmspace.h>
|
||||||
|
|
||||||
|
// When compiling programs with this class, the following gcc parameters
|
||||||
|
// dramatically increase performance and memory (RAM) efficiency, typically
|
||||||
|
// with little or no increase in code size.
|
||||||
|
// -felide-constructors
|
||||||
|
// -std=c++0x
|
||||||
|
|
||||||
|
// Brian Cook's "no overhead" Flash String type (message on Dec 14, 2010)
|
||||||
|
// modified by Mikal Hart for his FlashString library
|
||||||
|
class __FlashStringHelper;
|
||||||
|
#ifndef F
|
||||||
|
#define F(string_literal) (reinterpret_cast<__FlashStringHelper *>(PSTR(string_literal)))
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// An inherited class for holding the result of a concatenation. These
|
||||||
|
// result objects are assumed to be writable by subsequent concatenations.
|
||||||
|
class StringSumHelper;
|
||||||
|
|
||||||
|
// The string class
|
||||||
class String
|
class String
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
// constructors
|
// constructors
|
||||||
String( const char *value = "" );
|
String(const char *cstr = NULL);
|
||||||
String( const String &value );
|
String(const __FlashStringHelper *pgmstr);
|
||||||
String( const char );
|
String(const String &str);
|
||||||
String( const unsigned char );
|
#ifdef __GXX_EXPERIMENTAL_CXX0X__
|
||||||
String( const int, const int base=10);
|
String(String &&rval);
|
||||||
String( const unsigned int, const int base=10 );
|
String(StringSumHelper &&rval);
|
||||||
String( const long, const int base=10 );
|
#endif
|
||||||
String( const unsigned long, const int base=10 );
|
String(char c);
|
||||||
~String() { free(_buffer); _length = _capacity = 0;} //added _length = _capacity = 0;
|
String(unsigned char c);
|
||||||
|
String(int, unsigned char base=10);
|
||||||
|
String(unsigned int, unsigned char base=10);
|
||||||
|
String(long, unsigned char base=10);
|
||||||
|
String(unsigned long, unsigned char base=10);
|
||||||
|
~String(void);
|
||||||
|
|
||||||
// operators
|
// memory management
|
||||||
const String & operator = ( const String &rhs );
|
unsigned char reserve(unsigned int size);
|
||||||
const String & operator +=( const String &rhs );
|
inline unsigned int length(void) const {return len;}
|
||||||
//const String & operator +=( const char );
|
|
||||||
int operator ==( const String &rhs ) const;
|
|
||||||
int operator !=( const String &rhs ) const;
|
|
||||||
int operator < ( const String &rhs ) const;
|
|
||||||
int operator > ( const String &rhs ) const;
|
|
||||||
int operator <=( const String &rhs ) const;
|
|
||||||
int operator >=( const String &rhs ) const;
|
|
||||||
char operator []( unsigned int index ) const;
|
|
||||||
char& operator []( unsigned int index );
|
|
||||||
//operator const char *() const { return _buffer; }
|
|
||||||
|
|
||||||
// general methods
|
// copy and move
|
||||||
char charAt( unsigned int index ) const;
|
String & copy(const char *cstr, unsigned int length);
|
||||||
int compareTo( const String &anotherString ) const;
|
String & copy(const __FlashStringHelper *pgmstr);
|
||||||
unsigned char endsWith( const String &suffix ) const;
|
void move(String &rhs);
|
||||||
unsigned char equals( const String &anObject ) const;
|
String & operator = (const String &rhs);
|
||||||
unsigned char equalsIgnoreCase( const String &anotherString ) const;
|
String & operator = (const char *cstr);
|
||||||
int indexOf( char ch ) const;
|
String & operator = (const __FlashStringHelper *pgmstr);
|
||||||
int indexOf( char ch, unsigned int fromIndex ) const;
|
#ifdef __GXX_EXPERIMENTAL_CXX0X__
|
||||||
int indexOf( const String &str ) const;
|
String & operator = (String &&rval);
|
||||||
int indexOf( const String &str, unsigned int fromIndex ) const;
|
String & operator = (StringSumHelper &&rval);
|
||||||
int lastIndexOf( char ch ) const;
|
#endif
|
||||||
int lastIndexOf( char ch, unsigned int fromIndex ) const;
|
String & operator = (char c);
|
||||||
int lastIndexOf( const String &str ) const;
|
|
||||||
int lastIndexOf( const String &str, unsigned int fromIndex ) const;
|
|
||||||
const unsigned int length( ) const { return _length; }
|
|
||||||
void setCharAt(unsigned int index, const char ch);
|
|
||||||
unsigned char startsWith( const String &prefix ) const;
|
|
||||||
unsigned char startsWith( const String &prefix, unsigned int toffset ) const;
|
|
||||||
String substring( unsigned int beginIndex ) const;
|
|
||||||
String substring( unsigned int beginIndex, unsigned int endIndex ) const;
|
|
||||||
String toLowerCase( ) const;
|
|
||||||
String toUpperCase( ) const;
|
|
||||||
String trim( ) const;
|
|
||||||
void getBytes(unsigned char *buf, unsigned int bufsize);
|
|
||||||
void toCharArray(char *buf, unsigned int bufsize);
|
|
||||||
long toInt( );
|
|
||||||
const String& concat( const String &str );
|
|
||||||
String replace( char oldChar, char newChar );
|
|
||||||
String replace( const String& match, const String& replace );
|
|
||||||
friend String operator + ( String lhs, const String &rhs );
|
|
||||||
|
|
||||||
protected:
|
// append
|
||||||
char *_buffer; // the actual char array
|
String & append(const String &str);
|
||||||
unsigned int _capacity; // the array length minus one (for the '\0')
|
String & append(const char *cstr);
|
||||||
unsigned int _length; // the String length (not counting the '\0')
|
String & append(const __FlashStringHelper *pgmstr);
|
||||||
|
String & append(char c);
|
||||||
|
String & append(unsigned char c) {return append((char)c);}
|
||||||
|
String & append(int num);
|
||||||
|
String & append(unsigned int num);
|
||||||
|
String & append(long num);
|
||||||
|
String & append(unsigned long num);
|
||||||
|
String & operator += (const String &rhs) {return append(rhs);}
|
||||||
|
String & operator += (const char *cstr) {return append(cstr);}
|
||||||
|
String & operator += (const __FlashStringHelper *pgmstr) {return append(pgmstr);}
|
||||||
|
String & operator += (char c) {return append(c);}
|
||||||
|
String & operator += (unsigned char c) {return append((char)c);}
|
||||||
|
String & operator += (int num) {return append(num);}
|
||||||
|
String & operator += (unsigned int num) {return append(num);}
|
||||||
|
String & operator += (long num) {return append(num);}
|
||||||
|
String & operator += (unsigned long num) {return append(num);}
|
||||||
|
|
||||||
void getBuffer(unsigned int maxStrLen);
|
// concatenate
|
||||||
|
friend StringSumHelper & operator + (const StringSumHelper &lhs, const String &rhs);
|
||||||
|
friend StringSumHelper & operator + (const StringSumHelper &lhs, const char *cstr);
|
||||||
|
friend StringSumHelper & operator + (const StringSumHelper &lhs, const __FlashStringHelper *pgmstr);
|
||||||
|
friend StringSumHelper & operator + (const StringSumHelper &lhs, char c);
|
||||||
|
friend StringSumHelper & operator + (const StringSumHelper &lhs, unsigned char c);
|
||||||
|
friend StringSumHelper & operator + (const StringSumHelper &lhs, int num);
|
||||||
|
friend StringSumHelper & operator + (const StringSumHelper &lhs, unsigned int num);
|
||||||
|
friend StringSumHelper & operator + (const StringSumHelper &lhs, long num);
|
||||||
|
friend StringSumHelper & operator + (const StringSumHelper &lhs, unsigned long num);
|
||||||
|
|
||||||
private:
|
// comparison
|
||||||
|
int compareTo(const String &s) const;
|
||||||
|
unsigned char equals(const String &s) const;
|
||||||
|
unsigned char equals(const char *cstr) const;
|
||||||
|
unsigned char equals(const __FlashStringHelper *pgmstr) const;
|
||||||
|
unsigned char operator == (const String &rhs) const {return equals(rhs);}
|
||||||
|
unsigned char operator == (const char *cstr) const {return equals(cstr);}
|
||||||
|
unsigned char operator == (const __FlashStringHelper *pgmstr) const {return equals(pgmstr);}
|
||||||
|
unsigned char operator != (const String &rhs) const {return !equals(rhs);}
|
||||||
|
unsigned char operator != (const char *cstr) const {return !equals(cstr);}
|
||||||
|
unsigned char operator != (const __FlashStringHelper *pgmstr) const {return !equals(pgmstr);}
|
||||||
|
unsigned char operator < (const String &rhs) const;
|
||||||
|
unsigned char operator > (const String &rhs) const;
|
||||||
|
unsigned char operator <= (const String &rhs) const;
|
||||||
|
unsigned char operator >= (const String &rhs) const;
|
||||||
|
unsigned char equalsIgnoreCase(const String &s) const;
|
||||||
|
unsigned char startsWith( const String &prefix) const;
|
||||||
|
unsigned char startsWith(const String &prefix, unsigned int offset) const;
|
||||||
|
unsigned char endsWith(const String &suffix) const;
|
||||||
|
|
||||||
|
// character acccess
|
||||||
|
char charAt(unsigned int index) const;
|
||||||
|
void setCharAt(unsigned int index, char c);
|
||||||
|
char operator [] (unsigned int index) const;
|
||||||
|
char& operator [] (unsigned int index);
|
||||||
|
void getBytes(unsigned char *buf, unsigned int bufsize, unsigned int index=0) const;
|
||||||
|
void toCharArray(char *buf, unsigned int bufsize, unsigned int index=0) const
|
||||||
|
{getBytes((unsigned char *)buf, bufsize, index);}
|
||||||
|
|
||||||
|
// search
|
||||||
|
int indexOf( char ch ) const;
|
||||||
|
int indexOf( char ch, unsigned int fromIndex ) const;
|
||||||
|
int indexOf( const String &str ) const;
|
||||||
|
int indexOf( const String &str, unsigned int fromIndex ) const;
|
||||||
|
int lastIndexOf( char ch ) const;
|
||||||
|
int lastIndexOf( char ch, int fromIndex ) const;
|
||||||
|
int lastIndexOf( const String &str ) const;
|
||||||
|
int lastIndexOf( const String &str, int fromIndex ) const;
|
||||||
|
String substring( unsigned int beginIndex ) const;
|
||||||
|
String substring( unsigned int beginIndex, unsigned int endIndex ) const;
|
||||||
|
|
||||||
|
// modification
|
||||||
|
String & replace(char find, char replace);
|
||||||
|
String & replace(const String& find, const String& replace);
|
||||||
|
String & toLowerCase(void);
|
||||||
|
String & toUpperCase(void);
|
||||||
|
String & trim(void);
|
||||||
|
|
||||||
|
// parsing/conversion
|
||||||
|
long toInt(void) const;
|
||||||
|
|
||||||
|
protected:
|
||||||
|
char *buffer; // the actual char array
|
||||||
|
unsigned int capacity; // the array length minus one (for the '\0')
|
||||||
|
unsigned int len; // the String length (not counting the '\0')
|
||||||
|
unsigned char flags; // unused, for future features
|
||||||
|
protected:
|
||||||
|
void init(void);
|
||||||
|
unsigned char changeBuffer(unsigned int maxStrLen);
|
||||||
|
String & append(const char *cstr, unsigned int length);
|
||||||
};
|
};
|
||||||
|
|
||||||
// allocate buffer space
|
class StringSumHelper : public String
|
||||||
inline void String::getBuffer(unsigned int maxStrLen)
|
|
||||||
{
|
{
|
||||||
_capacity = maxStrLen;
|
public:
|
||||||
_buffer = (char *) malloc(_capacity + 1);
|
StringSumHelper(const String &s) : String(s) {}
|
||||||
if (_buffer == NULL) _length = _capacity = 0;
|
StringSumHelper(const char *p) : String(p) {}
|
||||||
}
|
StringSumHelper(const __FlashStringHelper *pgmstr) : String(pgmstr) {}
|
||||||
|
StringSumHelper(char c) : String(c) {}
|
||||||
|
StringSumHelper(unsigned char c) : String(c) {}
|
||||||
|
StringSumHelper(int num) : String(num, 10) {}
|
||||||
|
StringSumHelper(unsigned int num) : String(num, 10) {}
|
||||||
|
StringSumHelper(long num) : String(num, 10) {}
|
||||||
|
StringSumHelper(unsigned long num) : String(num, 10) {}
|
||||||
|
};
|
||||||
|
|
||||||
inline String operator+( String lhs, const String &rhs )
|
#endif // __cplusplus
|
||||||
{
|
#endif // String_class_h
|
||||||
return lhs += rhs;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
#endif
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user