1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
|
/*
* mystring.cc: Part of GNU CSSC.
*
* Copyright (C) 1997, 1998, 1999 Free Software Foundation, Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
*
* CSSC was originally Based on MySC, by Ross Ridge, which was
* placed in the Public Domain.
*
*
* Members of the class string.
*
*/
// We don't implement the standard string class, so we only have
// code here if USE_STANDARD_STRING is not defined.
#ifdef __GNUC__
#pragma implementation "mystring.h"
#endif
#include "cssc.h"
#include "mystring.h"
// If we have a standard <string> header we do not need to define the
// class mystring since it is just a typedef for the standard string
// class. To find out if we're using that string class, we include
// "mystring.h", which checks the macro HAVE_STRING from config.h,
// which tells us if we have a <string> header.
#ifndef USE_STANDARD_STRING
#ifdef CONFIG_SCCS_IDS
static const char rcs_id[] = "CSSC $Id: mystring.cc,v 1.14 2001/09/29 19:39:41 james_youngman Exp $";
#endif
struct mystring::MyStrRep
{
public:
charT *data;
int refs;
size_type len;
MyStrRep(const char *s, size_type sl) // length already measured.
{
data = new charT[sl+1];
len = sl;
memcpy(data, s, len*sizeof(charT));
data[sl] = 0; // terminate.
refs = 1;
}
~MyStrRep()
{
delete[] data;
data = 0;
refs = 0;
len = 0;
}
// We don't want the default copy methods.
private:
MyStrRep(const MyStrRep&);
MyStrRep& operator=(const MyStrRep&);
MyStrRep() : data(0), refs(0) { }
};
mystring::mystring()
{
rep = new MyStrRep("", 0);
}
mystring::mystring(const charT* s, size_type len)
{
rep = new MyStrRep(s, len);
}
mystring::mystring(const charT* s)
{
rep = new MyStrRep(s, strlen(s));
}
mystring::mystring(const mystring& from)
{
rep = from.rep;
rep->refs++;
}
mystring&
mystring::operator=(const mystring& from)
{
if (&from != this)
{
if (1 == rep->refs--)
delete rep;
rep = from.rep;
rep->refs++;
}
return *this;
}
mystring&
mystring::assign(const mystring &s, size_type pos)
{
// We _could_ cope with pos being zero specially,
// but we do not.
size_type flen = s.length();
if (pos < flen)
{
MyStrRep* new_rep = new MyStrRep(s.rep->data + pos, flen-pos);
if (1 == rep->refs--)
{
delete rep;
}
rep = new_rep;
}
else
{
// Tried to copy the bit of string beyond the end...
if (1 == rep->refs--)
{
delete rep;
}
rep = new MyStrRep("", 0);
}
return *this;
}
bool mystring::valid() const
{
return rep != 0;
}
bool mystring::empty() const
{
return length() == 0;
}
mystring::size_type
mystring::length() const
{
return rep->len;
}
mystring::charT
mystring::at(size_type pos) const
{
ASSERT(pos < length()); // TODO: throw exception??
return rep->data[pos];
}
mystring::ModifiableReference mystring::at(mystring::size_type pos)
{
ASSERT(pos < length()); // TODO: throw exception??
return ModifiableReference(*this, pos);
}
const char* mystring::c_str() const
{
return rep->data; // already terminated.
}
mystring
mystring::substr(size_type first, size_type last) const
{
ASSERT(length() > first);
if (last > length())
last = length();
return mystring(rep->data + first, last-first);
}
// Not very efficient but this is a get-you-home implementation
// anyway; the header <string> should provide the Real Thing anyway.
mystring mystring::operator+(const mystring &s) const
{
size_type newlen = length() + s.length();
charT *newdata = new charT[newlen];
memcpy(newdata, rep->data, rep->len);
memcpy(newdata+rep->len, s.rep->data, s.rep->len);
return mystring(newdata, newlen);
}
// TODO: either using memcmp() here is invalid,
// or we could just use strcmp()...
int
mystring::compare(const mystring &s) const
{
if (rep == s.rep)
{
return 0; // no difference
}
else
{
// Compare the two strings, up to the smaller of the two lengths.
bool me_longer = length() > s.length();
size_type minlen;
if (me_longer)
minlen = s.length();
else
minlen = length();
const int cmp = memcmp(rep->data, s.rep->data, minlen);
if (cmp)
return cmp;
// If the strings are of different length, and one is
// a prefix of the other, the longer string is "greater".
if (me_longer)
return 1;
else if (s.length() > length())
return -1;
else
return 0;
}
}
bool
mystring::operator==(const mystring &s) const
{
return compare(s) == 0;
}
bool
mystring::operator!=(const mystring &s) const
{
return compare(s) != 0;
}
void mystring::prepare_for_writing()
{
if (rep->refs > 1)
{
MyStrRep *oldrep = rep;
rep = new MyStrRep(rep->data, rep->len);
--oldrep->refs;
}
}
mystring::charT &
mystring::ModifiableReference::operator=(charT ch)
{
if (ch != s.rep->data[pos])
{
s.prepare_for_writing();
s.rep->data[pos] = ch;
}
return s.rep->data[pos];
}
mystring::ModifiableReference::operator char() const
{
return s.rep->data[pos];
}
mystring::size_type
mystring::find_last_of(charT needle) const
{
size_type xpos = length();
while (xpos-- > 0)
if (rep->data[xpos] == needle)
return xpos;
return npos;
}
#endif /* USE_STANDARD_STRING */
/* Local variables: */
/* mode: c++ */
/* End: */
|