dbertoni 01/01/12 15:25:50
Modified: c/src/XalanDOM XalanDOMString.cpp XalanDOMString.hpp
Log:
Changes to allowing keeping the size of the string as a member variable.
Revision Changes Path
1.6 +83 -3 xml-xalan/c/src/XalanDOM/XalanDOMString.cpp
Index: XalanDOMString.cpp
===================================================================
RCS file: /home/cvs/xml-xalan/c/src/XalanDOM/XalanDOMString.cpp,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- XalanDOMString.cpp 2001/01/08 18:19:55 1.5
+++ XalanDOMString.cpp 2001/01/12 23:25:48 1.6
@@ -81,6 +81,9 @@
XalanDOMString::XalanDOMString() :
m_data()
+#if defined(XALAN_DOMSTRING_CACHE_SIZE)
+ , m_size(0)
+#endif
{
}
@@ -91,6 +94,9 @@
size_type
theStartPosition,
size_type theCount) :
m_data()
+#if defined(XALAN_DOMSTRING_CACHE_SIZE)
+ , m_size(0)
+#endif
{
if (theSource.length() != 0)
{
@@ -104,6 +110,9 @@
const XalanDOMChar* theString,
size_type theCount) :
m_data()
+#if defined(XALAN_DOMSTRING_CACHE_SIZE)
+ , m_size(0)
+#endif
{
assert(theString != 0);
@@ -119,6 +128,9 @@
const char* theString,
size_type theCount) :
m_data()
+#if defined(XALAN_DOMSTRING_CACHE_SIZE)
+ , m_size(0)
+#endif
{
assert(theString != 0);
@@ -126,8 +138,12 @@
{
TranscodeFromLocalCodePage(theString, theCount, m_data, true);
- assert(m_data.back() == 0);
+#if defined(XALAN_DOMSTRING_CACHE_SIZE)
+ m_size = m_data.size() - 1;
+#endif
}
+
+ invariants();
}
@@ -136,6 +152,9 @@
size_type theCount,
XalanDOMChar theChar) :
m_data()
+#if defined(XALAN_DOMSTRING_CACHE_SIZE)
+ , m_size(0)
+#endif
{
if (theCount != 0)
{
@@ -143,7 +162,13 @@
// Null-terminate it...
m_data.back() = 0;
+
+#if defined(XALAN_DOMSTRING_CACHE_SIZE)
+ m_size = theCount;
+#endif
}
+
+ invariants();
}
@@ -155,6 +180,8 @@
append(theRHS, length(theRHS));
+ invariants();
+
return *this;
}
@@ -178,15 +205,24 @@
m_data.push_back(0);
+#if defined(XALAN_DOMSTRING_CACHE_SIZE)
+ m_size = theLength;
+#endif
+
assert(length() == theLength);
- assert(m_data.back() == 0);
}
else
{
m_data.insert(getBackInsertIterator(), theString,
theString + theLength);
+
+#if defined(XALAN_DOMSTRING_CACHE_SIZE)
+ m_size += theCount;
+#endif
}
}
+ invariants();
+
return *this;
}
@@ -203,14 +239,22 @@
m_data.back() = 0;
+#if defined(XALAN_DOMSTRING_CACHE_SIZE)
+ m_size = theCount;
+#endif
+
assert(length() == theCount);
}
else
{
m_data.insert(getBackInsertIterator(), theCount, theChar);
+
+#if defined(XALAN_DOMSTRING_CACHE_SIZE)
+ m_size += theCount;
+#endif
}
- assert(m_data.back() == 0);
+ invariants();
return *this;
}
@@ -225,6 +269,12 @@
{
m_data.insert(getIteratorForPosition(thePosition), theString, theString
+ theCount);
+#if defined(XALAN_DOMSTRING_CACHE_SIZE)
+ m_size += theCount;
+#endif
+
+ invariants();
+
return *this;
}
@@ -238,6 +288,12 @@
{
m_data.insert(getIteratorForPosition(thePosition), theCount, theChar);
+#if defined(XALAN_DOMSTRING_CACHE_SIZE)
+ m_size += theCount;
+#endif
+
+ invariants();
+
return *this;
}
@@ -250,6 +306,12 @@
{
m_data.insert(thePosition, theChar);
+#if defined(XALAN_DOMSTRING_CACHE_SIZE)
+ ++m_size;
+#endif
+
+ invariants();
+
return thePosition;
}
@@ -262,6 +324,12 @@
XalanDOMChar theChar)
{
m_data.insert(thePosition, theCount, theChar);
+
+#if defined(XALAN_DOMSTRING_CACHE_SIZE)
+ m_size += theCount;
+#endif
+
+ invariants();
}
@@ -273,6 +341,12 @@
const_iterator theLastPosition)
{
m_data.insert(theInsertPosition, theFirstPosition, theLastPosition);
+
+#if defined(XALAN_DOMSTRING_CACHE_SIZE)
+ m_size = m_data.size() - 1;
+#endif
+
+ invariants();
}
@@ -340,6 +414,8 @@
int
XalanDOMString::compare(const XalanDOMChar* theString) const
{
+ invariants();
+
return doCompare(c_str(), length(), theString, length(theString));
}
@@ -352,6 +428,8 @@
const XalanDOMChar* theString,
size_type theCount2) const
{
+ invariants();
+
return doCompare(c_str() + thePosition1, theCount1, theString,
theCount2);
}
@@ -360,6 +438,8 @@
XalanDOMString::CharVectorType
XalanDOMString::transcode() const
{
+ invariants();
+
CharVectorType theResult;
TranscodeToLocalCodePage(c_str(), length(), theResult, true);
1.9 +212 -5 xml-xalan/c/src/XalanDOM/XalanDOMString.hpp
Index: XalanDOMString.hpp
===================================================================
RCS file: /home/cvs/xml-xalan/c/src/XalanDOM/XalanDOMString.hpp,v
retrieving revision 1.8
retrieving revision 1.9
diff -u -r1.8 -r1.9
--- XalanDOMString.hpp 2001/01/08 18:19:55 1.8
+++ XalanDOMString.hpp 2001/01/12 23:25:49 1.9
@@ -81,9 +81,9 @@
#else
#define XALAN_USE_CUSTOM_STRING
+//#define XALAN_DOMSTRING_CACHE_SIZE
-
#include <cassert>
@@ -147,8 +147,14 @@
if (&theRHS != this)
{
m_data = theRHS.m_data;
+
+#if defined(XALAN_DOMSTRING_CACHE_SIZE)
+ m_size = theRHS.m_size;
+#endif
}
+ invariants();
+
return *this;
}
@@ -163,48 +169,72 @@
m_data[0] = theRHS;
m_data[1] = XalanDOMChar(0);
+#if defined(XALAN_DOMSTRING_CACHE_SIZE)
+ m_size = 1;
+#endif
+
+ invariants();
+
return *this;
}
iterator
begin()
{
+ invariants();
+
return m_data.begin();
}
const_iterator
begin() const
{
+ invariants();
+
return m_data.begin();
}
reverse_iterator
rbegin()
{
+ invariants();
+
return m_data.rbegin();
}
const_reverse_iterator
rbegin() const
{
+ invariants();
+
return m_data.rbegin();
}
size_type
size() const
{
+ invariants();
+
+#if defined(XALAN_DOMSTRING_CACHE_SIZE)
+ return m_size;
+#else
return m_data.empty() == true ? 0 : m_data.size() - 1;
+#endif
}
size_type
length() const
{
+ invariants();
+
return size();
}
size_type
max_size() const
{
+ invariants();
+
return size_type(~0);
}
@@ -213,39 +243,82 @@
size_type theCount,
XalanDOMChar theChar)
{
- m_data.resize(theCount + 1, theChar);
+ invariants();
+ const size_type theOldSize = size();
+
+ if (theOldSize == 0)
+ {
+ // If the string is of 0 length, resize but add an
+ // extra byte for the terminating byte.
+ m_data.resize(theCount + 1, theChar);
+ }
+ else
+ {
+ // If the string is not of 0 length, resize but
+ // put a copy of theChar where the terminating
+ // byte used to be.
+ m_data.resize(theCount, theChar);
+
+ m_data[theOldSize] = theChar;
+ }
+
+#if defined(XALAN_DOMSTRING_CACHE_SIZE)
+ m_size += theCount;
+#endif
+
+ // Terminate...
m_data.back() = 0;
+
+ invariants();
}
void
resize(size_type theCount)
{
+ invariants();
+
resize(theCount, XalanDOMChar(0));
}
size_type
capacity() const
{
+ invariants();
+
return m_data.capacity() - 1;
}
void
reserve(size_type theCount = 0)
{
+ invariants();
+
m_data.reserve(theCount + 1);
}
void
clear()
{
+ invariants();
+
XalanDOMCharVectorType().swap(m_data);
+
+#if defined(XALAN_DOMSTRING_CACHE_SIZE)
+ m_size = 0;
+#endif
+
+ invariants();
}
void
erase()
{
- m_data.clear();
+ invariants();
+
+ clear();
+
+ invariants();
}
void
@@ -253,26 +326,50 @@
size_type theStartPosition,
size_type theCount = size_type(npos))
{
- const iterator i = getIteratorForPosition(theStartPosition);
+ invariants();
- m_data.erase(i, i + (theCount == size_type(npos) ? length() :
theCount));
+ const size_type theActualCount =
+ theCount == size_type(npos) ? length() : theCount;
+
+ if (theStartPosition == 0 && theCount == size())
+ {
+ erase();
+ }
+ else
+ {
+ const iterator i =
getIteratorForPosition(theStartPosition);
+
+ m_data.erase(i, i + (theActualCount));
+
+#if defined(XALAN_DOMSTRING_CACHE_SIZE)
+ m_size -= theActualCount;
+#endif
+ }
+
+ invariants();
}
bool
empty() const
{
+ invariants();
+
return m_data.size() < 2 ? true : false;
}
const_reference
operator[](size_type theIndex) const
{
+ invariants();
+
return m_data[theIndex];
}
reference
operator[](size_type theIndex)
{
+ invariants();
+
return m_data[theIndex];
}
@@ -282,12 +379,16 @@
const_reference
at(size_type theIndex) const
{
+ invariants();
+
return m_data.at(theIndex);
}
reference
at(size_type theIndex)
{
+ invariants();
+
return m_data.at(theIndex);
}
#endif
@@ -295,6 +396,8 @@
const XalanDOMChar*
c_str() const
{
+ invariants();
+
// $$$ ToDo: Do we really want to do this?
// for convenience, we will return a pointer to
// a default empty string so that c_str() never
@@ -305,30 +408,48 @@
const XalanDOMChar*
data() const
{
+ invariants();
+
return c_str();
}
void
swap(XalanDOMString& theOther)
{
+ invariants();
+
m_data.swap(theOther.m_data);
+
+#if defined(XALAN_DOMSTRING_CACHE_SIZE)
+#if !defined(XALAN_NO_NAMESPACES)
+ using std::swap;
+#endif
+
+ swap(m_size, theOther.m_size);
+#endif
}
XalanDOMString&
operator+=(const XalanDOMString& theSource)
{
+ invariants();
+
return append(theSource);
}
XalanDOMString&
operator+=(const XalanDOMChar* theString)
{
+ invariants();
+
return append(theString);
}
XalanDOMString&
operator+=(XalanDOMChar theChar)
{
+ invariants();
+
append(1, theChar);
return *this;
@@ -337,9 +458,15 @@
XalanDOMString&
assign(const XalanDOMChar* theSource)
{
+ invariants();
+
erase();
+ invariants();
+
return append(theSource);
+
+ invariants();
}
XalanDOMString&
@@ -347,8 +474,12 @@
const XalanDOMChar* theSource,
size_type theCount)
{
+ invariants();
+
erase();
+ invariants();
+
return append(theSource, theCount);
}
@@ -358,16 +489,24 @@
size_type thePosition,
size_type theCount)
{
+ invariants();
+
erase();
+ invariants();
+
return append(theSource, thePosition, theCount);
}
XalanDOMString&
assign(const XalanDOMString& theSource)
{
+ invariants();
+
m_data = theSource.m_data;
+ invariants();
+
return *this;
}
@@ -376,8 +515,12 @@
size_type theCount,
XalanDOMChar theChar)
{
+ invariants();
+
erase();
+ invariants();
+
return append(theCount, theChar);
}
@@ -386,16 +529,24 @@
const_iterator theFirstPosition,
const_iterator theLastPosition)
{
+ invariants();
+
erase();
+ invariants();
+
insert(begin(), theFirstPosition, theLastPosition);
+ invariants();
+
return *this;
}
XalanDOMString&
append(const XalanDOMString& theSource)
{
+ invariants();
+
return append(theSource.c_str(), theSource.length());
}
@@ -405,6 +556,8 @@
size_type thePosition,
size_type theCount)
{
+ invariants();
+
return append(theSource.c_str() + thePosition, theCount);
}
@@ -418,6 +571,8 @@
{
assert(theString != 0);
+ invariants();
+
return append(theString, length(theString));
}
@@ -429,7 +584,11 @@
void
push_back(XalanDOMChar theChar)
{
+ invariants();
+
append(1, theChar);
+
+ invariants();
}
XalanDOMString&
@@ -437,6 +596,8 @@
size_type thePosition,
const XalanDOMString& theString)
{
+ invariants();
+
return insert(thePosition, theString.c_str(),
theString.length());
}
@@ -447,6 +608,8 @@
size_type thePosition2,
size_type theCount)
{
+ invariants();
+
return insert(thePosition1, theString.c_str() + thePosition2,
theCount);
}
@@ -461,6 +624,8 @@
size_type thePosition,
const XalanDOMChar* theString)
{
+ invariants();
+
return insert(thePosition, theString, length(theString));
}
@@ -495,12 +660,16 @@
assert(theCount == size_type(npos) && thePosition < length() ||
thePosition + theCount <= length());
+ invariants();
+
return XalanDOMString(*this, thePosition, theCount);
}
int
compare(const XalanDOMString& theString) const
{
+ invariants();
+
return compare(theString.c_str());
}
@@ -510,6 +679,8 @@
size_type theCount1,
const XalanDOMString& theString) const
{
+ invariants();
+
return compare(thePosition1, theCount1, theString.c_str(),
theString.length());
}
@@ -521,6 +692,8 @@
size_type thePosition2,
size_type theCount2) const
{
+ invariants();
+
return compare(thePosition1, theCount1, theString.c_str() +
thePosition2, theCount2);
}
@@ -537,6 +710,8 @@
int
compare(const char* theString) const
{
+ invariants();
+
return compare(XalanDOMString(theString));
}
@@ -547,6 +722,8 @@
const char* theString,
size_type theCount2 = size_type(npos))
const
{
+ invariants();
+
return compare(thePosition1, theCount1,
XalanDOMString(theString, theCount2));
}
@@ -615,6 +792,24 @@
protected:
/*
+ * Function to assert invariant conditions for the class.
+ *
+ * @return the iterator
+ */
+ void
+ invariants() const
+ {
+#if !defined(NDEBUG
+
+#if defined(XALAN_DOMSTRING_CACHE_SIZE)
+ assert(m_data.size() == 0 || m_size == m_data.size() - 1);
+#endif
+
+ assert(m_data.size() == 0 || m_data.back() == 0);
+#endif
+ }
+
+ /*
* Get an iterator to the position of the terminating null.
*
* @return the iterator
@@ -622,30 +817,42 @@
iterator
getBackInsertIterator()
{
+ invariants();
+
return m_data.size() == 0 ? m_data.end() : m_data.end() - 1;
}
const_iterator
getBackInsertIterator() const
{
+ invariants();
+
return m_data.size() == 0 ? m_data.end() : m_data.end() - 1;
}
iterator
getIteratorForPosition(size_type thePosition)
{
+ invariants();
+
return m_data.begin() + thePosition;
}
const_iterator
getIteratorForPosition(size_type thePosition) const
{
+ invariants();
+
return m_data.begin() + thePosition;
}
private:
XalanDOMCharVectorType m_data;
+
+#if defined(XALAN_DOMSTRING_CACHE_SIZE
+ unsigned long m_size;
+#endif
static const XalanDOMChar s_empty;
};