xmlstring.cpp
来自「IBM的解析xml的工具Xerces的源代码」· C++ 代码 · 共 1,907 行 · 第 1/4 页
CPP
1,907 行
return ret;}char* XMLString::replicate(const char* const toRep){ // If a null string, return a null string if (!toRep) return 0; // // Get the len of the source and allocate a new buffer. Make sure to // account for the nul terminator. // const unsigned int srcLen = strlen(toRep); char* ret = new char[srcLen+1]; // Copy over the text, adjusting for the size of a char memcpy(ret, toRep, (srcLen+1) * sizeof(char)); return ret;}char* XMLString::replicate( const char* const toRep , MemoryManager* const manager){ // If a null string, return a null string if (!toRep) return 0; // // Get the len of the source and allocate a new buffer. Make sure to // account for the nul terminator. // const unsigned int srcLen = strlen(toRep); char* ret = (char*) manager->allocate((srcLen+1) * sizeof(char)); //new char[srcLen+1]; // Copy over the text, adjusting for the size of a char memcpy(ret, toRep, (srcLen+1) * sizeof(char)); return ret;}bool XMLString::startsWith(const char* const toTest, const char* const prefix){ return (strncmp(toTest, prefix, strlen(prefix)) == 0);}bool XMLString::startsWithI(const char* const toTest , const char* const prefix){ return (strnicmp(toTest, prefix, strlen(prefix)) == 0);}unsigned int XMLString::stringLen(const char* const src){ return strlen(src);}char* XMLString::transcode(const XMLCh* const toTranscode){ return gTranscoder->transcode(toTranscode);}char* XMLString::transcode(const XMLCh* const toTranscode, MemoryManager* const manager){ return gTranscoder->transcode(toTranscode, manager);}bool XMLString::transcode( const XMLCh* const toTranscode , char* const toFill , const unsigned int maxChars , MemoryManager* const manager){ if (!gTranscoder->transcode(toTranscode, toFill, maxChars, manager)) return false; return true;}XMLCh* XMLString::transcode(const char* const toTranscode){ return gTranscoder->transcode(toTranscode);}XMLCh* XMLString::transcode(const char* const toTranscode, MemoryManager* const manager){ return gTranscoder->transcode(toTranscode, manager);}bool XMLString::transcode( const char* const toTranscode , XMLCh* const toFill , const unsigned int maxChars , MemoryManager* const manager){ if (!gTranscoder->transcode(toTranscode, toFill, maxChars, manager)) return false; return true;}void XMLString::trim(char* const toTrim){ const unsigned int len = strlen(toTrim); unsigned int skip, scrape; for (skip = 0; skip < len; skip++) { if (! isspace(toTrim[skip])) break; } for (scrape = len; scrape > skip; scrape--) { if (! isspace(toTrim[scrape - 1] )) break; } // Cap off at the scrap point if (scrape != len) toTrim[scrape] = 0; if (skip) { // Copy the chars down unsigned int index = 0; while (toTrim[skip]) toTrim[index++] = toTrim[skip++]; toTrim[index] = 0; }}void XMLString::subString(char* const targetStr, const char* const srcStr , const int startIndex, const int endIndex , MemoryManager* const manager){ if (targetStr == 0) ThrowXMLwithMemMgr(IllegalArgumentException, XMLExcepts::Str_ZeroSizedTargetBuf, manager); const int srcLen = strlen(srcStr); const int copySize = endIndex - startIndex; // Make sure the start index is within the XMLString bounds if ( startIndex < 0 || startIndex > endIndex || endIndex > srcLen) ThrowXMLwithMemMgr(ArrayIndexOutOfBoundsException, XMLExcepts::Str_StartIndexPastEnd, manager); for (int i= startIndex; i < endIndex; i++) { targetStr[i-startIndex] = srcStr[i]; } targetStr[copySize] = 0;}bool XMLString::isValidNOTATION(const XMLCh* const name , MemoryManager* const manager ){ // // NOTATATION: <URI>:<localPart> // where URI is optional // ':' and localPart must be present // int nameLen = XMLString::stringLen(name); int colPos = XMLString::lastIndexOf(name, chColon); if ((colPos == -1) || // no ':' (colPos == nameLen - 1) ) // <URI>':' return false; // Examine URI if (colPos > 0) { XMLCh* temp = (XMLCh*) &(name[colPos]); *temp = 0; try { XMLUri newURI(name, manager); // no relative uri support here *temp = chColon; } catch(const OutOfMemoryException&) { *temp = chColon; return false; } catch (...) { *temp = chColon; return false; } } // Examine localpart return XMLString::isValidNCName(&(name[colPos+1]));}/** * Deprecated: isValidNCName * Check first char, and then the rest of the name char. * But colon should be excluded */bool XMLString::isValidNCName(const XMLCh* const name) { return XMLChar1_0::isValidNCName(name, XMLString::stringLen(name));}/** * Deprecated: isValidName * Check first char, and then the rest of the name char * */bool XMLString::isValidName(const XMLCh* const name) { return XMLChar1_0::isValidName(name, XMLString::stringLen(name));}/** * isValidEncName * * [80] EncName ::= [A-Za-z] ([A-Za-z0-9._] | '-')* * */bool XMLString::isValidEncName(const XMLCh* const name){ if ( XMLString::stringLen(name) == 0 ) return false; const XMLCh* tempName = name; XMLCh firstChar = *tempName++; if (!isAlpha(firstChar)) return false; while(*tempName) { if (( !isAlpha(*tempName)) && ( !isDigit(*tempName)) && ( *tempName != chPeriod) && ( *tempName != chUnderscore) && ( *tempName != chDash) ) return false; tempName++; } return true;}/** * Deprecated: isValidQName * */bool XMLString::isValidQName(const XMLCh* const name){ return XMLChar1_0::isValidQName(name, XMLString::stringLen(name));}bool XMLString::isAlpha(XMLCh const theChar){ if ((( theChar >= chLatin_a ) && ( theChar <= chLatin_z )) || (( theChar >= chLatin_A ) && ( theChar <= chLatin_Z )) ) return true; return false;}bool XMLString::isDigit(XMLCh const theChar){ if (( theChar >= chDigit_0 ) && ( theChar <= chDigit_9 )) return true; return false;}bool XMLString::isAlphaNum(XMLCh const theChar){ return (isAlpha(theChar) || isDigit(theChar));}bool XMLString::isHex(XMLCh const theChar){ return (isDigit(theChar) || (theChar >= chLatin_a && theChar <= chLatin_f) || (theChar >= chLatin_A && theChar <= chLatin_F));}// Deprecatedbool XMLString::isAllWhiteSpace(const XMLCh* const toCheck){ return XMLChar1_0::isAllSpaces(toCheck, XMLString::stringLen(toCheck));}// ---------------------------------------------------------------------------// Wide char versions of most of the string methods// ---------------------------------------------------------------------------void XMLString::binToText( const unsigned long toFormat , XMLCh* const toFill , const unsigned int maxChars , const unsigned int radix , MemoryManager* const manager){ static const XMLCh digitList[16] = { chDigit_0, chDigit_1, chDigit_2, chDigit_3, chDigit_4, chDigit_5 , chDigit_6, chDigit_7, chDigit_8, chDigit_9, chLatin_A, chLatin_B , chLatin_C, chLatin_D, chLatin_e, chLatin_F }; if (!maxChars) ThrowXMLwithMemMgr(IllegalArgumentException, XMLExcepts::Str_ZeroSizedTargetBuf, manager); // Handle special case if (!toFormat) { toFill[0] = chDigit_0; toFill[1] = chNull; return; } // This is used to fill the temp buffer unsigned int tmpIndex = 0; // A copy of the conversion value that we can modify unsigned int tmpVal = toFormat; // // Convert into a temp buffer that we know is large enough. This avoids // having to check for overflow in the inner loops, and we have to flip // the resulting sring anyway. // XMLCh tmpBuf[128]; // // For each radix, do the optimal thing. For bin and hex, we can special // case them and do shift and mask oriented stuff. For oct and decimal // there isn't much to do but bull through it with divides. // if (radix == 2) { while (tmpVal) { if (tmpVal & 0x1UL) tmpBuf[tmpIndex++] = chDigit_1; else tmpBuf[tmpIndex++] = chDigit_0; tmpVal >>= 1; } } else if (radix == 16) { while (tmpVal) { const unsigned int charInd = (tmpVal & 0xFUL); tmpBuf[tmpIndex++] = digitList[charInd]; tmpVal >>= 4; } } else if ((radix == 8) || (radix == 10)) { while (tmpVal) { const unsigned int charInd = (tmpVal % radix); tmpBuf[tmpIndex++] = digitList[charInd]; tmpVal /= radix; } } else { ThrowXMLwithMemMgr(RuntimeException, XMLExcepts::Str_UnknownRadix, manager); } // See if have enough room in the caller's buffer if (tmpIndex > maxChars) { ThrowXMLwithMemMgr(IllegalArgumentException, XMLExcepts::Str_TargetBufTooSmall, manager); } // Reverse the tmp buffer into the caller's buffer unsigned int outIndex = 0; for (; tmpIndex > 0; tmpIndex--) toFill[outIndex++] = tmpBuf[tmpIndex-1]; // And cap off the caller's buffer toFill[outIndex] = chNull;}void XMLString::binToText( const unsigned int toFormat , XMLCh* const toFill , const unsigned int maxChars , const unsigned int radix , MemoryManager* const manager){ // Just call the unsigned long version binToText((unsigned long)toFormat, toFill, maxChars, radix, manager);}void XMLString::binToText( const long toFormat , XMLCh* const toFill , const unsigned int maxChars , const unsigned int radix , MemoryManager* const manager){ // // If its negative, then put a negative sign into the output and flip // the sign of the local temp value. // unsigned int startInd = 0; unsigned long actualVal; if (toFormat < 0) { toFill[0] = chDash; startInd++; actualVal = (unsigned long)(toFormat * -1); } else { actualVal = (unsigned long)(toFormat); } // And now call the unsigned long version binToText(actualVal, &toFill[startInd], maxChars, radix, manager);}void XMLString::binToText( const int toFormat , XMLCh* const toFill , const unsigned int maxChars , const unsigned int radix , MemoryManager* const manager){ // // If its negative, then put a negative sign into the output and flip // the sign of the local temp value. // unsigned int startInd = 0; unsigned long actualVal; if (toFormat < 0) { toFill[0] = chDash; startInd++; actualVal = (unsigned long)(toFormat * -1); } else { actualVal = (unsigned long)(toFormat); } // And now call the unsigned long version binToText(actualVal, &toFill[startInd], maxChars, radix, manager);}void XMLString::catString(XMLCh* const target, const XMLCh* const src){ // Get the starting point for the cat on the target XMLString unsigned int index = stringLen(target); // While the source is not zero, add them to target and bump const XMLCh* pszTmp = src; while (*pszTmp) target[index++] = *pszTmp++; // Cap off the target where we ended target[index] = chNull;}int XMLString::compareIString( const XMLCh* const str1 , const XMLCh* const str2){ // Refer this one to the transcoding service return XMLPlatformUtils::fgTransService->compareIString(str1, str2);}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?