(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.28.2.6 and 1.50

version 1.28.2.6, 2001/12/04 16:35:46 version 1.50, 2002/08/26 21:53:45
Line 1 
Line 1 
 //%///////////////////////////////////////////////////////////////////////////// //%/////////////////////////////////////////////////////////////////////////////
 // //
 // Copyright (c) 2000, 2001 The Open group, BMC Software, Tivoli Systems, IBM  // Copyright (c) 2000, 2001, 2002 BMC Software, Hewlett-Packard Company, IBM,
   // The Open Group, Tivoli Systems
 // //
 // 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 22 
Line 23 
 // //
 // Author: Mike Brasher (mbrasher@bmc.com) // Author: Mike Brasher (mbrasher@bmc.com)
 // //
 // Modified By:  // Modified By: Roger Kumpf, Hewlett-Packard Company (roger_kumpf@hp.com)
 // //
 //%///////////////////////////////////////////////////////////////////////////// //%/////////////////////////////////////////////////////////////////////////////
  
  
 #include <cctype> #include <cctype>
 #include "String.h" #include "String.h"
 #include "Exception.h"  #include "Array.h"
 #include "String.h"  #include "InternalException.h"
 #include <iostream> #include <iostream>
  
 PEGASUS_USING_STD; PEGASUS_USING_STD;
  
 PEGASUS_NAMESPACE_BEGIN PEGASUS_NAMESPACE_BEGIN
  
 #define PEGASUS_ARRAY_T String  ///////////////////////////////////////////////////////////////////////////////
 #include <Pegasus/Common/ArrayImpl.h>  //
 #undef PEGASUS_ARRAY_T  // String
   //
   ///////////////////////////////////////////////////////////////////////////////
   
   const String String::EMPTY = String();
   
   Uint32 _strnlen(const char* str, Uint32 n)
   {
       if (!str)
           throw NullPointer();
   
       for (Uint32 i=0; i<n; i++)
       {
           if (!*str)
           {
               return i;
           }
       }
   
       return n;
   }
  
 const String String::EMPTY;  Uint32 _strnlen(const Char16* str, Uint32 n)
   {
       if (!str)
           throw NullPointer();
  
 static inline void _SkipWhitespace(const Char16*& p)      for (Uint32 i=0; i<n; i++)
       {
           if (!*str)
 { {
     while (*p && isspace(*p))              return i;
         p++;          }
       }
   
       return n;
 } }
  
 inline Uint32 StrLen(const char* str)  inline Uint32 _StrLen(const char* str)
 { {
     if (!str)     if (!str)
         throw NullPointer();         throw NullPointer();
Line 57 
Line 86 
     return strlen(str);     return strlen(str);
 } }
  
 inline Uint32 StrLen(const Char16* str)  inline Uint32 _StrLen(const Char16* str)
 { {
     if (!str)     if (!str)
         throw NullPointer();         throw NullPointer();
Line 70 
Line 99 
     return n;     return n;
 } }
  
   class StringRep
   {
   public:
       StringRep()
       {}
       StringRep(const StringRep& r)
           : c16a(r.c16a)
       {}
       StringRep(const Char16* str)
           : c16a(str, _StrLen(str) + 1)
       {}
   
       Array<Char16> c16a;
   };
   
 String::String() String::String()
 { {
     _rep.append('\0');      _rep = new StringRep;
       _rep->c16a.append('\0');
 } }
  
 String::String(const String& x) : _rep(x._rep)  String::String(const String& str)
 { {
       _rep = new StringRep(*str._rep);
 } }
  
 String::String(const String& x, Uint32 n)  String::String(const String& str, Uint32 n)
 { {
     _rep.append('\0');      _rep = new StringRep;
     append(x.getData(), n);      assign(str.getData(), n);
 } }
  
 String::String(const Char16* x) : _rep(x, StrLen(x) + 1)  String::String(const Char16* str)
 { {
       _rep = new StringRep(str);
 } }
  
 String::String(const Char16* x, Uint32 n)  String::String(const Char16* str, Uint32 n)
 { {
     assign(x, n);      _rep = new StringRep;
       assign(str, n);
 } }
  
 String::String(const char* str) String::String(const char* str)
 { {
     Uint32 n = ::strlen(str) + 1;      _rep = new StringRep;
     reserve(n);      assign(str);
   }
  
     while (n--)  String::String(const char* str, Uint32 n)
         _rep.append(*str++);  {
       _rep = new StringRep;
       assign(str, n);
 } }
  
 String::String(const char* str, Uint32 n_)  String::~String()
 { {
     Uint32 n = PEG_min(strlen(str), n_);      delete _rep;
     reserve(n + 1);  }
  
     while (n--)  String& String::operator=(const String& str)
         _rep.append(*str++);  {
       return assign(str);
   }
  
     _rep.append('\0');  String& String::assign(const String& str)
   {
       _rep->c16a = str._rep->c16a;
       return *this;
 } }
  
 String& String::assign(const Char16* x)  String& String::assign(const Char16* str)
 { {
     _rep.clear();      _rep->c16a.clear();
     _rep.append(x, StrLen(x) + 1);      _rep->c16a.append(str, _StrLen(str) + 1);
     return *this;     return *this;
 } }
  
 String& String::assign(const Char16* str, Uint32 n) String& String::assign(const Char16* str, Uint32 n)
 { {
     _rep.clear();      _rep->c16a.clear();
     Uint32 m = PEG_min(StrLen(str), n);      Uint32 m = _strnlen(str, n);
     _rep.append(str, m);      _rep->c16a.append(str, m);
     _rep.append('\0');      _rep->c16a.append('\0');
     return *this;     return *this;
 } }
  
 String& String::assign(const char* x)  String& String::assign(const char* str)
 { {
     _rep.clear();      _rep->c16a.clear();
     Uint32 n = strlen(x);  
     _rep.reserve(n + 1);  
  
     while (n--)      Uint32 n = strlen(str) + 1;
         _rep.append(*x++);      _rep->c16a.reserveCapacity(n);
  
     _rep.append('\0');      while (n--)
           _rep->c16a.append(*str++);
  
     return *this;     return *this;
 } }
  
 String& String::assign(const char* x, Uint32 n_)  String& String::assign(const char* str, Uint32 n)
 { {
     _rep.clear();      _rep->c16a.clear();
  
     Uint32 n = PEG_min(strlen(x), n_);      Uint32 _n = _strnlen(str, n);
     _rep.reserve(n + 1);      _rep->c16a.reserveCapacity(_n + 1);
  
     while (n--)      while (_n--)
         _rep.append(*x++);          _rep->c16a.append(*str++);
  
     _rep.append('\0');      _rep->c16a.append('\0');
  
     return *this;     return *this;
 } }
  
 char* String::allocateCString(Uint32 extraBytes, Boolean noThrow) const  void String::clear()
 { {
     Uint32 n = size() + 1;      _rep->c16a.clear();
     char* str = new char[n + extraBytes];      _rep->c16a.append('\0');
     char* p = str;  }
     const Char16* q = getData();  
  
     for (Uint32 i = 0; i < n; i++)  void String::reserveCapacity(Uint32 capacity)
     {     {
         Uint16 c = *q++;      _rep->c16a.reserveCapacity(capacity + 1);
         *p++ = char(c);  
   
         if ((c & 0xff00) && !noThrow)  
             throw TruncatedCharacter();  
     }     }
  
     return str;  Uint32 String::size() const
   {
       return _rep->c16a.size() - 1;
 } }
  
 void String::appendToCString(  const Char16* String::getData() const
     char* str,  
     Uint32 length,  
     Boolean noThrow) const  
 { {
     if (!str)      return _rep->c16a.getData();
         throw NullPointer();  }
   
     Uint32 n = PEG_min(size(), length);  
  
     char* p = str + strlen(str);  char* String::allocateCString(Uint32 extraBytes, Boolean& truncatedCharacters) const
   {
       truncatedCharacters = false;
       Uint32 n = size() + 1;
       char* str = new char[n + extraBytes];
       char* p = str;
     const Char16* q = getData();     const Char16* q = getData();
  
     for (Uint32 i = 0; i < n; i++)     for (Uint32 i = 0; i < n; i++)
Line 198 
Line 248 
         Uint16 c = *q++;         Uint16 c = *q++;
         *p++ = char(c);         *p++ = char(c);
  
         if ((c & 0xff00) && !noThrow)          if (c & 0xff00)
             throw TruncatedCharacter();              truncatedCharacters = true;
       }
   
       return str;
     }     }
  
     *p = '\0';  char* String::allocateCString(Uint32 extraBytes) const
   {
       Boolean truncatedCharacters = false;
       return allocateCString(extraBytes, truncatedCharacters);
 } }
  
 Char16& String::operator[](Uint32 i) Char16& String::operator[](Uint32 i)
 { {
     if (i > size())     if (i > size())
         ThrowOutOfBounds();          throw IndexOutOfBoundsException();
  
     return _rep[i];      return _rep->c16a[i];
 } }
  
 const Char16 String::operator[](Uint32 i) const const Char16 String::operator[](Uint32 i) const
 { {
     if (i > size())     if (i > size())
         ThrowOutOfBounds();          throw IndexOutOfBoundsException();
  
     return _rep[i];      return _rep->c16a[i];
 } }
  
 String& String::append(const Char16* str, Uint32 n)  String& String::append(const Char16& c)
 { {
     Uint32 m = PEG_min(StrLen(str), n);      _rep->c16a.insert(_rep->c16a.size() - 1, c);
     _rep.reserve(_rep.size() + m);  
     _rep.remove(_rep.size() - 1);  
     _rep.append(str, m);  
     _rep.append('\0');  
     return *this;     return *this;
 } }
  
 void String::remove(Uint32 pos, Uint32 size)  String& String::append(const Char16* str, Uint32 n)
 {  
     if (size == PEG_NOT_FOUND)  
         size = this->size() - pos;  
   
     if (pos + size > this->size())  
         ThrowOutOfBounds();  
   
     if (size)  
         _rep.remove(pos, size);  
 }  
   
 int String::compare(const Char16* s1, const Char16* s2, Uint32 n)  
 {  
     while (n--)  
     {  
         int r = *s1++ - *s2++;  
   
         if (r)  
             return r;  
     }  
   
     return 0;  
 }  
   
 int String::compareNoCase(const char* s1, const char* s2, Uint32 n)  
 {  
     while (n--)  
     {  
         int r = tolower(*s1++) - tolower(*s2++);  
   
         if (r)  
             return r;  
     }  
   
     return 0;  
 }  
   
 Boolean String::equal(const String& x, const String& y)  
 { {
     if (x.size() != y.size())      Uint32 m = _strnlen(str, n);
         return false;      _rep->c16a.reserveCapacity(_rep->c16a.size() + m);
       _rep->c16a.remove(_rep->c16a.size() - 1);
     return String::compare(x.getData(), y.getData(), x.size()) == 0;      _rep->c16a.append(str, m);
       _rep->c16a.append('\0');
       return *this;
 } }
  
 Boolean String::equal(const String& x, const Char16* y)  String& String::append(const String& str)
 { {
     if (x.size() != StrLen(y))      return append(str.getData(), str.size());
         return false;  
   
     return String::compare(x.getData(), y, x.size()) == 0;  
 } }
  
 Boolean String::equal(const Char16* x, const String& y)  String& String::operator+=(const String& str)
 { {
     return equal(y, x);      return append(str);
 } }
  
 Boolean String::equal(const String& x, const char* y)  String& String::operator+=(Char16 c)
 { {
     return equal(x, String(y));      return append(c);
 } }
  
 Boolean String::equal(const char* x, const String& y)  String& String::operator+=(char c)
 { {
     return equal(String(x), y);      return append(Char16(c));
 } }
  
 Boolean String::equalNoCase(const String& x, const String& y)  void String::remove(Uint32 pos, Uint32 size)
 { {
     if (x.size() != y.size())      if (size == PEG_NOT_FOUND)
         return false;          size = this->size() - pos;
   
     const Char16* p = x.getData();  
     const Char16* q = y.getData();  
   
     Uint32 n = x.size();  
  
     while (n--)      if (pos + size > this->size())
     {          throw IndexOutOfBoundsException();
 #ifdef PEGASUS_HAS_EBCDIC  
         if (*p <= 255 && *q <= 255)  
 #else  
         if (*p <= 127 && *q <= 127)  
 #endif  
         {  
             if (tolower(*p++) != tolower(*q++))  
                 return false;  
         }  
         else if (*p++ != *q++)  
             return false;  
     }  
  
     return true;      if (size)
           _rep->c16a.remove(pos, size);
 } }
  
 String String::subString(Uint32 pos, Uint32 length) const String String::subString(Uint32 pos, Uint32 length) const
