diff options
author | Kohei Yoshida <kohei.yoshida@collabora.com> | 2013-10-07 12:15:28 -0400 |
---|---|---|
committer | Kohei Yoshida <kohei.yoshida@collabora.com> | 2013-10-08 15:44:33 -0400 |
commit | f4bda54cdaf13cf96ff7c9327036568825c8c323 (patch) | |
tree | 8fa25fe11fdc329e3a8a9b6a2750b75a7dd63577 /svl | |
parent | 6ddcbf4b2fddbb1bad2da05fc6ced1046493d058 (diff) |
Re-implement interning in order to return both string arrays.
One is for the cased string and the other one for the non-cased one.
Change-Id: I798687f2efecaaea73a09e0b3348f85a9d9e8c07
Diffstat (limited to 'svl')
-rw-r--r-- | svl/Library_svl.mk | 1 | ||||
-rw-r--r-- | svl/qa/unit/svl.cxx | 76 | ||||
-rw-r--r-- | svl/source/misc/sharedstring.cxx | 81 | ||||
-rw-r--r-- | svl/source/misc/sharedstringpool.cxx | 31 |
4 files changed, 134 insertions, 55 deletions
diff --git a/svl/Library_svl.mk b/svl/Library_svl.mk index dc2bf4134780..283e06b3b25a 100644 --- a/svl/Library_svl.mk +++ b/svl/Library_svl.mk @@ -110,6 +110,7 @@ $(eval $(call gb_Library_add_exception_objects,svl,\ svl/source/misc/lockfilecommon \ svl/source/misc/ownlist \ svl/source/misc/sharecontrolfile \ + svl/source/misc/sharedstring \ svl/source/misc/sharedstringpool \ svl/source/misc/strmadpt \ svl/source/misc/urihelper \ diff --git a/svl/qa/unit/svl.cxx b/svl/qa/unit/svl.cxx index 3cf7bdbbdbc5..0ac019b0d47b 100644 --- a/svl/qa/unit/svl.cxx +++ b/svl/qa/unit/svl.cxx @@ -283,42 +283,32 @@ void Test::testStringPool() SvtSysLocale aSysLocale; svl::SharedStringPool aPool(aSysLocale.GetCharClassPtr()); - const rtl_uString* p1 = aPool.intern("Andy"); - const rtl_uString* p2 = aPool.intern("Andy"); - CPPUNIT_ASSERT_EQUAL(p1, p2); + svl::SharedString p1, p2; + p1 = aPool.intern("Andy"); + p2 = aPool.intern("Andy"); + CPPUNIT_ASSERT_EQUAL(p1.getData(), p2.getData()); p2 = aPool.intern("Bruce"); - CPPUNIT_ASSERT_MESSAGE("They must differ.", p1 != p2); + CPPUNIT_ASSERT_MESSAGE("They must differ.", p1.getData() != p2.getData()); OUString aAndy("Andy"); - sal_uIntPtr si1 = aPool.getIdentifier("Andy"); - sal_uIntPtr si2 = aPool.getIdentifier(aAndy); - CPPUNIT_ASSERT_MESSAGE("Identifier shouldn't be 0.", si1); - CPPUNIT_ASSERT_MESSAGE("Identifier shouldn't be 0.", si2); - CPPUNIT_ASSERT_EQUAL(si1, si2); - si1 = aPool.getIdentifierIgnoreCase(aAndy); - CPPUNIT_ASSERT_MESSAGE("Case insensitive identifier shouldn't be 0.", si1); + p1 = aPool.intern("Andy"); + p2 = aPool.intern(aAndy); + CPPUNIT_ASSERT_MESSAGE("Identifier shouldn't be NULL.", p1.getData()); + CPPUNIT_ASSERT_MESSAGE("Identifier shouldn't be NULL.", p2.getData()); + CPPUNIT_ASSERT_EQUAL(p1.getData(), p2.getData()); // Test case insensitive string ID's. OUString aAndyLower("andy"), aAndyUpper("ANDY"); - si1 = aPool.getIdentifier("Andy"); - CPPUNIT_ASSERT_MESSAGE("This shouldn't be NULL.", si1); - aPool.intern(aAndyLower); - si2 = aPool.getIdentifier(aAndyLower); - CPPUNIT_ASSERT_MESSAGE("They must differ.", si1 != si2); - aPool.intern(aAndyUpper); - si2 = aPool.getIdentifier(aAndyUpper); - CPPUNIT_ASSERT_MESSAGE("They must differ.", si1 != si2); - - si1 = aPool.getIdentifierIgnoreCase("Andy"); - CPPUNIT_ASSERT_MESSAGE("This shouldn't be NULL.", si1); - si2 = aPool.getIdentifierIgnoreCase("andy"); - CPPUNIT_ASSERT_MESSAGE("This shouldn't be NULL.", si2); - CPPUNIT_ASSERT_EQUAL(si1, si2); - - si2 = aPool.getIdentifierIgnoreCase("ANDY"); - CPPUNIT_ASSERT_MESSAGE("This shouldn't be NULL.", si2); - CPPUNIT_ASSERT_EQUAL(si1, si2); + p1 = aPool.intern(aAndy); + p2 = aPool.intern(aAndyLower); + CPPUNIT_ASSERT_MESSAGE("Failed to intern strings.", p1.getData() && p2.getData()); + CPPUNIT_ASSERT_MESSAGE("These two ID's should differ.", p1.getData() != p2.getData()); + CPPUNIT_ASSERT_MESSAGE("These two ID's should be equal.", p1.getDataIgnoreCase() == p2.getDataIgnoreCase()); + p2 = aPool.intern(aAndyUpper); + CPPUNIT_ASSERT_MESSAGE("Failed to intern string.", p2.getData()); + CPPUNIT_ASSERT_MESSAGE("These two ID's should differ.", p1.getData() != p2.getData()); + CPPUNIT_ASSERT_MESSAGE("These two ID's should be equal.", p1.getDataIgnoreCase() == p2.getDataIgnoreCase()); } void Test::testStringPoolPurge() @@ -335,8 +325,8 @@ void Test::testStringPoolPurge() // Since no string objects referencing the pooled strings exist, purging // the pool should empty it. aPool.purge(); - CPPUNIT_ASSERT_MESSAGE("Wrong string count.", aPool.getCount() == 0); - CPPUNIT_ASSERT_MESSAGE("Wrong case insensitive string count.", aPool.getCountIgnoreCase() == 0); + CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(0), aPool.getCount()); + CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(0), aPool.getCountIgnoreCase()); // Now, create string objects on the heap. boost::scoped_ptr<OUString> pStr1(new OUString("Andy")); @@ -348,37 +338,37 @@ void Test::testStringPoolPurge() aPool.intern(*pStr3); aPool.intern(*pStr4); - CPPUNIT_ASSERT_MESSAGE("Wrong string count.", aPool.getCount() == 4); - CPPUNIT_ASSERT_MESSAGE("Wrong case insensitive string count.", aPool.getCountIgnoreCase() == 2); + CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(4), aPool.getCount()); + CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(2), aPool.getCountIgnoreCase()); // This shouldn't purge anything. aPool.purge(); - CPPUNIT_ASSERT_MESSAGE("Wrong string count.", aPool.getCount() == 4); - CPPUNIT_ASSERT_MESSAGE("Wrong case insensitive string count.", aPool.getCountIgnoreCase() == 2); + CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(4), aPool.getCount()); + CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(2), aPool.getCountIgnoreCase()); // Delete one heap string object, and purge. That should purge one string. pStr1.reset(); aPool.purge(); - CPPUNIT_ASSERT_MESSAGE("Wrong string count.", aPool.getCount() == 3); - CPPUNIT_ASSERT_MESSAGE("Wrong case insensitive string count.", aPool.getCountIgnoreCase() == 2); + CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(3), aPool.getCount()); + CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(2), aPool.getCountIgnoreCase()); // Ditto... pStr3.reset(); aPool.purge(); - CPPUNIT_ASSERT_MESSAGE("Wrong string count.", aPool.getCount() == 2); - CPPUNIT_ASSERT_MESSAGE("Wrong case insensitive string count.", aPool.getCountIgnoreCase() == 2); + CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(2), aPool.getCount()); + CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(2), aPool.getCountIgnoreCase()); // Again. pStr2.reset(); aPool.purge(); - CPPUNIT_ASSERT_MESSAGE("Wrong string count.", aPool.getCount() == 1); - CPPUNIT_ASSERT_MESSAGE("Wrong case insensitive string count.", aPool.getCountIgnoreCase() == 1); + CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(1), aPool.getCount()); + CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(1), aPool.getCountIgnoreCase()); // Delete 'Bruce' and purge. pStr4.reset(); aPool.purge(); - CPPUNIT_ASSERT_MESSAGE("Wrong string count.", aPool.getCount() == 0); - CPPUNIT_ASSERT_MESSAGE("Wrong case insensitive string count.", aPool.getCountIgnoreCase() == 0); + CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(0), aPool.getCount()); + CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(0), aPool.getCountIgnoreCase()); } void Test::checkPreviewString(SvNumberFormatter& aFormatter, diff --git a/svl/source/misc/sharedstring.cxx b/svl/source/misc/sharedstring.cxx new file mode 100644 index 000000000000..d5b27bdf0d10 --- /dev/null +++ b/svl/source/misc/sharedstring.cxx @@ -0,0 +1,81 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * 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/. + */ + +#include "svl/sharedstring.hxx" + +namespace svl { + +SharedString::SharedString() : mpData(NULL), mpDataIgnoreCase(NULL) {} + +SharedString::SharedString( rtl_uString* pData, rtl_uString* pDataIgnoreCase ) : + mpData(pData), mpDataIgnoreCase(pDataIgnoreCase) +{ + if (mpData) + rtl_uString_acquire(mpData); + if (mpDataIgnoreCase) + rtl_uString_acquire(mpDataIgnoreCase); +} + +SharedString::SharedString( const SharedString& r ) : mpData(r.mpData), mpDataIgnoreCase(r.mpDataIgnoreCase) +{ + if (mpData) + rtl_uString_acquire(mpData); + if (mpDataIgnoreCase) + rtl_uString_acquire(mpDataIgnoreCase); +} + +SharedString::~SharedString() +{ + if (mpData) + rtl_uString_release(mpData); + if (mpDataIgnoreCase) + rtl_uString_release(mpDataIgnoreCase); +} + +SharedString& SharedString::operator= ( const SharedString& r ) +{ + if (mpData) + rtl_uString_release(mpData); + if (mpDataIgnoreCase) + rtl_uString_release(mpDataIgnoreCase); + + mpData = r.mpData; + mpDataIgnoreCase = r.mpDataIgnoreCase; + + if (mpData) + rtl_uString_acquire(mpData); + if (mpDataIgnoreCase) + rtl_uString_acquire(mpDataIgnoreCase); + + return *this; +} + +rtl_uString* SharedString::getData() +{ + return mpData; +} + +const rtl_uString* SharedString::getData() const +{ + return mpData; +} + +rtl_uString* SharedString::getDataIgnoreCase() +{ + return mpDataIgnoreCase; +} + +const rtl_uString* SharedString::getDataIgnoreCase() const +{ + return mpDataIgnoreCase; +} + +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/svl/source/misc/sharedstringpool.cxx b/svl/source/misc/sharedstringpool.cxx index 805a6fc75576..6b8f15287aca 100644 --- a/svl/source/misc/sharedstringpool.cxx +++ b/svl/source/misc/sharedstringpool.cxx @@ -15,21 +15,29 @@ namespace svl { SharedStringPool::SharedStringPool() : mpCharClass(NULL) {} SharedStringPool::SharedStringPool( const CharClass* pCharClass ) : mpCharClass(pCharClass) {} -rtl_uString* SharedStringPool::intern( const OUString& rStr ) +SharedString SharedStringPool::intern( const OUString& rStr ) { InsertResultType aRes = findOrInsert(maStrPool, rStr); if (aRes.first == maStrPool.end()) // Insertion failed. - return NULL; + return SharedString(); rtl_uString* pOrig = aRes.first->pData; + if (!mpCharClass) + // We don't track case insensitive strings. + return SharedString(pOrig, NULL); + if (!aRes.second) + { // No new string has been inserted. Return the existing string in the pool. - return pOrig; + StrStoreType::iterator it = maStrStore.find(pOrig); + if (it == maStrStore.end()) + return SharedString(); - if (!mpCharClass) - return pOrig; + rtl_uString* pUpper = it->second.pData; + return SharedString(pOrig, pUpper); + } // This is a new string insertion. Establish mapping to upper-case variant. @@ -37,12 +45,11 @@ rtl_uString* SharedStringPool::intern( const OUString& rStr ) aRes = findOrInsert(maStrPoolUpper, aUpper); if (aRes.first == maStrPoolUpper.end()) // Failed to insert or fetch upper-case variant. Should never happen. - return pOrig; + return SharedString(); - // Set mapping. - maToUpperMap.insert(StrIdMapType::value_type(pOrig, *aRes.first)); + maStrStore.insert(StrStoreType::value_type(pOrig, *aRes.first)); - return pOrig; + return SharedString(pOrig, aRes.first->pData); } sal_uIntPtr SharedStringPool::getIdentifier( const OUString& rStr ) const @@ -58,8 +65,8 @@ sal_uIntPtr SharedStringPool::getIdentifierIgnoreCase( const OUString& rStr ) co // Not in the pool. return 0; - StrIdMapType::const_iterator itUpper = maToUpperMap.find(itOrig->pData); - if (itUpper == maToUpperMap.end()) + StrStoreType::const_iterator itUpper = maStrStore.find(itOrig->pData); + if (itUpper == maStrStore.end()) // Passed string is not in the pool. return 0; @@ -87,7 +94,7 @@ void SharedStringPool::purge() { // Remove it from the upper string map. This should unref the // upper string linked to this original string. - maToUpperMap.erase(p); + maStrStore.erase(p); } else // Still referenced outside the pool. Keep it. |