]>
cygwin.com Git - cygwin-apps/setup.git/blob - String++.cc
a92878fc21d37c29013b7b1c1b685c011baf270e
2 * Copyright (c) 2002, Robert Collins.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * A copy of the GNU General Public License can be found at
12 * Written by Robert Collins.
16 // A String class to replace all the char * manipulation.
22 #include "io_stream.h"
29 String::_data::_data(_data
const &aData
) : count (1), theString (new unsigned char[aData
.length
]), cstr(0), length (aData
.length
) {
30 memcpy (theString
, aData
.theString
, aData
.length
);
33 String::_data::_data(): count (1), theString(new unsigned char[0]), cstr (0), length (0) {}
34 String::_data::_data(size_t aLength
): count (1), theString(new unsigned char[aLength
]), cstr(0), length (aLength
) {}
35 String::_data::~_data ()
45 String::String (const char *acString
) : theData (new _data(acString
? strlen(acString
) : 0))
47 memcpy (theData
->theString
, acString
, theData
->length
);
52 if (--theData
->count
== 0)
56 String::String (int const anInt
)
60 theData
= new _data(os
.pcount());
61 memcpy (theData
->theString
, os
.str(), os
.pcount());
64 String::String (string
const &aString
) : theData (new _data (aString
.c_str() ? strlen (aString
.c_str()) : 0))
66 memcpy (theData
->theString
, aString
.c_str(), theData
->length
);
69 // able to cache the result if needed.
73 if (theData
->length
== 0)
75 char * tempcStr
=new char[theData
->length
+ 1];
76 // remove when exceptions are done
79 tempcStr
[theData
->length
] = '\0';
80 memcpy (tempcStr
, theData
->theString
, theData
->length
);
87 if (theData
->length
== 0)
89 char * tempcStr
=new char[theData
->length
+ 1];
90 // remove when exceptions are done
93 tempcStr
[theData
->length
] = '\0';
94 memcpy (tempcStr
, theData
->theString
, theData
->length
);
98 // able to cache the result if needed.
100 String::cstr_oneuse () const
102 if (theData
->length
== 0)
105 delete[] theData
->cstr
;
106 theData
->cstr
= new char[theData
->length
+ 1];
107 theData
->cstr
[theData
->length
] = '\0';
108 memcpy (theData
->cstr
, theData
->theString
, theData
->length
);
109 return theData
->cstr
;
112 // does this character exist in the string?
113 // 0 is false, 1 is the first position...
114 // XXX FIXME: Introduce npos, and change all
115 // if (size) calls to be if (size()==npos)
117 String::find(char aChar
) const
119 for (size_t i
=0; i
< theData
->length
; ++i
)
120 if (theData
->theString
[i
] == aChar
)
126 String::substr(size_t start
, size_t len
) const
128 // Adapt the C++ string class
129 return string(cstr_oneuse()).substr(start
, len
);
133 String::compare (String
const &aString
, size_t const count
) const
136 if (theData
== aString
.theData
)
138 size_t length
= count
? count
: theData
->length
;
139 if (length
> theData
->length
)
140 length
= theData
->length
;
141 if (length
> aString
.theData
->length
)
142 length
= aString
.theData
->length
;
144 for (i
=0; i
< length
; ++i
)
145 if (theData
->theString
[i
] < aString
.theData
->theString
[i
])
147 else if (theData
->theString
[i
] > aString
.theData
->theString
[i
])
150 if (i
== count
&& count
!= 0)
152 if (theData
->length
< aString
.theData
->length
)
154 else if (theData
->length
> aString
.theData
->length
)
160 String::casecompare (String
const &aString
, size_t const count
) const
163 if (theData
== aString
.theData
)
165 size_t length
= count
? count
: theData
->length
;
166 if (length
> theData
->length
)
167 length
= theData
->length
;
168 if (length
> aString
.theData
->length
)
169 length
= aString
.theData
->length
;
171 for (i
=0; i
< length
; ++i
)
172 if (toupper(theData
->theString
[i
]) < toupper(aString
.theData
->theString
[i
]))
174 else if (toupper(theData
->theString
[i
]) > toupper(aString
.theData
->theString
[i
]))
177 if (i
== count
&& count
!= 0)
179 if (theData
->length
< aString
.theData
->length
)
181 else if (theData
->length
> aString
.theData
->length
)
187 String::operator+= (String
const &aString
)
189 if (theData
->count
> 1)
191 _data
* someData
= new _data(*theData
);
196 unsigned char *tempString
= theData
->theString
;
197 theData
->theString
= new unsigned char [theData
->length
+ aString
.theData
->length
];
198 // remove when exceptions are done
199 if (!theData
->theString
)
201 memcpy (theData
->theString
, tempString
, theData
->length
);
203 memcpy (&theData
->theString
[theData
->length
], aString
.theData
->theString
, aString
.theData
->length
);
204 theData
->length
+= aString
.theData
->length
;
209 String::operator + (String
const &aString
) const
211 unsigned char *tempcString
= new unsigned char [theData
->length
+ aString
.theData
->length
];
212 // remove when exceptions are done
215 memcpy (tempcString
, theData
->theString
, theData
->length
);
216 memcpy (&tempcString
[theData
->length
], aString
.theData
->theString
, aString
.theData
->length
);
217 return absorb (tempcString
, theData
->length
+ aString
.theData
->length
);
221 String::operator + (char const *aString
) const
223 // expensive, but quick to code.
224 return *this + String (aString
);
228 String::operator == (String
const &rhs
) const
230 return compare (rhs
) ? false : true;
234 String::operator == (char const *rhs
) const
236 return compare (rhs
) ? false : true;
240 String::absorb (unsigned char *aString
, size_t aLength
)
243 theString
.theData
->theString
= aString
;
244 theString
.theData
->length
= aLength
;
249 String::casecompare (String
const lhs
, String
const rhs
)
251 return lhs
.casecompare (rhs
);
254 /* TODO: research how wide char and unicode interoperate with
258 operator << (ostream
&os
, String
const &theString
)
260 os
<< theString
.cstr_oneuse();
This page took 0.047541 seconds and 5 git commands to generate.