Line 400 
Line 397 
     return PEG_NOT_FOUND;     return PEG_NOT_FOUND;
 } }
  
 // ATTN:KS 5 apr 2000 Need to add the Char16* version.  
 Uint32 String::find(const char* s) const  
 {  
     return find(String(s));  
 }  
   
 Uint32 String::reverseFind(Char16 c) const Uint32 String::reverseFind(Char16 c) const
 { {
     const Char16* first = getData();     const Char16* first = getData();
Line 422 
Line 413 
  
 void String::toLower() void String::toLower()
 { {
     for (Char16* p = &_rep[0]; *p; p++)      for (Char16* p = &_rep->c16a[0]; *p; p++)
     {     {
 #ifdef PEGASUS_HAS_EBCDIC          if (*p <= PEGASUS_MAX_PRINTABLE_CHAR)
         if (*p <= 255)  
 #else  
         if (*p <= 127)  
 #endif  
             *p = tolower(*p);             *p = tolower(*p);
     }     }
 } }
  
 void String::translate(Char16 fromChar, Char16 toChar)  int String::compare(const String& s1, const String& s2, Uint32 n)
 {  
     for (Char16* p = &_rep[0]; *p; p++)  
     {     {
         if (*p == fromChar)      const Char16* s1c16 = s1.getData();
             *p = toChar;      const Char16* s2c16 = s2.getData();
     }  
 }  
  
 int String::compare(const Char16* s1, const Char16* s2)      while (n--)
 {  
     while (*s1 && *s2)  
     {     {
         int r = *s1++ - *s2++;          int r = *s1c16++ - *s2c16++;
  
         if (r)         if (r)
             return r;             return r;
     }     }
  
     if (*s2)  
         return -1;  
     else if (*s1)  
         return 1;  
   
     return 0;     return 0;
 } }
  
 int String::compareNoCase(const char* s1, const char* s2)  int String::compare(const String& s1, const String& s2)
 { {
     while (*s1 && *s2)      const Char16* s1c16 = s1.getData();
       const Char16* s2c16 = s2.getData();
   
       while (*s1c16 && *s2c16)
     {     {
         int r = tolower(*s1++) - tolower(*s2++);          int r = *s1c16++ - *s2c16++;
  
         if (r)         if (r)
             return r;             return r;
     }     }
  
     if (*s2)      if (*s2c16)
         return -1;         return -1;
     else if (*s1)      else if (*s1c16)
         return 1;         return 1;
  
     return 0;     return 0;
 } }
  
 PEGASUS_STD(ostream)& operator<<(PEGASUS_STD(ostream)& os, const String& x)  int String::compareNoCase(const char* s1, const char* s2, Uint32 n)
 {  
     for (Uint32 i = 0, n = x.size(); i < n; i++)  
         os << x[i];  
   
     return os;  
 }  
   
 void String::toLower(char* str)  
 {  
     while (*str)  
         tolower(*str++);  
 }  
   
 String ToLower(const String& str)  
 { {
     String tmp(str);      while (n--)
   
     for (Uint32 i = 0, n = tmp.size(); i < n; i++)  
     {     {
         Char16 c = tmp[i];          int r = tolower(*s1++) - tolower(*s2++);
  
 #ifdef PEGASUS_HAS_EBCDIC          if (r)
         if (c <= 255)              return r;
 #else  
         if (c <= 127)  
 #endif  
             tmp[i] = tolower(c);  
     }     }
  
     return tmp;      return 0;
 } }
  
 int CompareNoCase(const char* s1, const char* s2)  int String::compareNoCase(const char* s1, const char* s2)
 { {
     while (*s1 && *s2)     while (*s1 && *s2)
     {     {
Line 529 
Line 488 
     return 0;     return 0;
 } }
  
 Boolean GetLine(PEGASUS_STD(istream)& is, String& line)  int String::compareNoCase(const String& s1, const String& s2)
 { {
     line.clear();      const Char16* _s1 = s1.getData();
       const Char16* _s2 = s2.getData();
  
     Boolean gotChar = false;      while (*_s1 && *_s2)
     char c;  
   
     while (is.get(c))  
     {     {
         gotChar = true;          int r;
   
         if (c == '\n')  
             break;  
  
         line.append(c);          if (*_s1 <= PEGASUS_MAX_PRINTABLE_CHAR &&
               *_s2 <= PEGASUS_MAX_PRINTABLE_CHAR)
           {
               r = tolower(*_s1++) - tolower(*_s2++);
           }
           else
           {
               r = *_s1++ - *_s2++;
     }     }
  
     return gotChar;          if (r)
               return r;
 } }
  
 String::~String()      if (*_s2)
 {          return -1;
       else if (*_s1)
           return 1;
   
       return 0;
 } }
  
 String& String::assign(const String& x)  Boolean String::equal(const String& str1, const String& str2)
 { {
     _rep = x._rep;      return String::compare(str1, str2) == 0;
     return *this;  
 } }
  
 String& String::append(const Char16& c)  Boolean String::equalNoCase(const String& str1, const String& str2)
 { {
     _rep.insert(_rep.size() - 1, c);      if (str1.size() != str2.size())
     return *this;          return false;
 }  
  
 void String::clear()      const Char16* p = str1.getData();
       const Char16* q = str2.getData();
   
       Uint32 n = str1.size();
   
       while (n--)
 { {
     _rep.clear();          if (*p <= PEGASUS_MAX_PRINTABLE_CHAR &&
     _rep.append('\0');              *q <= PEGASUS_MAX_PRINTABLE_CHAR)
           {
               if (tolower(*p++) != tolower(*q++))
                   return false;
           }
           else if (*p++ != *q++)
               return false;
 } }
  
 void String::print() const      return true;
 {  
     cout << *this << endl;  
 } }
  
 void String::reserve(Uint32 capacity)  
 {  // ATTN-RK-P3-20020603: This code is not completely correct
     _rep.reserve(capacity + 1);   // Wildcard String matching function that may be useful in the future
 }  // The following code was provided by Bob Blair.
   
 const Array<String>& EmptyStringArray()  /* _StringMatch Match input MatchString against a GLOB style pattern
 {         Note that MatchChar is the char type so that this source
     static Array<String> tmp;         in portable to different string types. This is an internal function
     return tmp;  
 }    Results: The return value is 1 if string matches pattern, and
           0 otherwise.  The matching operation permits the following
 ////////////////////////////////////////////////////////////////////////////////          special characters in the pattern: *?\[] (see the manual
 //          entry for details on what these mean).
 // String matching routines borrowed from Tcl 8.0:  
 //    Side effects: None.
 ////////////////////////////////////////////////////////////////////////////////  
   
 ////////////////////////////////////////////////////////////////////////////////  
 //  
 // This software is copyrighted by the Regents of the University of  
 // California, Sun Microsystems, Inc., and other parties.  The following  
 // terms apply to all files associated with the software unless explicitly  
 // disclaimed in individual files.  
 //  
 // The authors hereby grant permission to use, copy, modify, distribute,  
 // and license this software and its documentation for any purpose, provided  
 // that existing copyright notices are retained in all copies and that this  
 // notice is included verbatim in any distributions. No written agreement,  
 // license, or royalty fee is required for any of the authorized uses.  
 // Modifications to this software may be copyrighted by their authors  
 // and need not follow the licensing terms described here, provided that  
 // the new terms are clearly indicated on the first page of each file where  
 // they apply.  
 //  
 // IN NO EVENT SHALL THE AUTHORS OR DISTRIBUTORS BE LIABLE TO ANY PARTY  
 // FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES  
 // ARISING OUT OF THE USE OF THIS SOFTWARE, ITS DOCUMENTATION, OR ANY  
 // DERIVATIVES THEREOF, EVEN IF THE AUTHORS HAVE BEEN ADVISED OF THE  
 // POSSIBILITY OF SUCH DAMAGE.  
 //  
 // THE AUTHORS AND DISTRIBUTORS SPECIFICALLY DISCLAIM ANY WARRANTIES,  
 // INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY,  
 // FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT.  THIS SOFTWARE  
 // IS PROVIDED ON AN "AS IS" BASIS, AND THE AUTHORS AND DISTRIBUTORS HAVE  
 // NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR  
 // MODIFICATIONS.  
 //  
 // GOVERNMENT USE: If you are acquiring this software on behalf of the  
 // U.S. government, the Government shall have only "Restricted Rights"  
 // in the software and related documentation as defined in the Federal  
 // Acquisition Regulations (FARs) in Clause 52.227.19 (c) (2).  If you  
 // are acquiring the software on behalf of the Department of Defense, the  
 // software shall be classified as "Commercial Computer Software" and the  
 // Government shall have only "Restricted Rights" as defined in Clause  
 // 252.227-7013 (c) (1) of DFARs.  Notwithstanding the foregoing, the  
 // authors grant the U.S. Government and others acting in its behalf  
 // permission to use and distribute the software in accordance with the  
 // terms specified in this license.  
 //  
 ////////////////////////////////////////////////////////////////////////////////  
   
   
 /*  
  *----------------------------------------------------------------------  
  *  
  * Tcl_StringMatch --  
  *  
  *      See if a particular string matches a particular pattern.  
  *  
  * Results:  
  *      The return value is 1 if string matches pattern, and  
  *      0 otherwise.  The matching operation permits the following  
  *      special characters in the pattern: *?\[] (see the manual  
  *      entry for details on what these mean).  
  *  
  * Side effects:  
  *      None.  
  *  
  *----------------------------------------------------------------------  
  */  */
  
 typedef Uint16 Tcl_Char;  /* MatchChar defined as a separate entity because this function source used
       elsewhere was an unsigned char *. Here we use Uint16 to  maintain 16 bit
       size.
   */
   typedef Uint16 MatchChar;
  
 inline Uint16 _ToLower(Uint16 ch) inline Uint16 _ToLower(Uint16 ch)
 { {
 #ifdef PEGASUS_HAS_EBCDIC      return ch <= PEGASUS_MAX_PRINTABLE_CHAR ? tolower(char(ch)) : ch;
     return ch <= 255 ? tolower(char(ch)) : ch;  
 #else  
     return ch <= 127 ? tolower(char(ch)) : ch;  
 #endif  
 } }
  
 inline Boolean _Equal(Uint16 ch1, Uint16 ch2, int nocase)  inline Boolean _Equal(MatchChar ch1, MatchChar ch2, int nocase)
 { {
     if (nocase)     if (nocase)
         return _ToLower(ch1) == _ToLower(ch2);         return _ToLower(ch1) == _ToLower(ch2);
Line 676 
Line 585 
         return ch1 == ch2;         return ch1 == ch2;
 } }
  
 int Tcl_StringMatch(  
     Tcl_Char *string,           /* String. */  
     Tcl_Char *pattern,          /* Pattern, which may contain special  
                                  * characters. */  
     int nocase)                 /* Ignore case if this is true */  
 {  
     Tcl_Char c2;  
   
     while (1) {  
         /* See if we're at the end of both the pattern and the string.  
          * If so, we succeeded.  If we're at the end of the pattern  
          * but not at the end of the string, we failed.  
          */  
  
         if (*pattern == 0) {  static const MatchChar *
             if (*string == 0) {  _matchrange(const MatchChar *range, MatchChar c, int nocase)
                 return 1;  {
             } else {    const MatchChar *p = range;
                 return 0;    const MatchChar *rstart = range + 1;
             }    const MatchChar *rend = 0;
         }    MatchChar compchar;
         if ((*string == 0) && (*pattern != '*')) {  
             return 0;  
         }  
   
         /* Check for a "*" as the next pattern character.  It matches  
          * any substring.  We handle this by calling ourselves  
          * recursively for each postfix of string, until either we  
          * match or we reach the end of the string.  
          */  
  
         if (*pattern == '*') {    for (rend = rstart; *rend && *rend != ']'; rend++);
             pattern += 1;    if (*rend == ']') {  // if there is an end to this pattern
             if (*pattern == 0) {      for (compchar = *rstart; rstart != rend; rstart++) {
                 return 1;        if (_Equal(*rstart, c, nocase))
           return ++rend;
         if (*rstart == '-') {
           rstart++;
           if (c >= compchar && c <= *rstart)
             return ++rend;
             }             }
             while (1) {  
                 if (Tcl_StringMatch(string, pattern, nocase)) {  
                     return 1;  
                 }                 }
                 if (*string == 0) {  
                     return 0;  
                 }                 }
                 string += 1;    return (const MatchChar *)0;
             }             }
   
   static int
   _StringMatch(
       const MatchChar *testString,
       const MatchChar *pattern,
       int nocase )                /* Ignore case if this is true */
   {
     const MatchChar *pat = pattern;
     const MatchChar *str = testString;
     unsigned int done = 0;
     unsigned int res = 0;  // the result: 1 == match
   
     while (!done) { // main loop walks through pattern and test string
       //cerr << "Comparing <" << *pat << "> and <" << *str << ">" << endl;
       if (!*pat) {                                         //end of pattern
         done = 1;                                          // we're done
         if (!*str)                                         //end of test, too?
           res = 1;                                         // then we matched
       } else {                                             //Not end of pattern
         if (!*str) {                                       // but end of test
           done = 1;                                        // We're done
           if (*pat == '*')                                 // If pattern openends
             res = 1;                                       //  then we matched
         } else {                                           //Not end of test
           if (*pat == '*') {                               //Ambiguuity found
             if (!*++pat) {                                 //and it ends pattern
               done = 1;                                    //  then we're done
               res = 1;                                     //  and match
             } else {                                       //if it doesn't end
               while (!done) {                              //  until we're done
                 if (_StringMatch(str, pat, nocase)) {      //  we recurse
                   done = 1;                                //if it recurses true
                   res = 1;                                 //  we done and match
                 } else {                                   //it recurses false
                   if (!*str)                               // see if test is done
                     done = 1;                              //  yes: we done
                   else                                     // not done:
                     str++;                                 //   keep testing
                 } // end test on recursive call
               } // end looping on recursive calls
             } // end logic when pattern is ambiguous
           } else {                                         //pattern not ambiguus
             if (*pat == '?') {                             //pattern is 'any'
               pat++, str++;                                //  so move along
             } else if (*pat == '[') {                      //see if it's a range
               pat = _matchrange(pat, *str, nocase);         // and is a match
               if (!pat) {                                  //It is not a match
                 done = 1;                                  //  we're done
                 res = 0;                                   //  no match
               } else {                                     //Range matches
                 str++, pat++;                              //  keep going
               }
             } else {               // only case left is individual characters
               if (!_Equal(*pat++, *str++, nocase))         // if they don't match
                 done = 1;                                  //   bail.
             }
           }  // end ("pattern is not ambiguous (*)" logic
         } // end logic when pattern and string still have data
       } // end logic when pattern still has data
     } // end main loop
     return res;
         }         }
  
         /* Check for a "?" as the next pattern character.  It matches  
          * any single character.  
          */  
  
         if (*pattern == '?') {  Boolean String::match(const String& str, const String& pattern)
             goto thisCharOK;  {
       return _StringMatch(
           (Uint16*)str.getData(), (Uint16*)pattern.getData(), 0) != 0;
   }
   
   Boolean String::matchNoCase(const String& str, const String& pattern)
   {
       return _StringMatch(
           (Uint16*)str.getData(), (Uint16*)pattern.getData(), 1) != 0;
         }         }
  
         /* Check for a "[" as the next pattern character.  It is followed  
          * by a list of characters that are acceptable, or by a range  
          * (two characters separated by "-").  
          */  
  
         if (*pattern == '[') {  ///////////////////////////////////////////////////////////////////////////////
             pattern += 1;  //
             while (1) {  // String-related functions
                 if ((*pattern == ']') || (*pattern == 0)) {  //
                     return 0;  ///////////////////////////////////////////////////////////////////////////////
   
   Boolean operator==(const String& str1, const String& str2)
   {
       return String::equal(str1, str2);
                 }                 }
                 if (_Equal(*pattern, *string, nocase)) {  
                     break;  Boolean operator==(const String& str1, const char* str2)
   {
       return String::equal(str1, str2);
                 }                 }
                 if (pattern[1] == '-') {  
                     c2 = pattern[2];  Boolean operator==(const char* str1, const String& str2)
                     if (c2 == 0) {  {
                         return 0;      return String::equal(str1, str2);
                     }                     }
                     if ((*pattern <= *string) && (c2 >= *string)) {  
                         break;  Boolean operator!=(const String& str1, const String& str2)
   {
       return !String::equal(str1, str2);
                     }                     }
                     if ((*pattern >= *string) && (c2 <= *string)) {  
                         break;  PEGASUS_STD(ostream)& operator<<(PEGASUS_STD(ostream)& os, const String& str)
   {
       for (Uint32 i = 0, n = str.size(); i < n; i++)
       {
           Uint16 code = str[i];
   
           if (code > 0 && code <= PEGASUS_MAX_PRINTABLE_CHAR)
           {
               os << char(code);
                     }                     }
                     pattern += 2;          else
           {
               // Print in hex format:
               char buffer[8];
               sprintf(buffer, "\\x%04X", code);
               os << buffer;
                 }                 }
                 pattern += 1;  
             }             }
             while (*pattern != ']') {  
                 if (*pattern == 0) {      return os;
                     pattern--;  
                     break;  
                 }                 }
                 pattern += 1;  
   String operator+(const String& str1, const String& str2)
   {
       return String(str1).append(str2);
             }             }
             goto thisCharOK;  
   Boolean operator<(const String& str1, const String& str2)
   {
       return String::compare(str1, str2) < 0;
         }         }
  
         /* If the next pattern character is '/', just strip off the '/'  Boolean operator<=(const String& str1, const String& str2)
          * so we do exact matching on the character that follows.  {
          */      return String::compare(str1, str2) <= 0;
   }
  
         if (*pattern == '\\') {  Boolean operator>(const String& str1, const String& str2)
             pattern += 1;  {
             if (*pattern == 0) {      return String::compare(str1, str2) > 0;
                 return 0;  
             }             }
   
   Boolean operator>=(const String& str1, const String& str2)
   {
       return String::compare(str1, str2) >= 0;
         }         }
  
         /* There's no special character.  Just make sure that the next  int CompareNoCase(const char* s1, const char* s2)
          * characters of each string match.  {
          */      while (*s1 && *s2)
       {
           int r = tolower(*s1++) - tolower(*s2++);
  
         if (!_Equal(*pattern, *string, nocase)) {          if (r)
             return 0;              return r;
         }         }
  
         thisCharOK: pattern += 1;      if (*s2)
         string += 1;          return -1;
     }      else if (*s1)
 }          return 1;
  
 Boolean String::match(const String& str, const String& pattern)      return 0;
 {  
     return Tcl_StringMatch(  
         (Uint16*)str.getData(), (Uint16*)pattern.getData(), 0) != 0;  
 } }
  
 Boolean String::matchNoCase(const String& str, const String& pattern)  int EqualNoCase(const char* s1, const char* s2)
 { {
     return Tcl_StringMatch(      return CompareNoCase(s1, s2) == 0;
         (Uint16*)str.getData(), (Uint16*)pattern.getData(), 1) != 0;  
 } }
  
 PEGASUS_NAMESPACE_END PEGASUS_NAMESPACE_END


Legend:
Removed from v.1.28.2.6  
changed lines
  Added in v.1.50

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2