* @file FBaseString.cpp
* @brief This is the implementation for String class.
*/
-
#include <wchar.h>
#include <math.h>
#include <stdlib.h>
#include <FBaseResult.h>
#include <FBaseSysLog.h>
#include <unique_ptr.h>
-#include "FBase_String.h"
+#include "FBaseUtil_AtomicOperations.h"
#include "FBaseUtil_IcuConverter.h"
-
namespace Tizen { namespace Base
{
, __pValue(null)
, __pStringImpl(null)
{
- int length = (pValue != null) ? wcslen(pValue) : 0;
-
- SysTryReturnVoidResult(NID_BASE, length >= 0, E_OUT_OF_RANGE,
- "String has wrong length. The length has to be more bigger than 0.");
-
if (pValue == null)
{
result r = InitializeToDefault(DEFAULT_CAPACITY);
}
else
{
+ int length = wcslen(pValue);
result r = InitializeToDefault(length + DEFAULT_CAPACITY);
SysTryReturnVoidResult(NID_BASE, r == E_SUCCESS, E_OUT_OF_MEMORY, "Memory allocation failed.");
, __pValue(null)
, __pStringImpl(null)
{
- SysTryReturnVoidResult(NID_BASE, value.__length >= 0, E_OUT_OF_RANGE, "The length has to be greater than 0.");
-
if (*(value.__pRefCount) != UNSHAREABLE)
{
- _String::AtomicInc(value.__pRefCount);
+ Utility::_AtomicOperations::AtomicInc(value.__pRefCount);
__pRefCount = value.__pRefCount;
__pValue = value.__pValue;
__capacity = value.__capacity;
{
int length = wcslen(value.__pValue);
- SysTryReturnVoidResult(NID_BASE, length >= 0, E_OUT_OF_RANGE,
- "String has wrong length. The length has to be more bigger than 0.");
-
result r = InitializeToDefault(length + DEFAULT_CAPACITY);
SysTryReturnVoidResult(NID_BASE, r == E_SUCCESS, E_OUT_OF_MEMORY, "Memory allocation failed.");
}
else
{
- _String::AtomicDec(__pRefCount);
+ Utility::_AtomicOperations::AtomicDec(__pRefCount);
__pRefCount = null;
}
}
{
return false;
}
-
return(CompareTo(rhs) == 0);
}
SysTryReturnResult(NID_BASE, pRefCntTemp != null, E_OUT_OF_MEMORY, "Memory allocation failed");
wcsncpy(__pValue, pValue, __length);
- _String::AtomicDec(__pRefCount);
+ Utility::_AtomicOperations::AtomicDec(__pRefCount);
__pRefCount = pRefCntTemp.release();
}
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;
- }
- else
- {
- hash = __hash;
+ num = (((num << 5) + num) + (num >> 27)) ^ pStr[0];
+ __hash = num + (num2 * HASH_MULTIPLIER);
}
-
- return hash;
+ return __hash;
}
result
result
String::IndexOf(const String& str, int startIndex, int& indexOf) const
{
- SysTryReturnResult(NID_BASE, (startIndex < __length), E_OUT_OF_RANGE,
+ SysTryReturnResult(NID_BASE, startIndex < __length, E_OUT_OF_RANGE,
"The startIndex(%d) MUST be less than the length of this string(%d).", startIndex, __length);
- SysTryReturnResult(NID_BASE, (startIndex >= 0), E_OUT_OF_RANGE,
+ SysTryReturnResult(NID_BASE, startIndex >= 0, E_OUT_OF_RANGE,
"The startIndex(%d) MUST be greater than or equal to 0.", startIndex);
if (str.IsEmpty())
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.");
- 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;
{
std::unique_ptr< int > pRefCntTemp(new (std::nothrow) int(1));
SysTryReturnResult(NID_BASE, pRefCntTemp != null, E_OUT_OF_MEMORY, "Memory allocation failed");
- _String::AtomicDec(__pRefCount);
+ Utility::_AtomicOperations::AtomicDec(__pRefCount);
__pRefCount = pRefCntTemp.release();
}
}
result
String::SetCharAt(wchar_t ch, int indexAt)
{
- SysTryReturn(NID_BASE,indexAt >= 0 && indexAt < __length, E_OUT_OF_RANGE, E_OUT_OF_RANGE,
+ SysTryReturn(NID_BASE, indexAt >= 0 && indexAt < __length, E_OUT_OF_RANGE, E_OUT_OF_RANGE,
"[%s] The indexAt(%d) MUST be greater than or equal to 0, and less then the length of this string(%d).",
GetErrorMessage(E_OUT_OF_RANGE), indexAt, __length);
SysTryReturn(NID_BASE, startIndex >= 0 && startIndex < __length, false, E_OUT_OF_RANGE,
"[%s] The startIndex(%d) MUST be greater than or equal to 0, and less than the length of this string(%d).",
GetErrorMessage(E_OUT_OF_RANGE), startIndex, __length);
- SysTryReturn(NID_BASE, str.__length > 0, false, E_INVALID_ARG,
+ SysTryReturn(NID_BASE, str.__length > 0, false, E_INVALID_ARG,
"[%s] Invalid argument is used. The length of str(%d) MUST be greater than 0.",
GetErrorMessage(E_INVALID_ARG), str.__length);
bool trimRight = lastIndex < __length;
bool trimLeft = startIndex > 0;
- if (!trimRight && !trimLeft) // nothing to trim
+ if (!trimRight && !trimLeft) // nothing to trim
{
return;
}
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
capacity = minCapacity;
}
- std::unique_ptr<wchar_t []> pNewValue(new (std::nothrow) wchar_t[capacity + 1]); // + 1 for null character
+ std::unique_ptr< wchar_t[] > pNewValue(new (std::nothrow) wchar_t[capacity + 1]); // + 1 for null character
SysTryReturn(NID_BASE, pNewValue != null, false, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.",
GetErrorMessage(E_OUT_OF_MEMORY));
std::unique_ptr< int > pRefCntTemp(new (std::nothrow) int(1));
SysTryReturn(NID_BASE, pRefCntTemp != null, false, E_OUT_OF_MEMORY, "[%s] Memory allocation failed",
GetErrorMessage(E_OUT_OF_MEMORY));
- _String::AtomicDec(__pRefCount);
+ Utility::_AtomicOperations::AtomicDec(__pRefCount);
__pRefCount = pRefCntTemp.release();
}
}
wcsncpy(__pValue, pValue, __length);
__pValue[__length] = '\0';
- _String::AtomicDec(__pRefCount);
+ Utility::_AtomicOperations::AtomicDec(__pRefCount);
__pRefCount = pRefCntTemp.release();
}