1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
|
/*
Copyright (C) 2005-2007 Feeling Software Inc.
Portions of the code are:
Copyright (C) 2005-2007 Sony Computer Entertainment America
MIT License: http://www.opensource.org/licenses/mit-license.php
*/
/*
Based on the FS Import classes:
Copyright (C) 2005-2006 Feeling Software Inc
Copyright (C) 2005-2006 Autodesk Media Entertainment
MIT License: http://www.opensource.org/licenses/mit-license.php
*/
/**
@file FUStringBuilder.h
This file contains the FUStringBuilderT template class,
its defined template classes and its helper classes.
*/
#ifndef _FCU_STRING_BUILDER_
#define _FCU_STRING_BUILDER_
/**
A dynamically-sized string object.
The template has two arguments: the character definition and
the sprintf() functor class for float to string conversions.
This class should be used for all the string operations, as it contains a
dynamically-resized buffer that is not directly tied to its content's length.
@ingroup FUtils
*/
template <class Char>
class FCOLLADA_EXPORT FUStringBuilderT
{
private:
Char* buffer;
size_t reserved;
size_t size;
public:
/** The standard string object which correspond to the builder. */
typedef fm::stringT<Char> String;
/** Creates a new builder with the content of the given string.
@param sz A string. Its content will be copied within the builder. */
FUStringBuilderT(const String& sz);
/** Creates a new builder with the content of the given character array.
@param sz A character array. Its content will be copied within the builder.
It must terminate with an element containing the 'zero' value. */
FUStringBuilderT(const Char* sz);
/** Creates a new builder with the given character repeated multiple times over the array.
@param ch A character to repeat.
@param count The number of times to repeat the given character. */
FUStringBuilderT(Char ch, size_t count);
/** Creates a new builder with an empty buffer.
@see reserve
@param reserved The number of character slots to reserve within the empty buffer. */
FUStringBuilderT(size_t reserved);
/** Creates a new builder with an empty buffer. */
FUStringBuilderT();
/** Deletes the builder. Its buffer will be cleared.
Any pointers to its data will be dangling. */
~FUStringBuilderT();
/** Reserves a given number of character slots.
If the builder has a buffer with a different number of character slots, a new
buffer will be allocated. If the builder has contents, it will be copied within
the new buffer. If there is more content than the new buffer can handle, it will
be discarded.
@param length The number of character slots to reserve. */
void reserve(size_t length);
/** Retrieves the length of the content within the builder.
@return The length of the string. */
inline size_t length() { return size; }
/** Clears the content of the builder.
This does not re-allocate a new buffer. */
void clear();
/** Retrieves whether the builder is empty.
A builder is considered empty when it has no content, regardless of
the size or allocation status of its buffer.
@return Whether the builder is empty. */
inline bool empty() const { return size == 0; }
/** Appends a character to the content of the builder.
@param c A character. May not be the 'zero' value. */
void append(Char c);
/** Appends a string to the content of the builder.
@param sz A string. */
void append(const String& sz);
/** Appends a character array to the content of the builder.
@param sz A character array. It must terminate with an
element containing the 'zero' value. */
void append(const Char* sz);
/** Appends a character array to the content of the builder.
@param sz A character array. It should not contain any 'zero'
characters before 'len'
@param len The number of characters to read from sz. */
void append(const Char* sz, size_t len);
/** Appends the content of a builder to the content of this builder.
@param b A string builder. */
void append(const FUStringBuilderT& b);
/** Appends the integer value, after converting it to a string,
to the content of the builder.
@param i An integer value. */
void append(const int32 i);
void append(const uint32 i); /**< See above. */
void append(const uint64 i); /**< See above. */
/** Appends the integer value, after converting it to a
fm::string, in hexadecimal, to the content of the builder.
The size of the integer will determine the number of characters used.
@param i An unsigned integer value. */
void appendHex(uint8 i);
template <class T> inline void appendHex(const T& i) { for (size_t j = 0; j < sizeof(T); ++j) appendHex(*(((uint8*)&i) + j)); } /**< See above. */
#if defined(WIN32)
inline void append(int i) { append((int32) i); } /**< See above. */
#ifdef _W64
inline void append(_W64 unsigned int i) { append((uint32) i); } /**< See above. */
#else
inline void append(unsigned int i) { append((uint32) i); } /**< See above. */
#endif
#endif // defined(WIN32)
/** Appends the floating-point value, after converting it to a string,
to the content of the builder. If the floating-point value is the special token
that represents infinity, the string "INF" is appended. If it represents
the negative infinity, the string "-INF" is appended. If it represents the
impossibility, the string "NaN" is appended.
@param f A floating-point value. */
void append(float f);
void append(double f); /**< See above. */
/** Appends a vector to the content of the builder.
@param v A vector. */
void append(const FMVector2& v);
void append(const FMVector3& v); /**< See above. */
void append(const FMVector4& v); /**< See above. */
/** Appends a value to the content of the builder.
This is a shortcut for the append function.
@see append
@param val A value. This may be numerical, a character, a character array or a string. */
template<typename TYPE> inline FUStringBuilderT& operator+=(const TYPE& val) { append(val); return *this; }
/** Appends a character array to the content of the builder.
A newline character will be appended after the character array.
@param sz A character array. It must terminate with an
element containing the 'zero' value. */
void appendLine(const Char* sz);
/** Removes a section of the content of the builder.
Every character that occurs after the given index will be removed,
resulting in a shrunk string.
@param start An index within the content of the builder. */
void remove(int32 start);
/** Removes a section of the content of the builder.
The substring defined by the 'start' and 'end' indices will be
removed. The 'start' character is removed and is replaced by
the 'end' character.
@param start The index of the first character of the substring to remove.
@param end The index of the first character after the removed substring. */
void remove(int32 start, int32 end);
/** Removes the last character of the content of the builder. */
inline void pop_back() { if (size > 0) --size; }
/** Sets the content of the builder to a given value.
This clears the builder of all its content and appends the given value.
@param val A value. This may be numerical, a character, a character array or a string. */
template<typename TYPE> inline void set(const TYPE& val) { clear(); append(val); }
template<typename TYPE> inline FUStringBuilderT& operator=(const TYPE& val) { clear(); append(val); return *this; } /**< See above. */
/** Converts the content of the builder to a standard string.
@return A string with the content of the builder. */
String ToString() const;
/** Converts the content of the builder to a character array.
@return A character array with the content of the builder.
This pointer is valid for the lifetime of the buffer of the builder, so
do not keep it around. This character array should not be modified. */
const Char* ToCharPtr() const;
inline operator const Char*() const { return ToCharPtr(); } /**< See above. */
/** Retrieves the index of the first character within the content of the builder
that is equivalent to the given character.
@param c The character to match.
@return The index of the first equivalent character. -1 is returned if no
character matches the given character. */
int32 index(Char c) const;
/** Retrieves the index of the last character within the content of the builder
that is equivalent to the given character.
@param c The character to match.
@return The index of the last equivalent character. -1 is returned if no
character matches the given character. */
int32 rindex(Char c) const;
/** Retrieves the last character within the content of the builder.
@return The last character of the builder. */
Char back() const { FUAssert(size > 0, return (Char) 0); return buffer[size-1]; }
private:
void enlarge(size_t minimum);
};
typedef FUStringBuilderT<fchar> FUStringBuilder; /**< A Unicode string builder. */
typedef FUStringBuilderT<char> FUSStringBuilder; /**< A 8-bit string builder. */
#if defined(__APPLE__)
#include "FUtils/FUStringBuilder.hpp"
#endif // __APPLE__
#endif // _FCU_STRING_BUILDER_
|