You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@xerces.apache.org by an...@locus.apache.org on 2000/02/04 00:07:28 UTC

cvs commit: xml-xerces/c/tests/DOM/DOMMemTest DOMMemTest.cpp

andyh       00/02/03 15:07:28

  Modified:    c/src/dom AttrImpl.cpp DOMString.cpp DOMString.hpp
               c/tests/DOM/DOMMemTest DOMMemTest.cpp
  Log:
  Add several new functions from Robert Weir to DOMString.
  
  Revision  Changes    Path
  1.4       +5 -1      xml-xerces/c/src/dom/AttrImpl.cpp
  
  Index: AttrImpl.cpp
  ===================================================================
  RCS file: /home/cvs/xml-xerces/c/src/dom/AttrImpl.cpp,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- AttrImpl.cpp	2000/01/22 01:38:29	1.3
  +++ AttrImpl.cpp	2000/02/03 23:07:27	1.4
  @@ -57,6 +57,9 @@
   
   /**
    * $Log: AttrImpl.cpp,v $
  + * Revision 1.4  2000/02/03 23:07:27  andyh
  + * Add several new functions from Robert Weir to DOMString.
  + *
    * Revision 1.3  2000/01/22 01:38:29  andyh
    * Remove compiler warnings in DOM impl classes
    *
  @@ -146,7 +149,8 @@
       for (node = getFirstChild(); node != null; node = node->getNextSibling())
           length += node->getNodeValue().length();
       
  -    DOMString retString(length);
  +    DOMString retString;
  +	retString.reserve(length);
       for (node = getFirstChild(); node != null; node = node->getNextSibling())
       {
           retString.appendData(node->getNodeValue());
  
  
  
  1.9       +157 -6    xml-xerces/c/src/dom/DOMString.cpp
  
  Index: DOMString.cpp
  ===================================================================
  RCS file: /home/cvs/xml-xerces/c/src/dom/DOMString.cpp,v
  retrieving revision 1.8
  retrieving revision 1.9
  diff -u -r1.8 -r1.9
  --- DOMString.cpp	2000/01/29 00:39:08	1.8
  +++ DOMString.cpp	2000/02/03 23:07:27	1.9
  @@ -56,6 +56,9 @@
   
   /**
    * $Log: DOMString.cpp,v $
  + * Revision 1.9  2000/02/03 23:07:27  andyh
  + * Add several new functions from Robert Weir to DOMString.
  + *
    * Revision 1.8  2000/01/29 00:39:08  andyh
    * Redo synchronization in DOMStringHandle allocator.  There
    * was a bug in the use of Compare and Swap.  Switched to mutexes.
  @@ -435,11 +438,10 @@
   
   
   
  -DOMString::DOMString(int initialBufferSize)
  +DOMString::DOMString(int nullValue)
   {
  -    fHandle = 0;
  -    if (initialBufferSize > 0)
  -        fHandle = DOMStringHandle::createNewStringHandle(initialBufferSize);
  +   assert(nullValue == 0);
  +   fHandle = 0;
   };
   
   
  @@ -469,13 +471,25 @@
   };
   
   
  +DOMString & DOMString::operator = (DOM_NullPtr *arg)
  +{
  +    assert(arg == 0);
  +    if (fHandle)
  +        fHandle->removeRef();
  +
  +    fHandle = 0;
  +    return *this;
  +};
  +
  +
  +#if 0
   DOMString DOMString::operator + (const DOMString &other)
   {
       DOMString retString = this->clone();
       retString.appendData(other);
       return retString;
   };
  -
  +#endif
   
   bool DOMString::operator ==(const DOMString &other) const
   {
  @@ -499,13 +513,31 @@
       return (fHandle != 0);
   };
   
  +
  +
  +void DOMString::reserve(size_t size)
  +{
  +	if (fHandle == 0)
  +	{
  +	    if (size > 0)
  +	        fHandle = DOMStringHandle::createNewStringHandle(size);
  +	}
  +}
  +
  +
  +
   void DOMString::appendData(const DOMString &other)
   {
       int i;
       if (other.fHandle == 0 || other.fHandle->fLength == 0)
           return;
   
  -    if (fHandle == 0 || fHandle->fLength == 0)
  +    // If this string is empty and this string does not have an
  +    //   already allocated buffer sufficient to hold the string being
  +    //   appended, return a clone of the other string.
  +    //
  +    if (fHandle == 0 || (fHandle->fLength == 0 &&
  +        fHandle->fDSData->fBufferLength < other.fHandle->fLength))
       {
           if (fHandle) fHandle->removeRef();
           this->fHandle = other.fHandle->cloneStringHandle();
  @@ -530,6 +562,9 @@
           fHandle->fDSData = newBuf;
       }
   
  +    //
  +    // This string now had enough buffer room to hold the data to
  +    //  be appended.  Go ahead and copy it in.
       XMLCh *srcP = other.fHandle->fDSData->fData;
       XMLCh *destP = &fHandle->fDSData->fData[fHandle->fLength];
       for (i=0; i<=other.fHandle->fLength; i++)
  @@ -539,6 +574,73 @@
   }
   
   
  +
  +void DOMString::appendData(XMLCh ch)
  +{
  +	int newLength = 0;
  +
  +	if (fHandle == 0)
  +	{
  +		fHandle = DOMStringHandle::createNewStringHandle(1);
  +		newLength = 1;
  +	}
  +	else
  +		newLength = fHandle->fLength + 1;
  +    
  +    if (newLength >= fHandle->fDSData->fBufferLength ||
  +        fHandle->fDSData->fRefCount > 1)
  +    {
  +        // We can't stick the data to be added onto the end of the
  +        //  existing string, either because there is not space in
  +        //  the buffer, or because the buffer is being shared with
  +        //  some other string.  So, make a new buffer.
  +        DOMStringData *newBuf = DOMStringData::allocateBuffer(newLength);
  +        XMLCh *newP = newBuf->fData;
  +        XMLCh *oldP = fHandle->fDSData->fData;
  +        for (int i=0; i<fHandle->fLength; ++i)
  +            newP[i] = oldP[i];
  +        
  +        fHandle->fDSData->removeRef();
  +        fHandle->fDSData = newBuf;
  +    }
  +
  +    XMLCh *destP = &fHandle->fDSData->fData[fHandle->fLength];
  +	destP[0] = ch;
  +
  +    fHandle->fLength ++;
  +}
  +
  +// TODO: A custom version could be written more efficiently, avoiding
  +// the creation of the temporary DOMString
  +void DOMString::appendData(const XMLCh* other)
  +{
  +	appendData(DOMString(other));
  +}
  +
  +
  +DOMString& DOMString::operator +=(const DOMString &other)
  +{
  +	appendData(other);
  +
  +	return *this;
  +}
  +
  +DOMString& DOMString::operator +=(const XMLCh *str)
  +{
  +	appendData(str);
  +
  +	return *this;
  +}
  +
  +DOMString& DOMString::operator +=(XMLCh ch)
  +{
  +	appendData(ch);
  +
  +	return *this;
  +}
  +
  +
  +
   XMLCh     DOMString::charAt(int index) const
   {
       XMLCh retCh = 0;
  @@ -882,6 +984,12 @@
   }
   
   
  +DOMString DOMString::transcode(const char* str)
  +{
  +    return DOMString(str);
  +}
  +
  +
   int DOMString::compareString(const DOMString &other) const
   {
       // Note: this strcmp does not match the semantics
  @@ -934,4 +1042,44 @@
       }
       return retString;
   };
  +
  +
  +DOMString operator + (const DOMString &lhs, const DOMString &rhs)
  +{
  +    DOMString retString = lhs.clone();
  +    retString.appendData(rhs);
  +    return retString;
  +}
  +
  +DOMString operator + (const DOMString &lhs, const XMLCh* rhs)
  +{
  +    DOMString retString = lhs.clone();
  +    retString.appendData(rhs);
  +    return retString;
  +}
  +
  +DOMString operator + (const XMLCh* lhs, const DOMString& rhs)
  +{
  +    DOMString retString = DOMString(lhs);
  +    retString.appendData(rhs);
  +    return retString;
  +}
  +
  +
  +DOMString operator + (const DOMString &lhs, XMLCh rhs)
  +{
  +    DOMString retString = lhs.clone();
  +    retString.appendData(rhs);
  +    return retString;
  +}
  +
  +DOMString operator + (XMLCh lhs, const DOMString& rhs)
  +{
  +    DOMString retString;
  +	retString.appendData(lhs);
  +    retString.appendData(rhs);
  +    return retString;
  +}
  +
  +
   
  
  
  
  1.5       +131 -10   xml-xerces/c/src/dom/DOMString.hpp
  
  Index: DOMString.hpp
  ===================================================================
  RCS file: /home/cvs/xml-xerces/c/src/dom/DOMString.hpp,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- DOMString.hpp	2000/01/05 22:16:26	1.4
  +++ DOMString.hpp	2000/02/03 23:07:27	1.5
  @@ -56,6 +56,9 @@
   
   /**
    * $Log: DOMString.hpp,v $
  + * Revision 1.5  2000/02/03 23:07:27  andyh
  + * Add several new functions from Robert Weir to DOMString.
  + *
    * Revision 1.4  2000/01/05 22:16:26  robweir
    * Move DOMString implementation class declarations into a new
    * file: DOMStringImpl.hpp.  Include this header in DOMString.hpp
  @@ -148,15 +151,9 @@
       DOMString(const char *other);
   
       /**
  -      * Constructor to build an empty DOMString with the character buffer
  -      *  preallocated to the specified size.  A DOMString will grow its buffer
  -      *  on demand, as characters are added, but it can be more efficient to
  -      *  allocate once in advance, if the size is known.
  -      *
  -      * @param initialBufferSize The initial size of the buffer inside the 
  -      * <code>DOMString</code>, measured in 16 bit characters.
  +      * Construct a null DOMString.
         */
  -    DOMString(int initalBufferSize);
  +    DOMString(int nullPointerValue);
   
       /**
         * Assignment operator.  Make destination DOMString refer to the same
  @@ -166,6 +163,10 @@
         */
       DOMString &        operator = (const DOMString &other);
   
  +
  +
  +    DOMString &        operator = (DOM_NullPtr *other);
  +
       //@}
       /** @name Destructor. */
       //@{
  @@ -186,7 +187,7 @@
         * @param other The string to be concatenated.
         * @return The concatenated object
         */
  -    DOMString   operator + (const DOMString &other);
  +    // DOMString   operator + (const DOMString &other);
   
       //@}
       /** @name Equality and Inequality operators. */
  @@ -242,6 +243,17 @@
       /** @name Functions to change the string. */
       //@{
   
  +
  +    /**
  +      * Preallocate storage in the string to hold a given number of characters.
  +      * A DOMString will grow its buffer on demand, as characters are added,
  +      * but it can be more efficient to allocate once in advance, if the size is known.
  +      *
  +      * @param size The number of 16 bit characters to reserve.
  +      */
  +    void reserve(size_t size);
  +
  +    
       /**
         * Appends the content of another <code>DOMString</code> to this string.
         *
  @@ -250,6 +262,45 @@
       void        appendData(const DOMString &other);
   
       /**
  +      * Append a single Unicode character to this string.
  +      *
  +      * @param other The object to be appended
  +      */
  +    void        appendData(XMLCh ch);
  +
  +     /**
  +      * Append a null-terminated XMLCh * (Unicode) string to this string.
  +      *
  +      * @param other The object to be appended
  +      */
  +    void        appendData(const XMLCh *other);
  +
  +
  +    /**
  +      * Appends the content of another <code>DOMString</code> to this string.
  +      *
  +      * @param other The object to be appended
  +      */
  +	DOMString& operator +=(const DOMString &other);
  +
  +
  +    /**
  +      * Appends the content of a c-style string to this string.
  +      *
  +      * @param other The string to be appended
  +      */
  +    DOMString& operator +=(const XMLCh* other);
  +
  +
  +    /**
  +      * Appends a character to this string.
  +      *
  +      * @param ch The character to be appended
  +      */
  +	DOMString& operator +=(XMLCh ch);
  +
  +
  +    /**
         * Clears the data of this <code>DOMString</code>.
         *
         * @param offset The position from the beginning from which the data must be deleted
  @@ -291,13 +342,26 @@
   
       /**
         * Returns a copy of the string, transcoded to the local code page. The
  -      * caller is responsible for cleaning up this buffer.
  +      * caller owns the (char *) string that is returned, and is responsible
  +      * for deleting it.
         *
         * @return A pointer to a newly allocated buffer of char elements, which
         *         represents the original string, but in the local encoding.
         */
       char        *transcode() const;
   
  +
  +    /**
  +      * Creates a DOMString, transcoded from an input 8 bit char * string
  +      * in the local code page.
  +      *
  +      * @param str The string to be transcoded
  +      * @return A new DOMString object
  +      */
  +    static DOMString transcode(const char* str);
  +
  +
  +
       /**
         * Returns a sub-string of the <code>DOMString</code> starting at a specified position.
         *
  @@ -390,5 +454,59 @@
       static int              gLiveStringDataCount;
       static int              gTotalStringDataCount;
   };
  +
  +
  +/****** Global Helper Functions ******/
  +
  +/**
  +  * Concatenate two DOMString's.
  +  *
  +  * @param lhs the first string
  +  * @param rhs the second string
  +  * @return The concatenated object
  +  */
  +CDOM_EXPORT DOMString operator + (const DOMString &lhs, const DOMString &rhs);
  +
  +/**
  +  * Concatenate a null terminated Unicode string to a DOMString.
  +  *
  +  * @param lhs the DOMString
  +  * @param rhs the XMLCh * string
  +  * @return The concatenated object
  +  */
  +CDOM_EXPORT DOMString operator + (const DOMString &lhs, const XMLCh* rhs);
  +
  +
  +/**
  +  * Concatenate a DOMString to a null terminated Unicode string
  +  *
  +  * @param lhs the null-terminated Unicode string
  +  * @param rhs the DOMString
  +  * @return The concatenated object
  +  */
  +CDOM_EXPORT DOMString operator + (const XMLCh* lhs, const DOMString &rhs);
  +
  +
  +/**
  +  * Concatenate a single Unicode character to a DOMString.
  +  *
  +  * @param lhs the DOMString
  +  * @param rhs the character
  +  * @return The concatenated object
  +  */
  +CDOM_EXPORT DOMString operator + (const DOMString &lhs, XMLCh rhs);
  +
  +
  +/**
  +  * Concatenate a DOMString to a single Unicode character.
  +  *
  +  * @param lhs the character
  +  * @param rhs the DOMString
  +  * @return The concatenated object
  +  */
  +CDOM_EXPORT DOMString operator + (XMLCh lhs, const DOMString &rhs);
  +
  +
  +
   
   #endif
  
  
  
  1.11      +85 -5     xml-xerces/c/tests/DOM/DOMMemTest/DOMMemTest.cpp
  
  Index: DOMMemTest.cpp
  ===================================================================
  RCS file: /home/cvs/xml-xerces/c/tests/DOM/DOMMemTest/DOMMemTest.cpp,v
  retrieving revision 1.10
  retrieving revision 1.11
  diff -u -r1.10 -r1.11
  --- DOMMemTest.cpp	2000/01/29 00:39:09	1.10
  +++ DOMMemTest.cpp	2000/02/03 23:07:28	1.11
  @@ -66,6 +66,9 @@
   
   /**
    * $Log: DOMMemTest.cpp,v $
  + * Revision 1.11  2000/02/03 23:07:28  andyh
  + * Add several new functions from Robert Weir to DOMString.
  + *
    * Revision 1.10  2000/01/29 00:39:09  andyh
    * Redo synchronization in DOMStringHandle allocator.  There
    * was a bug in the use of Compare and Swap.  Switched to mutexes.
  @@ -196,12 +199,15 @@
       //
       TESTPROLOG
       {
  +        //  ToDo - this test will fail on EBCDIC machines.  !!
   
           XMLCh a[] = {'H', 'e', 'l', 'l', 'o', 0};
           DOMString x(a);
           DOMString y = "Hello";
  -
           TASSERT(x.equals(y));
  +
  +        DOMString z(a+2, 3);
  +        TASSERT(z.equals("llo"));
       }
       TESTEPILOG
   
  @@ -213,6 +219,13 @@
           DOMString a;
           DOMString b;
           TASSERT (a==0);
  +        a = 0;
  +        TASSERT(a==0);
  +        TASSERT((a!=0) == false);
  +
  +        DOMString c(0);
  +        TASSERT(c==0);
  +        TASSERT(c==a);
       }
       TESTEPILOG
   
  @@ -225,6 +238,10 @@
           DOMString b = a.clone();
           TASSERT(a.equals(b));
           TASSERT(a != b);
  +        a = 0;
  +        TASSERT(b.equals("hello"));
  +        TASSERT(a == 0);
  +        TASSERT(a.equals(""));
       }
       TESTEPILOG
   
  @@ -284,6 +301,7 @@
   
       //
       //  Test 7  Append Data, Common case, no extra space in original buffer.
  +    //          Also, operator +=, which is a convenience alias for appendData.
       //
       TESTPROLOG;
       {
  @@ -293,6 +311,24 @@
           a.appendData(b);
           TASSERT(a.equals("Test 07append"));
           TASSERT(b.equals("append"));
  +
  +        a.appendData((XMLCh)0x31);
  +        TASSERT(a.equals("Test 07append1"));
  +
  +        XMLCh  s[] = {0x32, 0x33, 0x00};
  +        a.appendData(s);
  +        TASSERT(a.equals("Test 07append123"));
  +
  +        a = "Test 07a ";
  +        a += b;
  +        TASSERT(a.equals("Test 07a append"));
  +       
  +        a += (XMLCh)0x31;
  +        TASSERT(a.equals("Test 07a append1"));
  +
  +        a += s;
  +        TASSERT(a.equals("Test 07a append123"));
  +
       }
       TESTEPILOG
   
  @@ -302,11 +338,12 @@
       //
       TESTPROLOG;
       {
  -        DOMString a(100);
  +        DOMString a;
  +		a.reserve(100);
           DOMString b("Test 08");
           DOMString c("append");
   
  -        // TASSERT(a == 0);  // (should it?)
  +        TASSERT(a != 0);  // (The String object has an identity, even if no contents)
           TASSERT(a.length() == 0);
           a.appendData(b);
           TASSERT(a.equals(b));
  @@ -327,7 +364,8 @@
       //
       TESTPROLOG;
       {
  -        DOMString a(100);
  +        DOMString a;
  +		a.reserve(100);
           DOMString b("Test 09");
           DOMString c("append");
   
  @@ -351,9 +389,38 @@
           TASSERT(c.equals("append"));
           TASSERT(d.equals("Test 09"));
       };
  -    TESTEPILOG
  +    TESTEPILOG;
  +
  +
  +    //
  +    // Test 10   DOMString Operator +
  +    //
  +    TESTPROLOG;
  +    {
  +        DOMString a;
  +        DOMString b("DOMString ");
  +        XMLCh     s[] = {0x58, 0x4d, 0x4c, 0x20, 0x00};   // Unicode "XML "
  +        XMLCh     Z   = 0x5A;                             // Unicode 'Z'
  +
  +        a = b + b;
  +        TASSERT(a.equals("DOMString DOMString "));
   
  +        a = b + s;
  +        TASSERT(a.equals("DOMString XML "));
  +
  +        a = s + b;
  +        TASSERT(a.equals("XML DOMString "));
  +
  +        a = b + Z;
  +        TASSERT(a.equals("DOMString Z"));
  +
  +        a = Z + b;
  +        TASSERT(a.equals("ZDOMString "));
  +    }
  +    TESTEPILOG;
   
  +
  +
       //
       //  Test Doc01      Create a new empty document
       //
  @@ -787,6 +854,9 @@
   		char *roundTripString = DOMTestStr.transcode();
   		TASSERT(strcmp(testStr, roundTripString) == 0);
   		delete [] roundTripString;
  +
  +        DOMString domstr2 = DOMString::transcode(testStr);
  +        TASSERT(domstr2.equals(DOMTestStr));
   	}
       TESTEPILOG;
   
  @@ -1248,6 +1318,12 @@
       }
       TESTEPILOG;
   
  +
  +    {
  +        
  +
  +
  +    }
    
       //
       //  Print Final allocation stats for full test
  @@ -1257,3 +1333,4 @@
       return 0;
       };
       
  +