mirror of https://github.com/noisymime/Arduino.git
Checked in Xiaoyang's changes to String library
This commit is contained in:
parent
8fcb098533
commit
27b3254e99
|
@ -15,7 +15,7 @@
|
||||||
You should have received a copy of the GNU Lesser General Public
|
You should have received a copy of the GNU Lesser General Public
|
||||||
License along with this library; if not, write to the Free Software
|
License along with this library; if not, write to the Free Software
|
||||||
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_h
|
||||||
#define String_h
|
#define String_h
|
||||||
|
@ -27,17 +27,17 @@
|
||||||
|
|
||||||
class String
|
class String
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
// constructors
|
// constructors
|
||||||
String( const char *value = "" );
|
String( const char* value = "" );
|
||||||
String( const String &value );
|
String( const String &value );
|
||||||
explicit String( const char );
|
explicit String( const char );
|
||||||
explicit String( const unsigned char );
|
explicit String( const unsigned char );
|
||||||
explicit String( const int, const int base=10);
|
explicit String( const int, const int base=10);
|
||||||
explicit String( const unsigned int, const int base=10 );
|
explicit String( const unsigned int, const int base=10 );//negative sign only works on decimal
|
||||||
explicit String( const long, const int base=10 );
|
explicit String( const long, const int base=10 );
|
||||||
explicit String( const unsigned long, const int base=10 );
|
explicit String( const unsigned long, const int base=10 );//need add function to detect if number is bigger than the limit
|
||||||
~String() { free(_buffer); }
|
~String() { free(_buffer); _length = _capacity = 0;} //added _length = _capacity = 0;
|
||||||
|
|
||||||
// operators
|
// operators
|
||||||
const String & operator = ( const String &rhs );
|
const String & operator = ( const String &rhs );
|
||||||
|
@ -45,53 +45,65 @@ class String
|
||||||
const String & operator +=( const char );
|
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;
|
||||||
|
//is this useful?
|
||||||
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;
|
||||||
int operator >=( const String &rhs ) const;
|
int operator >=( const String &rhs ) const;
|
||||||
|
|
||||||
|
friend String operator + ( String lhs, const String &rhs );
|
||||||
|
|
||||||
char operator []( unsigned int index ) const;
|
char operator []( unsigned int index ) const;
|
||||||
char& operator []( unsigned int index );
|
char& operator []( unsigned int index );
|
||||||
//operator const char *() const { return _buffer; }
|
|
||||||
|
|
||||||
// general methods
|
// general methods
|
||||||
char charAt( unsigned int index ) const;
|
char charAt( unsigned int index ) const;
|
||||||
int compareTo( const String &anotherString ) const;
|
void setCharAt(unsigned int index, const char ch);
|
||||||
|
boolean startsWith( const String &prefix ) const;
|
||||||
|
boolean startsWith( const String &prefix, unsigned int toffset ) const;
|
||||||
boolean endsWith( const String &suffix ) const;
|
boolean endsWith( const String &suffix ) const;
|
||||||
boolean equals( const String &anObject ) const;
|
boolean contains( const String &str ) const; //added
|
||||||
boolean equalsIgnoreCase( const String &anotherString ) const;
|
boolean equals( const String &str ) const;
|
||||||
|
boolean equalsIgnoreCase( const String &str ) const;
|
||||||
|
|
||||||
|
String trim( ) const;
|
||||||
|
String toLowerCase( ) const;
|
||||||
|
String toUpperCase( ) const;
|
||||||
|
String replace( char oldChar, char newChar );
|
||||||
|
String replace( const String& match, const String& replace );
|
||||||
|
String substring( unsigned int beginIndex ) const;
|
||||||
|
String substring( unsigned int beginIndex, unsigned int endIndex ) const;
|
||||||
|
const String& append( const String &str );
|
||||||
|
const String& append( const char );
|
||||||
|
|
||||||
|
int compareTo( const String &str ) const;
|
||||||
|
int compareTo( const char* str ) const; //added
|
||||||
|
|
||||||
int indexOf( char ch ) const;
|
int indexOf( char ch ) const;
|
||||||
int indexOf( char ch, unsigned int fromIndex ) const;
|
int indexOf( char ch, unsigned int fromIndex ) const;
|
||||||
int indexOf( const String &str ) const;
|
int indexOf( const String &str ) const;
|
||||||
int indexOf( const String &str, unsigned int fromIndex ) const;
|
int indexOf( const String &str, unsigned int fromIndex ) const;
|
||||||
int lastIndexOf( char ch ) const;
|
int lastIndexOf( char ch ) const;
|
||||||
int lastIndexOf( char ch, unsigned int fromIndex ) const;
|
int lastIndexOf( char ch, unsigned int fromIndex ) const; //from right to left?
|
||||||
int lastIndexOf( const String &str ) const;
|
int lastIndexOf( const String &str ) const;
|
||||||
int lastIndexOf( const String &str, unsigned int fromIndex ) const;
|
int lastIndexOf( const String &str, unsigned int fromIndex ) const;
|
||||||
|
|
||||||
const unsigned int length( ) const { return _length; }
|
const unsigned int length( ) const { return _length; }
|
||||||
void setCharAt(unsigned int index, const char ch);
|
|
||||||
boolean startsWith( const String &prefix ) const;
|
|
||||||
boolean 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;
|
|
||||||
const byte *getBytes() const { return (byte*)_buffer; }
|
const byte *getBytes() const { return (byte*)_buffer; }
|
||||||
const char* toCharArray() const { return _buffer; }
|
const char* toCharArray() const { return _buffer; }
|
||||||
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:
|
//version checking
|
||||||
|
String version(void); // the version number
|
||||||
|
|
||||||
|
protected:
|
||||||
char *_buffer; // the actual char array
|
char *_buffer; // the actual char array
|
||||||
unsigned int _capacity; // the array length minus one (for the '\0')
|
unsigned int _capacity; // the array length minus one (for the '\0')
|
||||||
unsigned int _length; // the String length (not counting the '\0')
|
unsigned int _length; // the String length (not counting the '\0')
|
||||||
|
|
||||||
void getBuffer(unsigned int maxStrLen);
|
void getBuffer(unsigned int maxStrLen);
|
||||||
void doubleBuffer( );
|
//void doubleBuffer( );
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -100,16 +112,22 @@ inline void String::getBuffer(unsigned int maxStrLen)
|
||||||
{
|
{
|
||||||
_capacity = maxStrLen;
|
_capacity = maxStrLen;
|
||||||
_buffer = (char *) malloc(_capacity + 1);
|
_buffer = (char *) malloc(_capacity + 1);
|
||||||
|
if(_buffer == NULL){ //added for memory alloc error
|
||||||
|
_length = _capacity = 0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// double the buffer size
|
/* used only once, really not effcient
|
||||||
inline void String::doubleBuffer( )
|
// double the buffer size
|
||||||
{
|
inline void String::doubleBuffer( )
|
||||||
|
{
|
||||||
char *temp = _buffer;
|
char *temp = _buffer;
|
||||||
getBuffer( ++_capacity * 2 );
|
getBuffer( ++_capacity * 2 );
|
||||||
strcpy( _buffer, temp );
|
strcpy( _buffer, temp );
|
||||||
free(temp);
|
free(temp);
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
inline String operator+( String lhs, const String &rhs )
|
inline String operator+( String lhs, const String &rhs )
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in New Issue