XMLString.hpp

Go to the documentation of this file.
00001 /*
00002  * Licensed to the Apache Software Foundation (ASF) under one or more
00003  * contributor license agreements.  See the NOTICE file distributed with
00004  * this work for additional information regarding copyright ownership.
00005  * The ASF licenses this file to You under the Apache License, Version 2.0
00006  * (the "License"); you may not use this file except in compliance with
00007  * the License.  You may obtain a copy of the License at
00008  * 
00009  *      http://www.apache.org/licenses/LICENSE-2.0
00010  * 
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS,
00013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  */
00017 
00018 /*
00019  * $Id: XMLString.hpp 568078 2007-08-21 11:43:25Z amassari $
00020  */
00021 
00022 #if !defined(XMLSTRING_HPP)
00023 #define XMLSTRING_HPP
00024 
00025 #include <xercesc/util/BaseRefVectorOf.hpp>
00026 #include <xercesc/framework/XMLBuffer.hpp>
00027 #include <xercesc/framework/MemoryManager.hpp>
00028 #include <string.h>
00029 #include <assert.h>
00030 
00031 XERCES_CPP_NAMESPACE_BEGIN
00032 
00033 class XMLLCPTranscoder;
00045 class XMLUTIL_EXPORT XMLString
00046 {
00047 public:
00048     /* Static methods for native character mode string manipulation */
00049 
00050 
00067     static void catString
00068     (
00069                 char* const     target
00070         , const char* const     src
00071     );
00072 
00085     static void catString
00086     (
00087                 XMLCh* const    target
00088         , const XMLCh* const    src
00089     );
00091 
00105     static int compareIString
00106     (
00107         const   char* const     str1
00108         , const char* const     str2
00109     );
00110 
00121     static int compareIString
00122     (
00123         const   XMLCh* const    str1
00124         , const XMLCh* const    str2
00125     );
00126 
00138     static int compareIStringASCII
00139     (
00140         const   XMLCh* const    str1
00141         , const XMLCh* const    str2
00142     );
00143 
00144 
00145 
00159     static int compareNString
00160     (
00161         const   char* const     str1
00162         , const char* const     str2
00163         , const unsigned int    count
00164     );
00165 
00179     static int compareNString
00180     (
00181         const   XMLCh* const    str1
00182         , const XMLCh* const    str2
00183         , const unsigned int    count
00184     );
00185 
00186 
00200     static int compareNIString
00201     (
00202         const   char* const     str1
00203         , const char* const     str2
00204         , const unsigned int    count
00205     );
00206 
00221     static int compareNIString
00222     (
00223         const   XMLCh* const    str1
00224         , const XMLCh* const    str2
00225         , const unsigned int    count
00226     );
00227 
00240     static int compareString
00241     (
00242         const   char* const     str1
00243         , const char* const     str2
00244     );
00245 
00257     static int compareString
00258     (
00259         const   XMLCh* const    str1
00260         , const XMLCh* const    str2
00261     );
00262 
00271     static bool equals
00272     (
00273           const XMLCh* const    str1
00274         , const XMLCh* const    str2
00275     );
00276 
00277     static bool equals
00278     (
00279           const char* const    str1
00280         , const char* const    str2
00281     );
00282 
00309     static bool regionMatches
00310     (
00311         const   XMLCh* const    str1
00312         , const int             offset1
00313         , const XMLCh* const    str2
00314         , const int             offset2
00315         , const unsigned int    charCount
00316     );
00317 
00345     static bool regionIMatches
00346     (
00347         const   XMLCh* const    str1
00348         , const int             offset1
00349         , const XMLCh* const    str2
00350         , const int             offset2
00351         , const unsigned int    charCount
00352     );
00354 
00367     static void copyString
00368     (
00369                 char* const     target
00370         , const char* const     src
00371     );
00372 
00383     static void copyString
00384     (
00385                 XMLCh* const    target
00386         , const XMLCh* const    src
00387     );
00388 
00401     static bool copyNString
00402     (
00403                 XMLCh* const    target
00404         , const XMLCh* const    src
00405         , const unsigned int    maxChars
00406     );
00408 
00418     static unsigned int hash
00419     (
00420         const   char* const     toHash
00421         , const unsigned int    hashModulus
00422         , MemoryManager* const  manager = XMLPlatformUtils::fgMemoryManager
00423     );
00424 
00432     static unsigned int hash
00433     (
00434         const   XMLCh* const    toHash
00435         , const unsigned int    hashModulus
00436         , MemoryManager* const  manager = XMLPlatformUtils::fgMemoryManager
00437     );
00438 
00448     static unsigned int hashN
00449     (
00450         const   XMLCh* const    toHash
00451         , const unsigned int    numChars
00452         , const unsigned int    hashModulus
00453         , MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager
00454     );
00455 
00457 
00468     static int indexOf(const char* const toSearch, const char ch);
00469 
00478     static int indexOf(const XMLCh* const toSearch, const XMLCh ch);
00479 
00491     static int indexOf
00492     (
00493         const   char* const     toSearch
00494         , const char            chToFind
00495         , const unsigned int    fromIndex
00496         , MemoryManager* const  manager = XMLPlatformUtils::fgMemoryManager
00497     );
00498 
00510     static int indexOf
00511     (
00512         const   XMLCh* const    toSearch
00513         , const XMLCh           chToFind
00514         , const unsigned int    fromIndex
00515         , MemoryManager* const  manager = XMLPlatformUtils::fgMemoryManager
00516     );
00517 
00526     static int lastIndexOf(const char* const toSearch, const char ch);
00527 
00536     static int lastIndexOf(const XMLCh* const toSearch, const XMLCh ch);
00537 
00547     static int lastIndexOf
00548     (
00549         const XMLCh ch
00550         , const XMLCh* const toSearch
00551         , const unsigned int toSearchLen
00552     );
00553 
00565     static int lastIndexOf
00566     (
00567         const   char* const     toSearch
00568         , const char            chToFind
00569         , const unsigned int    fromIndex
00570         , MemoryManager* const  manager = XMLPlatformUtils::fgMemoryManager
00571     );
00572 
00584     static int lastIndexOf
00585     (
00586         const   XMLCh* const    toSearch
00587         , const XMLCh           ch
00588         , const unsigned int    fromIndex
00589         , MemoryManager* const  manager = XMLPlatformUtils::fgMemoryManager
00590     );
00592 
00600     static void moveChars
00601     (
00602                 XMLCh* const    targetStr
00603         , const XMLCh* const    srcStr
00604         , const unsigned int    count
00605     );
00606 
00608 
00620     static void subString
00621     (
00622                 char* const    targetStr
00623         , const char* const    srcStr
00624         , const int            startIndex
00625         , const int            endIndex
00626         , MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager
00627     );
00628 
00638     static void subString
00639     (
00640                 XMLCh* const    targetStr
00641         , const XMLCh* const    srcStr
00642         , const int             startIndex
00643         , const int             endIndex
00644         , MemoryManager* const  manager = XMLPlatformUtils::fgMemoryManager
00645     );
00646 
00657     static void subString
00658     (
00659                 XMLCh* const    targetStr
00660         , const XMLCh* const    srcStr
00661         , const int             startIndex
00662         , const int             endIndex
00663         , const int             srcStrLength
00664         , MemoryManager* const  manager = XMLPlatformUtils::fgMemoryManager
00665     );
00666 
00668 
00680     static char* replicate(const char* const toRep);
00681 
00690     static char* replicate(const char* const toRep,
00691                            MemoryManager* const manager);
00692 
00702     static XMLCh* replicate(const XMLCh* const toRep);
00703 
00712     static XMLCh* replicate(const XMLCh* const toRep,
00713                             MemoryManager* const manager);
00714 
00716 
00725     static bool startsWith
00726     (
00727         const   char* const     toTest
00728         , const char* const     prefix
00729     );
00730 
00737     static bool startsWith
00738     (
00739         const   XMLCh* const    toTest
00740         , const XMLCh* const    prefix
00741     );
00742 
00751     static bool startsWithI
00752     (
00753         const   char* const     toTest
00754         , const char* const     prefix
00755     );
00756 
00766     static bool startsWithI
00767     (
00768         const   XMLCh* const    toTest
00769         , const XMLCh* const    prefix
00770     );
00771 
00778     static bool endsWith
00779     (
00780         const   XMLCh* const    toTest
00781         , const XMLCh* const    suffix
00782     );
00783 
00784 
00793     static const XMLCh* findAny
00794     (
00795         const   XMLCh* const    toSearch
00796         , const XMLCh* const    searchList
00797     );
00798 
00807     static XMLCh* findAny
00808     (
00809                 XMLCh* const    toSearch
00810         , const XMLCh* const    searchList
00811     );
00812 
00819     static int patternMatch
00820     (
00821           const XMLCh* const    toSearch
00822         , const XMLCh* const    pattern
00823     );
00824 
00829     static unsigned int stringLen(const char* const src);
00830 
00835     static unsigned int stringLen(const XMLCh* const src);
00836 
00844     static bool isValidNOTATION(const XMLCh*         const name
00845                               ,       MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager);
00846 
00854     static bool isValidNCName(const XMLCh* const name);
00855 
00863     static bool isValidName(const XMLCh* const name);
00864 
00870     static bool isValidEncName(const XMLCh* const name);
00871 
00879     static bool isValidQName(const XMLCh* const name);
00880 
00887     static bool isAlpha(XMLCh const theChar);
00888 
00894     static bool isDigit(XMLCh const theChar);
00895 
00901     static bool isAlphaNum(XMLCh const theChar);
00902 
00908     static bool isHex(XMLCh const theChar);
00909 
00917     static bool isAllWhiteSpace(const XMLCh* const toCheck);
00918 
00924     static bool isInList(const XMLCh* const toFind, const XMLCh* const enumList);
00925 
00927 
00930 
00942     static void binToText
00943     (
00944         const   unsigned int    toFormat
00945         ,       char* const     toFill
00946         , const unsigned int    maxChars
00947         , const unsigned int    radix
00948         , MemoryManager* const  manager = XMLPlatformUtils::fgMemoryManager
00949     );
00950 
00962     static void binToText
00963     (
00964         const   unsigned int    toFormat
00965         ,       XMLCh* const    toFill
00966         , const unsigned int    maxChars
00967         , const unsigned int    radix
00968         , MemoryManager* const  manager = XMLPlatformUtils::fgMemoryManager
00969     );
00970 
00982     static void binToText
00983     (
00984         const   unsigned long   toFormat
00985         ,       char* const     toFill
00986         , const unsigned int    maxChars
00987         , const unsigned int    radix
00988         , MemoryManager* const  manager = XMLPlatformUtils::fgMemoryManager
00989     );
00990 
01002     static void binToText
01003     (
01004         const   unsigned long   toFormat
01005         ,       XMLCh* const    toFill
01006         , const unsigned int    maxChars
01007         , const unsigned int    radix
01008         , MemoryManager* const  manager = XMLPlatformUtils::fgMemoryManager
01009     );
01010 
01022     static void binToText
01023     (
01024         const   long            toFormat
01025         ,       char* const     toFill
01026         , const unsigned int    maxChars
01027         , const unsigned int    radix
01028         , MemoryManager* const  manager = XMLPlatformUtils::fgMemoryManager
01029     );
01030 
01042     static void binToText
01043     (
01044         const   long            toFormat
01045         ,       XMLCh* const    toFill
01046         , const unsigned int    maxChars
01047         , const unsigned int    radix
01048         , MemoryManager* const  manager = XMLPlatformUtils::fgMemoryManager
01049     );
01050 
01062     static void binToText
01063     (
01064         const   int             toFormat
01065         ,       char* const     toFill
01066         , const unsigned int    maxChars
01067         , const unsigned int    radix
01068         , MemoryManager* const  manager = XMLPlatformUtils::fgMemoryManager
01069     );
01070 
01082     static void binToText
01083     (
01084         const   int             toFormat
01085         ,       XMLCh* const    toFill
01086         , const unsigned int    maxChars
01087         , const unsigned int    radix
01088         , MemoryManager* const  manager = XMLPlatformUtils::fgMemoryManager
01089     );
01090 
01102     static bool textToBin
01103     (
01104         const   XMLCh* const    toConvert
01105         ,       unsigned int&   toFill
01106         ,       MemoryManager*  const manager = XMLPlatformUtils::fgMemoryManager
01107     );
01108 
01122     static int parseInt
01123     (
01124         const   XMLCh* const    toConvert
01125       , MemoryManager* const    manager = XMLPlatformUtils::fgMemoryManager
01126     );
01127 
01133     static void cut
01134     (
01135                 XMLCh* const    toCutFrom
01136         , const unsigned int    count
01137     );
01138 
01149     static char* transcode
01150     (
01151         const   XMLCh* const    toTranscode
01152     );
01153     static char* transcode
01154     (
01155         const   XMLCh* const         toTranscode
01156         ,       MemoryManager* const manager
01157     );
01158 
01174     static bool transcode
01175     (
01176         const   XMLCh* const    toTranscode
01177         ,       char* const     toFill
01178         , const unsigned int    maxChars
01179         , MemoryManager* const  manager = XMLPlatformUtils::fgMemoryManager
01180     );
01181 
01192     static XMLCh* transcode
01193     (
01194         const   char* const     toTranscode
01195     );
01196     static XMLCh* transcode
01197     (
01198         const   char* const          toTranscode
01199         ,       MemoryManager* const manager
01200     );
01201 
01212     static bool transcode
01213     (
01214         const   char* const     toTranscode
01215         ,       XMLCh* const    toFill
01216         , const unsigned int    maxChars
01217         , MemoryManager* const  manager = XMLPlatformUtils::fgMemoryManager
01218     );
01219 
01225     static void trim(char* const toTrim);
01226 
01232     static void trim(XMLCh* const toTrim);
01233 
01241     static BaseRefVectorOf<XMLCh>* tokenizeString(const XMLCh* const tokenizeSrc
01242                                         , MemoryManager*       const manager = XMLPlatformUtils::fgMemoryManager);
01243 
01245 
01256     static XMLCh* makeUName
01257     (
01258         const   XMLCh* const    pszURI
01259         , const XMLCh* const    pszName      
01260     );
01261 
01278     static unsigned int replaceTokens
01279     (
01280                 XMLCh* const    errText
01281         , const unsigned int    maxChars
01282         , const XMLCh* const    text1
01283         , const XMLCh* const    text2
01284         , const XMLCh* const    text3
01285         , const XMLCh* const    text4
01286         , MemoryManager* const  manager = XMLPlatformUtils::fgMemoryManager
01287     );
01288 
01293     static void upperCase(XMLCh* const toUpperCase);
01294 
01300     static void upperCaseASCII(XMLCh* const toUpperCase);
01301 
01306     static void lowerCase(XMLCh* const toLowerCase);
01307 
01313     static void lowerCaseASCII(XMLCh* const toLowerCase);
01314 
01318     static bool isWSReplaced(const XMLCh* const toCheck);
01319 
01323     static bool isWSCollapsed(const XMLCh* const toCheck);
01324 
01330     static void replaceWS(XMLCh* const toConvert
01331         , MemoryManager* const  manager = XMLPlatformUtils::fgMemoryManager);
01332 
01338     static void collapseWS(XMLCh* const toConvert
01339         , MemoryManager* const  manager = XMLPlatformUtils::fgMemoryManager);
01340 
01346     static void removeWS(XMLCh* const toConvert
01347     , MemoryManager*       const manager = XMLPlatformUtils::fgMemoryManager);
01348 
01349 
01355     static void removeChar(const XMLCh*     const srcString
01356                          , const XMLCh&           toRemove
01357                          ,       XMLBuffer&       dstBuffer);
01358 
01366     static void fixURI(const XMLCh* const str, XMLCh* const target);
01367 
01369 
01377     static void release(char** buf);
01378 
01385     static void release(XMLCh** buf);
01386 
01393     static void release(XMLByte** buf);
01394 
01403     static void release
01404     (
01405         void**  buf
01406         ,       MemoryManager* const manager
01407     );
01408 
01410 
01411 
01412 private :
01413 
01417     XMLString();
01419     ~XMLString();
01421 
01422 
01426     static void initString(XMLLCPTranscoder* const defToUse,
01427                            MemoryManager* const manager);
01428     static void termString();
01430 
01435     static bool validateRegion(const XMLCh* const str1, const int offset1,
01436                         const XMLCh* const str2, const int offset2,
01437                         const unsigned int charCount);
01438 
01439     static MemoryManager* fgMemoryManager;
01440 
01441     friend class XMLPlatformUtils;
01442 };
01443 
01444 
01445 // ---------------------------------------------------------------------------
01446 //  Inline some methods that are either just passthroughs to other string
01447 //  methods, or which are key for performance.
01448 // ---------------------------------------------------------------------------
01449 inline void XMLString::moveChars(       XMLCh* const targetStr
01450                                 , const XMLCh* const srcStr
01451                                 , const unsigned int count)
01452 {
01453     memcpy(targetStr, srcStr, count * sizeof(XMLCh));
01454 }
01455 
01456 inline unsigned int XMLString::stringLen(const XMLCh* const src)
01457 {
01458     if (src == 0 || *src == 0)
01459     {
01460         return 0;
01461    }
01462     else
01463    {
01464         const XMLCh* pszTmp = src + 1;
01465 
01466         while (*pszTmp)
01467             ++pszTmp;
01468 
01469         return (unsigned int)(pszTmp - src);
01470     }
01471 }
01472 
01473 inline XMLCh* XMLString::replicate(const XMLCh* const toRep,
01474                                    MemoryManager* const manager)
01475 {
01476     // If a null string, return a null string!
01477     XMLCh* ret = 0;
01478     if (toRep)
01479     {
01480         const unsigned int len = stringLen(toRep);
01481         ret = (XMLCh*) manager->allocate((len+1) * sizeof(XMLCh)); //new XMLCh[len + 1];
01482         memcpy(ret, toRep, (len + 1) * sizeof(XMLCh));
01483     }
01484     return ret;
01485 }
01486 
01487 inline bool XMLString::startsWith(  const   XMLCh* const    toTest
01488                                     , const XMLCh* const    prefix)
01489 {
01490     return (compareNString(toTest, prefix, stringLen(prefix)) == 0);
01491 }
01492 
01493 inline bool XMLString::startsWithI( const   XMLCh* const    toTest
01494                                     , const XMLCh* const    prefix)
01495 {
01496     return (compareNIString(toTest, prefix, stringLen(prefix)) == 0);
01497 }
01498 
01499 inline bool XMLString::endsWith(const XMLCh* const toTest,
01500                                 const XMLCh* const suffix)
01501 {
01502 
01503     unsigned int suffixLen = XMLString::stringLen(suffix);
01504 
01505     return regionMatches(toTest, XMLString::stringLen(toTest) - suffixLen,
01506                          suffix, 0, suffixLen);
01507 }
01508 
01509 inline bool XMLString::validateRegion(const XMLCh* const str1,
01510                                       const int offset1,
01511                                       const XMLCh* const str2,
01512                                       const int offset2,
01513                                       const unsigned int charCount)
01514 {
01515 
01516     if (offset1 < 0 || offset2 < 0 ||
01517         (offset1 + charCount) > XMLString::stringLen(str1) ||
01518         (offset2 + charCount) > XMLString::stringLen(str2) )
01519         return false;
01520 
01521     return true;
01522 }
01523 
01524 inline bool XMLString::equals(   const XMLCh* const    str1
01525                                , const XMLCh* const    str2)
01526 {
01527     const XMLCh* psz1 = str1;
01528     const XMLCh* psz2 = str2;
01529 
01530     if (psz1 == 0 || psz2 == 0) {
01531         if ((psz1 != 0 && *psz1) || (psz2 != 0 && *psz2))
01532             return false;
01533         else
01534             return true;
01535     }
01536 
01537     while (*psz1 == *psz2)
01538     {
01539         // If either has ended, then they both ended, so equal
01540         if (!*psz1)
01541             return true;
01542 
01543         // Move upwards for the next round
01544         psz1++;
01545         psz2++;
01546     }
01547     return false;
01548 }
01549 
01550 inline bool XMLString::equals(   const char* const    str1
01551                                , const char* const    str2)
01552 {
01553     const char* psz1 = str1;
01554     const char* psz2 = str2;
01555 
01556     if (psz1 == 0 || psz2 == 0) {
01557         if ((psz1 != 0 && *psz1) || (psz2 != 0 && *psz2))
01558             return false;
01559         else
01560             return true;
01561     }
01562 
01563     while (*psz1 == *psz2)
01564     {
01565         // If either has ended, then they both ended, so equal
01566         if (!*psz1)
01567             return true;
01568 
01569         // Move upwards for the next round
01570         psz1++;
01571         psz2++;
01572     }
01573     return false;
01574 }
01575 
01576 inline int XMLString::lastIndexOf(const XMLCh* const toSearch, const XMLCh ch)
01577 {
01578     return XMLString::lastIndexOf(ch, toSearch, stringLen(toSearch));
01579 }
01580 
01581 inline unsigned int XMLString::hash(   const   XMLCh* const    tohash
01582                                 , const unsigned int    hashModulus
01583                                 , MemoryManager* const)
01584 {  
01585     assert(hashModulus);
01586 
01587     if (tohash == 0 || *tohash == 0)
01588         return 0;
01589 
01590     const XMLCh* curCh = tohash;
01591     unsigned int hashVal = (unsigned int)(*curCh++);
01592 
01593     while (*curCh)
01594         hashVal = (hashVal * 38) + (hashVal >> 24) + (unsigned int)(*curCh++);
01595 
01596     // Divide by modulus
01597     return hashVal % hashModulus;
01598 }
01599 
01600 XERCES_CPP_NAMESPACE_END
01601 
01602 #endif

Generated on Thu Feb 19 11:35:44 2009 for Xerces-C++ by  doxygen 1.5.4