rangetoken.cpp
来自「IBM的解析xml的工具Xerces的源代码」· C++ 代码 · 共 674 行 · 第 1/2 页
CPP
674 行
else if (rangeTok->fRanges[j] < fRanges[i] || (rangeTok->fRanges[j] == fRanges[i] && rangeTok->fRanges[j+1] < fRanges[i+1])) { for (int count = 0; count < 2; count++) { result[k++] = rangeTok->fRanges[j++]; } } else { for (int count = 0; count < 2; count++) { result[k++] = fRanges[i++]; } } } fMemoryManager->deallocate(fRanges);//delete [] fRanges; fElemCount += rangeTok->fElemCount; fRanges = result; fMaxCount = newMaxCount;}void RangeToken::subtractRanges(RangeToken* const tok) { if (fRanges == 0 || tok->fRanges == 0) return; if (tok->getTokenType() == T_NRANGE) { intersectRanges(tok); return; } fCaseIToken = 0; sortRanges(); compactRanges(); tok->sortRanges(); tok->compactRanges(); unsigned int newMax = (fElemCount + tok->fElemCount >= fMaxCount) ? fMaxCount + tok->fMaxCount : fMaxCount; XMLInt32* result = (XMLInt32*) fMemoryManager->allocate ( newMax * sizeof(XMLInt32) );//new XMLInt32[newMax]; unsigned int newElemCount = 0; unsigned int srcCount = 0; unsigned int subCount = 0; while (srcCount < fElemCount && subCount < tok->fElemCount) { XMLInt32 srcBegin = fRanges[srcCount]; XMLInt32 srcEnd = fRanges[srcCount + 1]; XMLInt32 subBegin = tok->fRanges[subCount]; XMLInt32 subEnd = tok->fRanges[subCount + 1]; if (srcEnd < subBegin) { // no overlap result[newElemCount++] = fRanges[srcCount++]; result[newElemCount++] = fRanges[srcCount++]; } else if (srcEnd >= subBegin && srcBegin <= subEnd) { if (subBegin <= srcBegin && srcEnd <= subEnd) { srcCount += 2; } else if (subBegin <= srcBegin) { fRanges[srcCount] = subEnd + 1; subCount += 2; } else if (srcEnd <= subEnd) { result[newElemCount++] = srcBegin; result[newElemCount++] = subBegin - 1; srcCount += 2; } else { result[newElemCount++] = srcBegin; result[newElemCount++] = subBegin - 1; fRanges[srcCount] = subEnd + 1; subCount += 2; } } else if (subEnd < srcBegin) { subCount += 2; } else { fMemoryManager->deallocate(result);//delete [] result; ThrowXMLwithMemMgr(RuntimeException, XMLExcepts::Regex_SubtractRangesError, fMemoryManager); } } //end while while (srcCount < fElemCount) { result[newElemCount++] = fRanges[srcCount++]; result[newElemCount++] = fRanges[srcCount++]; } fMemoryManager->deallocate(fRanges);//delete [] fRanges; fRanges = result; fElemCount = newElemCount; fMaxCount = newMax;}/** * Ignore whether 'tok' is NRANGE or not. */void RangeToken::intersectRanges(RangeToken* const tok) { if (fRanges == 0 || tok->fRanges == 0) return; fCaseIToken = 0; sortRanges(); compactRanges(); tok->sortRanges(); tok->compactRanges(); unsigned int newMax = (fElemCount + tok->fElemCount >= fMaxCount) ? fMaxCount + tok->fMaxCount : fMaxCount; XMLInt32* result = (XMLInt32*) fMemoryManager->allocate ( newMax * sizeof(XMLInt32) );//new XMLInt32[newMax]; unsigned int newElemCount = 0; unsigned int srcCount = 0; unsigned int tokCount = 0; while (srcCount < fElemCount && tokCount < tok->fElemCount) { XMLInt32 srcBegin = fRanges[srcCount]; XMLInt32 srcEnd = fRanges[srcCount + 1]; XMLInt32 tokBegin = tok->fRanges[tokCount]; XMLInt32 tokEnd = tok->fRanges[tokCount + 1]; if (srcEnd < tokBegin) { srcCount += 2; } else if (srcEnd >= tokBegin && srcBegin <= tokEnd) { if (tokBegin <= srcBegin && srcEnd <= tokEnd) { result[newElemCount++] = srcBegin; result[newElemCount++] = srcEnd; srcCount += 2; } else if (tokBegin <= srcBegin) { result[newElemCount++] = srcBegin; result[newElemCount++] = tokEnd; tokCount += 2; if (tokCount < tok->fElemCount) fRanges[srcCount] = tokEnd + 1; else srcCount += 2; } else if (srcEnd <= tokEnd) { result[newElemCount++] = tokBegin; result[newElemCount++] = srcEnd; srcCount += 2; } else { result[newElemCount++] = tokBegin; result[newElemCount++] = tokEnd; tokCount += 2; if (tokCount < tok->fElemCount) fRanges[srcCount] = tokEnd + 1; else srcCount += 2; } } else if (tokEnd < srcBegin) { tokCount += 2; if (tokCount >= tok->fElemCount) srcCount += 2; } else { fMemoryManager->deallocate(result);//delete [] result; ThrowXMLwithMemMgr(RuntimeException, XMLExcepts::Regex_IntersectRangesError, fMemoryManager); } } //end while fMemoryManager->deallocate(fRanges);//delete [] fRanges; fRanges = result; fElemCount = newElemCount; fMaxCount = newMax;}/** * for RANGE: Creates complement. * for NRANGE: Creates the same meaning RANGE. */Token* RangeToken::complementRanges(RangeToken* const tok, TokenFactory* const tokFactory, MemoryManager* const manager) { if (tok->getTokenType() != T_RANGE && tok->getTokenType() != T_NRANGE) ThrowXMLwithMemMgr(IllegalArgumentException, XMLExcepts::Regex_ComplementRangesInvalidArg, manager); tok->sortRanges(); tok->compactRanges(); XMLInt32 lastElem = tok->fRanges[tok->fElemCount - 1]; RangeToken* rangeTok = tokFactory->createRange(); if (tok->fRanges[0] > 0) { rangeTok->addRange(0, tok->fRanges[0] - 1); } for (unsigned int i= 1; i< tok->fElemCount - 2; i += 2) { rangeTok->addRange(tok->fRanges[i] + 1, tok->fRanges[i+1] - 1); } if (lastElem != UTF16_MAX) { rangeTok->addRange(lastElem + 1, UTF16_MAX); } rangeTok->fCompacted = true; return rangeTok;}// ---------------------------------------------------------------------------// RangeToken: Match methods// ---------------------------------------------------------------------------bool RangeToken::match(const XMLInt32 ch) { if (fMap == 0) createMap(); bool ret; if (getTokenType() == T_RANGE) { if (ch < MAPSIZE) return ((fMap[ch/32] & (1<<(ch&0x1f))) != 0); ret = false; for (unsigned int i= fNonMapIndex; i< fElemCount; i +=2) { if (fRanges[i] <= ch && ch <= fRanges[i+1]) return true; } } else { if (ch < MAPSIZE) return ((fMap[ch/32] & (1<<(ch&0x1f))) == 0); ret = true; for (unsigned int i= fNonMapIndex; i< fElemCount; i += 2) { if (fRanges[i] <= ch && ch <= fRanges[i+1]) return false; } } return ret;}// ---------------------------------------------------------------------------// RangeToken: Private helpers methods// ---------------------------------------------------------------------------void RangeToken::expand(const unsigned int length) { unsigned int newMax = fElemCount + length; // Avoid too many reallocations by expanding by a percentage unsigned int minNewMax = (unsigned int)((double)fElemCount * 1.25); if (newMax < minNewMax) newMax = minNewMax; XMLInt32* newList = (XMLInt32*) fMemoryManager->allocate ( newMax * sizeof(XMLInt32) );//new XMLInt32[newMax]; for (unsigned int index = 0; index < fElemCount; index++) newList[index] = fRanges[index]; fMemoryManager->deallocate(fRanges);//delete [] fRanges; fRanges = newList; fMaxCount = newMax;}void RangeToken::createMap() { int asize = MAPSIZE/32; fMap = (int*) fMemoryManager->allocate(asize * sizeof(int));//new int[asize]; fNonMapIndex = fElemCount; for (int i = 0; i < asize; i++) { fMap[i] = 0; } for (unsigned int j= 0; j < fElemCount; j += 2) { XMLInt32 begin = fRanges[j]; XMLInt32 end = fRanges[j+1]; if (begin < MAPSIZE) { for (int k = begin; k <= end && k < MAPSIZE; k++) { fMap[k/32] |= 1<<(k&0x1F); } } else { fNonMapIndex = j; break; } if (end >= MAPSIZE) { fNonMapIndex = j; break; } }}XERCES_CPP_NAMESPACE_END/** * End of file RangeToken.cpp */
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?