(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 mike  1.10 // Revision 1.9  2001/04/11 07:03:02  mike
 27            // Port to Unix
 28            //
 29 mike  1.8  // Revision 1.7  2001/04/10 22:42:55  karl
 30            // Correct error in String find
 31            //
 32 karl  1.7  // Revision 1.6  2001/04/09 20:18:47  karl
 33            // add find substring function
 34            //
 35 karl  1.6  // Revision 1.5  2001/03/09 19:49:32  karl
 36            // long lines
 37            //
 38 karl  1.5  // Revision 1.4  2001/02/26 04:33:28  mike
 39            // Fixed many places where cim names compared with operator==(String,String).
 40            // Changed all of these to use CIMName::equal()
 41            //
 42 mike  1.4  // Revision 1.3  2001/02/11 17:19:30  mike
 43            // added reverseFind() method
 44            //
 45 mike  1.3  // Revision 1.2  2001/02/11 05:42:33  mike
 46            // new
 47            //
 48 mike  1.2  // Revision 1.1.1.1  2001/01/14 19:53:14  mike
 49            // Pegasus import
 50            //
 51 mike  1.1  //
 52            //END_HISTORY
 53            
 54 karl  1.6  
 55 mike  1.2  #include <cctype>
 56 mike  1.1  #include "String.h"
 57            #include "Exception.h"
 58            #include "String.h"
 59            
 60 karl  1.6  // For debugging
 61 karl  1.7  #include <iostream>
 62            using namespace std;
 63 karl  1.6  
 64            
 65 mike  1.1  PEGASUS_NAMESPACE_BEGIN
 66            
 67            const String String::EMPTY;
 68            
 69 mike  1.2  inline Uint32 StrLen(const char* str)
 70 mike  1.1  {
 71 mike  1.2      if (!str)
 72            	throw NullPointer();
 73            
 74                return strlen(str);
 75 mike  1.1  }
 76            
 77 mike  1.2  inline Uint32 StrLen(const Char16* str)
 78 mike  1.1  {
 79                if (!str)
 80 mike  1.2  	throw NullPointer();
 81 mike  1.1  
 82                Uint32 n = 0;
 83            
 84                while (*str++)
 85            	n++;
 86            
 87                return n;
 88            }
 89            
 90 karl  1.5  String::String()
 91 mike  1.2  {
 92 karl  1.5      _rep.append('\0');
 93 mike  1.1  }
 94            
 95 karl  1.5  String::String(const String& x) : _rep(x._rep)
 96 mike  1.1  {
 97            
 98            }
 99            
100            String::String(const String& x, Uint32 n)
101            {
102                _rep.append('\0');
103                append(x.getData(), n);
104            }
105            
106 karl  1.5  String::String(const Char16* x) : _rep(x, StrLen(x) + 1)
107            {
108 mike  1.1  
109            }
110            
111 karl  1.5  String::String(const Char16* x, Uint32 n)
112            {
113                assign(x, n);
114 mike  1.1  }
115            
116            String::String(const char* str)
117            {
118                Uint32 n = ::strlen(str) + 1;
119                reserve(n);
120            
121                while (n--)
122            	_rep.append(*str++);
123            }
124            
125 karl  1.5  String::String(const char* str, Uint32 n_)
126 mike  1.1  {
127                Uint32 n = _min(strlen(str), n_);
128                reserve(n + 1);
129            
130                while (n--)
131            	_rep.append(*str++);
132            
133                _rep.append('\0');
134            }
135            
136 karl  1.5  String& String::assign(const Char16* x)
137 mike  1.1  {
138                _rep.clear();
139                _rep.append(x, StrLen(x) + 1);
140 karl  1.5      return *this;
141 mike  1.1  }
142            
143            String& String::assign(const Char16* str, Uint32 n)
144            {
145                _rep.clear();
146                Uint32 m = _min(StrLen(str), n);
147                _rep.append(str, m);
148                _rep.append('\0');
149                return *this;
150            }
151            
152 karl  1.5  String& String::assign(const char* x)
153 mike  1.1  {
154                _rep.clear();
155                Uint32 n = strlen(x);
156                _rep.reserve(n + 1);
157            
158                while (n--)
159            	_rep.append(*x++);
160            
161                _rep.append('\0');
162            
163 karl  1.5      return *this;
164 mike  1.1  }
165            
166            String& String::assign(const char* x, Uint32 n_)
167            {
168                _rep.clear();
169            
170                Uint32 n = _min(strlen(x), n_);
171                _rep.reserve(n + 1);
172            
173                while (n--)
174            	_rep.append(*x++);
175            
176                _rep.append('\0');
177            
178 karl  1.5      return *this;
179 mike  1.1  }
180            
181            char* String::allocateCString(Uint32 extraBytes, Boolean noThrow) const
182            {
183                Uint32 n = getLength() + 1;
184                char* str = new char[n + extraBytes];
185                char* p = str;
186                const Char16* q = getData();
187            
188                for (Uint32 i = 0; i < n; i++)
189                {
190            	Uint16 c = *q++;
191            	*p++ = char(c);
192            
193            	if ((c & 0xff00) && !noThrow)
194            	    throw TruncatedCharacter();
195                }
196            
197                return str;
198            }
199            
200 mike  1.1  void String::appendToCString(
201 karl  1.5      char* str,
202                Uint32 length,
203 mike  1.1      Boolean noThrow) const
204            {
205                if (!str)
206            	throw NullPointer();
207            
208                Uint32 n = _min(getLength(), length);
209            
210                char* p = str + strlen(str);
211                const Char16* q = getData();
212            
213                for (Uint32 i = 0; i < n; i++)
214                {
215            	Uint16 c = *q++;
216            	*p++ = char(c);
217            
218            	if ((c & 0xff00) && !noThrow)
219            	    throw TruncatedCharacter();
220                }
221            
222                *p = '\0';
223            }
224 mike  1.1  
225 karl  1.5  Char16& String::operator[](Uint32 i)
226 mike  1.1  {
227                if (i > getLength())
228            	ThrowOutOfBounds();
229            
230 karl  1.5      return _rep[i];
231 mike  1.1  }
232            
233            const Char16 String::operator[](Uint32 i) const
234            {
235                if (i > getLength())
236            	ThrowOutOfBounds();
237            
238 karl  1.5      return _rep[i];
239 mike  1.1  }
240            
241            String& String::append(const Char16* str, Uint32 n)
242            {
243                Uint32 m = _min(StrLen(str), n);
244                _rep.reserve(_rep.getSize() + m);
245                _rep.remove(_rep.getSize() - 1);
246                _rep.append(str, m);
247                _rep.append('\0');
248                return *this;
249            }
250            
251            void String::remove(Uint32 pos, Uint32 size)
252            {
253                if (size == Uint32(-1))
254            	size = getLength() - pos;
255            
256                if (pos + size > getLength())
257            	ThrowOutOfBounds();
258            
259                if (size)
260 mike  1.1  	_rep.remove(pos, size);
261            }
262            
263 mike  1.4  int String::compare(const Char16* s1, const Char16* s2, Uint32 n)
264            {
265                while (n--)
266                {
267            	int r = *s1++ - *s2++;
268            
269            	if (r)
270            	    return r;
271                }
272            
273                return 0;
274            }
275            
276            Boolean String::equal(const String& x, const String& y)
277 mike  1.1  {
278                if (x.getLength() != y.getLength())
279            	return false;
280            
281                return String::compare(x.getData(), y.getData(), x.getLength()) == 0;
282            }
283            
284 mike  1.4  Boolean String::equal(const String& x, const Char16* y)
285 mike  1.1  {
286                if (x.getLength() != StrLen(y))
287            	return false;
288            
289                return String::compare(x.getData(), y, x.getLength()) == 0;
290            }
291            
292 mike  1.4  Boolean String::equal(const Char16* x, const String& y)
293 mike  1.1  {
294 mike  1.4      return equal(y, x);
295 mike  1.1  }
296            
297 mike  1.4  Boolean String::equal(const String& x, const char* y)
298 mike  1.1  {
299 mike  1.4      return equal(x, String(y));
300            }
301 mike  1.1  
302 mike  1.4  Boolean String::equal(const char* x, const String& y)
303            {
304                return equal(String(x), y);
305            }
306 mike  1.1  
307            
308            String String::subString(Uint32 pos, Uint32 length) const
309            {
310                if (pos < getLength())
311                {
312            	if (length == Uint32(-1))
313            	    length = getLength() - pos;
314            
315            	return String(getData() + pos, length);
316                }
317                else
318            	return String();
319            }
320            
321            Uint32 String::find(Char16 c) const
322            {
323                const Char16* first = getData();
324            
325                for (const Char16* p = first; *p; p++)
326                {
327 mike  1.1  	if (*p == c)
328            	    return  p - first;
329 mike  1.3      }
330            
331                return Uint32(-1);
332 karl  1.6  }
333            
334 karl  1.7  Uint32 String::find(const String& s) const
335 karl  1.6  {
336 karl  1.7      const Char16* pSubStr = s.getData();
337                const Char16* pStr = getData();
338                Uint32 subStrLen = s.getLength();
339                Uint32 strLen = getLength();
340                
341                // loop to find first char match
342                Uint32 loc = 0;
343                for( ; loc <= (strLen-subStrLen); loc++)
344 karl  1.6      {
345 karl  1.7  	if (*pStr++ == *pSubStr)  // match first char
346            	{
347            	    // point to substr 2nd char
348            	    const Char16* p = pSubStr + 1;
349            
350            	    // Test remaining chars for equal
351            	    Uint32 i = 1;
352            	    for (; i < subStrLen; i++)
353            		if (*pStr++ != *p++ )
354            		    {pStr--; break;} // break from loop
355            	    if (i == subStrLen)
356            		return loc;
357            	}
358                }
359 mike  1.9      return Uint32(-1);
360 karl  1.6  }
361 mike  1.8  
362 karl  1.6  // ATTN:KS 5 apr 2000 Need to add the Char16* version.
363            Uint32 String::find(const char* s) const
364            {
365                return find(String(s));
366 mike  1.3  }
367            
368            Uint32 String::reverseFind(Char16 c) const
369            {
370                const Char16* first = getData();
371                const Char16* last = getData() + getLength();
372            
373                while (last != first)
374                {
375            	if (*--last == c)
376            	    return last - first;
377 mike  1.1      }
378            
379                return Uint32(-1);
380            }
381            
382            int String::compare(const Char16* s1, const Char16* s2)
383            {
384                while (*s1 && *s2)
385                {
386            	int r = *s1++ - *s2++;
387            
388            	if (r)
389            	    return r;
390                }
391            
392                if (*s2)
393            	return -1;
394                else if (*s1)
395            	return 1;
396            
397                return 0;
398 mike  1.1  }
399            
400            std::ostream& operator<<(std::ostream& os, const String& x)
401            {
402                for (Uint32 i = 0, n = x.getLength(); i < n; i++)
403            	os << x[i];
404            
405                return os;
406 mike  1.2  }
407            
408            void String::toLower(char* str)
409            {
410                while (*str)
411            	tolower(*str++);
412            }
413            
414            String ToLower(const String& str)
415            {
416                String tmp(str);
417            
418                for (Uint32 i = 0, n = tmp.getLength(); i < n; i++)
419                {
420            	Char16 c = tmp[i];
421            
422            	if (c <= 127)
423            	    tmp[i] = tolower(c);
424                }
425            
426                return tmp;
427 mike  1.10 }
428            
429            int CompareIgnoreCase(const char* s1, const char* s2)
430            {
431                while (*s1 && *s2)
432                {
433            	int r = tolower(*s1++) - tolower(*s2++);
434            
435            	if (r)
436            	    return r;
437                }
438            
439                if (*s2)
440            	return -1;
441                else if (*s1)
442            	return 1;
443            
444                return 0;
445 mike  1.1  }
446            
447            PEGASUS_NAMESPACE_END

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2