dbertoni 00/12/01 13:40:55
Modified: c/src/XPath FunctionNormalizeSpace.cpp
FunctionNormalizeSpace.hpp
Log:
Add check to see if normalization is required.
Revision Changes Path
1.7 +87 -15 xml-xalan/c/src/XPath/FunctionNormalizeSpace.cpp
Index: FunctionNormalizeSpace.cpp
===================================================================
RCS file: /home/cvs/xml-xalan/c/src/XPath/FunctionNormalizeSpace.cpp,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -r1.6 -r1.7
--- FunctionNormalizeSpace.cpp 2000/11/21 22:00:24 1.6
+++ FunctionNormalizeSpace.cpp 2000/12/01 21:40:55 1.7
@@ -54,10 +54,11 @@
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
-#include <XPath/FunctionNormalizeSpace.hpp>
+#include "FunctionNormalizeSpace.hpp"
+
FunctionNormalizeSpace::FunctionNormalizeSpace()
{
}
@@ -78,7 +79,7 @@
{
assert(arg1.null() == false);
- return normalize(executionContext, arg1->str());
+ return normalize(executionContext, arg1);
}
@@ -111,12 +112,10 @@
XObjectPtr
FunctionNormalizeSpace::normalize(
XPathExecutionContext& executionContext,
- const XalanDOMString& theString)
+ const XalanDOMString& theString) const
{
const unsigned int theStringLength = length(theString);
- XalanDOMChar thePreviousChar = 0;
-
// A vector to contain the new characters. We'll use it to construct
// the result string.
#if defined(XALAN_NO_NAMESPACES)
@@ -132,6 +131,8 @@
// just reserve the space now.
theVector.reserve(theStringLength);
+ bool fPreviousIsSpace = false;
+
// OK, strip out any multiple spaces...
for (unsigned int i = 0; i < theStringLength; i++)
{
@@ -140,22 +141,29 @@
if (isXMLWhitespace(theCurrentChar) == true)
{
// If the previous character wasn't a space, and we've
- // encountered some non-space characters, then push the
- // space.
- if (isXMLWhitespace(thePreviousChar) == false &&
theVector.size() > 0)
+ // encountered some non-space characters, and it's not
+ // the last character in the string, then push the
+ // space character (not the original character).
+ if (fPreviousIsSpace == false)
{
-
theVector.push_back(XalanDOMChar(XalanUnicode::charSpace));
+ if (theVector.size() > 0 &&
+ i < theStringLength - 1)
+ {
+
theVector.push_back(XalanDOMChar(XalanUnicode::charSpace));
+ }
+
+ fPreviousIsSpace = true;
}
}
else
{
theVector.push_back(theCurrentChar);
- }
- thePreviousChar = theCurrentChar;
+ fPreviousIsSpace = false;
+ }
}
- VectorType::size_type theSize = theVector.size();
+ const VectorType::size_type theSize = theVector.size();
if (theSize == 0)
{
@@ -165,11 +173,31 @@
{
if (isXMLWhitespace(theVector.back()) == true)
{
- // The last character is a space, so remove it...
- --theSize;
+ return
executionContext.getXObjectFactory().createString(&*theVector.begin(), theSize
- 1);
}
+ else
+ {
+ return
executionContext.getXObjectFactory().createString(&*theVector.begin(), theSize);
+ }
+ }
+}
+
- return
executionContext.getXObjectFactory().createString(theVector.begin(), theSize);
+
+XObjectPtr
+FunctionNormalizeSpace::normalize(
+ XPathExecutionContext& executionContext,
+ const XObjectPtr& theArg) const
+{
+ const XalanDOMString& theString = theArg->str();
+
+ if (needsNormalization(theString) == false)
+ {
+ return theArg;
+ }
+ else
+ {
+ return normalize(executionContext, theString);
}
}
@@ -192,4 +220,48 @@
{
return XALAN_STATIC_UCODE_STRING(
"The normalize-space() function takes zero arguments or one
argument!");
+}
+
+
+
+bool
+FunctionNormalizeSpace::needsNormalization(const XalanDOMString&
theString) const
+{
+ const unsigned int theStringLength = length(theString);
+
+ bool fNormalize = false;
+
+ bool fPreviousIsSpace = false;
+
+ // OK, search for multiple spaces, or whitespace that is not the
+ // space character...
+ for (unsigned int i = 0; i < theStringLength && fNormalize == false;
++i)
+ {
+ const XalanDOMChar theCurrentChar = charAt(theString, i);
+
+ if (isXMLWhitespace(theCurrentChar) == false)
+ {
+ ++i;
+
+ fPreviousIsSpace = false;
+ }
+ else
+ {
+ if (i == 0 ||
+ i == theStringLength - 1 ||
+ theCurrentChar !=
XalanDOMChar(XalanUnicode::charSpace) ||
+ fPreviousIsSpace == true)
+ {
+ fNormalize = true;
+ }
+ else
+ {
+ fPreviousIsSpace = true;
+
+ ++i;
+ }
+ }
+ }
+
+ return fNormalize;
}
1.5 +8 -4 xml-xalan/c/src/XPath/FunctionNormalizeSpace.hpp
Index: FunctionNormalizeSpace.hpp
===================================================================
RCS file: /home/cvs/xml-xalan/c/src/XPath/FunctionNormalizeSpace.hpp,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- FunctionNormalizeSpace.hpp 2000/11/30 21:36:32 1.4
+++ FunctionNormalizeSpace.hpp 2000/12/01 21:40:55 1.5
@@ -106,14 +106,18 @@
private:
+ bool
+ needsNormalization(const XalanDOMString& theString) const;
+
XObjectPtr
normalize(
XPathExecutionContext& executionContext,
- const XalanDOMString& theString);
+ const XalanDOMString& theString) const;
- const XalanDOMString
- NormalizeSpace(
- const XalanDOMString& theSourceString) const;
+ XObjectPtr
+ normalize(
+ XPathExecutionContext& executionContext,
+ const XObjectPtr& theArg) const;
const XalanDOMString
getError() const;