[[project @ 2005-02-28 23:43:20 by unknown_lamer] unknown_lamer**20050228234320 This should fix a few issues with String handling 2005-02-28 Clinton Ebadi * source/Socket.C (readLine): return String (buf.c_str ()) instead of String (buf) so that only the buffer up to the first null is returned (readLine): Added a comment explaining the usage of buf (readLine): Changed type of length (std::string::size_type instead of std::size_t) * source/String.C: Reimplemented String on top of std::string to fix a few bugs and potential memory leaks (toLower): use Utils::to_lower (toUpper): Use Utils::to_upper (trim): Use Utils::trim_str * source/String.H: removed srep, replaced it with a std::string (my_string) ] { hunk ./ChangeLog 3 + * source/Socket.C (readLine): return String (buf.c_str ()) instead + of String (buf) so that only the buffer up to the first null is returned + (readLine): Added a comment explaining the usage of buf + (readLine): Changed type of length (std::string::size_type instead + of std::size_t) + + * source/String.C: Reimplemented String on top of std::string to + fix a few bugs and potential memory leaks + (toLower): use Utils::to_lower + (toUpper): Use Utils::to_upper + (trim): Use Utils::trim_str + + * source/String.H: removed srep, replaced it with a std::string (my_string) + hunk ./ChangeLog 32 - * source/String.C: remove - * source/String.H: remove - hunk ./TODO 57 +* See if Socket::readLine could be sped up (profile it first to see if + it even matters) hunk ./source/Socket.C 252 + // fixme: this could probably be sped up (use some faster way of + // reading from the socket than reading a char at a time) + + // We allocate the buffer statically to keep from having to overhead + // of reallocating a new buffer every time we call this (which is + // every time anything happens on IRC so the overhead of + // re-allocating a buffer every time is potentially too large). + // + // Since it is static, and the length of lines will differ for each + // read, an embedded \0 is inserted after the last character of the + // line and then a copy of the the c_str is returned as a String so + // that only the line that was just read is returned. hunk ./source/Socket.C 265 - int pos = 0, nb; - char r; - std::size_t length = buf.length (); hunk ./source/Socket.C 266 + int pos = 0; // pos in buffer + int nb; // number of bytes read by ::read + char r; // temp var for storing output of read into + std::string::size_type length = buf.length (); + hunk ./source/Socket.C 294 -if (pos > 1 && buf[pos-2] == '\r') + if (pos > 1 && buf[pos-2] == '\r') hunk ./source/Socket.C 297 - buf[pos-1] = '\0'; - - return String(buf); + buf[pos-1] = '\0'; + + // c_str () is used because the String constructor for std::string + // will copy the entire std::string into it when we only want it to + // copy up to the first null. + return String (buf.c_str ()); hunk ./source/String.C 20 +#include "Utils.H" hunk ./source/String.C 25 +#include hunk ./source/String.C 29 - p = new srep; - len = 0; - p->s = 0; hunk ./source/String.C 33 - p = new srep; - len = strlen(s); - p->s = new char[len + 1]; - std::strcpy(p->s, s); + my_string = s; hunk ./source/String.C 38 - p = new srep; - // We do this instead of just s.length () because there might be a - // \0 in the string before the end (e.g. this is a message from the - // Socket's buffer). - const char* temp_str = s.c_str (); - len = strlen (temp_str); - p->s = new char[len + 1]; - std::strcpy (p->s, temp_str); + my_string = s; hunk ./source/String.C 43 - s.p->n++; - p = s.p; - len = s.len; + my_string = s.my_string; hunk ./source/String.C 50 - - p = new srep; - len = strlen(temp.str().c_str ()); - p->s = new char[len + 1]; - strcpy(p->s, temp.str().c_str ()); + my_string = temp.str (); hunk ./source/String.C 55 - p = new srep; - p->s = new char[2]; - p->s[0] = c; - p->s[1] = '\0'; - len = 1; + my_string = std::string (1, c); hunk ./source/String.C 60 - if (--p->n == 0) { - delete[] p->s; - delete p; - } + hunk ./source/String.C 66 - if (p->n > 1) { - p->n--; - p = new srep; - } - else - delete[] p->s; - - len = strlen(s); - p->s = new char[len + 1]; - strcpy(p->s, s); - + my_string = s; + hunk ./source/String.C 74 - s.p->n++; // protection contre st = st - if (--p->n == 0) { - delete[] p->s; - delete p; - } - p = s.p; - len = s.len; + my_string = s.my_string; + hunk ./source/String.C 82 - if (p->n > 1) { - p->n--; - p = new srep; - } - else - delete[] p->s; - - len = s.length (); - p->s = new char[len + 1]; - strcpy(p->s, s.c_str ()); + my_string = s; hunk ./source/String.C 90 - return len; + return my_string.length (); hunk ./source/String.C 96 - if (!strchr(p->s, c)) - return -1; - - return (int)(strchr(p->s, c) - p->s); + return my_string.find (c); hunk ./source/String.C 100 -String::fill(char c) +String::fill (char c) hunk ./source/String.C 102 - for (char * s = p->s; *s; s++) - *s = c; + my_string.replace (0, + my_string.length () - 1, + my_string.length (), + c); hunk ./source/String.C 109 -String::pad(int n) +String::pad (int n) hunk ./source/String.C 111 - int l = len; - - if (n <= l) - return subString(0, n-1); - - char *temp = new char[n+1]; - strcpy(temp, p->s); + std::string temp = my_string; hunk ./source/String.C 113 - for (int i = l; i < n; i++) - temp[i] = ' '; - temp[n] = '\0'; + temp.resize (n, ' '); hunk ./source/String.C 115 - String res(temp); - delete temp; - - return res; + return temp; hunk ./source/String.C 119 -String::subString(int debut, int fin) +String::subString (int start, int end) hunk ./source/String.C 121 - if (fin < debut) return ""; + if (end < start) return ""; hunk ./source/String.C 123 - char * temp = new char[fin-debut+2]; - strncpy(temp, p->s + debut, fin - debut + 1); - temp[fin - debut + 1] = '\0'; - String res(temp); - delete temp; - return res; + return my_string.substr (start, (end - start) + 1); hunk ./source/String.C 133 -String::subString(int debut) +String::subString (int start) hunk ./source/String.C 135 - return subString(debut, len - 1); + return subString (start, my_string.length () - 1); hunk ./source/String.C 147 - char *temp = new char[len + 1]; - - for (int i = 0; i < len + 1; i++) - if (isupper(p->s[i])) - temp[i] = tolower(p->s[i]); - else - temp[i] = p->s[i]; - - String res(temp); - delete temp; - return res; + return Utils::to_lower (my_string); hunk ./source/String.C 153 - char *temp = new char[len + 1]; - - for (int i = 0; i < len + 1; i++) - if (islower(p->s[i])) - temp[i] = toupper(p->s[i]); - else - temp[i] = p->s[i]; - - String res(temp); - delete temp; - return res; + return Utils::to_upper (my_string); hunk ./source/String.C 159 - int i = 0, j = len - 1; - - while (i < j && (p->s[i] == ' ' || p->s[i] == '\t' || p->s[i] == '\r')) - i++; - - while (j > 0 && (p->s[j] == ' ' || p->s[j] == '\t' || p->s[j] == '\r')) - j--; - - return subString(i, j); + return Utils::trim_str (my_string); hunk ./source/String.C 163 -String::indexOf(char c) +String::indexOf (char c) hunk ./source/String.C 165 - char *s = std::strchr(p->s, c); - if (s) - return p->s - s; - - return -1; + std::string::size_type pos = my_string.find (c); + + if (pos == std::string::npos) + return -1; + else + return pos; hunk ./source/String.C 174 -String::operator[](int i) +String::operator[] (int i) hunk ./source/String.C 176 - if (i < 0 || len < i) { + if (i < 0 || my_string.length () < i) { hunk ./source/String.C 180 - return p->s[i]; + return my_string[i]; hunk ./source/String.C 186 - if (i < 0 || len < i) { + if (i < 0 || my_string.length () < i) { hunk ./source/String.C 190 - return p->s[i]; + return my_string[i]; hunk ./source/String.C 196 - return std::strcmp(p->s, s) == 0; + return my_string == s; hunk ./source/String.C 202 - return (p == s.p) || (std::strcmp(p->s, s.p->s) == 0); + return my_string == s.my_string; hunk ./source/String.C 208 - return (p->s == s.c_str ()) || (std::strcmp (p->s, s.c_str()) == 0); + return my_string == s; hunk ./source/String.C 214 - return std::strcmp(p->s, s) != 0; + return my_string != s; hunk ./source/String.C 220 - return std::strcmp(p->s, s.p->s) != 0; + return my_string != s.my_string; hunk ./source/String.C 226 - return ! (*this == s); + return my_string != s; hunk ./source/String.C 232 - return std::strcmp(p->s, s.p->s) < 0; + return my_string < s.my_string; hunk ./source/String.C 238 - return std::strcmp(p->s, s.c_str ()) < 0; + return my_string < s; hunk ./source/String.C 244 - return std::strcmp(p->s, s.p->s) > 0; + return my_string > s.my_string; hunk ./source/String.C 250 - return std::strcmp(p->s, s.p->s) <= 0; + return my_string <= s.my_string; hunk ./source/String.C 256 - return (*this < s) || (*this == s); + return my_string <= s; hunk ./source/String.C 262 - return std::strcmp(p->s, s.p->s) >= 0; + return my_string >= s.my_string; hunk ./source/String.C 267 - return ! (*this < s); + return my_string >= s; hunk ./source/String.C 273 - char *temp = new char[len + std::strlen(s) + 1]; - - std::strcpy(temp, p->s); - std::strcat(temp, s); - - String res(temp); - delete temp; - - return res; + return my_string + s; hunk ./source/String.C 279 - char * temp = new char[len + s.len + 1]; - - std::strcpy(temp, p->s); - std::strcat(temp, s.p->s); - - String res(temp); - delete temp; - - return res; + return my_string + s.my_string; hunk ./source/String.C 285 - char * temp = new char[len + s.length () + 1]; - std::strcpy (temp, p->s); - std::strcat (temp, s.c_str ()); - - String res (temp); - delete temp; - - return res; + return my_string + s; hunk ./source/String.C 290 - return p->s; + return my_string.c_str (); hunk ./source/String.C 295 - return std::string (p->s); + return my_string; hunk ./source/String.C 301 - return s << st.p->s; + return s << st.my_string; hunk ./source/String.C 307 - if (st.p->n > 1) { - st.p->n--; - st.p = new String::srep; - } - else - delete[] st.p->s; - - char buf[2048]; - char c; + // The original version of string grabbed an entire line with + // operator>> so this version has to too (otherwise I'd just replace + // String with std::string everywhere because it would be just as + // painful) hunk ./source/String.C 312 - s.getline (buf, 2048, '\n'); - //s.get(c); + std::string temp; hunk ./source/String.C 314 - st.len = strlen(buf); - st.p->s = new char[st.len + 1]; - strcpy(st.p->s, buf); + std::getline (s, temp); + + st.my_string = temp; hunk ./source/String.C 323 - std::string temp = s; - temp += p.p->s; - return temp; + return s + p.my_string; hunk ./source/String.H 27 - struct srep { - char *s; // pointer on the data - int n; // reference counter - srep() - { n = 1; } - }; - srep *p; - int len; - + std::string my_string; + }