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

Diff for /pegasus/src/Pegasus/Common/String.cpp between version 1.111.6.9 and 1.129.2.1

version 1.111.6.9, 2005/10/11 02:42:11 version 1.129.2.1, 2007/11/08 09:15:06
Line 1 
Line 1 
 //%2005////////////////////////////////////////////////////////////////////////  //%2006////////////////////////////////////////////////////////////////////////
 // //
 // Copyright (c) 2000, 2001, 2002 BMC Software; Hewlett-Packard Development // Copyright (c) 2000, 2001, 2002 BMC Software; Hewlett-Packard Development
 // Company, L.P.; IBM Corp.; The Open Group; Tivoli Systems. // Company, L.P.; IBM Corp.; The Open Group; Tivoli Systems.
Line 8 
Line 8 
 // IBM Corp.; EMC Corporation; VERITAS Software Corporation; The Open Group. // IBM Corp.; EMC Corporation; VERITAS Software Corporation; The Open Group.
 // Copyright (c) 2005 Hewlett-Packard Development Company, L.P.; IBM Corp.; // Copyright (c) 2005 Hewlett-Packard Development Company, L.P.; IBM Corp.;
 // EMC Corporation; VERITAS Software Corporation; The Open Group. // EMC Corporation; VERITAS Software Corporation; The Open Group.
   // Copyright (c) 2006 Hewlett-Packard Development Company, L.P.; IBM Corp.;
   // EMC Corporation; Symantec Corporation; The Open Group.
 // //
 // Permission is hereby granted, free of charge, to any person obtaining a copy // Permission is hereby granted, free of charge, to any person obtaining a copy
 // of this software and associated documentation files (the "Software"), to // of this software and associated documentation files (the "Software"), to
Line 27 
Line 29 
 // //
 //============================================================================== //==============================================================================
 // //
 // Author: Mike Brasher (mbrasher@bmc.com)  
 //  
 // Modified By:  
 //     Roger Kumpf, Hewlett-Packard Company (roger_kumpf@hp.com)  
 //     Josephine Eskaline Joyce, IBM (jojustin@in.ibm.com) for Bug#3297  
 //     David Dillard, VERITAS Software Corp. (david.dillard@veritas.com)  
 //     Mike Brasher (mike-brasher@austin.rr.com)  
 //  
 //%///////////////////////////////////////////////////////////////////////////// //%/////////////////////////////////////////////////////////////////////////////
  
 #include <cassert>  #include <Pegasus/Common/PegasusAssert.h>
   #include <cstring>
 #include "InternalException.h" #include "InternalException.h"
 #include "CommonUTF.h" #include "CommonUTF.h"
 #include "MessageLoader.h" #include "MessageLoader.h"
Line 54 
Line 49 
 // //
 // Compile-time macros (undefined by default). // Compile-time macros (undefined by default).
 // //
 //     PEGASUS_STRING_NO_THROW -- suppresses throwing of exceptions  
 //  
 //     PEGASUS_STRING_NO_UTF8 -- don't generate slower UTF8 code. //     PEGASUS_STRING_NO_UTF8 -- don't generate slower UTF8 code.
 // //
 //============================================================================== //==============================================================================
