39#include "blocxx/BLOCXX_config.h"
59#if defined(BLOCXX_HAVE_ISTREAM) && defined(BLOCXX_HAVE_OSTREAM)
69#define SNPRINTF _snprintf
71#define SNPRINTF snprintf
85strncmpi(
const char* s1,
const char* s2,
size_t n)
119 char* buf =
new char[arg.
m_len+1];
120 strcpy(buf, arg.
m_buf);
135#if defined(BLOCXX_AIX)
158#if defined(BLOCXX_WIN32)
159#define snprintf _snprintf
166 int len = snprintf(tmpbuf,
sizeof(tmpbuf),
"%d", val);
168 ::snprintf(bfr.
get(), len+1,
"%d", val);
176 int len = ::snprintf(tmpbuf,
sizeof(tmpbuf),
"%u", val);
178 ::snprintf(bfr.
get(), len+1,
"%u", val);
181#if defined(BLOCXX_INT32_IS_INT) && defined(BLOCXX_INT64_IS_LONG_LONG)
187 int len = snprintf(tmpbuf,
sizeof(tmpbuf),
"%ld", val);
189 ::snprintf(bfr.get(), len+1,
"%ld", val);
190 m_buf =
new ByteBuf(bfr, len);
197 int len = ::snprintf(tmpbuf,
sizeof(tmpbuf),
"%lu", val);
198 AutoPtrVec<char> bfr(
new char[len+1]);
199 ::snprintf(bfr.get(), len+1,
"%lu", val);
200 m_buf =
new ByteBuf(bfr, len);
203#if defined(BLOCXX_WIN32)
218#if defined(BLOCXX_INT64_IS_LONG)
220 ::snprintf(tmpbuf,
sizeof(tmpbuf),
"%lu", val);
222#elif defined(BLOCXX_INT64_IS_LONG_LONG)
237#if defined(BLOCXX_REAL32_IS_FLOAT)
238 ::SNPRINTF(tmpbuf,
sizeof(tmpbuf),
"%.*g", FLT_MANT_DIG * 3 / 10 + 1,
static_cast<double>(val));
239#elif defined(BLOCXX_REAL32_IS_DOUBLE)
240 ::SNPRINTF(tmpbuf,
sizeof(tmpbuf),
"%.*g", DBL_MANT_DIG * 3 / 10 + 1, val);
243#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))"
253#if defined(BLOCXX_REAL64_IS_DOUBLE)
254 ::SNPRINTF(tmpbuf,
sizeof(tmpbuf),
"%.*g", DBL_MANT_DIG * 3 / 10 + 1, val);
255#elif defined(BLOCXX_REAL64_IS_LONG_DOUBLE)
256 ::SNPRINTF(tmpbuf,
sizeof(tmpbuf),
"%.*Lg", LDBL_MANT_DIG * 3 / 10 + 1, val);
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))"
288 ::memcpy(bfr.
get(), str, len);
302 size_t sz = ra.
size();
306 for (
size_t i = 0;
i < sz;
i++)
308 buf += ra[
i].toString();
347 size_t len =
length() + 1;
348 char* str =
static_cast<char*
>(malloc(len));
349 ::strcpy(str,
c_str());
366#define vsnprintf _vsnprintf
380 n = vsnprintf(p.
get(),
size, fmt, ap);
382 if (n > -1 && n <
size)
385 return static_cast<int>(
length());
409 const char* lhs =
"";
414 return ::strcmp(lhs, arg);
426 const char* lhs =
"";
446 size_t len =
length() + ::strlen(arg);
453 ::strcat(bfr.
get(), arg);
463 size_t newlen =
length() + 1;
471 *(bfr.
get()+newlen) = 0;
499 int ndx =
static_cast<int>(
length() - ::strlen(arg));
507 : (::strcmp(
m_buf->
data()+ndx, arg) == 0);
539 for (
size_t i = 0;
i < len;
i++)
544 hash = (hash << 4) + (temp * 13);
545 UInt32 g = hash & 0xf0000000;
587 p = ::strstr(
m_buf->
data()+fromIndex, arg);
596 cc =
static_cast<size_t>(p -
m_buf->
data());
605 if (fromIndex ==
npos)
615 for (
size_t i = fromIndex;
i !=
npos;
i--)
632 if (fromIndex ==
npos || fromIndex >=
length())
634 if (
static_cast<int>(fromIndex =
length()-1) < 0)
640 int arglen = (arg) ? ::strlen(arg) : 0;
641 if (
static_cast<int>(fromIndex -= arglen - 1) < 0)
649 while (fromIndex !=
npos)
653 if (::strncmp(
m_buf->
data()+fromIndex, arg, arglen) == 0)
680 size_t arglen = ::strlen(arg);
691 cc = (::strncmp(
m_buf->
data(), arg, arglen) == 0);
715 else if (len ==
npos)
717 count = l - beginIndex;
719 if (count + beginIndex > l)
721 count = l - beginIndex;
725 return String(
static_cast<const char*
>(
m_buf->
data()+beginIndex), count);
736 while (isspace(*p) && *p !=
'\0')
751 while (isspace(*s1) && *s1 !=
'\0')
771 while (isspace(*s1) && s1 >=
m_buf->
data())
779 if (s1 < m_buf->data())
797 while (isspace(*s1) && *s1 !=
'\0')
808 const char* s2 = p2 - 1;
818 size_t len = (s2 - s1) + 1;
889 UInt32 len =
static_cast<UInt32
>(
length());
974template <
typename T,
typename FP>
982 T rv = fp(
m_buf->data(), &endptr);
983 if (*endptr !=
'\0' || errno == ERANGE || rv == HUGE_VAL || rv == -HUGE_VAL)
999#if defined(BLOCXX_REAL32_IS_FLOAT) && defined(BLOCXX_HAVE_STRTOF)
1001#elif defined(BLOCXX_REAL32_IS_DOUBLE) || (defined(BLOCXX_REAL32_IS_FLOAT) && !defined(BLOCXX_HAVE_STRTOF))
1009#if defined(BLOCXX_REAL64_IS_DOUBLE)
1011#elif defined(BLOCXX_REAL64_IS_LONG_DOUBLE)
1033template <
typename T,
typename FP,
typename FPRT>
1043 FPRT v = fp(
m_buf->data(), &endptr, base);
1044 T rv =
static_cast<T
>(v);
1045 if (*endptr !=
'\0' || errno == ERANGE || FPRT(rv) != v)
1061template <
typename T>
1067template <
typename T>
1073template <
typename T>
1079template <
typename T>
1165 bool last_was_delim =
false;
1184 last_was_delim =
true;
1188 last_was_delim =
false;
1202#ifdef BLOCXX_HAVE_STRTOLL
1206 return ::strtoll(nptr, endptr, base);
1210#if BLOCXX_SIZEOF_LONG_LONG_INT == 8
1211#define LLONG_MAX 9223372036854775807LL
1213#define LLONG_MAX 2147483647LL
1215#define LLONG_MIN (-LLONG_MAX - 1LL)
1221 long long acc, cutoff;
1223 int neg, any, cutlim;
1230 c = (
unsigned char) *s++;
1231 }
while (isspace(c));
1245 if ((base == 0 || base == 16)
1247 && (*s ==
'x' || *s ==
'X'))
1255 base = c ==
'0' ? 8 : 10;
1273 cutlim =
static_cast<int>(cutoff % base);
1284 for (acc = 0, any = 0;; c = (
unsigned char) *s++)
1290 else if (isalpha(c))
1292 c -= isupper(c) ?
'A' - 10 :
'a' - 10;
1308 if (acc < cutoff || acc == cutoff && c > cutlim)
1323 if (acc > cutoff || acc == cutoff && c > cutlim)
1339 *endptr = (
char *) (any ? s - 1 : nptr);
1346#ifdef BLOCXX_HAVE_STRTOULL
1347unsigned long long int
1350 return ::strtoull(nptr, endptr, base);
1354#if BLOCXX_SIZEOF_LONG_LONG_INT == 8
1355#define ULLONG_MAX 18446744073709551615ULL
1357#define ULLONG_MAX 4294967295ULL
1360unsigned long long int
1364 unsigned long long acc, cutoff, cutlim;
1370 c = (
unsigned char) *s++;
1371 }
while (isspace(c));
1385 if ((base == 0 || base == 16)
1387 && (*s ==
'x' || *s ==
'X'))
1395 base = c ==
'0' ? 8 : 10;
1397 cutoff =
ULLONG_MAX / (
unsigned long long)base;
1398 cutlim =
ULLONG_MAX % (
unsigned long long)base;
1399 for (acc = 0, any = 0;; c = (
unsigned char) *s++)
1405 else if (isalpha(c))
1407 c -= isupper(c) ?
'A' - 10 :
'a' - 10;
1413 if (c >= (
unsigned int)base)
1421 if (acc > cutoff || acc == cutoff && c > cutlim)
1430 acc *= (
unsigned long)base;
1437#pragma warning (push)
1438#pragma warning (disable: 4146)
1444#pragma warning (pop)
1450 *endptr = (
char *) (any ? s - 1 : nptr);
1516 const char* tmpChar = theStr;
1517 for (; *tmpChar && *tmpChar != c; tmpChar++)
1521 return ((*tmpChar) == c ? tmpChar : 0);
#define BLOCXX_ASSERT(CON)
BLOCXX_ASSERT works similar to the assert() macro, but instead of calling abort(),...
#define BLOCXX_DEFINE_EXCEPTION_WITH_ID(NAME)
Define a new exception class named <NAME>Exception that derives from Exception.
#define BLOCXX_THROW(exType, msg)
Throw an exception using FILE and LINE.
std::vector< char > & m_buf
Array<> wraps std::vector<> in COWReference<> adding ref counting and copy on write capability.
void append(const T &x)
Append an element to the end of the Array.
The AutoPtrVec class provides a simple class for smart pointers to a dynamically allocated array of o...
The Bool class is an abstraction for the boolean data type.
void swap(COWIntrusiveReference &rhs)
The Char16 class is an abstraction for a double byte character.
const char * c_str() const
ByteBuf(const ByteBuf &arg)
ByteBuf & operator=(const ByteBuf &arg)
ByteBuf(AutoPtrVec< char > &s, size_t len)
const char * getLine(std::istream &is, bool resetBuffer=true)
Fill this StringBuffer object with the next line from the given input stream.
This String class is an abstract data type that represents as NULL terminated string of characters.
bool equalsIgnoreCase(const String &arg) const
Determine if another String object is equal to this String object, ignoring case in the comparision.
const char * c_str() const
String & operator=(const String &arg)
Assignment operator.
void swap(String &x)
Swap this instance with another.
String & concat(const char *arg)
Append a C string to this String object.
String & erase()
Delete all the characters of the string.
static String getLine(std::istream &istr)
Reads from in input stream until a newline is encountered.
unsigned int toUnsignedInt(int base=10) const
int compareToIgnoreCase(const String &arg) const
Compare another string with this one ignoring case.
~String()
Destroy this String object.
UInt32 toUInt32(int base=10) const
static unsigned long long int strtoull(const char *nptr, char **endptr, int base)
Convert a null terminated string to an unsigned 64 bit value.
String()
Create a new String object with a length of 0.
String substring(size_t beginIndex, size_t length=npos) const
Create another String object that is comprised of a substring of this String object.
int format(const char *fmt,...)
Format this string according to the given format and variable argument list (printf style)
UInt8 toUInt8(int base=10) const
int toInt(int base=10) const
bool equals(const String &arg) const
Determine if another String object is equal to this String object.
static const char * strchr(const char *theStr, int c)
Find the first occurrence of a character in a null terminated string.
Char16 toChar16() const BLOCXX_DEPRECATED
DEPRECATED in favor of Char16::Char16(const String&) in 3.1.0.
String & toLowerCase()
Convert this String object to lower case characters.
void writeObject(std::streambuf &ostrm) const
Write this String object to the given ostream.
char * allocateCString() const
Create a null terminated string that contains the contents of this String.
String & trim()
Strip all leading and trailing space characters (as defined by the C function isspace()) from this St...
StringArray tokenize(const char *delims=" \n\r\t\v", EReturnDelimitersFlag returnDelimitersAsTokens=E_DISCARD_DELIMITERS, EEmptyTokenReturnFlag returnEmptyTokens=E_SKIP_EMPTY_TOKENS) const
Tokenize this String object using the given delimeters.
Int32 toInt32(int base=10) const
Int64 toInt64(int base=10) const
bool startsWith(const char *arg, EIgnoreCaseFlag ignoreCase=E_CASE_SENSITIVE) const
Determine if this String object starts with a given substring.
Int16 toInt16(int base=10) const
size_t indexOf(char ch, size_t fromIndex=0) const
Find the first occurence of a given character in this String object.
UInt16 toUInt16(int base=10) const
UInt64 toUInt64(int base=10) const
String & rtrim()
Strip all trailing space characters (as defined by the C function isspace()) from this String object.
size_t UTF8Length() const
If the string is not valid UTF-8, then the result of this function is undefined.
size_t lastIndexOf(char ch, size_t fromIndex=npos) const
Find the last occurence of a character in this String object.
static long long int strtoll(const char *nptr, char **endptr, int base)
Convert a null terminated string to an signed 64 bit value.
int compareTo(const String &arg) const
Compare another String object with this one.
Int8 toInt8(int base=10) const
String & toUpperCase()
Convert this String object to upper case characters.
char charAt(size_t ndx) const
Get the character at a specified index.
const char & operator[](size_t ndx) const
Operator [].
bool endsWith(const char *arg, EIgnoreCaseFlag ignoreCase=E_CASE_SENSITIVE) const
Determine if this String object ends with given C string.
String & ltrim()
Strip all leading space characters (as defined by the C function isspace()) from this String object.
void readObject(std::streambuf &istrm)
Read this String object from the given istream.
void readLen(std::streambuf &istrm, UInt32 &len)
void read(std::streambuf &istrm, void *dataIn, size_t dataInLen)
void write(std::streambuf &ostrm, void const *dataOut, size_t dataOutLen)
void writeLen(std::streambuf &ostrm, UInt32 len)
String toUpperCase(const char *input)
Convert the UTF-8 string to upper case and return the result.
BLOCXX_COMMON_API int compareToIgnoreCase(const char *str1, const char *str2)
Compares 2 UTF-8 strings, ignoring any case differences as defined by the Unicode spec CaseFolding....
bool toUpperCaseInPlace(char *input)
Convert the UTF-8 string to upper case.
String toLowerCase(const char *input)
Convert the UTF-8 string to lower case and return the result.
size_t charCount(const char *utf8str)
Count the number of UTF-8 chars in the string.
bool toLowerCaseInPlace(char *input)
Convert the UTF-8 string to lower case.
Char16 operator+(const Char16 &arg1, const Char16 &arg2)
unsigned long long int(* strtoullfp_t)(const char *, char **, int)
static int strncmpi(const char *s1, const char *s2, size_t n)
static T convertToUIntType(const String::buf_t &m_buf, const char *msg, int base)
static T convertToInt64Type(const String::buf_t &m_buf, const char *msg, int base)
unsigned long int(* strtoulfp_t)(const char *, char **, int)
long long int(* strtollfp_t)(const char *, char **, int)
long int(* strtolfp_t)(const char *, char **, int)
static const char cnullChar
static T doConvertToIntType(const String::buf_t &m_buf, const char *type, FP fp, int base)
ostream & operator<<(ostream &ostrm, const Bool &arg)
static T convertToUInt64Type(const String::buf_t &m_buf, const char *msg, int base)
static void throwStringConversion(const String::buf_t &m_buf, const char *type)
static T convertToIntType(const String::buf_t &m_buf, const char *msg, int base)
static T convertToRealType(const String::buf_t &m_buf, const char *type, FP fp)