CppUTest
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 
45 
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&);
59  SimpleString& operator+=(const SimpleString&);
60  SimpleString& operator+=(const char*);
61 
62  char at(int pos) const;
63  int find(char ch) const;
64  int findFrom(size_t starting_position, char ch) const;
65  bool contains(const SimpleString& other) const;
66  bool containsNoCase(const SimpleString& other) const;
67  bool startsWith(const SimpleString& other) const;
68  bool endsWith(const SimpleString& other) const;
69  void split(const SimpleString& split,
70  SimpleStringCollection& outCollection) const;
71  bool equalsNoCase(const SimpleString& str) const;
72 
73  size_t count(const SimpleString& str) const;
74 
75  void replace(char to, char with);
76  void replace(const char* to, const char* with);
77 
78  SimpleString toLower() const;
79  SimpleString subString(size_t beginPos, size_t amount) const;
80  SimpleString subStringFromTill(char startChar, char lastExcludedChar) const;
81  void copyToBuffer(char* buffer, size_t bufferSize) const;
82 
83  const char *asCharString() const;
84  size_t size() const;
85  bool isEmpty() const;
86 
87  static void padStringsToSameLength(SimpleString& str1, SimpleString& str2, char ch);
88 
89  static TestMemoryAllocator* getStringAllocator();
90  static void setStringAllocator(TestMemoryAllocator* allocator);
91 
92  static char* allocStringBuffer(size_t size);
93  static void deallocStringBuffer(char* str);
94 private:
95  char *buffer_;
96 
97  static TestMemoryAllocator* stringAllocator_;
98 
99  char* getEmptyString() const;
100 };
101 
103 {
104 public:
107 
108  void allocate(size_t size);
109 
110  size_t size() const;
111  SimpleString& operator[](size_t index);
112 
113 private:
114  SimpleString* collection_;
115  SimpleString empty_;
116  size_t size_;
117 
118  void operator =(SimpleStringCollection&);
120 };
121 
122 SimpleString StringFrom(bool value);
123 SimpleString StringFrom(const void* value);
124 SimpleString StringFrom(char value);
125 SimpleString StringFrom(const char *value);
126 SimpleString StringFromOrNull(const char * value);
127 SimpleString StringFrom(long value);
128 SimpleString StringFrom(int value);
129 SimpleString HexStringFrom(long value);
130 SimpleString StringFrom(double value, int precision = 6);
131 SimpleString StringFrom(const SimpleString& other);
132 SimpleString StringFromFormat(const char* format, ...);
133 SimpleString VStringFromFormat(const char* format, va_list args);
134 
135 #if CPPUTEST_USE_STD_CPP_LIB
136 
137 #include <string>
138 #include <stdint.h>
139 
140 SimpleString StringFrom(const std::string& other);
141 SimpleString StringFrom(unsigned long);
142 SimpleString StringFrom(uint32_t);
143 SimpleString StringFrom(uint16_t);
144 SimpleString StringFrom(uint8_t);
145 
146 #endif
147 
148 #endif
Definition: SimpleString.h:46
Definition: SimpleString.h:102
Definition: TestMemoryAllocator.h:49