Line 66 
Line 59 
 // //
 //============================================================================== //==============================================================================
  
   // Note: this table is much faster than the system toupper(). Please do not
   // change.
   
 const Uint8 _toUpperTable[256] = const Uint8 _toUpperTable[256] =
 { {
     0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,     0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
Line 102 
Line 98 
     0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF,     0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF,
 }; };
  
   // Note: this table is much faster than the system tulower(). Please do not
   // change.
   
 const Uint8 _toLowerTable[256] = const Uint8 _toLowerTable[256] =
 { {
     0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,     0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
Line 138 
Line 137 
     0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF,     0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF,
 }; };
  
 // Converts 16-bit characters to upper case.  // Converts 16-bit characters to upper case. This routine is faster than the
   // system toupper(). Please do not change.
 inline Uint16 _toUpper(Uint16 x) inline Uint16 _toUpper(Uint16 x)
 { {
     return (x & 0xFF00) ? x : _toUpperTable[x];     return (x & 0xFF00) ? x : _toUpperTable[x];
 } }
  
 // Converts 16-bit characters to lower case.  // Converts 16-bit characters to lower case. This routine is faster than the
   // system toupper(). Please do not change.
 inline Uint16 _toLower(Uint16 x) inline Uint16 _toLower(Uint16 x)
 { {
     return (x & 0xFF00) ? x : _toLowerTable[x];     return (x & 0xFF00) ? x : _toLowerTable[x];
Line 153 
Line 154 
 // Rounds x up to the nearest power of two (or just returns 8 if x < 8). // Rounds x up to the nearest power of two (or just returns 8 if x < 8).
 static Uint32 _roundUpToPow2(Uint32 x) static Uint32 _roundUpToPow2(Uint32 x)
 { {
 #ifndef PEGASUS_STRING_NO_THROW      // Check for potential overflow in x
       PEGASUS_CHECK_CAPACITY_OVERFLOW(x);
     if (x > 0x0FFFFFFF)  
         throw PEGASUS_STD(bad_alloc)();  
   
 #endif  
  
     if (x < 8)     if (x < 8)
         return 8;         return 8;
Line 177 
Line 174 
 template<class P, class Q> template<class P, class Q>
 static void _copy(P* p, const Q* q, size_t n) static void _copy(P* p, const Q* q, size_t n)
 { {
     // Use loop unrolling.      // The following employs loop unrolling for efficiency. Please do not
       // eliminate.
  
     while (n >= 8)     while (n >= 8)
     {     {
Line 211 
Line 209 
  
 static Uint16* _find(const Uint16* s, size_t n, Uint16 c) static Uint16* _find(const Uint16* s, size_t n, Uint16 c)
 { {
       // The following employs loop unrolling for efficiency. Please do not
       // eliminate.
   
     while (n >= 4)     while (n >= 4)
     {     {
         if (s[0] == c)         if (s[0] == c)
Line 285 
Line 286 
     return c1 - c2;     return c1 - c2;
 } }
  
 static int _compare(const Uint16* s1, const Uint16* s2, size_t n)  
 {  
     // This should only be called when s1 and s2 have the same length.  
   
     while (n-- && (*s1++ - *s2++) == 0)  
         ;  
   
     //  
   
     return s1[-1] - s2[-1];  
 }  
   
 static inline void _copy(Uint16* s1, const Uint16* s2, size_t n) static inline void _copy(Uint16* s1, const Uint16* s2, size_t n)
 { {
     memcpy(s1, s2, n * sizeof(Uint16));     memcpy(s1, s2, n * sizeof(Uint16));
 } }
  
 void StrinThrowOutOfBounds()  void StringThrowOutOfBounds()
 { {
     throw IndexOutOfBoundsException();     throw IndexOutOfBoundsException();
 } }
  
 #ifdef PEGASUS_STRING_NO_THROW  inline void _checkNullPointer(const void* ptr)
 # define _checkNullPointer(ARG) /* empty */  
 #else  
 template<class T>  
 inline void _checkNullPointer(const T* ptr)  
 { {
     if (!ptr)     if (!ptr)
         throw NullPointer();         throw NullPointer();
 } }
 #endif  
  
 static size_t _copyFromUTF8(Uint16* dest, const char* src, size_t n)  static void _StringThrowBadUTF8(Uint32 index)
   {
       MessageLoaderParms parms(
           "Common.String.BAD_UTF8",
           "The byte sequence starting at index $0 "
           "is not valid UTF-8 encoding.",
           index);
       throw Exception(parms);
   }
   
   static size_t _copyFromUTF8(
       Uint16* dest,
       const char* src,
       size_t n,
       size_t& utf8_error_index)
 { {
     Uint16* p = dest;     Uint16* p = dest;
     const Uint8* q = (const Uint8*)src;     const Uint8* q = (const Uint8*)src;
Line 400 
Line 398 
             if (c > n || !isValid_U8(q, c) ||             if (c > n || !isValid_U8(q, c) ||
                 UTF8toUTF16(&q, q + c, &p, p + n) != 0)                 UTF8toUTF16(&q, q + c, &p, p + n) != 0)
             {             {
                 MessageLoaderParms parms("Common.String.BAD_UTF8",                  utf8_error_index = q - (const Uint8*)src;
                     "The byte sequence starting at index $0 "                  return size_t(-1);
                     "is not valid UTF-8 encoding.",  
                      q - (const Uint8*)src);  
                 throw Exception(parms);  
             }             }
  
             n -= c;             n -= c;
Line 418 
Line 413 
 // terminator). // terminator).
 static inline size_t _copyToUTF8(char* dest, const Uint16* src, size_t n) static inline size_t _copyToUTF8(char* dest, const Uint16* src, size_t n)
 { {
       // The following employs loop unrolling for efficiency. Please do not
       // eliminate.
   
     const Uint16* q = src;     const Uint16* q = src;
     Uint8* p = (Uint8*)dest;     Uint8* p = (Uint8*)dest;
  
Line 469 
Line 467 
     return p - (Uint8*)dest;     return p - (Uint8*)dest;
 } }
  
 static inline size_t _convert(Uint16* p, const char* q, size_t n)  static inline size_t _convert(
       Uint16* p, const char* q, size_t n, size_t& utf8_error_index)
 { {
 #ifdef PEGASUS_STRING_NO_UTF8 #ifdef PEGASUS_STRING_NO_UTF8
     _copy(p, q, n);     _copy(p, q, n);
     return n;     return n;
 #else #else
     return _copyFromUTF8(p, q, n);      return _copyFromUTF8(p, q, n, utf8_error_index);
 #endif #endif
 } }
  
Line 526 
Line 525 
  
 inline StringRep* StringRep::alloc(size_t cap) inline StringRep* StringRep::alloc(size_t cap)
 { {
 #ifndef PEGASUS_STRING_NO_THROW      // Check for potential overflow in cap
       PEGASUS_CHECK_CAPACITY_OVERFLOW(cap);
     // Any string bigger than this is seriously suspect.  
     if (cap > 0x0FFFFFFF)  
         throw PEGASUS_STD(bad_alloc)();  
   
 #endif  
  
     StringRep* rep = (StringRep*)::operator new(     StringRep* rep = (StringRep*)::operator new(
         sizeof(StringRep) + cap * sizeof(Uint16));         sizeof(StringRep) + cap * sizeof(Uint16));
     rep->cap = cap;     rep->cap = cap;
     Atomic_create(&rep->refs, 1);      new(&rep->refs) AtomicInt(1);
  
     return rep;     return rep;
 } }
  
 static inline void _reserve(StringRep*& rep, Uint32 cap) static inline void _reserve(StringRep*& rep, Uint32 cap)
 { {
     if (cap > rep->cap || Atomic_get(&rep->refs) != 1)      if (cap > rep->cap || rep->refs.get() != 1)
     {     {
         size_t n = _roundUpToPow2(cap);         size_t n = _roundUpToPow2(cap);
         StringRep* newRep = StringRep::alloc(n);         StringRep* newRep = StringRep::alloc(n);
Line 579 
Line 573 
 StringRep* StringRep::create(const char* data, size_t size) StringRep* StringRep::create(const char* data, size_t size)
 { {
     StringRep* rep = StringRep::alloc(size);     StringRep* rep = StringRep::alloc(size);
     rep->size = _convert((Uint16*)rep->data, data, size);      size_t utf8_error_index;
     rep->data[rep->size] = '\0';      rep->size = _convert((Uint16*)rep->data, data, size, utf8_error_index);
  
     return rep;      if (rep->size == size_t(-1))
       {
           StringRep::free(rep);
           _StringThrowBadUTF8((Uint32)utf8_error_index);
 } }
  
 StringRep* StringRep::createASCII7(const char* data, size_t size)      rep->data[rep->size] = '\0';
 {  
     StringRep* rep = StringRep::alloc(size);  
     _copy((Uint16*)rep->data, data, size);  
     rep->data[rep->size = size] = '\0';  
     return rep;     return rep;
 } }
  
Line 602 
Line 596 
     while (*end++)     while (*end++)
         ;         ;
  
     return end - str - 1;      return (Uint32)(end - str - 1);
 } }
  
 //============================================================================== //==============================================================================
Line 634 
Line 628 
 String::String(const char* str) String::String(const char* str)
 { {
     _checkNullPointer(str);     _checkNullPointer(str);
     _rep = StringRep::create(str, strlen(str));  
 }  
  
 String::String(const char* str, String::ASCII7Tag tag)      // Set this just in case create() throws an exception.
 {      _rep = &StringRep::_emptyRep;
     _checkNullPointer(str);      _rep = StringRep::create(str, strlen(str));
     _rep = StringRep::createASCII7(str, strlen(str));  
 } }
  
 String::String(const char* str, Uint32 n) String::String(const char* str, Uint32 n)
 { {
     _checkNullPointer(str);     _checkNullPointer(str);
     _rep = StringRep::create(str, n);  
 }  
  
 String::String(const char* str, size_t n, String::ASCII7Tag tag)      // Set this just in case create() throws an exception.
 {      _rep = &StringRep::_emptyRep;
     _checkNullPointer(str);      _rep = StringRep::create(str, n);
     _rep = StringRep::createASCII7(str, n);  
 } }
  
 String::String(const String& s1, const String& s2) String::String(const String& s1, const String& s2)
Line 674 
Line 662 
     size_t n2 = strlen(s2);     size_t n2 = strlen(s2);
     _rep = StringRep::alloc(n1 + n2);     _rep = StringRep::alloc(n1 + n2);
     _copy(_rep->data, s1._rep->data, n1);     _copy(_rep->data, s1._rep->data, n1);
     _rep->size = n1 + _convert((Uint16*)_rep->data + n1, s2, n2);      size_t utf8_error_index;
       size_t tmp = _convert((Uint16*)_rep->data + n1, s2, n2, utf8_error_index);
   
       if (tmp == size_t(-1))
       {
           StringRep::free(_rep);
           _rep = &StringRep::_emptyRep;
           _StringThrowBadUTF8((Uint32)utf8_error_index);
       }
   
       _rep->size = n1 + tmp;
     _rep->data[_rep->size] = '\0';     _rep->data[_rep->size] = '\0';
 } }
  
Line 684 
Line 682 
     size_t n1 = strlen(s1);     size_t n1 = strlen(s1);
     size_t n2 = s2._rep->size;     size_t n2 = s2._rep->size;
     _rep = StringRep::alloc(n1 + n2);     _rep = StringRep::alloc(n1 + n2);
     _rep->size = n2 + _convert((Uint16*)_rep->data, s1, n1);      size_t utf8_error_index;
       size_t tmp = _convert((Uint16*)_rep->data, s1, n1, utf8_error_index);
   
       if (tmp ==  size_t(-1))
       {
           StringRep::free(_rep);
           _rep = &StringRep::_emptyRep;
           _StringThrowBadUTF8((Uint32)utf8_error_index);
       }
   
       _rep->size = n2 + tmp;
     _copy(_rep->data + n1, s2._rep->data, n2);     _copy(_rep->data + n1, s2._rep->data, n2);
     _rep->data[_rep->size] = '\0';     _rep->data[_rep->size] = '\0';
 } }
Line 704 
Line 712 
 { {
     _checkNullPointer(str);     _checkNullPointer(str);
  
     if (n > _rep->cap || Atomic_get(&_rep->refs) != 1)      if (n > _rep->cap || _rep->refs.get() != 1)
     {     {
         StringRep::unref(_rep);         StringRep::unref(_rep);
         _rep = StringRep::alloc(n);         _rep = StringRep::alloc(n);
Line 721 
Line 729 
 { {
     _checkNullPointer(str);     _checkNullPointer(str);
  
     if (n > _rep->cap || Atomic_get(&_rep->refs) != 1)      if (n > _rep->cap || _rep->refs.get() != 1)
     {     {
         StringRep::unref(_rep);         StringRep::unref(_rep);
         _rep = StringRep::alloc(n);         _rep = StringRep::alloc(n);
     }     }
  
     _rep->size = _convert(_rep->data, str, n);      size_t utf8_error_index;
     _rep->data[_rep->size] = 0;      _rep->size = _convert(_rep->data, str, n, utf8_error_index);
   
     return *this;  
 }  
   
 String& String::assignASCII7(const char* str, Uint32 n)  
 {  
     _checkNullPointer(str);  
  
     if (n > _rep->cap || Atomic_get(&_rep->refs) != 1)      if (_rep->size ==  size_t(-1))
     {     {
         StringRep::unref(_rep);          StringRep::free(_rep);
         _rep = StringRep::alloc(n);          _rep = &StringRep::_emptyRep;
           _StringThrowBadUTF8((Uint32)utf8_error_index);
     }     }
  
     _copy(_rep->data, str, n);      _rep->data[_rep->size] = 0;
     _rep->data[_rep->size = n] = 0;  
  
     return *this;     return *this;
 } }
Line 753 
Line 754 
 { {
     if (_rep->size)     if (_rep->size)
     {     {
         if (Atomic_get(&_rep->refs) == 1)          if (_rep->refs.get() == 1)
         {         {
             _rep->size = 0;             _rep->size = 0;
             _rep->data[0] = '\0';             _rep->data[0] = '\0';
Line 773 
Line 774 
  
 CString String::getCString() const CString String::getCString() const
 { {
       // A UTF8 string can have three times as many characters as its UTF16
       // counterpart, so we allocate extra memory for the worst case. In the
       // best case, we may need only one third of the memory allocated. But
       // downsizing the string afterwards is expensive and unecessary since
       // CString objects are usually short-lived (disappearing after only a few
       // instructions). CString objects are typically created on the stack as
       // means to obtain a char* pointer.
   
 #ifdef PEGASUS_STRING_NO_UTF8 #ifdef PEGASUS_STRING_NO_UTF8
     char* str = (char*)operator new(_rep->size + 1);     char* str = (char*)operator new(_rep->size + 1);
     _copy(str, _rep->data, _rep->size);     _copy(str, _rep->data, _rep->size);
     str[_rep->size] = '\0';     str[_rep->size] = '\0';
     return CString(str);     return CString(str);
 #else #else
     Uint32 n = 3 * _rep->size;      Uint32 n = (Uint32)(3 * _rep->size);
     char* str = (char*)operator new(n + 1);     char* str = (char*)operator new(n + 1);
     size_t size = _copyToUTF8(str, _rep->data, _rep->size);     size_t size = _copyToUTF8(str, _rep->data, _rep->size);
     str[size] = '\0';     str[size] = '\0';
Line 793 
Line 802 
  
     size_t oldSize = _rep->size;     size_t oldSize = _rep->size;
     size_t newSize = oldSize + n;     size_t newSize = oldSize + n;
     _reserve(_rep, newSize);      _reserve(_rep, (Uint32)newSize);
     _copy(_rep->data + oldSize, (Uint16*)str, n);     _copy(_rep->data + oldSize, (Uint16*)str, n);
     _rep->size = newSize;     _rep->size = newSize;
     _rep->data[newSize] = '\0';     _rep->data[newSize] = '\0';
Line 803 
Line 812 
  
 String& String::append(const String& str) String& String::append(const String& str)
 { {
     return append((Char16*)str._rep->data, str._rep->size);      return append((Char16*)(&(str._rep->data[0])), (Uint32)str._rep->size);
 } }
  
 String& String::append(const char* str, Uint32 size) String& String::append(const char* str, Uint32 size)
Line 813 
Line 822 
     size_t oldSize = _rep->size;     size_t oldSize = _rep->size;
     size_t cap = oldSize + size;     size_t cap = oldSize + size;
  
     _reserve(_rep, cap);      _reserve(_rep, (Uint32)cap);
     _rep->size += _convert((Uint16*)_rep->data + oldSize, str, size);      size_t utf8_error_index;
       size_t tmp = _convert(
           (Uint16*)_rep->data + oldSize, str, size, utf8_error_index);
   
       if (tmp ==  size_t(-1))
       {
           StringRep::free(_rep);
           _rep = &StringRep::_emptyRep;
           _StringThrowBadUTF8((Uint32)utf8_error_index);
       }
   
       _rep->size += tmp;
     _rep->data[_rep->size] = '\0';     _rep->data[_rep->size] = '\0';
  
     return *this;     return *this;
Line 823 
Line 843 
 void String::remove(Uint32 index, Uint32 n) void String::remove(Uint32 index, Uint32 n)
 { {
     if (n == PEG_NOT_FOUND)     if (n == PEG_NOT_FOUND)
         n = _rep->size - index;          n = (Uint32)(_rep->size - index);
  
     _checkBounds(index + n, _rep->size);     _checkBounds(index + n, _rep->size);
  
     if (Atomic_get(&_rep->refs) != 1)      if (_rep->refs.get() != 1)
         _rep = StringRep::copyOnWrite(_rep);         _rep = StringRep::copyOnWrite(_rep);
  
     assert(index + n <= _rep->size);      PEGASUS_ASSERT(index + n <= _rep->size);
  
     size_t rem = _rep->size - (index + n);     size_t rem = _rep->size - (index + n);
     Uint16* data = _rep->data;     Uint16* data = _rep->data;
Line 850 
Line 870 
     if (index < _rep->size)     if (index < _rep->size)
     {     {
         if (n == PEG_NOT_FOUND || n > _rep->size - index)         if (n == PEG_NOT_FOUND || n > _rep->size - index)
             n = _rep->size - index;              n = (Uint32)(_rep->size - index);
  
         return String((Char16*)_rep->data + index, n);          return String((Char16*)(_rep->data + index), n);
     }     }
  
     return String();     return String();
Line 863 
Line 883 
     Uint16* p = (Uint16*)_find(_rep->data, _rep->size, c);     Uint16* p = (Uint16*)_find(_rep->data, _rep->size, c);
  
     if (p)     if (p)
         return p - _rep->data;          return static_cast<Uint32>(p - _rep->data);
  
     return PEG_NOT_FOUND;     return PEG_NOT_FOUND;
 } }
Line 878 
Line 898 
     Uint16* p = (Uint16*)_find(_rep->data + index, _rep->size - index, c);     Uint16* p = (Uint16*)_find(_rep->data + index, _rep->size - index, c);
  
     if (p)     if (p)
         return p - _rep->data;          return static_cast<Uint32>(p - _rep->data);
  
     return PEG_NOT_FOUND;     return PEG_NOT_FOUND;
 } }
Line 899 
Line 919 
             break;             break;
  
         if (memcmp(p, s, n * sizeof(Uint16)) == 0)         if (memcmp(p, s, n * sizeof(Uint16)) == 0)
             return p - _rep->data;              return static_cast<Uint32>(p - _rep->data);
  
         p++;         p++;
         rem -= p - data;         rem -= p - data;
Line 927 
Line 947 
     while (q != p)     while (q != p)
     {     {
         if (*--q == x)         if (*--q == x)
             return q - p;              return static_cast<Uint32>(q - p);
     }     }
  
     return PEG_NOT_FOUND;     return PEG_NOT_FOUND;
Line 939 
Line 959 
  
     if (InitializeICU::initICUSuccessful())     if (InitializeICU::initICUSuccessful())
     {     {
         if (Atomic_get(&_rep->refs) != 1)          if (_rep->refs.get() != 1)
             _rep = StringRep::copyOnWrite(_rep);             _rep = StringRep::copyOnWrite(_rep);
  
         // This will do a locale-insensitive, but context-sensitive convert.         // This will do a locale-insensitive, but context-sensitive convert.
Line 974 
Line 994 
  
 #endif /* PEGASUS_HAS_ICU */ #endif /* PEGASUS_HAS_ICU */
  
     if (Atomic_get(&_rep->refs) != 1)      if (_rep->refs.get() != 1)
         _rep = StringRep::copyOnWrite(_rep);         _rep = StringRep::copyOnWrite(_rep);
  
     Uint16* p = _rep->data;     Uint16* p = _rep->data;
Line 993 
Line 1013 
  
     if (InitializeICU::initICUSuccessful())     if (InitializeICU::initICUSuccessful())
     {     {
         if (Atomic_get(&_rep->refs) != 1)          if (_rep->refs.get() != 1)
             _rep = StringRep::copyOnWrite(_rep);             _rep = StringRep::copyOnWrite(_rep);
  
         // This will do a locale-insensitive, but context-sensitive convert.         // This will do a locale-insensitive, but context-sensitive convert.
Line 1029 
Line 1049 
  
 #endif /* PEGASUS_HAS_ICU */ #endif /* PEGASUS_HAS_ICU */
  
     if (Atomic_get(&_rep->refs) != 1)      if (_rep->refs.get() != 1)
         _rep = StringRep::copyOnWrite(_rep);         _rep = StringRep::copyOnWrite(_rep);
  
     Uint16* p = _rep->data;     Uint16* p = _rep->data;
Line 1041 
Line 1061 
  
 int String::compare(const String& s1, const String& s2, Uint32 n) int String::compare(const String& s1, const String& s2, Uint32 n)
 { {
     assert(n <= s1._rep->size);      const Uint16* p1 = s1._rep->data;
     assert(n <= s2._rep->size);      const Uint16* p2 = s2._rep->data;
  
     // Ignoring error in which n is greater than s1.size() or s2.size()      while (n--)
     return _compare(s1._rep->data, s2._rep->data, n);      {
           int r = *p1++ - *p2++;
           if (r)
           {
               return r;
           }
           else if (!p1[-1])
           {
               // We must have encountered a null terminator in both s1 and s2
               return 0;
           }
       }
       return 0;
 } }
  
 int String::compare(const String& s1, const String& s2) int String::compare(const String& s1, const String& s2)
Line 1072 
Line 1104 
     if (InitializeICU::initICUSuccessful())     if (InitializeICU::initICUSuccessful())
     {     {
         return  u_strcasecmp(         return  u_strcasecmp(
             str1._rep->data, str2._rep->data, U_FOLD_CASE_DEFAULT);              (const UChar*)str1._rep->data,
               (const UChar*)str2._rep->data,
               U_FOLD_CASE_DEFAULT
               );
     }     }
  
 #endif /* PEGASUS_HAS_ICU */ #endif /* PEGASUS_HAS_ICU */
Line 1104 
Line 1139 
  
 #else /* PEGASUS_HAS_ICU */ #else /* PEGASUS_HAS_ICU */
  
       // The following employs loop unrolling for efficiency. Please do not
       // eliminate.
   
     Uint16* p = (Uint16*)s1.getChar16Data();     Uint16* p = (Uint16*)s1.getChar16Data();
     Uint16* q = (Uint16*)s2.getChar16Data();     Uint16* q = (Uint16*)s2.getChar16Data();
     Uint32 n = s2.size();     Uint32 n = s2.size();
Line 1194 
Line 1232 
  
 Boolean String::equal(const String& s1, const String& s2) Boolean String::equal(const String& s1, const String& s2)
 { {
     return s1._rep->size == s2._rep->size && memcmp(s1._rep->data,      return (s1._rep == s2._rep) ||
         s2._rep->data, s1._rep->size * sizeof(Uint16)) == 0;          (s1._rep->size == s2._rep->size) &&
           memcmp(s1._rep->data,
                  s2._rep->data,
                  s1._rep->size * sizeof(Uint16)) == 0;
 } }
  
 Boolean String::equal(const String& s1, const char* s2) Boolean String::equal(const String& s1, const char* s2)
Line 1224 
Line 1265 
  
 PEGASUS_STD(ostream)& operator<<(PEGASUS_STD(ostream)& os, const String& str) PEGASUS_STD(ostream)& operator<<(PEGASUS_STD(ostream)& os, const String& str)
 { {
 #if defined(PEGASUS_OS_OS400)  
   
     CString cstr = str.getCString();  
     const char* utf8str = cstr;  
     os << utf8str;  
     return os;  
 #else  
   
 #if defined(PEGASUS_HAS_ICU) #if defined(PEGASUS_HAS_ICU)
  
     if (InitializeICU::initICUSuccessful())     if (InitializeICU::initICUSuccessful())
Line 1267 
Line 1300 
     }     }
  
     return os;     return os;
 #endif // PEGASUS_OS_OS400  
 } }
  
 void StringAppendCharAux(StringRep*& _rep) void StringAppendCharAux(StringRep*& _rep)
Line 1290 
Line 1322 
     _rep = tmp;     _rep = tmp;
 } }
  
   void AssignASCII(String& s, const char* str, Uint32 n)
   {
       class StringLayout
       {
       public:
           StringRep* rep;
       };
   
       StringLayout* that = (StringLayout*)&s;
   
       _checkNullPointer(str);
   
       if (n > that->rep->cap || that->rep->refs.get() != 1)
       {
           StringRep::unref(that->rep);
           that->rep = StringRep::alloc(n);
       }
   
       _copy(that->rep->data, str, n);
       that->rep->size = n;
       that->rep->data[that->rep->size] = 0;
   }
   
 PEGASUS_NAMESPACE_END PEGASUS_NAMESPACE_END
  
 /* /*
Line 1418 
Line 1473 
         This avoids slower UTF8 processing when not needed.         This avoids slower UTF8 processing when not needed.
  
 ================================================================================ ================================================================================
   
 TO-DO:  
   
     (+) [DONE] Use PEGASUS_USE_EXPERIMENTAL_INTERFACES  
   
     (+) [DONE] Submit BUG-2754 (Windows buffer limit).  
   
     (+) [DONE] Eliminate char versions of find() and append().  
   
     (+) [DONE] Remove PEGASUS_MAX_PRINTABLE_CHARACTER from Config.h  
   
     (+) [DONE] Change _next_pow_2() to _roundUpToPow2().  
   
     (+) [DONE] Change '99' to '2' in StringRep constructor (comment as well).  
   
     (+) [DONE] Comment StringRep allocation layout.  
   
     (+) [DONE] Conceal private inline functions.  
   
     (+) [DONE] Shorten inclusion of StringInline.h in String.h.  
   
     (+) [DONE] Change USE_INTERNAL_INLINE TO DISABLE_INTERNAL_INLINE or get  
         rid of altogether.  
   
     (+) [DONE] useCamelNotationOnAllFunctionNames.  
   
     (+) [DONE] Check for overlow condition in StringRep::alloc().  
   
     (+) [DONE] Remove tabs (used vim ":set expandtab" and ":retab").  
   
     -----------  
   
     (+) Fix throw-related memory leak.  
   
     (+) DOC++ String.h  
   
     (+) Look at PEP223 for coding security guidelines.  
   
     (+) Replace AtomicInt with new Atomic implementation.  
   
     (+) Implement Atomic operations for HP.  
   
 ================================================================================  
 */ */


Legend:
Removed from v.1.111.6.9  
changed lines
  Added in v.1.129.2.1

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2