39 #include "blocxx/BLOCXX_config.h"
46 #if defined(BLOCXX_HAVE_ISTREAM) && defined(BLOCXX_HAVE_OSTREAM)
58 #if defined(BLOCXX_AIX)
64 m_allocated(allocSize > 0 ? allocSize : BLOCXX_DEFAULT_ALLOCATION_UNIT),
65 m_bfr(new char[m_allocated])
70 StringBuffer::StringBuffer(
const char* arg) :
72 m_allocated(m_len + BLOCXX_DEFAULT_ALLOCATION_UNIT),
73 m_bfr(new char[m_allocated])
78 StringBuffer::StringBuffer(
const String& arg) :
80 m_allocated(m_len + BLOCXX_DEFAULT_ALLOCATION_UNIT),
81 m_bfr(new char[m_allocated])
83 ::strcpy(m_bfr, arg.c_str());
86 StringBuffer::StringBuffer(
const StringBuffer& arg) :
87 m_len(arg.m_len), m_allocated(arg.m_allocated),
88 m_bfr(new char[arg.m_allocated])
90 ::memmove(m_bfr, arg.m_bfr, arg.m_len + 1);
94 StringBuffer::operator= (
const String& arg)
96 StringBuffer(arg).swap(*
this);
101 StringBuffer::operator= (
const char* str)
123 StringBuffer::reset()
131 StringBuffer::truncate(
size_t index)
142 StringBuffer::operator[] (
size_t ndx)
const
144 return (ndx > m_len) ? 0 : m_bfr[ndx];
149 StringBuffer::operator += (
Bool v)
153 #if defined(BLOCXX_WIN32)
154 #define snprintf _snprintf // stupid windoze...
158 StringBuffer::operator += (
UInt8 v)
161 ::snprintf(bfr,
sizeof(bfr),
"%u", UInt32(v));
166 StringBuffer::operator += (
Int8 v)
169 ::snprintf(bfr,
sizeof(bfr),
"%d", Int32(v));
174 StringBuffer::operator += (UInt16 v)
177 ::snprintf(bfr,
sizeof(bfr),
"%u", UInt32(v));
182 StringBuffer::operator += (Int16 v)
185 ::snprintf(bfr,
sizeof(bfr),
"%d", Int32(v));
190 StringBuffer::operator += (UInt32 v)
193 ::snprintf(bfr,
sizeof(bfr),
"%u", v);
198 StringBuffer::operator += (Int32 v)
201 ::snprintf(bfr,
sizeof(bfr),
"%d", v);
204 #if defined(BLOCXX_INT32_IS_INT) && defined(BLOCXX_INT64_IS_LONG_LONG)
207 StringBuffer::operator += (
unsigned long v)
210 ::snprintf(bfr,
sizeof(bfr),
"%lu", v);
215 StringBuffer::operator += (
long v)
218 ::snprintf(bfr,
sizeof(bfr),
"%ld", v);
224 StringBuffer::operator += (UInt64 v)
227 #if BLOCXX_SIZEOF_LONG_INT == 8
228 ::snprintf(bfr,
sizeof(bfr),
"%lu", v);
230 ::snprintf(bfr,
sizeof(bfr),
"%llu", v);
236 StringBuffer::operator += (Int64 v)
239 #if BLOCXX_SIZEOF_LONG_INT == 8
240 ::snprintf(bfr,
sizeof(bfr),
"%ld", v);
242 ::snprintf(bfr,
sizeof(bfr),
"%lld", v);
249 StringBuffer::operator += (Real32 v)
253 #if defined(BLOCXX_REAL32_IS_FLOAT)
254 ::snprintf(bfr,
sizeof(bfr),
"%.*g", FLT_MANT_DIG * 3 / 10 + 1,
static_cast<double>(v));
255 #elif defined(BLOCXX_REAL32_IS_DOUBLE)
256 ::snprintf(bfr,
sizeof(bfr),
"%.*g", DBL_MANT_DIG * 3 / 10 + 1, v);
259 #error "The formula for computing the number of digits of precision for a floating point needs to be implmented. It's ceiling(bits * log(FLT_RADIX) / log(10))"
265 StringBuffer::operator += (Real64 v)
269 #if defined(BLOCXX_REAL64_IS_DOUBLE)
270 ::snprintf(bfr,
sizeof(bfr),
"%.*g", DBL_MANT_DIG * 3 / 10 + 1, v);
271 #elif defined(BLOCXX_REAL64_IS_LONG_DOUBLE)
272 ::snprintf(bfr,
sizeof(bfr),
"%.*Lg", LDBL_MANT_DIG * 3 / 10 + 1, v);
275 #error "The formula for computing the number of digits of precision for a floating point needs to be implmented. It's ceiling(bits * log(FLT_RADIX) / log(10))"
279 #if defined(BLOCXX_WIN32)
284 StringBuffer::append(
const char* str,
const size_t len)
287 ::strncpy(m_bfr+m_len, str, len);
294 StringBuffer::equals(
const char* arg)
const
296 return ::strcmp(arg, m_bfr) == 0;
303 return ::strcmp(arg.
m_bfr, m_bfr) == 0;
308 StringBuffer::endsWith(
char ch)
const
310 return (m_len && m_bfr[m_len-1] == ch);
315 StringBuffer::startsWith(
char ch)
const
317 return (m_len && m_bfr[0] == ch);
336 while (m_len && isspace(m_bfr[m_len-1]))
344 while (*p && isspace(*p))
351 m_len -= (p - m_bfr);
352 memmove(m_bfr, p, m_len+1);
362 StringBuffer::getLine(std::istream& is,
bool resetBuffer)
372 std::streambuf *sb = is.rdbuf();
376 int ch = sb->sbumpc();
379 is.setstate(count == 0
380 ? (std::ios::failbit | std::ios::eofbit) : std::ios::eofbit);
391 append(
static_cast<char>(ch));
395 const char* p = ::strchr(m_bfr,
'\r');
398 truncate(
size_t(p-m_bfr));
405 std::ostream&
operator<<(std::ostream& ostr,
const StringBuffer& b)
407 ostr.write(b.c_str(), b.length());
412 bool operator==(
const StringBuffer& x,
const StringBuffer& y)
418 bool operator!=(
const StringBuffer& x,
const StringBuffer& y)
424 bool operator==(
const StringBuffer& x,
const String& y)
426 return x.equals(y.c_str());
430 bool operator!=(
const StringBuffer& x,
const String& y)
436 bool operator==(
const String& x,
const StringBuffer& y)
438 return x.equals(y.c_str());