From 342b273234405ab76dc159d2e402bfb1ddfa1d8f Mon Sep 17 00:00:00 2001 From: Pixel Date: Mon, 3 Oct 2011 14:48:05 -0700 Subject: First commit - very basic features. --- src/BString.cc | 108 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100644 src/BString.cc (limited to 'src/BString.cc') diff --git a/src/BString.cc b/src/BString.cc new file mode 100644 index 0000000..61abb8c --- /dev/null +++ b/src/BString.cc @@ -0,0 +1,108 @@ +#include +#include + +#include "BString.h" + +void Balau::String::set(const char * fmt, va_list ap) { + char * t; + unsigned int l; +#ifdef _WIN32 + // Microsoft is stupid. + char tt[65536]; + l = _vsnprintf(tt, sizeof(tt) - 1, fmt, ap); + tt[65535] = 0; + t = ::strdup(tt); +#else + l = vasprintf(&t, fmt, ap); +#endif + assign(t, l); + free(t); +} + +int Balau::String::strchrcnt(char c) const { + unsigned int l = length(); + int r = 0; + const char * buffer = data(); + + for (unsigned int i = 0; i < l; i++) + if (buffer[i] == c) + r++; + + return r; +} + +Balau::String & Balau::String::do_ltrim() { + unsigned int l = length(), s = 0; + const char * buffer = data(); + + for (unsigned int i = 0; i < l; i++) + if (isspace(buffer[i])) + s++; + else + break; + + erase(0, s); + + return *this; +} + +Balau::String & Balau::String::do_rtrim() { + unsigned int l = length(), p = l; + const char * buffer = data(); + + for (unsigned int i = l - 1; i >= 0; i--) + if (isspace(buffer[i])) + p--; + else + break; + + erase(p); + + return *this; +} + +Balau::String & Balau::String::do_upper() { + unsigned int l = length(); + + for (unsigned int i = 0; i < l; i++) + (*this)[i] = toupper((*this)[i]); + + return *this; +} + +Balau::String & Balau::String::do_lower() { + unsigned int l = length(); + + for (unsigned int i = 0; i < l; i++) + (*this)[i] = tolower((*this)[i]); + + return *this; +} + +Balau::String & Balau::String::do_iconv(const char * from, const char * _to) { + iconv_t cd; + const String to = String(_to) + "//TRANSLIT"; + + const char * inbuf; + char * outbuf, * t; + size_t inleft, outleft; + + if ((cd = iconv_open(to.c_str(), from)) == (iconv_t) (-1)) + return *this; + + inleft = length(); + outleft = inleft * 8; + inbuf = c_str(); + t = outbuf = (char *) malloc(outleft + 1); + memset(t, 0, outleft + 1); +#ifdef HAVE_PROPER_ICONV + ::iconv(cd, &inbuf, &inleft, &outbuf, &outleft); +#else + ::iconv(cd, const_cast(&inbuf), &inleft, &outbuf, &outleft); +#endif + + assign(t, outbuf - t); + free(t); + + return *this; +} -- cgit v1.2.3