Sunteți pe pagina 1din 16

WString.

/*
WString.h - String library for Wiring & Arduino
...mostly rewritten by Paul Stoffregen...
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


modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,


but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/

#ifndef String_class_h
#define String_class_h
#ifdef __cplusplus

#include <stdlib.h>
#include <string.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

class __FlashStringHelper;
#define F(string_literal) (reinterpret_cast<const __FlashStringHelper *>(PSTR(string_literal)))

// 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
{
// use a function pointer to allow for "if (s)" without the
// complications of an operator bool(). for more information, see:
// http://www.artima.com/cppsource/safebool.html
typedef void (String::*StringIfHelperType)() const;
void StringIfHelper() const {}

public:
// constructors
// creates a copy of the initial value.
// if the initial value is null or invalid, or if memory allocation
// fails, the string will be marked as invalid (i.e. "if (s)" will
// be false).
String(const char *cstr = "");
String(const String &str);
String(const __FlashStringHelper *str);
#if __cplusplus >= 201103L || defined(__GXX_EXPERIMENTAL_CXX0X__)
String(String &&rval);
String(StringSumHelper &&rval);
#endif
explicit String(char c);
explicit String(unsigned char, unsigned char base=10);
explicit String(int, unsigned char base=10);
explicit String(unsigned int, unsigned char base=10);
explicit String(long, unsigned char base=10);
explicit String(unsigned long, unsigned char base=10);
explicit String(float, unsigned char decimalPlaces=2);
explicit String(double, unsigned char decimalPlaces=2);
~String(void);

// memory management
// return true on success, false on failure (in which case, the string
// is left unchanged). reserve(0), if successful, will validate an
// invalid string (i.e., "if (s)" will be true afterwards)
unsigned char reserve(unsigned int size);
inline unsigned int length(void) const {return len;}

// creates a copy of the assigned value. if the value is null or


// invalid, or if the memory allocation fails, the string will be
// marked as invalid ("if (s)" will be false).
String & operator = (const String &rhs);
String & operator = (const char *cstr);
String & operator = (const __FlashStringHelper *str);
#if __cplusplus >= 201103L || defined(__GXX_EXPERIMENTAL_CXX0X__)
String & operator = (String &&rval);
String & operator = (StringSumHelper &&rval);
#endif

// concatenate (works w/ built-in types)

// returns true on success, false on failure (in which case, the string
// is left unchanged). if the argument is null or invalid, the
// concatenation is considered unsucessful.
unsigned char concat(const String &str);
unsigned char concat(const char *cstr);
unsigned char concat(char c);
unsigned char concat(unsigned char c);
unsigned char concat(int num);
unsigned char concat(unsigned int num);
unsigned char concat(long num);
unsigned char concat(unsigned long num);
unsigned char concat(float num);
unsigned char concat(double num);
unsigned char concat(const __FlashStringHelper * str);

// if there's not enough memory for the concatenated value, the string
// will be left unchanged (but this isn't signalled in any way)
String & operator += (const String &rhs) {concat(rhs); return (*this);}
String & operator += (const char *cstr) {concat(cstr); return (*this);}
String & operator += (char c) {concat(c); return (*this);}
String & operator += (unsigned char num) {concat(num); return (*this);}
String & operator += (int num) {concat(num); return (*this);}
String & operator += (unsigned int num) {concat(num); return (*this);}
String & operator += (long num) {concat(num); return (*this);}
String & operator += (unsigned long num) {concat(num); return (*this);}
String & operator += (float num) {concat(num); return (*this);}
String & operator += (double num) {concat(num); return (*this);}
String & operator += (const __FlashStringHelper *str){concat(str); return (*this);}

friend StringSumHelper & operator + (const StringSumHelper &lhs, const String &rhs);
friend StringSumHelper & operator + (const StringSumHelper &lhs, const char *cstr);
friend StringSumHelper & operator + (const StringSumHelper &lhs, char c);
friend StringSumHelper & operator + (const StringSumHelper &lhs, unsigned char num);
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);
friend StringSumHelper & operator + (const StringSumHelper &lhs, float num);
friend StringSumHelper & operator + (const StringSumHelper &lhs, double num);
friend StringSumHelper & operator + (const StringSumHelper &lhs, const __FlashStringHelper
*rhs);

// comparison (only works w/ Strings and "strings")


operator StringIfHelperType() const { return buffer ? &String::StringIfHelper : 0; }
int compareTo(const String &s) const;
unsigned char equals(const String &s) const;
unsigned char equals(const char *cstr) 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 String &rhs) const {return !equals(rhs);}
unsigned char operator != (const char *cstr) const {return !equals(cstr);}
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); }
const char* c_str() const { return buffer; }
char* begin() { return buffer; }
char* end() { return buffer + length(); }
const char* begin() const { return c_str(); }
const char* end() const { return c_str() + length(); }

// 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, unsigned int fromIndex ) const;
int lastIndexOf( const String &str ) const;
int lastIndexOf( const String &str, unsigned int fromIndex ) const;
String substring( unsigned int beginIndex ) const { return substring(beginIndex, len); };
String substring( unsigned int beginIndex, unsigned int endIndex ) const;

// modification
void replace(char find, char replace);
void replace(const String& find, const String& replace);
void remove(unsigned int index);
void remove(unsigned int index, unsigned int count);
void toLowerCase(void);
void toUpperCase(void);
void trim(void);

// parsing/conversion
long toInt(void) const;
float toFloat(void) const;
double toDouble(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')
protected:
void init(void);
void invalidate(void);
unsigned char changeBuffer(unsigned int maxStrLen);
unsigned char concat(const char *cstr, unsigned int length);

// copy and move


String & copy(const char *cstr, unsigned int length);
String & copy(const __FlashStringHelper *pstr, unsigned int length);
#if __cplusplus >= 201103L || defined(__GXX_EXPERIMENTAL_CXX0X__)
void move(String &rhs);
#endif
};

class StringSumHelper : public String


{
public:
StringSumHelper(const String &s) : String(s) {}
StringSumHelper(const char *p) : String(p) {}
StringSumHelper(char c) : String(c) {}
StringSumHelper(unsigned char num) : String(num) {}
StringSumHelper(int num) : String(num) {}
StringSumHelper(unsigned int num) : String(num) {}
StringSumHelper(long num) : String(num) {}
StringSumHelper(unsigned long num) : String(num) {}
StringSumHelper(float num) : String(num) {}
StringSumHelper(double num) : String(num) {}
};

#endif // __cplusplus
#endif // String_class_h

+++++++++

String.h
#ifndef STRING_H
#define STRING_H
#define _CRT_SECURE_NO_WARNINGS

#define CAPACITY_CONSTANT 1.7

#include <iostream>
#include <gtest\gtest.h>

class String
{
public:
class Substring
{
int offset;
int length;
String *str;
public:
Substring(String &, int, int);
Substring operator=(const String &);
Substring operator=(const char *);
operator String();
};
friend class Substring;

class Buffer
{
friend class Substring;
friend class String;

FRIEND_TEST(StringTest, PerformanceSubstringTest);
char *string;
int references;
int length;
int capacity;
public:
Buffer();
Buffer(const char*, int);
~Buffer();
Buffer& operator++();
Buffer& operator--();
operator int();
};

class Char
{
char &chr;
void operator &();
public:
Char(char &);
Char& operator = (const char&);

bool operator==(const char&) const;

operator char();
};

Buffer *buf;

public:
String();
String(const char*);
String(const char*, int);
String(const String &);
~String();

int length() const;

char* c_str() const;

friend std::ostream& operator<<(std::ostream &out, const String &s);


friend std::istream& operator>>(std::istream &in, String &s);

String operator=(const String &);


String operator+(const String &);
String operator+=(const String &);

Substring substr(int, int);


void remove(int, int);
void insert(int, const String &);

void reserve(int res);


int capacity() const;

Substring operator()(int, int);


Char operator[](int) const;

bool operator!() const;

bool operator==(const String &) const;


bool operator!=(const String &) const;
bool operator<(const String &) const;
bool operator>(const String &) const;
bool operator>=(const String &) const;
bool operator<=(const String &) const;

bool operator==(const char *) const;


bool operator!=(const char *) const;
bool operator<(const char *) const;
bool operator>(const char *) const;
bool operator>=(const char *) const;
bool operator<=(const char *) const;

int rc();
};

#endif

++++++++++

168 lines (149 sloc) 5.16 KB


/*
* string.h
*
* Definitions for memory and string functions.
*/

