// filename: String.cpp // author: John Flores // date: May, 2003. // // Permission is hereby granted, free of charge, to any person obtaining a // copy of this software and associated documentation files (the "Software"), // to deal in the Software without restriction, including without limitation // the rights to use, copy, modify, merge, publish, distribute, sublicense, // and/or sell copies of the Software, and to permit persons to whom the // Software is furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. #include "String.h" int String::find(const char *s, int sz) const { static unsigned idx = 0; String *ptr = const_cast(this); if (ptr != this) { ptr = const_cast(this); idx = 0; } int i; while (idx < len) { i = 0; while (s[i] == buf[i + idx]) { i++; if (i == sz) return idx++; } idx++; } idx = 0; return NPOS; } String String::substr(int idx, int sz) { String s; if (len - idx - sz < 0) sz = len; s.set(buf + idx, sz); return s; } void String::resize(unsigned sz) { unsigned newbufsz = bufsz? bufsz: INITSZ; while (newbufsz < sz) newbufsz *= 2; char *newbuf = new char[newbufsz]; assert(newbuf); if (buf) { memcpy(newbuf, buf, len + 1); delete [] buf; } buf = newbuf; bufsz = newbufsz; } String &String::operator=(const char *s) { set(s, strlen(s)); return *this; } void String::set(const char *s, unsigned sz) { if (bufsz < sz + 1) resize(sz + 1); memcpy(buf, s, sz); buf[len = sz] = '\0'; } String::String(): buf(NULL), bufsz(0), len(0) { set("", 0); } String::String(char c): buf(NULL), bufsz(0), len(0) { set(&c, 1); } String::String(const char *s): buf(NULL), bufsz(0), len(0) { set(s, strlen(s)); } String::String(const String &s): buf(NULL), bufsz(0), len(0) { set(s.buf, s.len); } String::~String() { delete [] buf; } String &String::insert(unsigned idx, char c) { return insert(idx, &c, 1); } String &String::insert(unsigned idx, const char *s) { return insert(idx, s, strlen(s)); } String &String::insert(unsigned idx, const String &s) { return insert(idx, s.buf, s.len); } String &String::insert(unsigned idx, const char *s, unsigned sz) { assert(idx >= 0 && idx <= len); if (bufsz < len + sz + 1) resize(len + sz + 1); memmove(buf + idx + sz, buf + idx, len - idx + 1); memcpy(buf + idx, s, sz); len += sz; return *this; } String &String::insert(unsigned idx, const String &s, unsigned sz) { return insert(idx, s.buf, sz); } String &String::remove(int idx, int sz) { memmove(buf + idx, buf + idx + sz, len - idx - sz + 1); len -= sz; return *this; }