diff options
Diffstat (limited to 'lib/String.cc')
-rw-r--r-- | lib/String.cc | 111 |
1 files changed, 58 insertions, 53 deletions
diff --git a/lib/String.cc b/lib/String.cc index c4fbaf9..ec78c22 100644 --- a/lib/String.cc +++ b/lib/String.cc @@ -5,6 +5,9 @@ #ifdef HAVE_CONFIG_H #include "config.h" #endif +#ifdef HAVE_GMP +#include <gmpxx.h> +#endif #include "BString.h" #include "Exceptions.h" @@ -21,18 +24,19 @@ String::String(const String & s) : str(Base::strdup(s.str)), siz(s.siz) { #ifdef DEBUG fprintf(stderr, "Duplicating string `%s', from %p to %p, from this %p to this %p\n", str, s.str, str, s, this); #endif - ostr = Base::strdup(str); } String::String(char c) : siz(1) { - char * t = (char *) malloc(2); - #ifdef DEBUG fprintf(stderr, "Creating a string with `%c' at %p, this = %p\n", c, str, this); #endif +#ifndef HAVE_ASPRINTF + char * t = (char *) malloc(2); sprintf(t, "%c", c); str = t; - ostr = Base::strdup(str); +#else + asprintf(&str, "%c", c); +#endif } String::String(const char * s) : str(Base::strdup(s)), siz(::strlen(str)) { @@ -42,121 +46,114 @@ String::String(const char * s) : str(Base::strdup(s)), siz(::strlen(str)) { #ifdef DEBUG fprintf(stderr, "Creating a string with `%s' at %p from %p, this = %p\n", str, str, s, this); #endif - ostr = Base::strdup(str); } -#if 0 -String::String(const char * s, ...) { - va_list ap; - -#ifdef DEBUG - fprintf(stderr, "Creating a String with s = '%s'\n", s); -#endif - - va_start(ap, s); -#ifdef HAVE_VSNPRINTF - vsnprintf(t, BUFSIZ, s, ap); -#else - vsprintf(t, s, ap); -#endif - str = Base::strdup(t); - va_end(ap); - siz = ::strlen(str); -} -#endif - String::String(int hs, char * s) : str(Base::strdup(s ? s : "")), siz(hs) { #ifdef DEBUG fprintf(stderr, "Fast-Creating a string with `%s' at %p from %p, this = %p\n", str, str, s, this); #endif - ostr = Base::strdup(str); } String::String(int i) { +#ifndef HAVE_ASPRINTF char t[20]; - sprintf(t, "%i", i); str = Base::strdup(t); +#else + asprintf(&str, "%i", i); +#endif siz = ::strlen(str); } String::String(unsigned int i) { +#ifndef HAVE_ASPRINTF char t[20]; - sprintf(t, "%u", i); str = Base::strdup(t); +#else + asprintf(&str, "%u", i); +#endif siz = ::strlen(str); } String::String(int64 l) { +#ifndef HAVE_ASPRINTF char t[40]; - sprintf(t, "%lld", l); str = Base::strdup(t); +#else + asprintf(&str, "%lld", l); +#endif siz = ::strlen(str); } String::String(uint64 l) { +#ifndef HAVE_ASPRINTF char t[40]; - sprintf(t, "%llu", l); str = Base::strdup(t); +#else + asprintf(&str, "%llu", l); +#endif siz = ::strlen(str); } String::String(double d) { +#ifndef HAVE_ASPRINTF char t[30]; - sprintf(t, "%g", d); str = Base::strdup(t); +#else + asprintf(&str, "%g", d); +#endif siz = ::strlen(str); } String::~String() { #ifdef DEBUG fprintf(stderr, "Destroying string @ %p, freeing %p.\n", this, str); - fprintf(stderr, "Destroying string `%s'\n", ostr); #endif free(str); } -const char * String::set(const char * s, ...) { - va_list ap; - +const char * String::set(const char * s, va_list ap) { + const char * r; + free(str); if (!s) { - free(str); str = Base::strdup(""); - t[0] = 0; - return t; + return str; } - va_start(ap, s); +#ifdef HAVE_VASPRINTF + vasprintf(&str, s, ap); + r = str; #ifdef HAVE_VSNPRINTF vsnprintf(t, BUFSIZ, s, ap); #else vsprintf(t, s, ap); #endif - free(str); - str = Base::strdup(t); - va_end(ap); + str = Base::strdup(r = t); +#endif siz = ::strlen(str); - return t; + return r; } -const char * String::set(const ugly_string & s, ...) { +const char * String::set(const char * s, ...) { + const char * r; va_list ap; + va_start(ap, s); + r = set(s, ap); + va_end(ap); + return r; +} +const char * String::set(const ugly_string & s, ...) { + const char * r; + va_list ap; va_start(ap, s); -#ifdef HAVE_VSNPRINTF - vsnprintf(t, BUFSIZ, s.p, ap); -#else - vsprintf(t, s.p, ap); -#endif - free(str); - str = Base::strdup(t); + r = set(s.p, ap); va_end(ap); - siz = ::strlen(str); - return t; + return r; } int String::scanf(const char * s, ...) const { @@ -164,7 +161,11 @@ int String::scanf(const char * s, ...) const { int t; va_start(ap, s); +#ifdef HAVE_GMP + t = gmp_vsscanf(str, s, ap); +#else t = vsscanf(str, s, ap); +#endif va_end(ap); return t; } @@ -174,7 +175,11 @@ int String::scanf(const ugly_string & s, ...) const { int t; va_start(ap, s); +#ifdef HAVE_GMP + t = gmp_vsscanf(str, s.p, ap); +#else t = vsscanf(str, s.p, ap); +#endif va_end(ap); return t; } |