#ifndef _STRING_H_
#define _STRING_H_

#include "_ansi.h"
#include <sys/reent.h>
#include <sys/cdefs.h>
#include <sys/features.h>

#define __need_size_t
#define __need_NULL
#include <stddef.h>

_BEGIN_STD_C

_PTR _EXFUN(memchr,(const _PTR, int, size_t));


int _EXFUN(memcmp,(const _PTR, const _PTR, size_t));
_PTR _EXFUN(memcpy,(_PTR __restrict, const _PTR __restrict, size_t));
_PTR _EXFUN(memmove,(_PTR, const _PTR, size_t));
_PTR _EXFUN(memset,(_PTR, int, size_t));
char *_EXFUN(strcat,(char *__restrict, const char *__restrict));
char *_EXFUN(strchr,(const char *, int));
int _EXFUN(strcmp,(const char *, const char *));
int _EXFUN(strcoll,(const char *, const char *));
char *_EXFUN(strcpy,(char *__restrict, const char *__restrict));
size_t _EXFUN(strcspn,(const char *, const char *));
char *_EXFUN(strerror,(int));
size_t _EXFUN(strlen,(const char *));
char *_EXFUN(strncat,(char *__restrict, const char *__restrict, size_t));
int _EXFUN(strncmp,(const char *, const char *, size_t));
char *_EXFUN(strncpy,(char *__restrict, const char *__restrict, size_t));
char *_EXFUN(strpbrk,(const char *, const char *));
char *_EXFUN(strrchr,(const char *, int));
size_t _EXFUN(strspn,(const char *, const char *));
char *_EXFUN(strstr,(const char *, const char *));
#ifndef _REENT_ONLY
char *_EXFUN(strtok,(char *__restrict, const char *__restrict));
#endif
size_t _EXFUN(strxfrm,(char *__restrict, const char *__restrict, size_t));

