dbertoni    00/08/10 11:17:08

  Modified:    c/src/PlatformSupport DOMStringHelper.cpp
                        DOMStringHelper.hpp
  Log:
  Improved implementations which do fewer dynamic memory allocations.
  
  Revision  Changes    Path
  1.29      +127 -99   xml-xalan/c/src/PlatformSupport/DOMStringHelper.cpp
  
  Index: DOMStringHelper.cpp
  ===================================================================
  RCS file: /home/cvs/xml-xalan/c/src/PlatformSupport/DOMStringHelper.cpp,v
  retrieving revision 1.28
  retrieving revision 1.29
  diff -u -r1.28 -r1.29
  --- DOMStringHelper.cpp       2000/07/13 22:21:25     1.28
  +++ DOMStringHelper.cpp       2000/08/10 18:17:07     1.29
  @@ -102,7 +102,7 @@
   
   
   // The maximum number of digits that sprintf can put in a buffer.
  -// 100 for now.  We're using this because we want to avoid transcoding
  +// 50 for now.  We're using this because we want to avoid transcoding
   // number strings when we don't have to,
   const size_t MAX_PRINTF_DIGITS = 50;
   
  @@ -136,40 +136,6 @@
   
   
   
  -
  -#if !defined(XALAN_FULL_WCHAR_SUPPORT)
  -
  -// Simulates the java String method indexOf().  Returns the index of theChar
  -// in theString, or length(theString) if the character is not found.
  -XALAN_PLATFORMSUPPORT_EXPORT_FUNCTION(unsigned int)
  -indexOf(
  -                     const XalanDOMChar*             theString,
  -                     XalanDOMChar                    theChar)
  -{
  -     const unsigned int      theLength = length(theString);
  -
  -     if (theLength == 0)
  -     {
  -             return theLength;
  -     }
  -     else
  -     {
  -             unsigned int    theIndex = 0;
  -
  -             while(theIndex < theLength &&
  -                       theString[theIndex] != theChar)
  -             {
  -                     ++theIndex;
  -             }
  -
  -             return theIndex == theLength ? theLength : theIndex;
  -     }
  -}
  -
  -#endif
  -
  -
  -
   XALAN_PLATFORMSUPPORT_EXPORT_FUNCTION(unsigned int)
   indexOf(
                        const XalanDOMChar*             theString,
  @@ -261,8 +227,6 @@
   
   
   
  -#if !defined(XALAN_FULL_WCHAR_SUPPORT)
  -
   XALAN_PLATFORMSUPPORT_EXPORT_FUNCTION(unsigned int)
   lastIndexOf(
                        const XalanDOMChar*             theString,
  @@ -278,6 +242,7 @@
        {
                unsigned int    theIndex = theLength - 1;
   
  +             // Rely on wrap-around...
                while(theIndex < theLength && theString[theIndex] != theChar)
                {
                        theIndex--;
  @@ -287,8 +252,6 @@
        }
   }
   
  -#endif
  -
   
   
   XALAN_PLATFORMSUPPORT_EXPORT_FUNCTION(bool)
  @@ -503,7 +466,7 @@
   
                        // Reserve the buffer now.  We don't have to 
null-terminate,
                        // because the XalanDOMString constructor will take a 
size
  -                     // option.
  +                     // parameter.
                        theBuffer.reserve(theLength);
   
                        const XalanDOMChar* const       ptr = 
theString.rawBuffer();
  @@ -569,8 +532,42 @@
   }
   
   
  +
  +inline bool
  +doEqualsIgnoreCase(
  +                     const XalanDOMChar*             theLHS,
  +                     const XalanDOMChar*             theRHS,
  +                     unsigned int                    theLength)
  +{
  +     // Check each character, converting to uppercase
  +     // for the test.
  +     unsigned int    i = 0;
  +
  +     for(; i < theLength; i++)
  +     {
  +             const XalanDOMChar      charLHS = theLHS[i];
  +             const XalanDOMChar      charRHS = theRHS[i];
  +
  +             if (charLHS != charRHS &&
  +                     towupper(charLHS) != charRHS &&
  +                     charLHS != towupper(charRHS))
  +             {
  +                     break;
  +             }
  +     }
  +
  +     // Did we reach the end of the string?
  +     if (i == theLength)
  +     {
  +             return true;
  +     }
  +     else
  +     {
  +             return false;
  +     }
  +}
  +
   
  -#if !defined(_MSC_VER)
   
   XALAN_PLATFORMSUPPORT_EXPORT_FUNCTION(bool)
   equalsIgnoreCase(
  @@ -587,31 +584,12 @@
        // If they are equal, then compare
        if (theLength == length(theRHS))
        {
  -             // Check each character, converting to uppercase
  -             // for the test.
  -             unsigned int    i = 0;
  -
  -             for(; i < theLength; i++)
  -             {
  -                     if (towupper(theLHS[i]) !=
  -                                             towupper(theRHS[i]))
  -                     {
  -                             break;
  -                     }
  -             }
  -
  -             // Did we reach the end of the string?
  -             if (i == theLength)
  -             {
  -                     fResult = true;
  -             }
  +             fResult = doEqualsIgnoreCase(theLHS, theRHS, theLength);
        }
   
        return fResult;
   }
   
  -#endif
  -
   
   
   XALAN_PLATFORMSUPPORT_EXPORT_FUNCTION(bool)
  @@ -637,15 +615,22 @@
        {
                assert(c_wstr(theLHS) != 0);
                assert(c_wstr(theRHS) != 0);
  +
  +             unsigned const int      theLHSLength = length(theLHS);
   
  -             return equalsIgnoreCase(c_wstr(theLHS), c_wstr(theRHS));
  +             if (theLHSLength == length(theRHS))
  +             {
  +                     return doEqualsIgnoreCase(c_wstr(theLHS), 
c_wstr(theRHS), theLHSLength);
  +             }
  +             else
  +             {
  +                     return false;
  +             }
        }
   }
   
   
   
  -#if !defined(XALAN_FULL_WCHAR_SUPPORT)
  -
   XALAN_PLATFORMSUPPORT_EXPORT_FUNCTION(int)
   compare(
                        const XalanDOMChar*             theLHS,
  @@ -704,8 +689,6 @@
        return theResult;
   }
   
  -#endif
  -
   
   
   struct WideStringLexicalCompare
  @@ -861,19 +844,12 @@
        {
                theVector.reserve(theLength + 1);
   
  -             for(unsigned int        i = 0; i < theLength; i++)
  +             for(unsigned int i = 0; i < theLength; i++)
                {
  -#if defined(XALAN_OLD_STYLE_CASTS)
                        // Assert that the truncation will not affect the 
resulting character.
  -                     assert(theString[i] == (char)theString[i]);
  +                     assert(theString[i] == char(theString[i]));
   
  -                     theVector.push_back((char)theString[i]);
  -#else
  -                     // Assert that the truncation will not affect the 
resulting character.
  -                     assert(theString[i] == static_cast<char>(theString[i]));
  -
  -                     theVector.push_back(static_cast<char>(theString[i]));
  -#endif
  +                     theVector.push_back(char(theString[i]));
                }
   
                // Put a terminating 0 byte.
  @@ -1011,8 +987,10 @@
                }
                else
                {
  -                     ostrstream      theFormatter;
  +                     char            theBuffer[MAX_PRINTF_DIGITS + 1];
   
  +                     ostrstream      theFormatter(theBuffer, 
sizeof(theBuffer));
  +
                        theFormatter << theDouble << '\0';
   
                        // OK, now we have to clean up the output for
  @@ -1025,9 +1003,7 @@
                        // the decimal point, since any values with no
                        // fractional part were printed as integers
                        XalanDOMCharVectorType  theResult =
  -                             MakeXalanDOMCharVector(theFormatter.str(), 
false);
  -
  -                     theFormatter.freeze(false);
  +                             MakeXalanDOMCharVector(theBuffer, false);
   
                        XalanDOMCharVectorType::iterator        thePosition = 
theResult.end();
   
  @@ -1055,7 +1031,10 @@
   XALAN_PLATFORMSUPPORT_EXPORT_FUNCTION(XalanDOMString)
   LongToHexDOMString(long              theLong)
   {
  -#if defined(XALAN_FULL_WCHAR_SUPPORT)
  +// I'm 99% sure that we don't need to use swprintf
  +// to format, since strings of numbers don't to be
  +// generated as wide strings.
  +#if defined(XALAN_USE_WCHAR_SUPPORT)
   
        wchar_t         theBuffer[MAX_PRINTF_DIGITS + 1];
   
  @@ -1066,17 +1045,23 @@
        return XalanDOMString(theBuffer, length(theBuffer));
   
   #else
  +
  +     char            theBuffer[MAX_PRINTF_DIGITS + 1];
   
  -     ostrstream      theFormatter;
  +     ostrstream      theFormatter(theBuffer, sizeof(theBuffer));
   
        theFormatter << hex << theLong << '\0';
   
  -     const XalanDOMString    theString = theFormatter.str();
  +     // We don't need to transcode, so just make it a
  +     // wide character string...
  +     wchar_t         theResult[MAX_PRINTF_DIGITS + 1];
   
  -     theFormatter.freeze(false);
  +     const unsigned int      theLength = length(theBuffer);
   
  -     return theString;
  +     copy(theBuffer, theBuffer + theLength, theResult);
   
  +     return XalanDOMString(theResult, theLength);
  +
   #endif
   }
   
  @@ -1085,15 +1070,21 @@
   XALAN_PLATFORMSUPPORT_EXPORT_FUNCTION(XalanDOMString)
   UnsignedLongToHexDOMString(unsigned long     theUnsignedLong)
   {
  -     ostrstream      theFormatter;
  +     char            theBuffer[MAX_PRINTF_DIGITS + 1];
  +
  +     ostrstream      theFormatter(theBuffer, sizeof(theBuffer));
   
        theFormatter << hex << theUnsignedLong << '\0';
  +
  +     // We don't need to transcode, so just make it a
  +     // wide character string...
  +     wchar_t         theResult[MAX_PRINTF_DIGITS + 1];
   
  -     const XalanDOMString    theString = theFormatter.str();
  +     const unsigned int      theLength = length(theBuffer);
   
  -     theFormatter.freeze(false);
  +     copy(theBuffer, theBuffer + theLength, theResult);
   
  -     return theString;
  +     return XalanDOMString(theResult, theLength);
   }
   
   
  @@ -1101,7 +1092,7 @@
   XALAN_PLATFORMSUPPORT_EXPORT_FUNCTION(XalanDOMString)
   LongToDOMString(long theLong)
   {
  -#if defined(XALAN_FULL_WCHAR_SUPPORT)
  +#if defined(XALAN_USE_WCHAR_SUPPORT)
   
        wchar_t         theBuffer[MAX_PRINTF_DIGITS + 1];
   
  @@ -1113,15 +1104,21 @@
   
   #else
   
  -     ostrstream      theFormatter;
  +     char            theBuffer[MAX_PRINTF_DIGITS + 1];
   
  +     ostrstream      theFormatter(theBuffer, sizeof(theBuffer));
  +
        theFormatter << theLong << '\0';
  +
  +     // We don't need to transcode, so just make it a
  +     // wide character string...
  +     wchar_t         theResult[MAX_PRINTF_DIGITS + 1];
   
  -     XalanDOMString  theString = theFormatter.str();
  +     const unsigned int      theLength = length(theBuffer);
   
  -     theFormatter.freeze(false);
  +     copy(theBuffer, theBuffer + theLength, theResult);
   
  -     return theString;
  +     return XalanDOMString(theResult, theLength);
   
   #endif
   }
  @@ -1131,8 +1128,35 @@
   XALAN_PLATFORMSUPPORT_EXPORT_FUNCTION(XalanDOMString)
   UnsignedLongToDOMString(unsigned long        theUnsignedLong)
   {
  -#if defined(XALAN_FULL_WCHAR_SUPPORT)
  +#if 1
  +
  +     wchar_t                 theBuffer[MAX_PRINTF_DIGITS + 1];
   
  +     wchar_t*                thePointer = &theBuffer[MAX_PRINTF_DIGITS + 1];
  +     wchar_t* const  theEnd = thePointer;
  +
  +     // Null terminate it...
  +     *thePointer = 0;
  +
  +     do
  +     {
  +             // Next spot...
  +             --thePointer;
  +
  +             // Isolate the left most character.  We may need to
  +             // change this to a switch statement on platforms
  +             // that are not ASCII-based (i.e. EBCDIC)
  +             *thePointer = wchar_t(theUnsignedLong % 10 + '0');
  +
  +             // OK, we're done with it...
  +             theUnsignedLong /= 10;
  +     }
  +     while(theUnsignedLong != 0);
  +
  +     return XalanDOMString(thePointer, theEnd - thePointer);
  +
  +#elif defined(XALAN_USE_WCHAR_SUPPORT)
  +
        wchar_t         theBuffer[MAX_PRINTF_DIGITS + 1];
   
        swprintf(theBuffer,
  @@ -1143,15 +1167,19 @@
   
   #else
   
  -     ostrstream      theFormatter;
  +     char            theBuffer[MAX_PRINTF_DIGITS + 1];
   
  +     ostrstream      theFormatter(theBuffer, sizeof(theBuffer));
  +
        theFormatter << theUnsignedLong << '\0';
  +
  +     wchar_t         theResult[MAX_PRINTF_DIGITS + 1];
   
  -     XalanDOMString  theString = theFormatter.str();
  +     const unsigned int      theLength = length(theBuffer);
   
  -     theFormatter.freeze(false);
  +     copy(theBuffer, theBuffer + theLength, theResult);
   
  -     return theString;
  +     return XalanDOMString(theResult, theLength);
   
   #endif
   }
  
  
  
  1.25      +58 -53    xml-xalan/c/src/PlatformSupport/DOMStringHelper.hpp
  
  Index: DOMStringHelper.hpp
  ===================================================================
  RCS file: /home/cvs/xml-xalan/c/src/PlatformSupport/DOMStringHelper.hpp,v
  retrieving revision 1.24
  retrieving revision 1.25
  diff -u -r1.24 -r1.25
  --- DOMStringHelper.hpp       2000/07/21 19:18:55     1.24
  +++ DOMStringHelper.hpp       2000/08/10 18:17:07     1.25
  @@ -114,6 +114,23 @@
   
   
   /**
  + * Reserve some space in the string for more efficient
  + * concatenation...
  + * 
  + * @param theString target string
  + * @param theCount The amount of space to reserve
  + */
  +inline void
  +reserve(
  +                     XalanDOMString&         theString,
  +                     unsigned int            theCount)
  +{
  +     theString.reserve(theCount);
  +}
  +
  + 
  + 
  +/**
    * Get the underlying representation of the target XalanDOMString as a
    * null-terminated string
    * 
  @@ -169,22 +186,6 @@
   }
   
   
  -#if 0
  -/**
  - * Get the underlying representation of the target XalanDOMString as an 
array of
  - * XalanDOMChar, not guaranteed to be null-terminated.
  - * 
  - * @param theString target string
  - * @return array of XalanDOMChar
  - */
  -inline const XalanDOMChar*
  -toCharArray(XalanDOMString&          theString)
  -{
  -     return theString.rawBuffer();
  -}
  -#endif
  -
  -
   
   /**
    * Simulates the java String method length() for a XalanDOMString
  @@ -212,7 +213,9 @@
   {
        assert(theBuffer != 0);
   
  -#if defined(XALAN_FULL_WCHAR_SUPPORT)
  +     // For the time being, we're using our own custom routine,
  +     // since performance is better.
  +#if defined(XALAN_USE_WCHAR_SUPPORT)
        return wcslen(theBuffer);
   #else
        const XalanDOMChar*             theBufferPointer = theBuffer;
  @@ -242,13 +245,24 @@
   
   
   
  -#if defined(XALAN_FULL_WCHAR_SUPPORT)
  -
  +/**
  + * Simulates the java String method indexOf().
  + * 
  + * @param theString string to search
  + * @param theChar   character searched for
  + * @return the index of theChar in theString,
  + * or length(theString) if the character is not
  + * found.    
  + */
   inline unsigned int
   indexOf(
                        const XalanDOMChar*             theString,
                        XalanDOMChar                    theChar)
   {
  +     // For the time being, we're using our own custom routine,
  +     // since performance is better.
  +#if defined(XALAN_USE_WCHAR_SUPPORT)
  +
        const XalanDOMChar* const       thePointer =
                        wcschr(theString, theChar);
   
  @@ -260,23 +274,20 @@
        {
                return thePointer - theString;
        }
  -}
   
   #else
  -/**
  - * Simulates the java String method indexOf().
  - * 
  - * @param theString string to search
  - * @param theChar   character searched for
  - * @return the index of theChar in theString,
  - * or length(theString) if the character is not
  - * found.    
  - */
  -XALAN_PLATFORMSUPPORT_EXPORT_FUNCTION(unsigned int)
  -indexOf(
  -                     const XalanDOMChar*             theString,
  -                     XalanDOMChar                    theChar);
  +
  +     const XalanDOMChar*             thePointer = theString;
  +
  +     while(*thePointer != theChar && *thePointer != 0)
  +     {
  +             ++thePointer;
  +     }
  +
  +     return thePointer - theString;
  +
   #endif
  +}
   
   
   
  @@ -346,7 +357,10 @@
    * or length(theString) if the character is not
    * found.
    */
  -#if defined(XALAN_FULL_WCHAR_SUPPORT)
  +
  +// For the time being, we're using our own custom routine,
  +// since performance is better.
  +#if defined(XALAN_USE_WCHAR_SUPPORT)
   
   inline unsigned int
   lastIndexOf(
  @@ -726,7 +740,11 @@
   inline bool
   isSpace(XalanDOMChar theChar)
   {
  -     return iswspace(theChar) ? true : false;
  +     return theChar > 0x20 ? false :
  +                (theChar == 0x20 ||
  +                 theChar == 0xD ||
  +                 theChar == 0xA ||
  +                 theChar == 0x9) ? true : false;
   }
   
   
  @@ -857,8 +875,11 @@
   
   
   
  +
  +// For the time being, we're using our own custom routine,
  +// since performance is better.
   
  -#if defined(XALAN_FULL_WCHAR_SUPPORT)
  +#if defined(XALAN_USE_WCHAR_SUPPORT)
   
   /**
    * Compare the contents of two strings.
  @@ -889,7 +910,7 @@
   
   
   
  -#if defined(XALAN_FULL_WCHAR_SUPPORT)
  +#if defined(XALAN_USE_WCHAR_SUPPORT)
   
   /**
    * Compare the contents of two strings using the
  @@ -1081,21 +1102,6 @@
   
   
   
  -#if defined(_MSC_VER)
  -
  -inline bool
  -equalsIgnoreCase(
  -                     const XalanDOMChar*             theLHS,
  -                     const XalanDOMChar*             theRHS)
  -{
  -     assert(theLHS != 0);
  -     assert(theRHS != 0);
  -
  -     return _wcsicmp(theLHS, theRHS) == 0 ? true : false;
  -}
  -
  -#else
  -
   /**
    * Compare the contents of two strings for equality, without regard for case
    * 
  @@ -1107,7 +1113,6 @@
   equalsIgnoreCase(
                        const XalanDOMChar*             theLHS,
                        const XalanDOMChar*             theRHS);
  -#endif
   
   
   
  
  
  

Reply via email to