(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.30 and 1.38

version 1.30, 2001/12/13 14:32:18 version 1.38, 2002/05/06 06:43:38
Line 41 
Line 41 
 #include <Pegasus/Common/ArrayImpl.h> #include <Pegasus/Common/ArrayImpl.h>
 #undef PEGASUS_ARRAY_T #undef PEGASUS_ARRAY_T
  
 const String String::EMPTY;  const String String::EMPTY = String();
  
   #if 0    // Apparently dead code
 static inline void _SkipWhitespace(const Char16*& p) static inline void _SkipWhitespace(const Char16*& p)
 { {
     while (*p && isspace(*p))     while (*p && isspace(*p))
         p++;         p++;
 } }
   #endif
   
   Uint32 _strnlen(const char* str, Uint32 n)
   {
       if (!str)
           throw NullPointer();
   
       for (Uint32 i=0; i<n; i++)
       {
           if (!*str)
           {
               return i;
           }
       }
   
       return n;
   }
   
   Uint32 _strnlen(const Char16* str, Uint32 n)
   {
       if (!str)
           throw NullPointer();
   
       for (Uint32 i=0; i<n; i++)
       {
           if (!*str)
           {
               return i;
           }
       }
   
       return n;
   }
  
 inline Uint32 StrLen(const char* str) inline Uint32 StrLen(const char* str)
 { {
Line 82 
Line 116 
  
 String::String(const String& x, Uint32 n) String::String(const String& x, Uint32 n)
 { {
     _rep.append('\0');      assign(x.getData(), n);
     append(x.getData(), n);  
 } }
  
 String::String(const Char16* x) : _rep(x, StrLen(x) + 1) String::String(const Char16* x) : _rep(x, StrLen(x) + 1)
Line 98 
Line 131 
  
 String::String(const char* str) String::String(const char* str)
 { {
     Uint32 n = ::strlen(str) + 1;      assign(str);
     reserve(n);  
   
     while (n--)  
         _rep.append(*str++);  
 } }
  
 String::String(const char* str, Uint32 n_) String::String(const char* str, Uint32 n_)
 { {
     Uint32 n = PEG_min(strlen(str), n_);      assign(str, n_);
     reserve(n + 1);  
   
     while (n--)  
         _rep.append(*str++);  
   
     _rep.append('\0');  
 } }
  
 String& String::assign(const Char16* x) String& String::assign(const Char16* x)
Line 126 
Line 149 
 String& String::assign(const Char16* str, Uint32 n) String& String::assign(const Char16* str, Uint32 n)
 { {
     _rep.clear();     _rep.clear();
     Uint32 m = PEG_min(StrLen(str), n);      Uint32 m = _strnlen(str, n);
     _rep.append(str, m);     _rep.append(str, m);
     _rep.append('\0');     _rep.append('\0');
     return *this;     return *this;
Line 135 
Line 158 
 String& String::assign(const char* x) String& String::assign(const char* x)
 { {
     _rep.clear();     _rep.clear();
     Uint32 n = strlen(x);  
     _rep.reserve(n + 1);      Uint32 n = strlen(x) + 1;
       _rep.reserve(n);
  
     while (n--)     while (n--)
         _rep.append(*x++);         _rep.append(*x++);
  
     _rep.append('\0');  
   
     return *this;     return *this;
 } }
  
Line 150 
Line 172 
 { {
     _rep.clear();     _rep.clear();
  
     Uint32 n = PEG_min(strlen(x), n_);      Uint32 n = _strnlen(x, n_);
     _rep.reserve(n + 1);     _rep.reserve(n + 1);
  
     while (n--)     while (n--)
Line 188 
Line 210 
     if (!str)     if (!str)
         throw NullPointer();         throw NullPointer();
  
     Uint32 n = PEG_min(size(), length);      Uint32 n = _pegasusMin(size(), length);
  
     char* p = str + strlen(str);     char* p = str + strlen(str);
     const Char16* q = getData();     const Char16* q = getData();
Line 223 
Line 245 
  
 String& String::append(const Char16* str, Uint32 n) String& String::append(const Char16* str, Uint32 n)
 { {
     Uint32 m = PEG_min(StrLen(str), n);      Uint32 m = _strnlen(str, n);
     _rep.reserve(_rep.size() + m);     _rep.reserve(_rep.size() + m);
     _rep.remove(_rep.size() - 1);     _rep.remove(_rep.size() - 1);
     _rep.append(str, m);     _rep.append(str, m);
Line 400 
Line 422 
     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 Uint32 String::find(const char* s) const
 { {
     return find(String(s));     return find(String(s));
 } }
  
   Uint32 String::find(const Char16* 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 586 
Line 612 
     static Array<String> tmp;     static Array<String> tmp;
     return tmp;     return tmp;
 } }
   //#define NEWMATCHFUNCTION
   #if defined NEWMATCHFUNCTION
    // Wildcard String matching function that may be useful in the future
   // The following code was provided by Bob Blair.
   
   /* _StringMatch Match input MatchString against a GLOB style pattern
          Note that MatchChar is the char type so that this source
          in portable to different string types. This is an internal function
   
     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.
    */
   
   /* 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)
   {
   #ifdef PEGASUS_HAS_EBCDIC
       return ch <= 255 ? tolower(char(ch)) : ch;
   #else
       return ch <= 127 ? tolower(char(ch)) : ch;
   #endif
   }
  
   inline Boolean _Equal(MatchChar ch1, MatchChar ch2, int nocase)
   {
       if (nocase)
           return _ToLower(ch1) == _ToLower(ch2);
       else
           return ch1 == ch2;
   }
   
   
   static const MatchChar *
   _matchrange(const MatchChar *range, MatchChar c, int nocase)
   {
     const MatchChar *p = range;
     const MatchChar *rstart = range + 1;
     const MatchChar *rend = 0;
     MatchChar compchar;
   
     for (rend = rstart; *rend && *rend != ']'; rend++);
     if (*rend == ']') {  // if there is an end to this pattern
       for (compchar = *rstart; rstart != rend; rstart++) {
         if (_Equal(*rstart, c, nocase))
           return ++rend;
         if (*rstart == '-') {
           rstart++;
           if (c >= compchar && c <= *rstart)
             return ++rend;
         }
       }
     }
     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 = 1;                                   //  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;
   }
   
   #else
 //////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
 // //
 // String matching routines borrowed from Tcl 8.0: // String matching routines borrowed from Tcl 8.0:
Line 657 
Line 809 
  *----------------------------------------------------------------------  *----------------------------------------------------------------------
  */  */
  
 typedef Uint16 Tcl_Char;  typedef Uint16 MatchChar;
  
 inline Uint16 _ToLower(Uint16 ch) inline Uint16 _ToLower(Uint16 ch)
 { {
Line 676 
Line 828 
         return ch1 == ch2;         return ch1 == ch2;
 } }
  
 int Tcl_StringMatch(  int _StringMatch(
     Tcl_Char *string,           /* String. */      MatchChar *string,          /* String. */
     Tcl_Char *pattern,          /* Pattern, which may contain special      MatchChar *pattern,         /* Pattern, which may contain special
                                  * characters. */                                  * characters. */
     int nocase)                 /* Ignore case if this is true */     int nocase)                 /* Ignore case if this is true */
 { {
     Tcl_Char c2;      MatchChar c2;
  
     while (1) {     while (1) {
         /* See if we're at the end of both the pattern and the string.         /* See if we're at the end of both the pattern and the string.
Line 713 
Line 865 
                 return 1;                 return 1;
             }             }
             while (1) {             while (1) {
                 if (Tcl_StringMatch(string, pattern, nocase)) {                  if (_StringMatch(string, pattern, nocase)) {
                     return 1;                     return 1;
                 }                 }
                 if (*string == 0) {                 if (*string == 0) {
Line 793 
Line 945 
         string += 1;         string += 1;
     }     }
 } }
   #endif
 Boolean String::match(const String& str, const String& pattern) Boolean String::match(const String& str, const String& pattern)
 { {
     return Tcl_StringMatch(      return _StringMatch(
         (Uint16*)str.getData(), (Uint16*)pattern.getData(), 0) != 0;         (Uint16*)str.getData(), (Uint16*)pattern.getData(), 0) != 0;
 } }
  
 Boolean String::matchNoCase(const String& str, const String& pattern) Boolean String::matchNoCase(const String& str, const String& pattern)
 { {
     return Tcl_StringMatch(      return _StringMatch(
         (Uint16*)str.getData(), (Uint16*)pattern.getData(), 1) != 0;         (Uint16*)str.getData(), (Uint16*)pattern.getData(), 1) != 0;
 } }
  


Legend:
Removed from v.1.30  
changed lines
  Added in v.1.38

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2