#if __POSIX_VISIBLE
char *_EXFUN(strtok_r,(char *__restrict, const char *__restrict, char **__restrict));
#endif
#if __BSD_VISIBLE
int _EXFUN(bcmp,(const void *, const void *, size_t));
void _EXFUN(bcopy,(const void *, void *, size_t));
void _EXFUN(bzero,(void *, size_t));
int _EXFUN(ffs,(int));
char *_EXFUN(index,(const char *, int));
#endif
#if __BSD_VISIBLE || __XSI_VISIBLE
_PTR _EXFUN(memccpy,(_PTR __restrict, const _PTR __restrict, int, size_t));
#endif
#if __GNU_VISIBLE
_PTR _EXFUN(mempcpy,(_PTR, const _PTR, size_t));
_PTR _EXFUN(memmem, (const _PTR, size_t, const _PTR, size_t));
#endif
_PTR _EXFUN(memrchr,(const _PTR, int, size_t));
#if __GNU_VISIBLE
_PTR _EXFUN(rawmemchr,(const _PTR, int));
#endif
#if __BSD_VISIBLE
char *_EXFUN(rindex,(const char *, int));
#endif
char *_EXFUN(stpcpy,(char *__restrict, const char *__restrict));
char *_EXFUN(stpncpy,(char *__restrict, const char *__restrict, size_t));
#if __BSD_VISIBLE || __POSIX_VISIBLE
int _EXFUN(strcasecmp,(const char *, const char *));
#endif
#if __GNU_VISIBLE
char *_EXFUN(strcasestr,(const char *, const char *));
char *_EXFUN(strchrnul,(const char *, int));
#endif
#if __XSI_VISIBLE >= 500
char *_EXFUN(strdup,(const char *));
#endif
#ifndef __STRICT_ANSI__
char *_EXFUN(_strdup_r,(struct _reent *, const char *));
#endif
#if __XSI_VISIBLE >= 700
char *_EXFUN(strndup,(const char *, size_t));
#endif

