You are viewing a plain text version of this content. The canonical link for it is here.
Posted to c-dev@xerces.apache.org by "Nadav Aharoni (GENERIZE)" <na...@generize.com> on 2000/10/11 19:41:24 UTC

XMLString::trim

The documentation for version 1.3.0 says that a bug in XMLString::trim was
fixed.
I hope that I downloaded the right file, and if I did, there is still a
problem with this function.

The problem was that when the characters are copied down, the string is not
terminated in the right place. I added one line. The code I added is between
//+++ and //--- (I'm working with the MS environment, no decent diff...)

I only fixed the problem with the XMLCh* version of the funtion. There is an
overloaded function for char* that probably can be changed in the same way.
A strange thing about the char* version is that is uses the following to
check if a char is a not space:
        if (toTrim[skip] > ' ')
That seems to assume that every char which is less or equal to ' ' (space)
is a space.
I would prefer isspace() ...

Here is the code for the XMLCh* version:

void XMLString::trim(XMLCh* const toTrim)
{
    const unsigned int len = stringLen(toTrim);
	
    unsigned int skip, scrape;
	
    for (skip = 0; skip < len; skip++)
    {
        if (!XMLPlatformUtils::fgTransService->isSpace(toTrim[skip]))
            break;
    }
	
    for (scrape = len; scrape > skip; scrape--)
    {
        if (!XMLPlatformUtils::fgTransService->isSpace(toTrim[scrape - 1]))
            break;
    }
	
    // Cap off at the scrap point
    if (scrape)
        toTrim[scrape] = 0;
	
    if (skip)
    {
        // Copy the chars down
        unsigned int index = 0;
        while (toTrim[skip])
            toTrim[index++] = toTrim[skip++];
		
		// +++
		// Terminate with a zero
		toTrim[index]=0;
		// ---
    }
}


Possible bug in VecAttributesImpl.cpp

Posted by "Perry A. Caro" <ca...@Adobe.COM>.
The following looks like a bug to me.  My client is in the
ContentHandler::startElement callback, and I'm asking for the value of an
attribute by qName:

	const XMLCh* val = attr.getValue(AN_ATTR);

It turns out that this element does not have an attribute of the specified
name.  However, instead of returning NULL like the doc stays, it throws an
ArrayIndexOutOfBoundsException.

The error appears to be in the handling of the -1 return value when an
attribute is not found.  The code in VecAttributesImpl.cpp is:

const XMLCh* VecAttributesImpl::getValue(const XMLCh* const QName) const 
{
	return getValue(getIndex(QName)) ;
}

Notice that getIndex returns int, but getValue takes unsigned int.  The code
in getValue is:

const XMLCh* VecAttributesImpl::getValue(const unsigned int index) const
{
    if (index >= fCount)
        ThrowXML(ArrayIndexOutOfBoundsException,
XMLExcepts::AttrList_BadIndex);
    return fVector->elementAt(index)->getValue();
}

index is >= fCount, because it is -1 cast to unsigned.

In any case, I think getValue should change to be something like:

const XMLCh* VecAttributesImpl::getValue(const XMLCh* const QName) const 
{
	const int i = getIndex(QName);
	return (i >= 0) ? getValue(i) : 0;
}

Likewise for getType, getValue, etc., and anywhere that the result of
getIndex is being passed.

Perry

Re: XMLString::trim

Posted by Andy Heninger <an...@jtcsv.com>.
XMLString::trimRight on all counts.  Your patch in in CVS.  Thanks.

Andy Heninger
IBM XML Technology Group, Cupertino, CA
heninger@us.ibm.com

----- Original Message -----
From: Nadav Aharoni (GENERIZE)
To: xerces-c-dev@xml.apache.org
Sent: Wednesday, October 11, 2000 10:41 AM
Subject: XMLString::trim


The documentation for version 1.3.0 says that a bug in XMLString::trim was
fixed.
I hope that I downloaded the right file, and if I did, there is still a
problem with this function.
The problem was that when the characters are copied down, the string is
not terminated in the right place. I added one line. The code I added is
between //+++ and //--- (I'm working with the MS environment, no decent
diff...)
I only fixed the problem with the XMLCh* version of the funtion. There is
an overloaded function for char* that probably can be changed in the same
way.
A strange thing about the char* version is that is uses the following to
check if a char is a not space:
        if (toTrim[skip] > ' ')
That seems to assume that every char which is less or equal to ' ' (space)
is a space.
I would prefer isspace() ...
Here is the code for the XMLCh* version:
void XMLString::trim(XMLCh* const toTrim)
{
    const unsigned int len = stringLen(toTrim);

    unsigned int skip, scrape;

    for (skip = 0; skip < len; skip++)
    {
        if (!XMLPlatformUtils::fgTransService->isSpace(toTrim[skip]))
            break;
    }

    for (scrape = len; scrape > skip; scrape--)
    {
        if (!XMLPlatformUtils::fgTransService->isSpace(toTrim[scrape -
1]))
            break;
    }

    // Cap off at the scrap point
    if (scrape)
        toTrim[scrape] = 0;

    if (skip)
    {
        // Copy the chars down
        unsigned int index = 0;
        while (toTrim[skip])
            toTrim[index++] = toTrim[skip++];

                // +++
                // Terminate with a zero
                toTrim[index]=0;
                // ---
    }
}