From: dahyeong.kim Date: Tue, 1 Oct 2013 11:42:51 +0000 (+0900) Subject: [3.0] Modify String::Replace() which shared buffer even when the whole string was... X-Git-Tag: accepted/tizen/20131018.104622~8^2~5 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=bab4f67b6303736396284b4fda7e1aab38cf3315;p=platform%2Fframework%2Fnative%2Fappfw.git [3.0] Modify String::Replace() which shared buffer even when the whole string was replaced Change-Id: I2e638ea4f122c9bbb22aa82653e81be42b43e521 Signed-off-by: dahyeong.kim --- diff --git a/src/base/FBaseString.cpp b/src/base/FBaseString.cpp index 05eac69..abb38f1 100644 --- a/src/base/FBaseString.cpp +++ b/src/base/FBaseString.cpp @@ -898,7 +898,7 @@ 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; } @@ -906,8 +906,7 @@ String::Replace(const String& org, const String& rep) 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, @@ -919,68 +918,55 @@ String::Replace(const String& org, const String& rep, int startIndex) 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."); - wcsncpy(__pValue, rep.__pValue, rep.__length); + r = EnsureCapacity(repLen); + SysTryReturnResult(NID_BASE, r == E_SUCCESS, r, "Propagating."); - __length = rep.__length; + wcsncpy(__pValue, rep.__pValue, repLen); + + __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;