Merge "[Native][25/11/2013][Add]Adding BigInteger class" into tizen
[platform/framework/native/appfw.git] / src / base / FBaseString.cpp
index 64b28e8..abb38f1 100644 (file)
@@ -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.");
+
+               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;
@@ -1430,18 +1416,7 @@ String::Contains(const String& str) const
                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