[Libreoffice-commits] core.git: Branch 'feature/saxparser' - 2 commits - include/sax sax/qa sax/source

Matúš Kukan matus.kukan at gmail.com
Wed Oct 16 02:44:51 PDT 2013


 include/sax/fastattribs.hxx          |   20 ++++++++----
 sax/qa/cppunit/parser.cxx            |   10 ------
 sax/source/fastparser/fastparser.cxx |    8 ++--
 sax/source/tools/fastattribs.cxx     |   57 +++++++++++++++++++++++++----------
 4 files changed, 60 insertions(+), 35 deletions(-)

New commits:
commit 050247997caf09a61ead4d494c8722957d550129
Author: Matúš Kukan <matus.kukan at gmail.com>
Date:   Wed Oct 16 11:15:31 2013 +0200

    fastparser: don't use OStrings in attribute list, just simple char buffer
    
    Change-Id: I4879563fae3b85c68bbd1c4b260f9833848f4bda

diff --git a/include/sax/fastattribs.hxx b/include/sax/fastattribs.hxx
index 88e9d61..238b7d4 100644
--- a/include/sax/fastattribs.hxx
+++ b/include/sax/fastattribs.hxx
@@ -40,9 +40,9 @@ struct UnknownAttribute
     OString maName;
     OString maValue;
 
-    UnknownAttribute( const OUString& rNamespaceURL, const OString& rName, const OString& rValue );
+    UnknownAttribute( const OUString& rNamespaceURL, const OString& rName, const sal_Char* pValue );
 
-    UnknownAttribute( const OString& rName, const OString& rValue );
+    UnknownAttribute( const OString& rName, const sal_Char* pValue );
 
     void FillAttribute( ::com::sun::star::xml::Attribute* pAttrib ) const;
 };
@@ -56,9 +56,10 @@ public:
     virtual ~FastAttributeList();
 
     void clear();
+    void add( sal_Int32 nToken, const sal_Char* pValue, size_t nValueLength = 0 );
     void add( sal_Int32 nToken, const OString& rValue );
-    void addUnknown( const OUString& rNamespaceURL, const OString& rName, const OString& rValue );
-    void addUnknown( const OString& rName, const OString& rValue );
+    void addUnknown( const OUString& rNamespaceURL, const OString& rName, const sal_Char* pValue );
+    void addUnknown( const OString& rName, const sal_Char* pValue );
 
     // XFastAttributeList
     virtual ::sal_Bool SAL_CALL hasAttribute( ::sal_Int32 Token ) throw (::com::sun::star::uno::RuntimeException);
@@ -70,11 +71,18 @@ public:
     virtual ::com::sun::star::uno::Sequence< ::com::sun::star::xml::FastAttribute > SAL_CALL getFastAttributes() throw (::com::sun::star::uno::RuntimeException);
 
 private:
+    inline sal_Int32 AttributeValueLength(sal_Int32 i);
+
+private:
+    sal_Char *mpChunk; ///< buffer to store all attribute values - null terminated strings
+    sal_Int32 mnChunkLength; ///< size of allocated memory for mpChunk
+    // maAttributeValues stores pointers, relative to mpChunk, for each attribute value string
+    // length of the string is maAttributeValues[n+1] - maAttributeValues[n] - 1
+    // maAttributeValues[0] == 0
+    std::vector< sal_Int32 > maAttributeValues;
     std::vector< sal_Int32 > maAttributeTokens;
-    std::vector< OString > maAttributeValues;
     UnknownAttributeList maUnknownAttributes;
     ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XFastTokenHandler > mxTokenHandler;
-
 };
 
 }
diff --git a/sax/source/fastparser/fastparser.cxx b/sax/source/fastparser/fastparser.cxx
index 664b28b..4a1d814 100644
--- a/sax/source/fastparser/fastparser.cxx
+++ b/sax/source/fastparser/fastparser.cxx
@@ -986,10 +986,10 @@ void FastSaxParser::callbackStartElement( const XML_Char* pwName, const XML_Char
                 {
                     sal_Int32 nAttributeToken = GetTokenWithPrefix( pPrefix, nPrefixLen, pName, nNameLen );
                     if( nAttributeToken != FastToken::DONTKNOW )
-                        rEvent.mxAttributes->add( nAttributeToken, OString(awAttributes[i+1]) );
+                        rEvent.mxAttributes->add( nAttributeToken, awAttributes[i+1] );
                     else
                         rEvent.mxAttributes->addUnknown( GetNamespaceURL( pPrefix, nPrefixLen ),
-                                OString(pName, nNameLen), OString(awAttributes[i+1]) );
+                                OString(pName, nNameLen), awAttributes[i+1] );
                 }
             }
             else
