XMLString.hpp
Go to the documentation of this file.
/*
* 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
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* $Id: XMLString.hpp 1677601 2015-05-04 14:20:36Z amassari $
*/
#if !defined(XERCESC_INCLUDE_GUARD_XMLSTRING_HPP)
#define XERCESC_INCLUDE_GUARD_XMLSTRING_HPP
#include <xercesc/util/BaseRefVectorOf.hpp>
#include <xercesc/framework/XMLBuffer.hpp>
#include <xercesc/framework/MemoryManager.hpp>
#include <string.h>
#include <assert.h>
XERCES_CPP_NAMESPACE_BEGIN
class XMLLCPTranscoder;
class XMLUTIL_EXPORT XMLString
{
public:
/* Static methods for native character mode string manipulation */
static void catString
(
char* const target
, const char* const src
);
static void catString
(
XMLCh* const target
, const XMLCh* const src
);
static int compareIString
(
const char* const str1
, const char* const str2
);
static int compareIString
(
const XMLCh* const str1
, const XMLCh* const str2
);
static int compareIStringASCII
(
const XMLCh* const str1
, const XMLCh* const str2
);
static int compareNString
(
const char* const str1
, const char* const str2
, const XMLSize_t count
);
static int compareNString
(
const XMLCh* const str1
, const XMLCh* const str2
, const XMLSize_t count
);
static int compareNIString
(
const char* const str1
, const char* const str2
, const XMLSize_t count
);
static int compareNIString
(
const XMLCh* const str1
, const XMLCh* const str2
, const XMLSize_t count
);
static int compareString
(
const char* const str1
, const char* const str2
);
static int compareString
(
const XMLCh* const str1
, const XMLCh* const str2
);
static bool equals
(
const XMLCh* str1
, const XMLCh* str2
);
static bool equalsN
(
const XMLCh* str1
, const XMLCh* str2
, XMLSize_t n
);
static bool equals
(
const char* str1
, const char* str2
);
static bool equalsN
(
const char* str1
, const char* str2
, XMLSize_t n
);
static bool regionMatches
(
const XMLCh* const str1
, const int offset1
, const XMLCh* const str2
, const int offset2
, const XMLSize_t charCount
);
static bool regionIMatches
(
const XMLCh* const str1
, const int offset1
, const XMLCh* const str2
, const int offset2
, const XMLSize_t charCount
);
static void copyString
(
char* const target
, const char* const src
);
static void copyString
(
XMLCh* const target
, const XMLCh* const src
);
static bool copyNString
(
XMLCh* const target
, const XMLCh* const src
, const XMLSize_t maxChars
);
static XMLSize_t hash
(
const char* const toHash
, const XMLSize_t hashModulus
);
static XMLSize_t hash
(
const XMLCh* const toHash
, const XMLSize_t hashModulus
);
static XMLSize_t hashN
(
const XMLCh* const toHash
, const XMLSize_t numChars
, const XMLSize_t hashModulus
);
static int indexOf(const char* const toSearch, const char ch);
static int indexOf(const XMLCh* const toSearch, const XMLCh ch);
static int indexOf
(
const char* const toSearch
, const char chToFind
, const XMLSize_t fromIndex
, MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager
);
static int indexOf
(
const XMLCh* const toSearch
, const XMLCh chToFind
, const XMLSize_t fromIndex
, MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager
);
static int lastIndexOf(const char* const toSearch, const char ch);
static int lastIndexOf(const XMLCh* const toSearch, const XMLCh ch);
static int lastIndexOf
(
const XMLCh ch
, const XMLCh* const toSearch
, const XMLSize_t toSearchLen
);
static int lastIndexOf
(
const char* const toSearch
, const char chToFind
, const XMLSize_t fromIndex
, MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager
);
static int lastIndexOf
(
const XMLCh* const toSearch
, const XMLCh ch
, const XMLSize_t fromIndex
, MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager
);
static void moveChars
(
XMLCh* const targetStr
, const XMLCh* const srcStr
, const XMLSize_t count
);
static void subString
(
char* const targetStr
, const char* const srcStr
, const XMLSize_t startIndex
, const XMLSize_t endIndex
, MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager
);
static void subString
(
XMLCh* const targetStr
, const XMLCh* const srcStr
, const XMLSize_t startIndex
, const XMLSize_t endIndex
, MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager
);
static void subString
(
XMLCh* const targetStr
, const XMLCh* const srcStr
, const XMLSize_t startIndex
, const XMLSize_t endIndex
, const XMLSize_t srcStrLength
, MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager
);
static char* replicate(const char* const toRep,
MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager);
static XMLCh* replicate(const XMLCh* const toRep,
MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager);
static bool startsWith
(
const char* const toTest
, const char* const prefix
);
static bool startsWith
(
const XMLCh* const toTest
, const XMLCh* const prefix
);
static bool startsWithI
(
const char* const toTest
, const char* const prefix
);
static bool startsWithI
(
const XMLCh* const toTest
, const XMLCh* const prefix
);
static bool endsWith
(
const XMLCh* const toTest
, const XMLCh* const suffix
);
static const XMLCh* findAny
(
const XMLCh* const toSearch
, const XMLCh* const searchList
);
static XMLCh* findAny
(
XMLCh* const toSearch
, const XMLCh* const searchList
);
static int patternMatch
(
const XMLCh* const toSearch
, const XMLCh* const pattern
);
static XMLSize_t stringLen(const char* const src);
static XMLSize_t stringLen(const XMLCh* const src);
static bool isValidNOTATION(const XMLCh* const name
, MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager);
static bool isValidEncName(const XMLCh* const name);
static bool isAlpha(XMLCh const theChar);
static bool isDigit(XMLCh const theChar);
static bool isAlphaNum(XMLCh const theChar);
static bool isHex(XMLCh const theChar);
static bool isInList(const XMLCh* const toFind, const XMLCh* const enumList);
static void sizeToText
(
const XMLSize_t toFormat
, char* const toFill
, const XMLSize_t maxChars
, const unsigned int radix
, MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager
);
static void sizeToText
(
const XMLSize_t toFormat
, XMLCh* const toFill
, const XMLSize_t maxChars
, const unsigned int radix
, MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager
);
static void binToText
(
const unsigned int toFormat
, char* const toFill
, const XMLSize_t maxChars
, const unsigned int radix
, MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager
);
static void binToText
(
const unsigned int toFormat
, XMLCh* const toFill
, const XMLSize_t maxChars
, const unsigned int radix
, MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager
);
static void binToText
(
const unsigned long toFormat
, char* const toFill
, const XMLSize_t maxChars
, const unsigned int radix
, MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager
);
static void binToText
(
const unsigned long toFormat
, XMLCh* const toFill
, const XMLSize_t maxChars
, const unsigned int radix
, MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager
);
static void binToText
(
const int toFormat
, char* const toFill
, const XMLSize_t maxChars
, const unsigned int radix
, MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager
);
static void binToText
(
const int toFormat
, XMLCh* const toFill
, const XMLSize_t maxChars
, const unsigned int radix
, MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager
);
static void binToText
(
const long toFormat
, char* const toFill
, const XMLSize_t maxChars
, const unsigned int radix
, MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager
);
static void binToText
(
const long toFormat
, XMLCh* const toFill
, const XMLSize_t maxChars
, const unsigned int radix
, MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager
);
static bool textToBin
(
const XMLCh* const toConvert
, unsigned int& toFill
, MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager
);
static int parseInt
(
const XMLCh* const toConvert
, MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager
);
static void cut
(
XMLCh* const toCutFrom
, const XMLSize_t count
);
static char* transcode
(
const XMLCh* const toTranscode
, MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager
);
static bool transcode
(
const XMLCh* const toTranscode
, char* const toFill
, const XMLSize_t maxChars
, MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager
);
static XMLCh* transcode
(
const char* const toTranscode
, MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager
);
static bool transcode
(
const char* const toTranscode
, XMLCh* const toFill
, const XMLSize_t maxChars
, MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager
);
static void trim(char* const toTrim);
static void trim(XMLCh* const toTrim);
static BaseRefVectorOf<XMLCh>* tokenizeString(const XMLCh* const tokenizeSrc
, MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager);
static BaseRefVectorOf<XMLCh>* tokenizeString(const XMLCh* const tokenizeSrc
, XMLCh delimiter
, MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager);
static XMLCh* makeUName
(
const XMLCh* const pszURI
, const XMLCh* const pszName
);
static XMLSize_t replaceTokens
(
XMLCh* const errText
, const XMLSize_t maxChars
, const XMLCh* const text1
, const XMLCh* const text2
, const XMLCh* const text3
, const XMLCh* const text4
, MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager
);
static void upperCase(XMLCh* const toUpperCase);
static void upperCaseASCII(XMLCh* const toUpperCase);
static void lowerCase(XMLCh* const toLowerCase);
static void lowerCaseASCII(XMLCh* const toLowerCase);
static bool isWSReplaced(const XMLCh* const toCheck);
static bool isWSCollapsed(const XMLCh* const toCheck);
static void replaceWS(XMLCh* toConvert
, MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager);
static void collapseWS(XMLCh* toConvert
, MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager);
static void removeWS(XMLCh* toConvert
, MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager);
static void removeChar(const XMLCh* const srcString
, const XMLCh& toRemove
, XMLBuffer& dstBuffer);
static void fixURI(const XMLCh* const str, XMLCh* const target);
static void release
(
char** buf
, MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager
);
static void release
(
XMLCh** buf
, MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager
);
private :
XMLString();
~XMLString();
static void initString(XMLLCPTranscoder* const defToUse,
MemoryManager* const manager);
static void termString();
static bool validateRegion(const XMLCh* const str1, const int offset1,
const XMLCh* const str2, const int offset2,
const XMLSize_t charCount);
static MemoryManager* fgMemoryManager;
friend class XMLPlatformUtils;
};
// ---------------------------------------------------------------------------
// Inline some methods that are either just passthroughs to other string
// methods, or which are key for performance.
// ---------------------------------------------------------------------------
inline void XMLString::moveChars( XMLCh* const targetStr
, const XMLCh* const srcStr
, const XMLSize_t count)
{
memmove(targetStr, srcStr, count * sizeof(XMLCh));
}
inline XMLSize_t XMLString::stringLen(const XMLCh* const src)
{
if (src == 0)
return 0;
const XMLCh* pszTmp = src;
while (*pszTmp++) ;
return (pszTmp - src - 1);
}
inline XMLCh* XMLString::replicate(const XMLCh* const toRep,
MemoryManager* const manager)
{
// If a null string, return a null string!
XMLCh* ret = 0;
if (toRep)
{
const XMLSize_t len = stringLen(toRep);
ret = (XMLCh*) manager->allocate((len+1) * sizeof(XMLCh)); //new XMLCh[len + 1];
memcpy(ret, toRep, (len + 1) * sizeof(XMLCh));
}
return ret;
}
inline bool XMLString::startsWith( const XMLCh* const toTest
, const XMLCh* const prefix)
{
return (compareNString(toTest, prefix, stringLen(prefix)) == 0);
}
inline bool XMLString::startsWithI( const XMLCh* const toTest
, const XMLCh* const prefix)
{
return (compareNIString(toTest, prefix, stringLen(prefix)) == 0);
}
inline bool XMLString::endsWith(const XMLCh* const toTest,
const XMLCh* const suffix)
{
XMLSize_t suffixLen = XMLString::stringLen(suffix);
return regionMatches(toTest, (int)(XMLString::stringLen(toTest) - suffixLen),
suffix, 0, suffixLen);
}
inline bool XMLString::validateRegion(const XMLCh* const str1,
const int offset1,
const XMLCh* const str2,
const int offset2,
const XMLSize_t charCount)
{
if (offset1 < 0 || offset2 < 0 ||
(offset1 + charCount) > XMLString::stringLen(str1) ||
(offset2 + charCount) > XMLString::stringLen(str2) )
return false;
return true;
}
inline bool XMLString::equals( const XMLCh* str1
, const XMLCh* str2)
{
if (str1 == str2)
return true;
if (str1 == 0 || str2 == 0)
return ((!str1 || !*str1) && (!str2 || !*str2));
while (*str1)
if(*str1++ != *str2++) // they are different (or str2 is shorter and we hit the NULL)
return false;
// either both ended (and *str2 is 0 too), or str2 is longer
return (*str2==0);
}
inline bool XMLString::equalsN(const XMLCh* str1,
const XMLCh* str2,
XMLSize_t n)
{
if (str1 == str2 || n == 0)
return true;
if (str1 == 0 || str2 == 0)
return ((!str1 || !*str1) && (!str2 || !*str2));
for (; n != 0 && *str1 && *str2; --n, ++str1, ++str2)
if(*str1 != *str2)
break;
return n == 0 || *str1 == *str2; // either equal or both ended premat.
}
inline bool XMLString::equals( const char* str1
, const char* str2)
{
if (str1 == str2)
return true;
if (str1 == 0 || str2 == 0)
return ((!str1 || !*str1) && (!str2 || !*str2));
while (*str1)
if(*str1++ != *str2++) // they are different (or str2 is shorter and we hit the NULL)
return false;
// either both ended (and *str2 is 0 too), or str2 is longer
return (*str2==0);
}
inline bool XMLString::equalsN(const char* str1,
const char* str2,
XMLSize_t n)
{
if (str1 == str2 || n == 0)
return true;
if (str1 == 0 || str2 == 0)
return ((!str1 || !*str1) && (!str2 || !*str2));
for (; n != 0 && *str1 && *str2; --n, ++str1, ++str2)
if(*str1 != *str2)
break;
return n == 0 || *str1 == *str2; // either equal or both ended premat.
}
inline int XMLString::lastIndexOf(const XMLCh* const toSearch, const XMLCh ch)
{
return XMLString::lastIndexOf(ch, toSearch, stringLen(toSearch));
}
inline XMLSize_t XMLString::hash(const XMLCh* const tohash
, const XMLSize_t hashModulus)
{
if (tohash == 0 || *tohash == 0)
return 0;
const XMLCh* curCh = tohash;
XMLSize_t hashVal = (XMLSize_t)(*curCh++);
while (*curCh)
hashVal = (hashVal * 38) + (hashVal >> 24) + (XMLSize_t)(*curCh++);
// Divide by modulus
return hashVal % hashModulus;
}
inline XMLSize_t XMLString::hashN(const XMLCh* const tohash
, const XMLSize_t n
, const XMLSize_t hashModulus)
{
if (tohash == 0 || n == 0)
return 0;
const XMLCh* curCh = tohash;
XMLSize_t hashVal = (XMLSize_t)(*curCh++);
for(XMLSize_t i=0;i<n;i++)
hashVal = (hashVal * 38) + (hashVal >> 24) + (XMLSize_t)(*curCh++);
// Divide by modulus
return hashVal % hashModulus;
}
XERCES_CPP_NAMESPACE_END
#endif
XMLString::replicate
static char * replicate(const char *const toRep, MemoryManager *const manager=XMLPlatformUtils::fgMemoryManager)
Replicates a string NOTE: The returned buffer is allocated with the MemoryManager.
XMLPlatformUtils::fgMemoryManager
static MemoryManager * fgMemoryManager
The configurable memory manager.
Definition: PlatformUtils.hpp:121
MemoryManager.hpp
XMLString::hashN
static XMLSize_t hashN(const XMLCh *const toHash, const XMLSize_t numChars, const XMLSize_t hashModulus)
Hashes a string given a modulus taking a maximum number of characters as the limit.
Definition: XMLString.hpp:1612
XMLString::moveChars
static void moveChars(XMLCh *const targetStr, const XMLCh *const srcStr, const XMLSize_t count)
Moves X number of chars.
Definition: XMLString.hpp:1453
MemoryManager
Configurable memory manager.
Definition: MemoryManager.hpp:39
XMLString::stringLen
static XMLSize_t stringLen(const char *const src)
Get the length of the string.
XMLCh
uint16_t XMLCh
Definition: Xerces_autoconf_config.hpp:120
XERCES_CPP_NAMESPACE_BEGIN
#define XERCES_CPP_NAMESPACE_BEGIN
Definition: XercesDefs.hpp:112
XMLPlatformUtils
Utilities that must be implemented in a platform-specific way.
Definition: PlatformUtils.hpp:68
XMLString::lastIndexOf
static int lastIndexOf(const char *const toSearch, const char ch)
Provides the index of the last occurrence of a character within a string.
XMLSize_t
size_t XMLSize_t
Definition: Xerces_autoconf_config.hpp:112
XMLString::startsWithI
static bool startsWithI(const char *const toTest, const char *const prefix)
Tells if the sub-string appears within a string at the beginning without regard to case...
XMLUTIL_EXPORT
#define XMLUTIL_EXPORT
Definition: XercesDefs.hpp:162
XMLString::equalsN
static bool equalsN(const XMLCh *str1, const XMLCh *str2, XMLSize_t n)
compares str1 and str2
Definition: XMLString.hpp:1540
XMLString::startsWith
static bool startsWith(const char *const toTest, const char *const prefix)
Tells if the sub-string appears within a string at the beginning.
XERCES_CPP_NAMESPACE_END
#define XERCES_CPP_NAMESPACE_END
Definition: XercesDefs.hpp:113
MemoryManager::allocate
virtual void * allocate(XMLSize_t size)=0
This method allocates requested memory.
XMLLCPTranscoder
Definition: TransService.hpp:388
XMLString
Class for representing native character strings and handling common string operations.
Definition: XMLString.hpp:45
XMLString::hash
static XMLSize_t hash(const char *const toHash, const XMLSize_t hashModulus)
Hashes a string given a modulus.
XMLString::endsWith
static bool endsWith(const XMLCh *const toTest, const XMLCh *const suffix)
Tells if the sub-string appears within a string at the end.
Definition: XMLString.hpp:1498
XMLString::equals
static bool equals(const XMLCh *str1, const XMLCh *str2)
compares str1 and str2
Definition: XMLString.hpp:1523