fork

Fork of cpputest by Rohit Grover

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers SimpleString.h Source File

SimpleString.h

00001 /*
00002  * Copyright (c) 2007, Michael Feathers, James Grenning and Bas Vodde
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions are met:
00007  *     * Redistributions of source code must retain the above copyright
00008  *       notice, this list of conditions and the following disclaimer.
00009  *     * Redistributions in binary form must reproduce the above copyright
00010  *       notice, this list of conditions and the following disclaimer in the
00011  *       documentation and/or other materials provided with the distribution.
00012  *     * Neither the name of the <organization> nor the
00013  *       names of its contributors may be used to endorse or promote products
00014  *       derived from this software without specific prior written permission.
00015  *
00016  * THIS SOFTWARE IS PROVIDED BY THE EARLIER MENTIONED AUTHORS ``AS IS'' AND ANY
00017  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00018  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00019  * DISCLAIMED. IN NO EVENT SHALL <copyright holder> BE LIABLE FOR ANY
00020  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00021  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00022  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00023  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00024  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00025  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00026  */
00027 
00028 ///////////////////////////////////////////////////////////////////////////////
00029 //
00030 // SIMPLESTRING.H
00031 //
00032 // One of the design goals of CppUnitLite is to compilation with very old C++
00033 // compilers.  For that reason, the simple string class that provides
00034 // only the operations needed in CppUnitLite.
00035 //
00036 ///////////////////////////////////////////////////////////////////////////////
00037 
00038 #ifndef D_SimpleString_h
00039 #define D_SimpleString_h
00040 
00041 #include "StandardCLibrary.h"
00042 
00043 class SimpleStringCollection;
00044 class TestMemoryAllocator;
00045 
00046 class SimpleString
00047 {
00048     friend bool operator==(const SimpleString& left, const SimpleString& right);
00049     friend bool operator!=(const SimpleString& left, const SimpleString& right);
00050 
00051 public:
00052     SimpleString(const char *value = "");
00053     SimpleString(const char *value, size_t repeatCount);
00054     SimpleString(const SimpleString& other);
00055     ~SimpleString();
00056 
00057     SimpleString& operator=(const SimpleString& other);
00058     SimpleString operator+(const SimpleString&);
00059     SimpleString& operator+=(const SimpleString&);
00060     SimpleString& operator+=(const char*);
00061 
00062     char at(int pos) const;
00063     int find(char ch) const;
00064     int findFrom(size_t starting_position, char ch) const;
00065     bool contains(const SimpleString& other) const;
00066     bool containsNoCase(const SimpleString& other) const;
00067     bool startsWith(const SimpleString& other) const;
00068     bool endsWith(const SimpleString& other) const;
00069     void split(const SimpleString& split,
00070                     SimpleStringCollection& outCollection) const;
00071     bool equalsNoCase(const SimpleString& str) const;
00072 
00073     size_t count(const SimpleString& str) const;
00074 
00075     void replace(char to, char with);
00076     void replace(const char* to, const char* with);
00077 
00078     SimpleString toLower() const;
00079     SimpleString subString(size_t beginPos, size_t amount) const;
00080     SimpleString subStringFromTill(char startChar, char lastExcludedChar) const;
00081     void copyToBuffer(char* buffer, size_t bufferSize) const;
00082 
00083     const char *asCharString() const;
00084     size_t size() const;
00085     bool isEmpty() const;
00086 
00087     static void padStringsToSameLength(SimpleString& str1, SimpleString& str2, char ch);
00088 
00089     static TestMemoryAllocator* getStringAllocator();
00090     static void setStringAllocator(TestMemoryAllocator* allocator);
00091 
00092     static char* allocStringBuffer(size_t size);
00093     static char* StrNCpy(char* s1, const char* s2, size_t n);
00094     static void deallocStringBuffer(char* str);
00095 private:
00096     char *buffer_;
00097 
00098     static TestMemoryAllocator* stringAllocator_;
00099 
00100     char* getEmptyString() const; 
00101     static char* copyToNewBuffer(const char* bufferToCopy, size_t bufferSize=0);
00102 };
00103 
00104 class SimpleStringCollection
00105 {
00106 public:
00107     SimpleStringCollection();
00108     ~SimpleStringCollection();
00109 
00110     void allocate(size_t size);
00111 
00112     size_t size() const;
00113     SimpleString& operator[](size_t index);
00114 
00115 private:
00116     SimpleString* collection_;
00117     SimpleString empty_;
00118     size_t size_;
00119 
00120     void operator =(SimpleStringCollection&);
00121     SimpleStringCollection(SimpleStringCollection&);
00122 };
00123 
00124 SimpleString StringFrom(bool value);
00125 SimpleString StringFrom(const void* value);
00126 SimpleString StringFrom(char value);
00127 SimpleString StringFrom(const char *value);
00128 SimpleString StringFromOrNull(const char * value);
00129 SimpleString StringFrom(int value);
00130 SimpleString StringFrom(unsigned int value);
00131 SimpleString StringFrom(long value);
00132 SimpleString StringFrom(unsigned long value);
00133 SimpleString HexStringFrom(long value);
00134 SimpleString HexStringFrom(unsigned long value);
00135 SimpleString HexStringFrom(const void* value);
00136 SimpleString StringFrom(double value, int precision = 6);
00137 SimpleString StringFrom(const SimpleString& other);
00138 SimpleString StringFromFormat(const char* format, ...) __check_format__(printf, 1, 2);
00139 SimpleString VStringFromFormat(const char* format, va_list args);
00140 
00141 #if CPPUTEST_USE_STD_CPP_LIB
00142 
00143 #include <string>
00144 #include <stdint.h>
00145 
00146 SimpleString StringFrom(const std::string& other);
00147 
00148 #endif
00149 
00150 #endif