NetBurner 3.1
SimpleString.h
1 /*
2  * Copyright (c) 2007, Michael Feathers, James Grenning and Bas Vodde
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  * * Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * * Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  * * Neither the name of the <organization> nor the
13  * names of its contributors may be used to endorse or promote products
14  * derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE EARLIER MENTIONED AUTHORS ``AS IS'' AND ANY
17  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED. IN NO EVENT SHALL <copyright holder> BE LIABLE FOR ANY
20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
29 //
30 // SIMPLESTRING.H
31 //
32 // One of the design goals of CppUnitLite is to compilation with very old C++
33 // compilers. For that reason, the simple string class that provides
34 // only the operations needed in CppUnitLite.
35 //
37 
38 #ifndef D_SimpleString_h
39 #define D_SimpleString_h
40 
41 #include "StandardCLibrary.h"
42 
43 class SimpleStringCollection;
44 class TestMemoryAllocator;
45 
46 class SimpleString
47 {
48  friend bool operator==(const SimpleString& left, const SimpleString& right);
49  friend bool operator!=(const SimpleString& left, const SimpleString& right);
50 
51 public:
52  SimpleString(const char *value = "");
53  SimpleString(const char *value, size_t repeatCount);
54  SimpleString(const SimpleString& other);
55  ~SimpleString();
56 
57  SimpleString& operator=(const SimpleString& other);
58  SimpleString operator+(const SimpleString&) const;
59  SimpleString& operator+=(const SimpleString&);
60  SimpleString& operator+=(const char*);
61 
62  static const size_t npos = (size_t) -1;
63 
64  char at(size_t pos) const;
65  size_t find(char ch) const;
66  size_t findFrom(size_t starting_position, char ch) const;
67  bool contains(const SimpleString& other) const;
68  bool containsNoCase(const SimpleString& other) const;
69  bool startsWith(const SimpleString& other) const;
70  bool endsWith(const SimpleString& other) const;
71  void split(const SimpleString& split,
72  SimpleStringCollection& outCollection) const;
73  bool equalsNoCase(const SimpleString& str) const;
74 
75  size_t count(const SimpleString& str) const;
76 
77  void replace(char to, char with);
78  void replace(const char* to, const char* with);
79 
80  SimpleString lowerCase() const;
81  SimpleString subString(size_t beginPos) const;
82  SimpleString subString(size_t beginPos, size_t amount) const;
83  SimpleString subStringFromTill(char startChar, char lastExcludedChar) const;
84  void copyToBuffer(char* buffer, size_t bufferSize) const;
85 
86  const char *asCharString() const;
87  size_t size() const;
88  bool isEmpty() const;
89 
90  static void padStringsToSameLength(SimpleString& str1, SimpleString& str2, char ch);
91 
92  static TestMemoryAllocator* getStringAllocator();
93  static void setStringAllocator(TestMemoryAllocator* allocator);
94 
95  static int AtoI(const char*str);
96  static int StrCmp(const char* s1, const char* s2);
97  static size_t StrLen(const char*);
98  static int StrNCmp(const char* s1, const char* s2, size_t n);
99  static char* StrNCpy(char* s1, const char* s2, size_t n);
100  static char* StrStr(const char* s1, const char* s2);
101  static char ToLower(char ch);
102  static int MemCmp(const void* s1, const void *s2, size_t n);
103  static char* allocStringBuffer(size_t size, const char* file, int line);
104  static void deallocStringBuffer(char* str, const char* file, int line);
105 private:
106  char *buffer_;
107 
108  static TestMemoryAllocator* stringAllocator_;
109 
110  char* getEmptyString() const;
111  static char* copyToNewBuffer(const char* bufferToCopy, size_t bufferSize=0);
112  static bool isDigit(char ch);
113  static bool isSpace(char ch);
114  static bool isUpper(char ch);
115 };
116 
117 class SimpleStringCollection
118 {
119 public:
120  SimpleStringCollection();
121  ~SimpleStringCollection();
122 
123  void allocate(size_t size);
124 
125  size_t size() const;
126  SimpleString& operator[](size_t index);
127 
128 private:
129  SimpleString* collection_;
130  SimpleString empty_;
131  size_t size_;
132 
133  void operator =(SimpleStringCollection&);
134  SimpleStringCollection(SimpleStringCollection&);
135 };
136 
137 SimpleString StringFrom(bool value);
138 SimpleString StringFrom(const void* value);
139 SimpleString StringFrom(void (*value)());
140 SimpleString StringFrom(char value);
141 SimpleString StringFrom(const char *value);
142 SimpleString StringFromOrNull(const char * value);
143 SimpleString StringFrom(int value);
144 SimpleString StringFrom(unsigned int value);
145 SimpleString StringFrom(long value);
146 SimpleString StringFrom(unsigned long value);
147 SimpleString StringFrom(cpputest_longlong value);
148 SimpleString StringFrom(cpputest_ulonglong value);
149 SimpleString HexStringFrom(unsigned int value);
150 SimpleString HexStringFrom(int value);
151 SimpleString HexStringFrom(signed char value);
152 SimpleString HexStringFrom(long value);
153 SimpleString HexStringFrom(unsigned long value);
154 SimpleString HexStringFrom(cpputest_longlong value);
155 SimpleString HexStringFrom(cpputest_ulonglong value);
156 SimpleString HexStringFrom(const void* value);
157 SimpleString HexStringFrom(void (*value)());
158 SimpleString StringFrom(double value, int precision = 6);
159 SimpleString StringFrom(const SimpleString& other);
160 SimpleString StringFromFormat(const char* format, ...) __check_format__(printf, 1, 2);
161 SimpleString VStringFromFormat(const char* format, va_list args);
162 SimpleString StringFromBinary(const unsigned char* value, size_t size);
163 SimpleString StringFromBinaryOrNull(const unsigned char* value, size_t size);
164 SimpleString StringFromBinaryWithSize(const unsigned char* value, size_t size);
165 SimpleString StringFromBinaryWithSizeOrNull(const unsigned char* value, size_t size);
166 SimpleString StringFromMaskedBits(unsigned long value, unsigned long mask, size_t byteCount);
167 SimpleString StringFromOrdinalNumber(unsigned int number);
168 SimpleString BracketsFormattedHexStringFrom(int value);
169 SimpleString BracketsFormattedHexStringFrom(unsigned int value);
170 SimpleString BracketsFormattedHexStringFrom(long value);
171 SimpleString BracketsFormattedHexStringFrom(unsigned long value);
172 SimpleString BracketsFormattedHexStringFrom(cpputest_longlong value);
173 SimpleString BracketsFormattedHexStringFrom(cpputest_ulonglong value);
174 SimpleString BracketsFormattedHexStringFrom(signed char value);
175 SimpleString BracketsFormattedHexString(SimpleString hexString);
176 
177 
178 
179 #if CPPUTEST_USE_STD_CPP_LIB
180 
181 #include <string>
182 
183 SimpleString StringFrom(const std::string& other);
184 
185 #endif
186 
187 #endif