📄 stafstring.cpp
字号:
STAFString testString(stringToCheck); STAFString wcString(wildcardString); if (caseSensitive == kSTAFStringCaseInsensitive) { testString.upperCase(); wcString.upperCase(); } unsigned int currWCIndex = 0; unsigned int nextWCIndex = 0; unsigned int wcCharIndex = 0; STAFString wcData; STAFString charData; unsigned int currTestIndex = 0; unsigned int nextTestIndex = 0; unsigned int deltaChars = 0; unsigned int numQuestions = 0; bool hasWildcard = false; if ((testString.length() == 0) && (wcString.length() == 0)) { *matches = 1; } else if ((testString.length() == 0) && (wcString.findFirstNotOf(sStar) != STAFString::kNPos)) { *matches = 0; } else if (wcString.length() == 0) { *matches = 0; } else if (wcString == testString) { *matches = 1; return retCode; } else if ((wcString.findFirstOf(sStar) == STAFString::kNPos) && (testString.length() != wcString.length())) { *matches = 0; } else if ((wcString.findFirstOf(sQuestion) != STAFString::kNPos) && (testString.length() < wcString.count(sQuestion))) { *matches = 0; } else *matches = 1; for (; (*matches != 0) && (currWCIndex < wcString.length()) && (nextTestIndex < testString.length()); currWCIndex = nextWCIndex, currTestIndex = nextTestIndex + charData.length()) { wcCharIndex = wcString.findFirstNotOf(sWildCards, currWCIndex); wcData = wcString.subString(currWCIndex, wcCharIndex - currWCIndex); nextWCIndex = wcString.findFirstOf(sWildCards, wcCharIndex); charData = wcString.subString(wcCharIndex, nextWCIndex - wcCharIndex); hasWildcard = (wcData.count(sStar) > 0); numQuestions = wcData.count(sQuestion); if (charData.length() != 0) nextTestIndex = testString.find(charData, currTestIndex + numQuestions); else nextTestIndex = testString.length(); deltaChars = nextTestIndex - currTestIndex; if (!hasWildcard && (deltaChars > numQuestions)) *matches = 0; else if (nextTestIndex == STAFString::kNPos) *matches = 0; else if (nextWCIndex == STAFString::kNPos) { // Verify remaining characters in wildcard string match STAFString wcRemainChars = wcString.subString(wcCharIndex, wcString.length()); if (wcRemainChars.length() != 0) { if (testString.find(wcRemainChars, testString.length() - wcRemainChars.length()) == STAFString::kNPos) { *matches = 0; } } else if (currTestIndex == testString.length() && wcData == "?") { *matches = 0; } } } } catch (...) { retCode = kSTAFUnknownError; } return retCode;}STAFRC_t STAFStringFind(STAFStringConst_t aString, STAFStringConst_t aSubStr, unsigned int index, unsigned int corb, unsigned int *pos, unsigned int *osRC){ if ((aString == 0) || (aSubStr == 0)) return kSTAFInvalidObject; if (pos == 0) return kSTAFInvalidParm; char *ptr = aString->pBuffer; char *key = aSubStr->pBuffer; char *lim = aString->pBuffer + aString->fByteLen; // default to not found *pos = 0xffffffff; // This is legal. If the user tries to find a string starting at an // index that is larger than the size of the string, then the search // string is simply not found. if (index >= (corb ? aString->fByteLen : aString->fCharLen)) return kSTAFOk; ptr = FWDN(ptr, index, corb); unsigned int loc = index; // this is called "the naive search string algorithm", // I could use something like the Boyer-Moore, or KMP, // but it may not be worth the extra work for the size // of string we will be dealing with ;) // start searching for the substring, but only look for // lead byte, when found call memcmp to actually do the // matching while (ptr < lim) { // loop until ptr matches key while ((ptr < lim) && (*ptr != *key)) { ptr = NEXT(ptr); loc++; } // if above limits then not found if (ptr >= lim) break; // avoid calling memcmp if out-of-bounds memory a- // reas are going to be touched if (ptr + aSubStr->fByteLen > lim) break; if (memcmp(ptr, key, aSubStr->fByteLen) == 0) { // if we dealt with chars, return char location, // else return byte location *pos = (corb ? ptr - aString->pBuffer : loc); break; } ptr = NEXT(ptr); loc++; } return kSTAFOk;}STAFRC_t STAFStringFindFirstOf(STAFStringConst_t aString, STAFStringConst_t aSet, unsigned int index, unsigned int corb, unsigned int *pos, unsigned int *osRC){ if ((aString == 0) || (aSet == 0)) return kSTAFInvalidObject; if (pos == 0) return kSTAFInvalidParm; unsigned int loc = 0; STAFRC_t rc = kSTAFOk; char *key = aString->pBuffer; char *lim = aString->pBuffer + aString->fByteLen; *pos = 0xffffffff; // default to not found // if index is beyond string's length, just say not found if (index >= (corb ? aString->fByteLen : aString->fCharLen)) return kSTAFOk; key = FWDN(key, index, corb); loc = index; struct STAFStringImplementation wrap; while (key < lim) { unsigned int pos2; wrap.pBuffer = key; wrap.fBuffLen = BYTES(key); wrap.fByteLen = BYTES(key); wrap.fCharLen = 1; rc = STAFStringFind(aSet, &wrap, 0, 0, &pos2, osRC); if (rc) break; if (pos2 != 0xffffffff) { *pos = (corb ? key - aString->pBuffer : loc); break; } key = NEXT(key); loc++; } return rc;}STAFRC_t STAFStringFindLastOf(STAFStringConst_t aString, STAFStringConst_t aSet, unsigned int index, unsigned int corb, unsigned int *pos, unsigned int *osRC){ if ((aString == 0) || (aSet == 0)) return kSTAFInvalidObject; if (pos == 0) return kSTAFInvalidParm; unsigned int loc = 0; STAFRC_t rc = kSTAFOk; char *key = aString->pBuffer + aString->fByteLen; char *lim = aString->pBuffer; *pos = 0xffffffff; // default to not found // if index is beyond string's length, just say not found if (index >= (corb ? aString->fByteLen : aString->fCharLen)) return kSTAFOk; lim = FWDN(lim, index, corb); key = REWN(key, 1, corb); loc = (corb ? aString->fByteLen - 1 : aString->fCharLen - 1); struct STAFStringImplementation wrap; while (key >= lim) { unsigned int pos2; wrap.pBuffer = key; wrap.fBuffLen = BYTES(key); wrap.fByteLen = BYTES(key); wrap.fCharLen = 1; rc = STAFStringFind(aSet, &wrap, 0, 0, &pos2, osRC); if (rc) break; if (pos2 != 0xffffffff) { *pos = (corb ? key - aString->pBuffer : loc); break; } if (key == lim) break; key = PREV(key); loc--; } return rc;}STAFRC_t STAFStringFindFirstNotOf(STAFStringConst_t aString, STAFStringConst_t aSet, unsigned int index, unsigned int corb, unsigned int *pos, unsigned int *osRC){ if ((aString == 0) || (aSet == 0)) return kSTAFInvalidObject; if (pos == 0) return kSTAFInvalidParm; unsigned int loc = 0; STAFRC_t rc = kSTAFOk; char *key = aString->pBuffer; char *lim = aString->pBuffer + aString->fByteLen; *pos = 0xffffffff; // default to not found // if index is beyond string's length, just say not found if (index >= (corb ? aString->fByteLen : aString->fCharLen)) return kSTAFOk; key = FWDN(key, index, corb); loc = index; struct STAFStringImplementation wrap; while (key < lim) { unsigned int pos2; wrap.pBuffer = key; wrap.fBuffLen = BYTES(key); wrap.fByteLen = BYTES(key); wrap.fCharLen = 1; rc = STAFStringFind(aSet, &wrap, 0, 0, &pos2, osRC); if (rc) break; if (pos2 == 0xffffffff) { *pos = (corb ? key - aString->pBuffer : loc); break; } key = NEXT(key); loc++; } return rc;}STAFRC_t STAFStringFindLastNotOf(STAFStringConst_t aString, STAFStringConst_t aSet, unsigned int index, unsigned int corb, unsigned int *pos, unsigned int *osRC){ if ((aString == 0) || (aSet == 0)) return kSTAFInvalidObject; if (pos == 0) return kSTAFInvalidParm; unsigned int loc = 0; STAFRC_t rc = kSTAFOk; char *key = aString->pBuffer + aString->fByteLen; char *lim = aString->pBuffer; *pos = 0xffffffff; // default to not found // if index is beyond string's length, just say not found if (index >= (corb ? aString->fByteLen : aString->fCharLen)) return kSTAFOk; lim = FWDN(lim, index, corb); key = REWN(key, 1, corb); loc = (corb ? aString->fByteLen - 1 : aString->fCharLen - 1); struct STAFStringImplementation wrap; while (key >= lim) { unsigned int pos2; wrap.pBuffer = key; wrap.fBuffLen = BYTES(key); wrap.fByteLen = BYTES(key); wrap.fCharLen = 1; rc = STAFStringFind(aSet, &wrap, 0, 0, &pos2, osRC); if (rc) break; if (pos2 == 0xffffffff) { *pos = (corb ? key - aString->pBuffer : loc); break; } key = PREV(key); loc--; } return rc;}STAFRC_t STAFStringDestruct(STAFString_t *pString, unsigned int *osRC){ if (pString == 0) return kSTAFInvalidObject; if ((*pString)->pBuffer != EMPTY_STRING) delete[] (*pString)->pBuffer; delete *pString; *pString = 0; return kSTAFOk;}STAFRC_t STAFStringFreeBuffer(const char *buffer, unsigned int *osRC){ if (buffer == 0) return kSTAFInvalidObject; // Note: WIN32 doesn't overload delete[] for const char* so we need // to cast it delete[] (char *)buffer; return kSTAFOk;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -