(file) Return to String.cpp CVS log (file) (dir) Up to [Pegasus] / pegasus / src / Pegasus / Common

  1 mike  1.1 //BEGIN_LICENSE
  2           //
  3           // Copyright (c) 2000 The Open Group, BMC Software, Tivoli Systems, IBM
  4           //
  5           // Permission is hereby granted, free of charge, to any person obtaining a
  6           // copy of this software and associated documentation files (the "Software"),
  7           // to deal in the Software without restriction, including without limitation
  8           // the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9           // and/or sell copies of the Software, and to permit persons to whom the
 10           // Software is furnished to do so, subject to the following conditions:
 11           //
 12           // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 13           // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 14           // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 15           // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 16           // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 17           // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 18           // DEALINGS IN THE SOFTWARE.
 19           //
 20           //END_LICENSE
 21           //BEGIN_HISTORY
 22 mike  1.1 //
 23           // Author:
 24           //
 25 mike  1.2 // $Log: String.cpp,v $
 26 karl  1.5 // Revision 1.4  2001/02/26 04:33:28  mike
 27           // Fixed many places where cim names compared with operator==(String,String).
 28           // Changed all of these to use CIMName::equal()
 29           //
 30 mike  1.4 // Revision 1.3  2001/02/11 17:19:30  mike
 31           // added reverseFind() method
 32           //
 33 mike  1.3 // Revision 1.2  2001/02/11 05:42:33  mike
 34           // new
 35           //
 36 mike  1.2 // Revision 1.1.1.1  2001/01/14 19:53:14  mike
 37           // Pegasus import
 38           //
 39 mike  1.1 //
 40           //END_HISTORY
 41           
 42 mike  1.2 #include <cctype>
 43 mike  1.1 #include "String.h"
 44           #include "Exception.h"
 45           #include "String.h"
 46           
 47           PEGASUS_NAMESPACE_BEGIN
 48           
 49           const String String::EMPTY;
 50           
 51 mike  1.2 inline Uint32 StrLen(const char* str)
 52 mike  1.1 {
 53 mike  1.2     if (!str)
 54           	throw NullPointer();
 55           
 56               return strlen(str);
 57 mike  1.1 }
 58           
 59 mike  1.2 inline Uint32 StrLen(const Char16* str)
 60 mike  1.1 {
 61               if (!str)
 62 mike  1.2 	throw NullPointer();
 63 mike  1.1 
 64               Uint32 n = 0;
 65           
 66               while (*str++)
 67           	n++;
 68           
 69               return n;
 70           }
 71           
 72 karl  1.5 String::String()
 73 mike  1.2 {
 74 karl  1.5     _rep.append('\0');
 75 mike  1.1 }
 76           
 77 karl  1.5 String::String(const String& x) : _rep(x._rep)
 78 mike  1.1 {
 79           
 80           }
 81           
 82           String::String(const String& x, Uint32 n)
 83           {
 84               _rep.append('\0');
 85               append(x.getData(), n);
 86           }
 87           
 88 karl  1.5 String::String(const Char16* x) : _rep(x, StrLen(x) + 1)
 89           {
 90 mike  1.1 
 91           }
 92           
 93 karl  1.5 String::String(const Char16* x, Uint32 n)
 94           {
 95               assign(x, n);
 96 mike  1.1 }
 97           
 98           String::String(const char* str)
 99           {
100               Uint32 n = ::strlen(str) + 1;
101               reserve(n);
102           
103               while (n--)
104           	_rep.append(*str++);
105           }
106           
107 karl  1.5 String::String(const char* str, Uint32 n_)
108 mike  1.1 {
109               Uint32 n = _min(strlen(str), n_);
110               reserve(n + 1);
111           
112               while (n--)
113           	_rep.append(*str++);
114           
115               _rep.append('\0');
116           }
117           
118 karl  1.5 String& String::assign(const Char16* x)
119 mike  1.1 {
120               _rep.clear();
121               _rep.append(x, StrLen(x) + 1);
122 karl  1.5     return *this;
123 mike  1.1 }
124           
125           String& String::assign(const Char16* str, Uint32 n)
126           {
127               _rep.clear();
128               Uint32 m = _min(StrLen(str), n);
129               _rep.append(str, m);
130               _rep.append('\0');
131               return *this;
132           }
133           
134 karl  1.5 String& String::assign(const char* x)
135 mike  1.1 {
136               _rep.clear();
137               Uint32 n = strlen(x);
138               _rep.reserve(n + 1);
139           
140               while (n--)
141           	_rep.append(*x++);
142           
143               _rep.append('\0');
144           
145 karl  1.5     return *this;
146 mike  1.1 }
147           
148           String& String::assign(const char* x, Uint32 n_)
149           {
150               _rep.clear();
151           
152               Uint32 n = _min(strlen(x), n_);
153               _rep.reserve(n + 1);
154           
155               while (n--)
156           	_rep.append(*x++);
157           
158               _rep.append('\0');
159           
160 karl  1.5     return *this;
161 mike  1.1 }
162           
163           char* String::allocateCString(Uint32 extraBytes, Boolean noThrow) const
164           {
165               Uint32 n = getLength() + 1;
166               char* str = new char[n + extraBytes];
167               char* p = str;
168               const Char16* q = getData();
169           
170               for (Uint32 i = 0; i < n; i++)
171               {
172           	Uint16 c = *q++;
173           	*p++ = char(c);
174           
175           	if ((c & 0xff00) && !noThrow)
176           	    throw TruncatedCharacter();
177               }
178           
179               return str;
180           }
181           
182 mike  1.1 void String::appendToCString(
183 karl  1.5     char* str,
184               Uint32 length,
185 mike  1.1     Boolean noThrow) const
186           {
187               if (!str)
188           	throw NullPointer();
189           
190               Uint32 n = _min(getLength(), length);
191           
192               char* p = str + strlen(str);
193               const Char16* q = getData();
194           
195               for (Uint32 i = 0; i < n; i++)
196               {
197           	Uint16 c = *q++;
198           	*p++ = char(c);
199           
200           	if ((c & 0xff00) && !noThrow)
201           	    throw TruncatedCharacter();
202               }
203           
204               *p = '\0';
205           }
206 mike  1.1 
207 karl  1.5 Char16& String::operator[](Uint32 i)
208 mike  1.1 {
209               if (i > getLength())
210           	ThrowOutOfBounds();
211           
212 karl  1.5     return _rep[i];
213 mike  1.1 }
214           
215           const Char16 String::operator[](Uint32 i) const
216           {
217               if (i > getLength())
218           	ThrowOutOfBounds();
219           
220 karl  1.5     return _rep[i];
221 mike  1.1 }
222           
223           String& String::append(const Char16* str, Uint32 n)
224           {
225               Uint32 m = _min(StrLen(str), n);
226               _rep.reserve(_rep.getSize() + m);
227               _rep.remove(_rep.getSize() - 1);
228               _rep.append(str, m);
229               _rep.append('\0');
230               return *this;
231           }
232           
233           void String::remove(Uint32 pos, Uint32 size)
234           {
235               if (size == Uint32(-1))
236           	size = getLength() - pos;
237           
238               if (pos + size > getLength())
239           	ThrowOutOfBounds();
240           
241               if (size)
242 mike  1.1 	_rep.remove(pos, size);
243           }
244           
245 mike  1.4 int String::compare(const Char16* s1, const Char16* s2, Uint32 n)
246           {
247               while (n--)
248               {
249           	int r = *s1++ - *s2++;
250           
251           	if (r)
252           	    return r;
253               }
254           
255               return 0;
256           }
257           
258           Boolean String::equal(const String& x, const String& y)
259 mike  1.1 {
260               if (x.getLength() != y.getLength())
261           	return false;
262           
263               return String::compare(x.getData(), y.getData(), x.getLength()) == 0;
264           }
265           
266 mike  1.4 Boolean String::equal(const String& x, const Char16* y)
267 mike  1.1 {
268               if (x.getLength() != StrLen(y))
269           	return false;
270           
271               return String::compare(x.getData(), y, x.getLength()) == 0;
272           }
273           
274 mike  1.4 Boolean String::equal(const Char16* x, const String& y)
275 mike  1.1 {
276 mike  1.4     return equal(y, x);
277 mike  1.1 }
278           
279 mike  1.4 Boolean String::equal(const String& x, const char* y)
280 mike  1.1 {
281 mike  1.4     return equal(x, String(y));
282           }
283 mike  1.1 
284 mike  1.4 Boolean String::equal(const char* x, const String& y)
285           {
286               return equal(String(x), y);
287           }
288 mike  1.1 
289           
290           String String::subString(Uint32 pos, Uint32 length) const
291           {
292               if (pos < getLength())
293               {
294           	if (length == Uint32(-1))
295           	    length = getLength() - pos;
296           
297           	return String(getData() + pos, length);
298               }
299               else
300           	return String();
301           }
302           
303           Uint32 String::find(Char16 c) const
304           {
305               const Char16* first = getData();
306           
307               for (const Char16* p = first; *p; p++)
308               {
309 mike  1.1 	if (*p == c)
310           	    return  p - first;
311 mike  1.3     }
312           
313               return Uint32(-1);
314           }
315           
316           Uint32 String::reverseFind(Char16 c) const
317           {
318               const Char16* first = getData();
319               const Char16* last = getData() + getLength();
320           
321               while (last != first)
322               {
323           	if (*--last == c)
324           	    return last - first;
325 mike  1.1     }
326           
327               return Uint32(-1);
328           }
329           
330           int String::compare(const Char16* s1, const Char16* s2)
331           {
332               while (*s1 && *s2)
333               {
334           	int r = *s1++ - *s2++;
335           
336           	if (r)
337           	    return r;
338               }
339           
340               if (*s2)
341           	return -1;
342               else if (*s1)
343           	return 1;
344           
345               return 0;
346 mike  1.1 }
347           
348           std::ostream& operator<<(std::ostream& os, const String& x)
349           {
350               for (Uint32 i = 0, n = x.getLength(); i < n; i++)
351           	os << x[i];
352           
353               return os;
354 mike  1.2 }
355           
356           void String::toLower(char* str)
357           {
358               while (*str)
359           	tolower(*str++);
360           }
361           
362           String ToLower(const String& str)
363           {
364               String tmp(str);
365           
366               for (Uint32 i = 0, n = tmp.getLength(); i < n; i++)
367               {
368           	Char16 c = tmp[i];
369           
370           	if (c <= 127)
371           	    tmp[i] = tolower(c);
372               }
373           
374               return tmp;
375 mike  1.1 }
376           
377           PEGASUS_NAMESPACE_END

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2