@@ -998,9 +998,9 @@ void FastSaxParser::callbackStartElement( const XML_Char* pwName, const XML_Char
                 {
                     sal_Int32 nAttributeToken = GetToken( pName, nNameLen );
                     if( nAttributeToken != FastToken::DONTKNOW )
-                        rEvent.mxAttributes->add( nAttributeToken, OString(awAttributes[i+1]) );
+                        rEvent.mxAttributes->add( nAttributeToken, awAttributes[i+1] );
                     else
-                        rEvent.mxAttributes->addUnknown( OString(pName, nNameLen), OString(awAttributes[i+1]) );
+                        rEvent.mxAttributes->addUnknown( OString(pName, nNameLen), awAttributes[i+1] );
                 }
             }
         }
diff --git a/sax/source/tools/fastattribs.cxx b/sax/source/tools/fastattribs.cxx
index b25ff08..8020866 100644
--- a/sax/source/tools/fastattribs.cxx
+++ b/sax/source/tools/fastattribs.cxx
@@ -27,13 +27,13 @@ using namespace ::com::sun::star::xml::sax;
 namespace sax_fastparser
 {
 
-UnknownAttribute::UnknownAttribute( const OUString& rNamespaceURL, const OString& rName, const OString& rValue )
-    : maNamespaceURL( rNamespaceURL ), maName( rName ), maValue( rValue )
+UnknownAttribute::UnknownAttribute( const OUString& rNamespaceURL, const OString& rName, const sal_Char* pValue )
+    : maNamespaceURL( rNamespaceURL ), maName( rName ), maValue( pValue )
 {
 }
 
-UnknownAttribute::UnknownAttribute( const OString& rName, const OString& rValue )
-    : maName( rName ), maValue( rValue )
+UnknownAttribute::UnknownAttribute( const OString& rName, const sal_Char* pValue )
+    : maName( rName ), maValue( pValue )
 {
 }
 
@@ -50,33 +50,54 @@ void UnknownAttribute::FillAttribute( Attribute* pAttrib ) const
 FastAttributeList::FastAttributeList( const ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XFastTokenHandler >& xTokenHandler )
 : mxTokenHandler( xTokenHandler )
 {
+    // random initial size of buffer to store attribute values
+    mnChunkLength = 100;
+    mpChunk = (sal_Char *) malloc( mnChunkLength );
+    maAttributeValues.push_back( 0 );
 }
 
 FastAttributeList::~FastAttributeList()
 {
+    free( mpChunk );
 }
 
 void FastAttributeList::clear()
 {
     maAttributeTokens.clear();
     maAttributeValues.clear();
+    maAttributeValues.push_back( 0 );
     maUnknownAttributes.clear();
 }
 
-void FastAttributeList::add( sal_Int32 nToken, const OString& rValue )
+void FastAttributeList::add( sal_Int32 nToken, const sal_Char* pValue, size_t nValueLength )
 {
     maAttributeTokens.push_back( nToken );
-    maAttributeValues.push_back( rValue );
+    if (nValueLength == 0)
+        nValueLength = strlen(pValue);
+    sal_Int32 nWritePosition = maAttributeValues.back();
+    maAttributeValues.push_back( maAttributeValues.back() + nValueLength + 1 );
+    if (maAttributeValues.back() > mnChunkLength)
+    {
+        mnChunkLength = maAttributeValues.back();
+        mpChunk = (sal_Char *) realloc( mpChunk, mnChunkLength );
+    }
+    strncpy(mpChunk + nWritePosition, pValue, nValueLength);
+    mpChunk[nWritePosition + nValueLength] = '\0';
+}
+
+void FastAttributeList::add( sal_Int32 nToken, const OString& rValue )
+{
+    add( nToken, rValue.getStr(), rValue.getLength() );
 }
 
-void FastAttributeList::addUnknown( const OUString& rNamespaceURL, const OString& rName, const OString& rValue )
+void FastAttributeList::addUnknown( const OUString& rNamespaceURL, const OString& rName, const sal_Char* pValue )
 {
-    maUnknownAttributes.push_back( UnknownAttribute( rNamespaceURL, rName, rValue ) );
+    maUnknownAttributes.push_back( UnknownAttribute( rNamespaceURL, rName, pValue ) );
 }
 
-void FastAttributeList::addUnknown( const OString& rName, const OString& rValue )
+void FastAttributeList::addUnknown( const OString& rName, const sal_Char* pValue )
 {
-    maUnknownAttributes.push_back( UnknownAttribute( rName, rValue ) );
+    maUnknownAttributes.push_back( UnknownAttribute( rName, pValue ) );
 }
 
 // XFastAttributeList
@@ -94,7 +115,7 @@ sal_Int32 FastAttributeList::getValueToken( ::sal_Int32 Token ) throw (SAXExcept
     for (size_t i = 0; i < maAttributeTokens.size(); ++i)
         if (maAttributeTokens[i] == Token)
         {
-            Sequence< sal_Int8 > aSeq( (sal_Int8*) maAttributeValues[i].getStr(), maAttributeValues[i].getLength() );
+            Sequence< sal_Int8 > aSeq( (sal_Int8*) mpChunk + maAttributeValues[i], AttributeValueLength(i) );
             return mxTokenHandler->getTokenFromUTF8( aSeq );
         }
 
@@ -106,7 +127,7 @@ sal_Int32 FastAttributeList::getOptionalValueToken( ::sal_Int32 Token, ::sal_Int
     for (size_t i = 0; i < maAttributeTokens.size(); ++i)
         if (maAttributeTokens[i] == Token)
         {
-            Sequence< sal_Int8 > aSeq( (sal_Int8*) maAttributeValues[i].getStr(), maAttributeValues[i].getLength() );
+            Sequence< sal_Int8 > aSeq( (sal_Int8*) mpChunk + maAttributeValues[i], AttributeValueLength(i) );
             return mxTokenHandler->getTokenFromUTF8( aSeq );
         }
 
@@ -117,7 +138,7 @@ OUString FastAttributeList::getValue( ::sal_Int32 Token ) throw (SAXException, R
 {
     for (size_t i = 0; i < maAttributeTokens.size(); ++i)
         if (maAttributeTokens[i] == Token)
-            return OStringToOUString( maAttributeValues[i], RTL_TEXTENCODING_UTF8 );
+            return OUString( mpChunk + maAttributeValues[i], AttributeValueLength(i), RTL_TEXTENCODING_UTF8 );
 
     throw SAXException();
 }
@@ -126,7 +147,7 @@ OUString FastAttributeList::getOptionalValue( ::sal_Int32 Token ) throw (Runtime
 {
     for (size_t i = 0; i < maAttributeTokens.size(); ++i)
         if (maAttributeTokens[i] == Token)
-            return OStringToOUString( maAttributeValues[i], RTL_TEXTENCODING_UTF8 );
+            return OUString( mpChunk + maAttributeValues[i], AttributeValueLength(i), RTL_TEXTENCODING_UTF8 );
 
     return OUString();
 }
@@ -145,12 +166,18 @@ Sequence< FastAttribute > FastAttributeList::getFastAttributes(  ) throw (Runtim
     for (size_t i = 0; i < maAttributeTokens.size(); ++i)
     {
         pAttr->Token = maAttributeTokens[i];
-        pAttr->Value = OStringToOUString( maAttributeValues[i], RTL_TEXTENCODING_UTF8 );
+        pAttr->Value = OUString( mpChunk + maAttributeValues[i], AttributeValueLength(i), RTL_TEXTENCODING_UTF8 );
         pAttr++;
     }
     return aSeq;
 }
 
+sal_Int32 FastAttributeList::AttributeValueLength(sal_Int32 i)
+{
+    // Pointers to null terminated strings
+    return maAttributeValues[i + 1] - maAttributeValues[i] - 1;
+}
+
 }
 
 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
commit af994e4381e2557684745c73080b3cf2eae9f8a2
Author: Matúš Kukan <matus.kukan at gmail.com>
Date:   Wed Oct 16 08:40:56 2013 +0200

    fix license header in previous commit
    
    Change-Id: Ibf04dab1d5dd138e39645b33ceca5191b7bd11b9

diff --git a/sax/qa/cppunit/parser.cxx b/sax/qa/cppunit/parser.cxx
index 861c53f..ac4ac53 100644
--- a/sax/qa/cppunit/parser.cxx
+++ b/sax/qa/cppunit/parser.cxx
@@ -5,16 +5,6 @@
  * This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
- *
- * This file incorporates work covered by the following license notice:
- *
- *   Licensed to the Apache Software Foundation (ASF) under one or more
- *   contributor license agreements. See the NOTICE file distributed
- *   with this work for additional information regarding copyright
- *   ownership. The ASF licenses this file to you under the Apache
- *   License, Version 2.0 (the "License"); you may not use this file
- *   except in compliance with the License. You may obtain a copy of
- *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
  */
 
 #include <sal/config.h>


More information about the Libreoffice-commits mailing list