{
return false;
}
-
return(CompareTo(rhs) == 0);
}
int
String::GetHashCode(void) const
{
- int hash = 0;
+ if (__length == 0)
+ {
+ return 0;
+ }
if (__hash == 0)
{
+ const int DEFAULT_HASH_VALUE = 352654597;
+ const int HASH_MULTIPLIER = 1566083941;
+
+ int num = DEFAULT_HASH_VALUE;
+ int num2 = DEFAULT_HASH_VALUE;
wchar_t* pStr = __pValue;
- for (int i = 0; i < __length; ++i)
+ for (int i = __length; i >= 2 ; i -= 4)
{
- hash = (hash << 5) - hash + (int) *pStr++;
+ num = (((num << 5) + num) + (num >> 27)) ^ pStr[0];
+ num2 = (((num2 << 5) + num2) + (num2 >> 27)) ^ pStr[1];
+ pStr += 2;
}
- __hash = hash;
+ num = (((num << 5) + num) + (num >> 27)) ^ pStr[0];
+ __hash = num + (num2 * HASH_MULTIPLIER);
}
- else
- {
- hash = __hash;
- }
-
- return hash;
+ return __hash;
}
result
String::Replace(const String& org, const String& rep)
{
result r = Replace(org, rep, 0);
- SysTryReturn(NID_BASE, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
+ SysTryReturnResult(NID_BASE, r == E_SUCCESS, r, "Propagating.");
return r;
}
result
String::Replace(const String& org, const String& rep, int startIndex)
{
- const int orgLen = org.__length;
- SysTryReturnResult(NID_BASE, orgLen > 0, E_INVALID_ARG, "The length of org(%d) MUST be greater than 0.", orgLen);
+ SysTryReturnResult(NID_BASE, org.__length > 0, E_INVALID_ARG, "The length of org MUST be greater than 0.");
SysTryReturnResult(NID_BASE,
startIndex >= 0 && startIndex < __length, E_OUT_OF_RANGE,
return E_SUCCESS;
}
+ int orgLen = org.__length;
+ int repLen = rep.__length;
if ((orgLen == __length) && (*this == org))
{
- const int newLength = rep.__length;
- if (EnsureCapacity(newLength) != E_SUCCESS)
- {
- SetCapacity(newLength);
- }
+ result r = AboutToModify(__capacity);
+ SysTryReturnResult(NID_BASE, r == E_SUCCESS, E_OUT_OF_MEMORY, "Memory allocation failed.");
+
+ r = EnsureCapacity(repLen);
+ SysTryReturnResult(NID_BASE, r == E_SUCCESS, r, "Propagating.");
- wcsncpy(__pValue, rep.__pValue, rep.__length);
+ wcsncpy(__pValue, rep.__pValue, repLen);
- __length = rep.__length;
+ __length = repLen;
__pValue[__length] = '\0';
__hash = 0;
return E_SUCCESS;
}
- int repLen = rep.__length;
+ int matchedCount = 0;
- wchar_t* pOrg = org.__pValue;
- wchar_t* pRep = rep.__pValue;
+ wchar_t* pBeg = __pValue + startIndex;
+ wchar_t* pMatch = null;
- int count = 0;
+ while ((pMatch = wcsstr(pBeg, org.__pValue)) != null)
{
- wchar_t* pBeg = (__pValue + startIndex);
- wchar_t* pEnd = pBeg + __length;
- while (pBeg < pEnd)
- {
- wchar_t* pTarget = null;
- while ((pTarget = (wchar_t*) wcsstr(pBeg, pOrg)) != null)
- {
- ++count;
- pBeg = pTarget + orgLen;
- }
- pBeg += wcslen(pBeg) + 1;
- }
+ ++matchedCount;
+ pBeg = pMatch + orgLen;
}
- if (count > 0)
+ if (matchedCount > 0)
{
result r = AboutToModify(__capacity);
SysTryReturnResult(NID_BASE, r == E_SUCCESS, E_OUT_OF_MEMORY, "Memory allocation failed.");
- const int newLength = (count * (repLen - orgLen)) + __length;
+ const int newLength = (matchedCount * (repLen - orgLen)) + __length;
r = EnsureCapacity(newLength);
- SysTryReturn(NID_BASE, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
+ SysTryReturnResult(NID_BASE, r == E_SUCCESS, r, "Propagating.");
- wchar_t* pBeg = (__pValue + startIndex);
- wchar_t* pEnd = pBeg + __length;
- while (pBeg < pEnd)
+ pBeg = __pValue + startIndex;
+ while ((pMatch = wcsstr(pBeg, org.__pValue)) != null)
{
- wchar_t* pTarget = null;
- while ((pTarget = (wchar_t*) wcsstr(pBeg, pOrg)) != null)
- {
- int balance = __length - int(pTarget - (__pValue + startIndex) + orgLen);
- wmemmove(pTarget + repLen, pTarget + orgLen, balance);
- wmemcpy(pTarget, pRep, repLen);
- pBeg = pTarget + repLen;
- pTarget[repLen + balance] = 0;
- __length += (repLen - orgLen);
- }
- pBeg += (wcslen(pBeg) + 1);
+ int count = __length - (pMatch - __pValue) - orgLen;
+ wmemmove(pMatch + repLen, pMatch + orgLen, count);
+ wmemcpy(pMatch, rep.__pValue, repLen);
+
+ pBeg = pMatch + repLen;
+ pMatch[repLen + count] = '\0';
+ __length += (repLen - orgLen);
}
__length = newLength;
return true;
}
- wchar_t* pStart = __pValue;
- wchar_t* pEnd = pStart + __length;
- while (pStart < pEnd)
- {
- while (wcsstr(pStart, str.__pValue) != null)
- {
- return true;
- }
- ++pStart;
- }
-
- return false;
+ return (wcsstr(__pValue, str.__pValue) != null);
}
bool