LRAUV  revA
Str.h
Go to the documentation of this file.
1 #if !defined(__STR_H__)
2 #define __STR_H__
3 
4 #include <string.h>
5 
12 class Str
13 {
14 public:
15 
17  static const size_t NO_POS;
18  static const size_t MAX_LEN;
19  static const Str EMPTY_STR;
20  static char NoChars_[1];
21 
24  Str( const char* str = NULL, size_t length = NO_POS );
25 
28  Str( const Str& str, const size_t offset = 0, size_t count = NO_POS );
29 
32  Str( const bool var );
33 
36  Str( const double var, unsigned int precision = 6 )
37  : length_( 0 ),
38  chars_( NoChars_ )
39  {
40  intoString( var, precision );
41  }
42 
45  Str( const int var, unsigned int radix = 10, unsigned int radixOffset = 0 )
46  : length_( 0 ),
47  chars_( NoChars_ )
48  {
49  intoString( var, radix, radixOffset );
50  }
51 
54  Str( const size_t var, unsigned int radix = 16 )
55  : length_( 0 ),
56  chars_( NoChars_ )
57  {
58  intoString( ( int )var, radix, 0 );
59  }
60 
62  virtual ~Str();
63 
64  // operator overloading helper
65  friend Str operator +( const char* var, const Str& str );
66  friend Str operator +( const Str& var, const Str& str );
67 
68  // set the value
69  Str& set( const char* str = NULL, size_t length = NO_POS );
70 
71  // operator overloading
72  Str& operator =( const char* str );
73  Str& operator =( const Str& str );
74  Str& operator =( const double var )
75  {
76  intoString( var );
77  return *this;
78  }
79  Str substr( const size_t offset, size_t count = NO_POS ) const;
80  char operator[]( const size_t index ) const;
81 
82  Str& operator +=( const bool str )
83  {
84  return * this += ( Str ) str;
85  }
86  Str& operator +=( const unsigned char str )
87  {
88  return * this += Str( ( const char* ) & str, ( size_t )1 );
89  }
90  Str& operator +=( const double str )
91  {
92  return * this += ( Str ) str;
93  }
94  Str& operator +=( const int str )
95  {
96  return * this += ( Str ) str;
97  }
98  Str& operator +=( const size_t str )
99  {
100  return * this += ( Str ) str;
101  }
102  Str& operator +=( const char* str )
103  {
104  return * this += ( Str ) str;
105  }
106  Str& operator +=( const Str& str );
107  Str& append( const Str& str )
108  {
109  return * this += ( Str ) str;
110  }
111  Str& append( const char* str, size_t length )
112  {
113  return * this += Str( str, length );
114  }
115  Str& pushBack( const unsigned char str )
116  {
117  return * this += str;
118  }
119 
120  Str& operator << ( const bool str )
121  {
122  return * this += str;
123  }
124  Str& operator << ( const unsigned char str )
125  {
126  return * this += str;
127  }
128  Str& operator << ( const double str )
129  {
130  return * this += str;
131  }
132  Str& operator << ( const int str )
133  {
134  return * this += str;
135  }
136  Str& operator << ( const size_t str )
137  {
138  return * this += str;
139  }
140  Str& operator << ( const char* str )
141  {
142  return * this += str;
143  }
144  Str& operator << ( const Str& str )
145  {
146  return * this += str;
147  }
148 
149 
150  // add more logic comparison operators as following, for example, although not efficient
151  bool operator !=( const Str& str ) const
152  {
153  return compare( str ) != 0;
154  }
155 
156  bool operator !=( const char* str ) const
157  {
158  return compare( str ) != 0;
159  }
160 
161  int compare( const Str& str ) const;
162  int compare( const char* str ) const;
163 
164  bool operator ==( const Str& str ) const
165  {
166  return compare( str ) == 0;
167  }
168 
169  bool operator ==( const char* str ) const
170  {
171  return compare( str ) == 0;
172  }
173 
174  bool operator<( const Str& str ) const
175  {
176  return compare( str ) < 0;
177  }
178 
179  // c type string conversion
180  //operator char*()
181  //{
182  // return chars_;
183  //}
184  //operator const char*() const
185  //{
186  // return chars_;
187  //}
188  const char* cStr() const
189  {
190  return chars_;
191  }
192  const char* data() const
193  {
194  return chars_;
195  }
196  size_t length() const
197  {
198  return length_;
199  }
200  size_t size() const
201  {
202  return length_;
203  }
204 
205  size_t findLastOf( const unsigned char str ) const;
206 
207  size_t findLastOf( const char* str ) const;
208 
209  size_t find( const unsigned char str, size_t offset = 0 ) const;
210 
211  size_t find( const char* str, size_t offset = 0 ) const;
212 
213  size_t find( const Str& str, size_t offset = 0 ) const;
214 
215  static size_t FindLastOf( const char* str, const unsigned char theChar, size_t length = NO_POS );
216 
217  static size_t Find( const char* str, const unsigned char theChar, size_t length = NO_POS );
218 
219  bool startsWith( const char* str, size_t length = NO_POS ) const;
220 
221  bool startsWith( const Str& str ) const
222  {
223  return startsWith( str.cStr(), str.length() );
224  }
225 
226  bool endsWith( const Str& str ) const
227  {
228  return length_ >= str.length_ && find( str ) == length_ - str.length_;
229  }
230 
231  bool setChar( size_t index, const unsigned char theChar );
232 
233  void replaceChar( const char replace, const char replaceWith );
234 
235  Str* split( int& num, const char* splitSpec, size_t length = NO_POS ) const;
236 
237  Str* split( int& num, const Str str ) const
238  {
239  return split( num, str.chars_, str.length_ );
240  }
241 
243  Str asHex() const;
244 
247  static unsigned int IntToAscii( int value, char* str, unsigned int base, unsigned int bufSize, unsigned int baseOffset = 0 );
248 
249 protected:
250 
251  // have any good conversion method ?
252  virtual void intoString( const double var, unsigned int precision = 10 );
253  virtual void intoString( int var, unsigned int radix, unsigned int radixOffset = 0 );
254 
255  // data block
256  size_t length_;
257  char* chars_;
258 
259 };
260 
261 #endif
Str & operator<<(const bool str)
Definition: Str.h:120
Str(const int var, unsigned int radix=10, unsigned int radixOffset=0)
int constructor Unit tests in Str_Test.testIntConstructor
Definition: Str.h:45
Str(const double var, unsigned int precision=6)
double constructor Unit tests in Str_Test.testDoubleConstructor
Definition: Str.h:36
Str & append(const Str &str)
Definition: Str.h:107
static char NoChars_[1]
Definition: Str.h:20
static const size_t MAX_LEN
Definition: Str.h:18
size_t size() const
Definition: Str.h:200
bool startsWith(const char *str, size_t length=NO_POS) const
Definition: Str.cpp:352
Str asHex() const
Return a new Str, with a hex representation of each character of this string.
Definition: Str.cpp:467
bool endsWith(const Str &str) const
Definition: Str.h:226
bool operator==(const Str &str) const
Definition: Str.h:164
char * chars_
Definition: Str.h:257
static size_t FindLastOf(const char *str, const unsigned char theChar, size_t length=NO_POS)
Definition: Str.cpp:320
bool setChar(size_t index, const unsigned char theChar)
Definition: Str.cpp:361
size_t length() const
Definition: Str.h:196
bool operator<(const Str &str) const
Definition: Str.h:174
Str & operator+=(const bool str)
Definition: Str.h:82
Str(const size_t var, unsigned int radix=16)
size_t constructor Unit tests in Str_Test.testUnsignedIntConstructor
Definition: Str.h:54
size_t find(const unsigned char str, size_t offset=0) const
Definition: Str.cpp:280
static const size_t NO_POS
static constants
Definition: Str.h:17
Str & operator=(const char *str)
Definition: Str.cpp:217
Str * split(int &num, const Str str) const
Definition: Str.h:237
bool operator!=(const Str &str) const
Definition: Str.h:151
Str(const char *str=NULL, size_t length=NO_POS)
char* constructor Unit tests in Str_Test.testConstructorCharStar
Definition: Str.cpp:21
Replacement for standard template class string.
Definition: Str.h:12
size_t findLastOf(const unsigned char str) const
returns the position of the specified character in the string, looking from the end of the string tow...
Definition: Str.cpp:247
const char * data() const
Definition: Str.h:192
bool startsWith(const Str &str) const
Definition: Str.h:221
static size_t Find(const char *str, const unsigned char theChar, size_t length=NO_POS)
Definition: Str.cpp:336
int compare(const Str &str) const
Definition: Str.cpp:141
Str substr(const size_t offset, size_t count=NO_POS) const
Definition: Str.cpp:74
const char * cStr() const
Definition: Str.h:188
virtual void intoString(const double var, unsigned int precision=10)
Definition: Str.cpp:478
static const Str EMPTY_STR
Definition: Str.h:19
friend Str operator+(const char *var, const Str &str)
Definition: Str.cpp:100
Str & pushBack(const unsigned char str)
Definition: Str.h:115
Str * split(int &num, const char *splitSpec, size_t length=NO_POS) const
Definition: Str.cpp:429
void replaceChar(const char replace, const char replaceWith)
Definition: Str.cpp:421
char operator[](const size_t index) const
Definition: Str.cpp:91
size_t length_
Definition: Str.h:256
virtual ~Str()
destructor
Definition: Str.cpp:64
static unsigned int IntToAscii(int value, char *str, unsigned int base, unsigned int bufSize, unsigned int baseOffset=0)
convert an integer to a string, returns the length of the string Unit tests in Str_Test.testIntToAscii
Definition: Str.cpp:371
Str & append(const char *str, size_t length)
Definition: Str.h:111
Str & set(const char *str=NULL, size_t length=NO_POS)
Definition: Str.cpp:165