bool isEqualNoCase(const StringData& left, const StringData& right) { if (left.length() != right.length()) return false; for (unsigned int i = 0; i < left.length(); i++) { if (toupper(left[i]) != toupper(right[i])) return false; } return true; }
StringData StringData::xorString(const StringData& str1, const StringData& str2) { int maxLen = str1.length() > str2.length() ? str1.length() : str2.length(); int minLen = str1.length() > str2.length() ? str2.length() : str1.length(); char *buff = new char[maxLen]; int i = 0; for (i = 0; i < minLen; i++) { buff[i] = str1[i] ^ str2[i]; } if (str1.length() > str2.length()) { for (i = minLen; i < maxLen; i++) buff[i] = ~str1[i]; } else { for (i = minLen; i < maxLen; i++) buff[i] = ~str2[i]; } StringData data = StringData(buff, maxLen); delete[] buff; return data; }
int StringData::match(StringData match, StringData* retModifiedData, bool replace, StringData replaceWith) { int retVal = FIRST; int pos = buf.find(match.buf); if (pos == -1) { return NOT_FOUND; } else { if (pos != 0) retVal = FOUND; } unsigned int replacePos = pos + match.length(); if (retModifiedData) { (*retModifiedData) = buf.substr(0, pos); } if (replace) { if (replacePos <= buf.size()) { buf.replace(0, replacePos, replaceWith.buf); } } return retVal; }
void StringData::inflate(int size, const StringData &fillval, bool before) { if ((int) buf.length() >= size) return; StringData dtVal; int iInflateSize = size - buf.length(); for (unsigned int i = 0; i < iInflateSize / fillval.length(); i++) { dtVal += fillval; } if (iInflateSize % fillval.length() > 0) dtVal += fillval.substr(0, iInflateSize % fillval.length()); if (before) operator =(dtVal + *this); else operator =(*this + dtVal); }
void StringData::replace(const StringData& from, const StringData to) { int pos = 0; while ((pos = find(from)) != -1) { replace(pos, from.length(), to); } }
map<int, StringData> StringData::split(const StringData& sp, bool bCut) const { StringData dtVal; map<int, StringData> mapVal; StringData data(*this); int i = 0; while (data.match(sp, &dtVal, true) != NOT_FOUND) { mapVal[i++] = dtVal; if (bCut) { while (data.substr(0, sp.length()) == sp) data = data.substr(sp.length()); } } if (data.length() > 0) mapVal[i] = data; return mapVal; }
bool StringData::isEndWith(const StringData& str, bool bCase) const { if (buf.length() < str.length()) return false; StringData subStr = buf.substr(buf.length() - str.length()); if (bCase) { if (subStr == str) return true; else return false; } else { if (compareNoCase(subStr.getData(), str.getData(), str.length()) == 0) return true; else return false; } }
int StringData::findNoCase(const StringData& match, int start/*=0*/) { const char* pStrCompare = match.getDataBuf(); const char* pStr = getDataBuf(); int iLen = match.length(); int iMaxLen = length(); for (int i = 0; i <= iMaxLen - iLen; i++) { if (!compareNoCase(pStr + i, pStrCompare, iLen)) return i; } return -1; }
int StringData::findlastNoCase(const StringData& match, int stop/*=-1*/) const { if (stop >= (int) buf.size() || stop <= 0) stop = buf.size() - 1; const char* pStr = buf.c_str(); const char* pCompare = match.getDataBuf(); int len = match.length(); for (int i = stop - len + 1; i >= 0; i--) { if (!compareNoCase(pStr + i, pCompare, len)) return i; } return -1; }
void getDataSection(const StringData& data, unsigned int& start, StringData& dtRet, bool& bIsNumric) { bIsNumric = (data[start] <= '9' && data[start] >= '0'); while (start < data.length()) { if (bIsNumric) { if (!(data[start] <= '9' && data[start] >= '0')) break; } else { if (data[start] <= '9' && data[start] >= '0') break; } dtRet += data[start++]; } }
bool StringData::operator>(const StringData& data) const { if (length() > 0 && data.length() == 0) return true; if (length() == 0 && data.length() > 0) return false; if (length() == 0 && data.length() == 0) return false; unsigned int pos1 = 0; unsigned int pos2 = 0; while (true) { if (pos1 == length() && pos2 == data.buf.length()) return false; if (pos1 == length() && pos2 < data.buf.length()) return false; if (pos2 == length() && pos1 < length()) return true; KData dtData1, dtData2; bool isNumeric1, isNumeric2; getDataSection(*this, pos1, dtData1, isNumeric1); getDataSection(data, pos2, dtData2, isNumeric2); if (isNumeric1 && !isNumeric2) return false; else if (!isNumeric1 && isNumeric2) return true; else if (isNumeric1 && isNumeric2) { if (dtData1.length() > dtData2.length()) { return true; } else if (dtData2.length() > dtData1.length()) { return false; } else { if (dtData1.buf == dtData2.buf) continue; else return (dtData1.buf > dtData2.buf); } } else if (!isNumeric1 && !isNumeric2) { unsigned int len = min(dtData1.length(), dtData2.length()); for (unsigned int i = 0; i < len; i++) { char ch1 = dtData1[i]; char ch2 = dtData2[i]; bool bUpcase1, bUpcase2; if (ch1 >= 'A' && ch1 <= 'Z') { bUpcase1 = true; ch1 += ('a' - 'A'); } else { bUpcase1 = false; } if (ch2 >= 'A' && ch2 <= 'Z') { bUpcase2 = true; ch2 += ('a' - 'A'); } else { bUpcase2 = false; } if (ch2 == ch1) { if (bUpcase1 == bUpcase2) continue; else if (bUpcase1) return true; else return false; } else { return ch1 > ch2; } } if (dtData1.length() == dtData2.length()) continue; else if (dtData1.length() > dtData2.length()) return true; else return false; } } }