Xerces-C++  3.1.2
XMLString.hpp
Go to the documentation of this file.
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 /*
19  * $Id: XMLString.hpp 932887 2010-04-11 13:04:59Z borisk $
20  */
21 
22 #if !defined(XERCESC_INCLUDE_GUARD_XMLSTRING_HPP)
23 #define XERCESC_INCLUDE_GUARD_XMLSTRING_HPP
24 
25 #include <xercesc/util/BaseRefVectorOf.hpp>
26 #include <xercesc/framework/XMLBuffer.hpp>
28 #include <string.h>
29 #include <assert.h>
30 
32 
33 class XMLLCPTranscoder;
46 {
47 public:
48  /* Static methods for native character mode string manipulation */
49 
50 
67  static void catString
68  (
69  char* const target
70  , const char* const src
71  );
72 
85  static void catString
86  (
87  XMLCh* const target
88  , const XMLCh* const src
89  );
91 
105  static int compareIString
106  (
107  const char* const str1
108  , const char* const str2
109  );
110 
121  static int compareIString
122  (
123  const XMLCh* const str1
124  , const XMLCh* const str2
125  );
126 
138  static int compareIStringASCII
139  (
140  const XMLCh* const str1
141  , const XMLCh* const str2
142  );
143 
144 
145 
159  static int compareNString
160  (
161  const char* const str1
162  , const char* const str2
163  , const XMLSize_t count
164  );
165 
179  static int compareNString
180  (
181  const XMLCh* const str1
182  , const XMLCh* const str2
183  , const XMLSize_t count
184  );
185 
186 
200  static int compareNIString
201  (
202  const char* const str1
203  , const char* const str2
204  , const XMLSize_t count
205  );
206 
221  static int compareNIString
222  (
223  const XMLCh* const str1
224  , const XMLCh* const str2
225  , const XMLSize_t count
226  );
227 
240  static int compareString
241  (
242  const char* const str1
243  , const char* const str2
244  );
245 
257  static int compareString
258  (
259  const XMLCh* const str1
260  , const XMLCh* const str2
261  );
262 
271  static bool equals
272  (
273  const XMLCh* str1
274  , const XMLCh* str2
275  );
276 
286  static bool equalsN
287  (
288  const XMLCh* str1
289  , const XMLCh* str2
290  , XMLSize_t n
291  );
292 
293  static bool equals
294  (
295  const char* str1
296  , const char* str2
297  );
298 
308  static bool equalsN
309  (
310  const char* str1
311  , const char* str2
312  , XMLSize_t n
313  );
314 
341  static bool regionMatches
342  (
343  const XMLCh* const str1
344  , const int offset1
345  , const XMLCh* const str2
346  , const int offset2
347  , const XMLSize_t charCount
348  );
349 
377  static bool regionIMatches
378  (
379  const XMLCh* const str1
380  , const int offset1
381  , const XMLCh* const str2
382  , const int offset2
383  , const XMLSize_t charCount
384  );
386 
399  static void copyString
400  (
401  char* const target
402  , const char* const src
403  );
404 
415  static void copyString
416  (
417  XMLCh* const target
418  , const XMLCh* const src
419  );
420 
433  static bool copyNString
434  (
435  XMLCh* const target
436  , const XMLCh* const src
437  , const XMLSize_t maxChars
438  );
440 
449  static XMLSize_t hash
450  (
451  const char* const toHash
452  , const XMLSize_t hashModulus
453  );
454 
461  static XMLSize_t hash
462  (
463  const XMLCh* const toHash
464  , const XMLSize_t hashModulus
465  );
466 
475  static XMLSize_t hashN
476  (
477  const XMLCh* const toHash
478  , const XMLSize_t numChars
479  , const XMLSize_t hashModulus
480  );
481 
483 
494  static int indexOf(const char* const toSearch, const char ch);
495 
504  static int indexOf(const XMLCh* const toSearch, const XMLCh ch);
505 
517  static int indexOf
518  (
519  const char* const toSearch
520  , const char chToFind
521  , const XMLSize_t fromIndex
523  );
524 
536  static int indexOf
537  (
538  const XMLCh* const toSearch
539  , const XMLCh chToFind
540  , const XMLSize_t fromIndex
542  );
543 
552  static int lastIndexOf(const char* const toSearch, const char ch);
553 
562  static int lastIndexOf(const XMLCh* const toSearch, const XMLCh ch);
563 
573  static int lastIndexOf
574  (
575  const XMLCh ch
576  , const XMLCh* const toSearch
577  , const XMLSize_t toSearchLen
578  );
579 
591  static int lastIndexOf
592  (
593  const char* const toSearch
594  , const char chToFind
595  , const XMLSize_t fromIndex
597  );
598 
610  static int lastIndexOf
611  (
612  const XMLCh* const toSearch
613  , const XMLCh ch
614  , const XMLSize_t fromIndex
616  );
618 
626  static void moveChars
627  (
628  XMLCh* const targetStr
629  , const XMLCh* const srcStr
630  , const XMLSize_t count
631  );
632 
634 
646  static void subString
647  (
648  char* const targetStr
649  , const char* const srcStr
650  , const XMLSize_t startIndex
651  , const XMLSize_t endIndex
653  );
654 
664  static void subString
665  (
666  XMLCh* const targetStr
667  , const XMLCh* const srcStr
668  , const XMLSize_t startIndex
669  , const XMLSize_t endIndex
671  );
672 
683  static void subString
684  (
685  XMLCh* const targetStr
686  , const XMLCh* const srcStr
687  , const XMLSize_t startIndex
688  , const XMLSize_t endIndex
689  , const XMLSize_t srcStrLength
691  );
692 
694 
707  static char* replicate(const char* const toRep,
709 
720  static XMLCh* replicate(const XMLCh* const toRep,
722 
724 
733  static bool startsWith
734  (
735  const char* const toTest
736  , const char* const prefix
737  );
738 
745  static bool startsWith
746  (
747  const XMLCh* const toTest
748  , const XMLCh* const prefix
749  );
750 
759  static bool startsWithI
760  (
761  const char* const toTest
762  , const char* const prefix
763  );
764 
774  static bool startsWithI
775  (
776  const XMLCh* const toTest
777  , const XMLCh* const prefix
778  );
779 
786  static bool endsWith
787  (
788  const XMLCh* const toTest
789  , const XMLCh* const suffix
790  );
791 
792 
801  static const XMLCh* findAny
802  (
803  const XMLCh* const toSearch
804  , const XMLCh* const searchList
805  );
806 
815  static XMLCh* findAny
816  (
817  XMLCh* const toSearch
818  , const XMLCh* const searchList
819  );
820 
827  static int patternMatch
828  (
829  const XMLCh* const toSearch
830  , const XMLCh* const pattern
831  );
832 
837  static XMLSize_t stringLen(const char* const src);
838 
843  static XMLSize_t stringLen(const XMLCh* const src);
844 
852  static bool isValidNOTATION(const XMLCh* const name
854 
860  static bool isValidEncName(const XMLCh* const name);
861 
868  static bool isAlpha(XMLCh const theChar);
869 
875  static bool isDigit(XMLCh const theChar);
876 
882  static bool isAlphaNum(XMLCh const theChar);
883 
889  static bool isHex(XMLCh const theChar);
890 
896  static bool isInList(const XMLCh* const toFind, const XMLCh* const enumList);
897 
899 
902 
914  static void sizeToText
915  (
916  const XMLSize_t toFormat
917  , char* const toFill
918  , const XMLSize_t maxChars
919  , const unsigned int radix
921  );
922 
934  static void sizeToText
935  (
936  const XMLSize_t toFormat
937  , XMLCh* const toFill
938  , const XMLSize_t maxChars
939  , const unsigned int radix
941  );
942 
954  static void binToText
955  (
956  const unsigned int toFormat
957  , char* const toFill
958  , const XMLSize_t maxChars
959  , const unsigned int radix
961  );
962 
974  static void binToText
975  (
976  const unsigned int toFormat
977  , XMLCh* const toFill
978  , const XMLSize_t maxChars
979  , const unsigned int radix
981  );
982 
994  static void binToText
995  (
996  const unsigned long toFormat
997  , char* const toFill
998  , const XMLSize_t maxChars
999  , const unsigned int radix
1001  );
1002 
1014  static void binToText
1015  (
1016  const unsigned long toFormat
1017  , XMLCh* const toFill
1018  , const XMLSize_t maxChars
1019  , const unsigned int radix
1021  );
1022 
1034  static void binToText
1035  (
1036  const int toFormat
1037  , char* const toFill
1038  , const XMLSize_t maxChars
1039  , const unsigned int radix
1041  );
1042 
1054  static void binToText
1055  (
1056  const int toFormat
1057  , XMLCh* const toFill
1058  , const XMLSize_t maxChars
1059  , const unsigned int radix
1061  );
1062 
1074  static void binToText
1075  (
1076  const long toFormat
1077  , char* const toFill
1078  , const XMLSize_t maxChars
1079  , const unsigned int radix
1081  );
1082 
1094  static void binToText
1095  (
1096  const long toFormat
1097  , XMLCh* const toFill
1098  , const XMLSize_t maxChars
1099  , const unsigned int radix
1101  );
1102 
1114  static bool textToBin
1115  (
1116  const XMLCh* const toConvert
1117  , unsigned int& toFill
1119  );
1120 
1134  static int parseInt
1135  (
1136  const XMLCh* const toConvert
1138  );
1139 
1145  static void cut
1146  (
1147  XMLCh* const toCutFrom
1148  , const XMLSize_t count
1149  );
1150 
1162  static char* transcode
1163  (
1164  const XMLCh* const toTranscode
1166  );
1167 
1183  static bool transcode
1184  (
1185  const XMLCh* const toTranscode
1186  , char* const toFill
1187  , const XMLSize_t maxChars
1189  );
1190 
1202  static XMLCh* transcode
1203  (
1204  const char* const toTranscode
1206  );
1207 
1218  static bool transcode
1219  (
1220  const char* const toTranscode
1221  , XMLCh* const toFill
1222  , const XMLSize_t maxChars
1224  );
1225 
1231  static void trim(char* const toTrim);
1232 
1238  static void trim(XMLCh* const toTrim);
1239 
1247  static BaseRefVectorOf<XMLCh>* tokenizeString(const XMLCh* const tokenizeSrc
1249 
1251 
1262  static XMLCh* makeUName
1263  (
1264  const XMLCh* const pszURI
1265  , const XMLCh* const pszName
1266  );
1267 
1284  static XMLSize_t replaceTokens
1285  (
1286  XMLCh* const errText
1287  , const XMLSize_t maxChars
1288  , const XMLCh* const text1
1289  , const XMLCh* const text2
1290  , const XMLCh* const text3
1291  , const XMLCh* const text4
1293  );
1294 
1299  static void upperCase(XMLCh* const toUpperCase);
1300 
1306  static void upperCaseASCII(XMLCh* const toUpperCase);
1307 
1312  static void lowerCase(XMLCh* const toLowerCase);
1313 
1319  static void lowerCaseASCII(XMLCh* const toLowerCase);
1320 
1324  static bool isWSReplaced(const XMLCh* const toCheck);
1325 
1329  static bool isWSCollapsed(const XMLCh* const toCheck);
1330 
1336  static void replaceWS(XMLCh* toConvert
1338 
1344  static void collapseWS(XMLCh* toConvert
1346 
1352  static void removeWS(XMLCh* toConvert
1354 
1355 
1361  static void removeChar(const XMLCh* const srcString
1362  , const XMLCh& toRemove
1363  , XMLBuffer& dstBuffer);
1364 
1372  static void fixURI(const XMLCh* const str, XMLCh* const target);
1373 
1375 
1384  static void release
1385  (
1386  char** buf
1388  );
1389 
1397  static void release
1398  (
1399  XMLCh** buf
1401  );
1403 
1404 
1405 private :
1406 
1410  XMLString();
1412  ~XMLString();
1414 
1415 
1419  static void initString(XMLLCPTranscoder* const defToUse,
1420  MemoryManager* const manager);
1421  static void termString();
1423 
1428  static bool validateRegion(const XMLCh* const str1, const int offset1,
1429  const XMLCh* const str2, const int offset2,
1430  const XMLSize_t charCount);
1431 
1432  static MemoryManager* fgMemoryManager;
1433 
1434  friend class XMLPlatformUtils;
1435 };
1436 
1437 
1438 // ---------------------------------------------------------------------------
1439 // Inline some methods that are either just passthroughs to other string
1440 // methods, or which are key for performance.
1441 // ---------------------------------------------------------------------------
1442 inline void XMLString::moveChars( XMLCh* const targetStr
1443  , const XMLCh* const srcStr
1444  , const XMLSize_t count)
1445 {
1446  memcpy(targetStr, srcStr, count * sizeof(XMLCh));
1447 }
1448 
1449 inline XMLSize_t XMLString::stringLen(const XMLCh* const src)
1450 {
1451  if (src == 0)
1452  return 0;
1453 
1454  const XMLCh* pszTmp = src;
1455 
1456  while (*pszTmp++) ;
1457 
1458  return (pszTmp - src - 1);
1459 }
1460 
1461 inline XMLCh* XMLString::replicate(const XMLCh* const toRep,
1462  MemoryManager* const manager)
1463 {
1464  // If a null string, return a null string!
1465  XMLCh* ret = 0;
1466  if (toRep)
1467  {
1468  const XMLSize_t len = stringLen(toRep);
1469  ret = (XMLCh*) manager->allocate((len+1) * sizeof(XMLCh)); //new XMLCh[len + 1];
1470  memcpy(ret, toRep, (len + 1) * sizeof(XMLCh));
1471  }
1472  return ret;
1473 }
1474 
1475 inline bool XMLString::startsWith( const XMLCh* const toTest
1476  , const XMLCh* const prefix)
1477 {
1478  return (compareNString(toTest, prefix, stringLen(prefix)) == 0);
1479 }
1480 
1481 inline bool XMLString::startsWithI( const XMLCh* const toTest
1482  , const XMLCh* const prefix)
1483 {
1484  return (compareNIString(toTest, prefix, stringLen(prefix)) == 0);
1485 }
1486 
1487 inline bool XMLString::endsWith(const XMLCh* const toTest,
1488  const XMLCh* const suffix)
1489 {
1490 
1491  XMLSize_t suffixLen = XMLString::stringLen(suffix);
1492 
1493  return regionMatches(toTest, (int)(XMLString::stringLen(toTest) - suffixLen),
1494  suffix, 0, suffixLen);
1495 }
1496 
1497 inline bool XMLString::validateRegion(const XMLCh* const str1,
1498  const int offset1,
1499  const XMLCh* const str2,
1500  const int offset2,
1501  const XMLSize_t charCount)
1502 {
1503 
1504  if (offset1 < 0 || offset2 < 0 ||
1505  (offset1 + charCount) > XMLString::stringLen(str1) ||
1506  (offset2 + charCount) > XMLString::stringLen(str2) )
1507  return false;
1508 
1509  return true;
1510 }
1511 
1512 inline bool XMLString::equals( const XMLCh* str1
1513  , const XMLCh* str2)
1514 {
1515  if (str1 == str2)
1516  return true;
1517 
1518  if (str1 == 0 || str2 == 0)
1519  return ((!str1 || !*str1) && (!str2 || !*str2));
1520 
1521  while (*str1)
1522  if(*str1++ != *str2++) // they are different (or str2 is shorter and we hit the NULL)
1523  return false;
1524 
1525  // either both ended (and *str2 is 0 too), or str2 is longer
1526  return (*str2==0);
1527 }
1528 
1529 inline bool XMLString::equalsN(const XMLCh* str1,
1530  const XMLCh* str2,
1531  XMLSize_t n)
1532 {
1533  if (str1 == str2 || n == 0)
1534  return true;
1535 
1536  if (str1 == 0 || str2 == 0)
1537  return ((!str1 || !*str1) && (!str2 || !*str2));
1538 
1539  for (; n != 0 && *str1 && *str2; --n, ++str1, ++str2)
1540  if(*str1 != *str2)
1541  break;
1542 
1543  return n == 0 || *str1 == *str2; // either equal or both ended premat.
1544 }
1545 
1546 inline bool XMLString::equals( const char* str1
1547  , const char* str2)
1548 {
1549  if (str1 == str2)
1550  return true;
1551 
1552  if (str1 == 0 || str2 == 0)
1553  return ((!str1 || !*str1) && (!str2 || !*str2));
1554 
1555  while (*str1)
1556  if(*str1++ != *str2++) // they are different (or str2 is shorter and we hit the NULL)
1557  return false;
1558 
1559  // either both ended (and *str2 is 0 too), or str2 is longer
1560  return (*str2==0);
1561 }
1562 
1563 inline bool XMLString::equalsN(const char* str1,
1564  const char* str2,
1565  XMLSize_t n)
1566 {
1567  if (str1 == str2 || n == 0)
1568  return true;
1569 
1570  if (str1 == 0 || str2 == 0)
1571  return ((!str1 || !*str1) && (!str2 || !*str2));
1572 
1573  for (; n != 0 && *str1 && *str2; --n, ++str1, ++str2)
1574  if(*str1 != *str2)
1575  break;
1576 
1577  return n == 0 || *str1 == *str2; // either equal or both ended premat.
1578 }
1579 
1580 inline int XMLString::lastIndexOf(const XMLCh* const toSearch, const XMLCh ch)
1581 {
1582  return XMLString::lastIndexOf(ch, toSearch, stringLen(toSearch));
1583 }
1584 
1585 inline XMLSize_t XMLString::hash(const XMLCh* const tohash
1586  , const XMLSize_t hashModulus)
1587 {
1588  if (tohash == 0 || *tohash == 0)
1589  return 0;
1590 
1591  const XMLCh* curCh = tohash;
1592  XMLSize_t hashVal = (XMLSize_t)(*curCh++);
1593 
1594  while (*curCh)
1595  hashVal = (hashVal * 38) + (hashVal >> 24) + (XMLSize_t)(*curCh++);
1596 
1597  // Divide by modulus
1598  return hashVal % hashModulus;
1599 }
1600 
1601 inline XMLSize_t XMLString::hashN(const XMLCh* const tohash
1602  , const XMLSize_t n
1603  , const XMLSize_t hashModulus)
1604 {
1605  if (tohash == 0 || n == 0)
1606  return 0;
1607 
1608  const XMLCh* curCh = tohash;
1609  XMLSize_t hashVal = (XMLSize_t)(*curCh++);
1610 
1611  for(XMLSize_t i=0;i<n;i++)
1612  hashVal = (hashVal * 38) + (hashVal >> 24) + (XMLSize_t)(*curCh++);
1613 
1614  // Divide by modulus
1615  return hashVal % hashModulus;
1616 }
1617 
1619 
1620 #endif
static char * replicate(const char *const toRep, MemoryManager *const manager=XMLPlatformUtils::fgMemoryManager)
Replicates a string NOTE: The returned buffer is allocated with the MemoryManager.
static MemoryManager * fgMemoryManager
The configurable memory manager.
Definition: PlatformUtils.hpp:121
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:1601
static void moveChars(XMLCh *const targetStr, const XMLCh *const srcStr, const XMLSize_t count)
Moves X number of chars.
Definition: XMLString.hpp:1442
static bool regionMatches(const XMLCh *const str1, const int offset1, const XMLCh *const str2, const int offset2, const XMLSize_t charCount)
Lexicographically compares str1 and str2 regions and returns true if they are equal, otherwise false.
Configurable memory manager.
Definition: MemoryManager.hpp:39
static XMLSize_t stringLen(const char *const src)
Get the length of the string.
SIZE_T XMLSize_t
Definition: Xerces_autoconf_config.borland.hpp:86
#define XERCES_CPP_NAMESPACE_BEGIN
Definition: XercesDefs.hpp:112
Utilities that must be implemented in a platform-specific way.
Definition: PlatformUtils.hpp:68
wchar_t XMLCh
Definition: Xerces_autoconf_config.borland.hpp:92
static int lastIndexOf(const char *const toSearch, const char ch)
Provides the index of the last occurrence of a character within a string.
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...
#define XMLUTIL_EXPORT
Definition: XercesDefs.hpp:162
static bool equalsN(const XMLCh *str1, const XMLCh *str2, XMLSize_t n)
compares str1 and str2
Definition: XMLString.hpp:1529
static bool startsWith(const char *const toTest, const char *const prefix)
Tells if the sub-string appears within a string at the beginning.
#define XERCES_CPP_NAMESPACE_END
Definition: XercesDefs.hpp:113
virtual void * allocate(XMLSize_t size)=0
This method allocates requested memory.
Definition: TransService.hpp:387
Class for representing native character strings and handling common string operations.
Definition: XMLString.hpp:45
static XMLSize_t hash(const char *const toHash, const XMLSize_t hashModulus)
Hashes a string given a modulus.
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:1487
static int compareNIString(const char *const str1, const char *const str2, const XMLSize_t count)
Lexicographically compares, at most, the first count characters in str1 and str2 without regard to ca...
static bool equals(const XMLCh *str1, const XMLCh *str2)
compares str1 and str2
Definition: XMLString.hpp:1512
static int compareNString(const char *const str1, const char *const str2, const XMLSize_t count)
Lexicographically compares, at most, the first count characters in str1 and str2 and returns a value ...