diff options
Diffstat (limited to 'lib/String.cc')
-rw-r--r-- | lib/String.cc | 83 |
1 files changed, 45 insertions, 38 deletions
diff --git a/lib/String.cc b/lib/String.cc index 16c7db3..0e22de0 100644 --- a/lib/String.cc +++ b/lib/String.cc @@ -12,29 +12,35 @@ extern "C" { char String::t[BUFSIZ]; -String::String(const String & s) : str(strdup(s.str)) { } +String::String(const String & s) : str(::strdup(s.str)), siz(s.siz) { } -String::String(char c) { - char t[2]; +String::String(char c) : siz(1) { + static char t[2]; sprintf(t, "%c", c); - str = strdup(t); + str = ::strdup(t); } -String::String(const char * s) : str(s ? strdup(s) : strdup("")) { } +String::String(const char * s) : str(s ? ::strdup(s) : ::strdup("")) { + siz = ::strlen(str); +} + +String::String(int hs, const char * s) : str(s ? ::strdup(s) : ::strdup("")), siz(hs) { } String::String(int i) { char t[20]; sprintf(t, "%i", i); - str = strdup(t); + str = ::strdup(t); + siz = ::strlen(str); } String::String(double d) { char t[30]; sprintf(t, "%g", d); - str = strdup(t); + str = ::strdup(t); + siz = ::strlen(str); } String::~String() { @@ -47,8 +53,9 @@ char * String::set(char * s, ...) { va_start(ap, s); vsprintf(t, s, ap); free(str); - str = strdup(t); + str = ::strdup(t); va_end(ap); + siz = ::strlen(str); return t; } @@ -56,8 +63,8 @@ char * String::to_charp(size_t from, ssize_t to) const { if (to < 0) { strcpy(t, &(str[from])); } else { - if (to >= strlen()) { - to = strlen() - 1; + if (to >= siz) { + to = siz - 1; } if (to >= from) { int i; @@ -72,6 +79,14 @@ char * String::to_charp(size_t from, ssize_t to) const { return t; } +String String::extract(size_t from, ssize_t to) const { + return String(to_charp(from, to)); +} + +char * String::strdup(size_t from, ssize_t to) const { + return ::strdup(to_charp(from, to)); +} + int String::to_int(void) const { int r; @@ -90,7 +105,8 @@ String & String::operator=(const String & s) { if (str != s.str) { // On évite l'autodestruction... free(str); - str = strdup(s.str); + str = s.strdup(); + siz = s.siz; } return *this; } @@ -98,14 +114,15 @@ String & String::operator=(const String & s) { String String::operator+(const String & s) const { strcpy(t, str); strcat(t, s.str); - return String(t); + return String(siz + s.siz, t); } String & String::operator+=(const String & s) { strcpy(t, str); strcat(t, s.str); free(str); - str = strdup(t); + str = ::strdup(t); + siz += s.siz; return (*this); } @@ -151,11 +168,11 @@ bool String::operator>(const String & s) const { } size_t String::strlen() const { - return (str ? ::strlen(str) : 0); + return (siz); } char String::operator[](size_t i) const { - if (i >= strlen()) { + if (i >= siz) { return 0; } else { return str[i]; @@ -163,9 +180,7 @@ char String::operator[](size_t i) const { } ssize_t String::strchr(char c, size_t from) const { - size_t s = strlen(); - - for (size_t i = from; i < s; i++) { + for (size_t i = from; i < siz; i++) { if (str[i] == c) return i; } @@ -173,9 +188,7 @@ ssize_t String::strchr(char c, size_t from) const { } ssize_t String::strrchr(char c) const { - size_t s = strlen(); - - for (size_t i = s - 1; i >= 0; i--) { + for (size_t i = siz - 1; i >= 0; i--) { if (str[i] == c) return i; } @@ -194,9 +207,7 @@ ssize_t String::strstr(const String & s) const { int String::strchrcnt(char c) const { size_t i, cnt = 0; - size_t s = strlen(); - - for (i = 0; i < s; i++) { + for (i = 0; i < siz; i++) { if (str[i] == c) cnt++; } @@ -205,18 +216,18 @@ int String::strchrcnt(char c) const { String String::to_sqldate(void) const { /* DD/MM/YYYY ==> YYYYMMMDD */ - return (is_date() ? String(to_charp(6, 9)) + to_charp(3, 4) + to_charp(0, 1) : ""); + return (is_date() ? extract(6, 9) + extract(3, 4) + extract(0, 1) : ""); } String String::to_sqltime(void) const { /* h:m ==> h * 60 + m */ int p = strchr(':'); - return (is_time() ? String(String(to_charp(0, p - 1)).to_int() * 60 + String(to_charp(p + 1)).to_int()) : ""); + return (is_time() ? String(extract(0, p - 1).to_int() * 60 + extract(p + 1).to_int()) : ""); } String String::from_sqldate(void) const { /* YYYYMMDD ==> DD/MM/YYYY */ - return ((strlen() == 8) && is_number() ? String(to_charp(6, 7)) + '/' + to_charp(4, 5) + '/' + to_charp(0, 3) : ""); + return ((strlen() == 8) && is_number() ? extract(6, 7) + '/' + extract(4, 5) + '/' + extract(0, 3) : ""); } String String::from_sqltime(void) const { @@ -231,9 +242,9 @@ bool String::is_date(void) const { if (strlen() != 10) return false; if ((str[2] != '/') || (str[5] != '/') || - (!String(to_charp(0, 1)).is_number()) || - (!String(to_charp(3, 4)).is_number()) || - (!String(to_charp(6, 9)).is_number())) { + (!extract(0, 1).is_number()) || + (!extract(3, 4).is_number()) || + (!extract(6, 9).is_number())) { return (isDateArgument(to_sqldate().str)); } @@ -251,20 +262,16 @@ double String::datedif(const String & s) const { } bool String::is_number(void) const { - size_t s = strlen(); - - - for (size_t i = ((str[i] == '-') ? 1 : 0); i < s; i++) { + for (size_t i = ((str[i] == '-') ? 1 : 0); i < siz; i++) { if ((str[i] > '9') || (str[i] < '0')) return false; } return true; } bool String::is_float(void) const { - size_t s = strlen(); bool seendot = false; - for (size_t i = ((str[i] == '-') ? 1 : 0); i < s; i++) { + for (size_t i = ((str[i] == '-') ? 1 : 0); i < siz; i++) { if ((str[i] > '9') || (str[i] < '0')) { if ((str[i] == '.') && !seendot) { seendot = true; @@ -284,8 +291,8 @@ bool String::is_time(void) const { // On accepte les heures sous le format xxxxxx:yy pour pouvoir indiquer des durées. - if ((!String(to_charp(0, p - 1)).is_number()) || (!String(to_charp(p + 1)).is_number())) + if ((!extract(0, p - 1).is_number()) || (!extract(p + 1).is_number())) return false; - return (String(to_charp(p + 1)).to_int() < 60) ? true : false; + return (extract(p + 1).to_int() < 60) ? true : false; } |