summaryrefslogtreecommitdiff
path: root/lib/String.cc
diff options
context:
space:
mode:
Diffstat (limited to 'lib/String.cc')
-rw-r--r--lib/String.cc36
1 files changed, 25 insertions, 11 deletions
diff --git a/lib/String.cc b/lib/String.cc
index d587b75..39ea9d8 100644
--- a/lib/String.cc
+++ b/lib/String.cc
@@ -10,15 +10,15 @@ extern "C" {
int isDateArgument(char *);
}
-char String::t[BUFSIZ];
+char String::t[BUFSIZ + 1];
String::String(const String & s) : str(::strdup(s.str)), siz(s.siz) { }
String::String(char c) : siz(1) {
- static char t[2];
+ char * t = (char *) malloc(2);
sprintf(t, "%c", c);
- str = ::strdup(t);
+ str = t;
}
String::String(const char * s) : str(s ? ::strdup(s) : ::strdup("")) {
@@ -61,13 +61,18 @@ const char * String::set(char * s, ...) {
const char * String::to_charp(size_t from, ssize_t to) const {
if (to < 0) {
- strcpy(t, &(str[from]));
+ strncpy(t, &(str[from]), BUFSIZ);
} else {
if (to >= siz) {
to = siz - 1;
}
+
+ if ((to - from) > BUFSIZ) {
+ from -= (to - from) - BUFSIZ;
+ }
+
if (to >= from) {
- int i;
+ int i;
for (i = 0; i <= to - from; i++) {
t[i] = str[i + from];
}
@@ -112,16 +117,25 @@ String & String::operator=(const String & s) {
}
String String::operator+(const String & s) const {
- strcpy(t, str);
- strcat(t, s.str);
- return String(siz + s.siz, t);
+ char * t = (char *) malloc(s.siz + siz + 1), * u;
+ String o;
+
+ strcpy((u = t), str);
+ u += siz;
+ strcpy(u, s.str);
+ o = String(siz + s.siz, t);
+ free(t);
+ return o;
}
String & String::operator+=(const String & s) {
- strcpy(t, str);
- strcat(t, s.str);
+ char * t = (char *) malloc(s.siz + siz + 1), * u;
+
+ strcpy((u = t), str);
+ u += siz;
+ strcat(u, s.str);
free(str);
- str = ::strdup(t);
+ str = t;
siz += s.siz;
return (*this);
}