#ifndef __STRICT_ANSI__
char *_EXFUN(_strndup_r,(struct _reent *, const char *, size_t));
#endif

#if __GNU_VISIBLE
int _EXFUN(ffsl,(long));
int _EXFUN(ffsll, (long long));
#endif

/* There are two common strerror_r variants. If you request


_GNU_SOURCE, you get the GNU version; otherwise you get the POSIX
version. POSIX requires that #undef strerror_r will still let you
invoke the underlying function, but that requires gcc support. */
#if __GNU_VISIBLE
char *_EXFUN(strerror_r,(int, char *, size_t));
#else
# ifdef __GNUC__
int _EXFUN(strerror_r,(int, char *, size_t))
__asm__ (__ASMNAME ("__xpg_strerror_r"));
# else
int _EXFUN(__xpg_strerror_r,(int, char *, size_t));
# define strerror_r __xpg_strerror_r
# endif
#endif

/* Reentrant version of strerror. */


char * _EXFUN(_strerror_r, (struct _reent *, int, int, int *));
#if __BSD_VISIBLE
size_t _EXFUN(strlcat,(char *, const char *, size_t));
size_t _EXFUN(strlcpy,(char *, const char *, size_t));
#endif
#if __BSD_VISIBLE || __POSIX_VISIBLE
int _EXFUN(strncasecmp,(const char *, const char *, size_t));
#endif
#if !defined(__STRICT_ANSI__) || __POSIX_VISIBLE >= 200809 || \
__XSI_VISIBLE >= 700
size_t _EXFUN(strnlen,(const char *, size_t));
#endif
#if __BSD_VISIBLE
char *_EXFUN(strsep,(char **, const char *));
#endif

/*
* The origin of these is unknown to me so I am conditionalizing them
* on __STRICT_ANSI__. Finetuning this is definitely needed. --joel
*/
#if !defined(__STRICT_ANSI__)
char *_EXFUN(strlwr,(char *));
char *_EXFUN(strupr,(char *));
#endif

#ifndef DEFS_H /* Kludge to work around problem compiling in gdb */


char *_EXFUN(strsignal, (int __signo));
#endif

#ifdef __CYGWIN__
int _EXFUN(strtosigno, (const char *__name));
#endif

#if defined _GNU_SOURCE && defined __GNUC__


#define strdupa(__s) \
(__extension__ ({const char *__in = (__s); \
size_t __len = strlen (__in) + 1; \
char * __out = (char *) __builtin_alloca (__len); \
(char *) memcpy (__out, __in, __len);}))
#define strndupa(__s, __n) \
(__extension__ ({const char *__in = (__s); \
size_t __len = strnlen (__in, (__n)) + 1; \
char *__out = (char *) __builtin_alloca (__len); \
__out[__len-1] = '\0'; \
(char *) memcpy (__out, __in, __len-1);}))
#endif /* _GNU_SOURCE && __GNUC__ */

#include <sys/string.h>

_END_STD_C

#endif /* _STRING_H_ */

++++++++++

S-ar putea să vă placă și