Go to the documentation of this file.
39 #include "blocxx/BLOCXX_config.h"
59 #if defined(BLOCXX_HAVE_ISTREAM) && defined(BLOCXX_HAVE_OSTREAM)
69 #define SNPRINTF _snprintf
71 #define SNPRINTF snprintf
85 strncmpi(
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 // stupid windoze...
166 int len = snprintf(tmpbuf,
sizeof(tmpbuf),
"%d", val);
168 ::snprintf(bfr.get(), len+1,
"%d", val);
169 m_buf =
new ByteBuf(bfr, len);
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)
210 OStringStream ss(33);
212 m_buf =
new ByteBuf(ss.c_str());
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 // stupid windoze
380 n = vsnprintf(p.
get(),
size, fmt, ap);
382 if (n > -1 && n <
size)
385 return static_cast<int>(
length());
397 #undef vsnprintf // stupid windoze
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());
974 template <
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)
1000 return convertToRealType<Real32>(
m_buf,
"Real32", &strtof);
1001 #elif defined(BLOCXX_REAL32_IS_DOUBLE) || (defined(BLOCXX_REAL32_IS_FLOAT) && !defined(BLOCXX_HAVE_STRTOF))
1002 return convertToRealType<Real32>(
m_buf,
"Real32", &strtod);
1009 #if defined(BLOCXX_REAL64_IS_DOUBLE)
1010 return convertToRealType<Real64>(
m_buf,
"Real64", &strtod);
1011 #elif defined(BLOCXX_REAL64_IS_LONG_DOUBLE)
1012 return convertToRealType<Real64>(
m_buf,
"Real64", &strtold);
1033 template <
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)
1061 template <
typename T>
1065 return doConvertToIntType<T, strtoulfp_t, unsigned long int>(
m_buf, msg, &strtoul, base);
1067 template <
typename T>
1071 return doConvertToIntType<T, strtolfp_t, long int>(
m_buf, msg, &strtol, base);
1073 template <
typename T>
1077 return doConvertToIntType<T, strtoullfp_t, unsigned long long int>(
m_buf, msg, &
String::strtoull, base);
1079 template <
typename T>
1089 return convertToUIntType<UInt8>(
m_buf,
"UInt8", base);
1095 return convertToIntType<Int8>(
m_buf,
"Int8", base);
1101 return convertToUIntType<UInt16>(
m_buf,
"UInt16", base);
1107 return convertToIntType<Int16>(
m_buf,
"Int16", base);
1113 return convertToUIntType<UInt32>(
m_buf,
"UInt32", base);
1119 return convertToIntType<Int32>(
m_buf,
"Int32", base);
1125 return convertToUInt64Type<UInt64>(
m_buf,
"UInt64", base);
1131 return convertToInt64Type<Int64>(
m_buf,
"Int64", base);
1137 return convertToUIntType<unsigned int>(
m_buf,
"unsigned int", base);
1143 return convertToIntType<int>(
m_buf,
"int", base);
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);
1343 #endif // #ifdef BLOCXX_HAVE_STRTOLL
1346 #ifdef BLOCXX_HAVE_STRTOULL
1347 unsigned 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
1360 unsigned 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);
1454 #endif // #ifdef BLOCXX_HAVE_STRTOULL
1516 const char* tmpChar = theStr;
1517 for (; *tmpChar && *tmpChar != c; tmpChar++)
1521 return ((*tmpChar) == c ? tmpChar : 0);
bool toLowerCaseInPlace(char *input)
Convert the UTF-8 string to lower case.
ostream & operator<<(ostream &ostrm, const Bool &arg)
Insert the string representation of a Bool object into a given stream.
unsigned long long int(* strtoullfp_t)(const char *, char **, int)
UInt8 toUInt8(int base=10) const
static int strncmpi(const char *s1, const char *s2, size_t n)
std::vector< char > & m_buf
String toLowerCase(const char *input)
Convert the UTF-8 string to lower case and return the result.
The Char16 class is an abstraction for a double byte character.
const char & operator[](size_t ndx) const
Operator [].
Int16 toInt16(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.
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.
The Bool class is an abstraction for the boolean data type.
static T convertToRealType(const String::buf_t &m_buf, const char *type, FP fp)
static unsigned long long int strtoull(const char *nptr, char **endptr, int base)
Convert a null terminated string to an unsigned 64 bit value.
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....
~String()
Destroy this String object.
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.
void swap(String &x)
Swap this instance with another.
#define BLOCXX_THROW(exType, msg)
Throw an exception using FILE and LINE.
unsigned int toUnsignedInt(int base=10) const
UInt16 toUInt16(int base=10) const
void write(std::streambuf &ostrm, void const *dataOut, size_t dataOutLen)
void readLen(std::streambuf &istrm, UInt32 &len)
UInt64 toUInt64(int base=10) const
void read(std::streambuf &istrm, void *dataIn, size_t dataInLen)
ByteBuf(const ByteBuf &arg)
char * allocateCString() const
Create a null terminated string that contains the contents of this String.
long int(* strtolfp_t)(const char *, char **, int)
Int64 toInt64(int base=10) const
size_t lastIndexOf(char ch, size_t fromIndex=npos) const
Find the last occurence of a character in this String object.
String & ltrim()
Strip all leading space characters (as defined by the C function isspace()) from this String object.
Char16 toChar16() const BLOCXX_DEPRECATED
DEPRECATED in favor of Char16::Char16(const String&) in 3.1.0.
static const char * strchr(const char *theStr, int c)
Find the first occurrence of a character in a null terminated string.
size_t charCount(const char *utf8str)
Count the number of UTF-8 chars in the string.
bool endsWith(const char *arg, EIgnoreCaseFlag ignoreCase=E_CASE_SENSITIVE) const
Determine if this String object ends with given C string.
String & rtrim()
Strip all trailing space characters (as defined by the C function isspace()) from this String object.
const char * c_str() const
ByteBuf & operator=(const ByteBuf &arg)
String & toUpperCase()
Convert this String object to upper case characters.
unsigned long int(* strtoulfp_t)(const char *, char **, int)
int toInt(int base=10) const
String()
Create a new String object with a length of 0.
static T convertToUInt64Type(const String::buf_t &m_buf, const char *msg, int base)
static String getLine(std::istream &istr)
Reads from in input stream until a newline is encountered.
static T convertToIntType(const String::buf_t &m_buf, const char *msg, int base)
void swap(COWIntrusiveReference &rhs)
String & trim()
Strip all leading and trailing space characters (as defined by the C function isspace()) from this St...
void append(const T &x)
Append an element to the end of the Array.
static const char cnullChar
Int8 toInt8(int base=10) const
String & erase()
Delete all the characters of the string.
ByteBuf(AutoPtrVec< char > &s, size_t len)
String & operator=(const String &arg)
Assignment operator.
static long long int strtoll(const char *nptr, char **endptr, int base)
Convert a null terminated string to an signed 64 bit value.
static void throwStringConversion(const String::buf_t &m_buf, const char *type)
void writeLen(std::streambuf &ostrm, UInt32 len)
static T convertToInt64Type(const String::buf_t &m_buf, const char *msg, int base)
static T convertToUIntType(const String::buf_t &m_buf, const char *msg, int base)
Int32 toInt32(int base=10) const
String & toLowerCase()
Convert this String object to lower case characters.
Char16 operator+(const Char16 &arg1, const Char16 &arg2)
The AutoPtrVec class provides a simple class for smart pointers to a dynamically allocated array of o...
#define BLOCXX_DEFINE_EXCEPTION_WITH_ID(NAME)
Define a new exception class named <NAME>Exception that derives from Exception.
size_t UTF8Length() const
If the string is not valid UTF-8, then the result of this function is undefined.
void writeObject(std::streambuf &ostrm) const
Write this String object to the given ostream.
int compareTo(const String &arg) const
Compare another String object with this one.
size_t indexOf(char ch, size_t fromIndex=0) const
Find the first occurence of a given character in this String object.
void readObject(std::streambuf &istrm)
Read this String object from the given istream.
bool toUpperCaseInPlace(char *input)
Convert the UTF-8 string to upper case.
String toUpperCase(const char *input)
Convert the UTF-8 string to upper case and return the result.
String & concat(const char *arg)
Append a C string to this String object.
int format(const char *fmt,...)
Format this string according to the given format and variable argument list (printf style)
UInt32 toUInt32(int base=10) const
char charAt(size_t ndx) const
Get the character at a specified index.
static T doConvertToIntType(const String::buf_t &m_buf, const char *type, FP fp, int base)
const char * c_str() const
long long int(* strtollfp_t)(const char *, char **, int)
Array<> wraps std::vector<> in COWReference<> adding ref counting and copy on write capability.
#define BLOCXX_ASSERT(CON)
BLOCXX_ASSERT works similar to the assert() macro, but instead of calling abort(),...
bool equalsIgnoreCase(const String &arg) const
Determine if another String object is equal to this String object, ignoring case in the comparision.
int compareToIgnoreCase(const String &arg) const
Compare another string with this one ignoring case.
bool equals(const String &arg) const
Determine if another String object is equal to this String object.
String substring(size_t beginIndex, size_t length=npos) const
Create another String object that is comprised of a substring of this String object.