2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 // Licensed under the Apache License, Version 2.0 (the License);
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
8 // http://www.apache.org/licenses/LICENSE-2.0
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
18 * @file FBaseString.cpp
19 * @brief This is the implementation for String class.
27 #include <FBaseString.h>
28 #include <FBaseInt8.h>
29 #include <FBaseShort.h>
30 #include <FBaseInteger.h>
31 #include <FBaseLong.h>
32 #include <FBaseLongLong.h>
33 #include <FBaseFloat.h>
34 #include <FBaseDouble.h>
35 #include <FBaseCharacter.h>
36 #include <FBaseResult.h>
37 #include <FBaseSysLog.h>
38 #include <unique_ptr.h>
39 #include "FBaseUtil_AtomicOperations.h"
40 #include "FBaseUtil_IcuConverter.h"
42 namespace Tizen { namespace Base
45 const float String::GROWTH_FACTOR = 1.5;
46 const int String::UNSHAREABLE = Integer::VALUE_MAX;
56 result r = InitializeToDefault(DEFAULT_CAPACITY);
57 SysTryReturnVoidResult(NID_BASE, r == E_SUCCESS, E_OUT_OF_MEMORY, "Memory allocation failed.");
60 String::String(int capacity)
70 capacity = DEFAULT_CAPACITY;
73 result r = InitializeToDefault(capacity);
74 SysTryReturnVoidResult(NID_BASE, r == E_SUCCESS, E_OUT_OF_MEMORY, "Memory allocation failed.");
77 String::String(wchar_t ch)
85 result r = InitializeToDefault(DEFAULT_CAPACITY);
86 SysTryReturnVoidResult(NID_BASE, r == E_SUCCESS, E_OUT_OF_MEMORY, "Memory allocation failed.");
93 String::String(const wchar_t* pValue)
103 result r = InitializeToDefault(DEFAULT_CAPACITY);
104 SysTryReturnVoidResult(NID_BASE, r == E_SUCCESS, E_OUT_OF_MEMORY, "Memory allocation failed.");
108 int length = wcslen(pValue);
109 result r = InitializeToDefault(length + DEFAULT_CAPACITY);
110 SysTryReturnVoidResult(NID_BASE, r == E_SUCCESS, E_OUT_OF_MEMORY, "Memory allocation failed.");
112 wcsncpy(__pValue, pValue, length);
113 __pValue[length] = '\0';
118 String::String(const char* pValue)
124 , __pStringImpl(null)
126 if (pValue == null || strlen(pValue) == 0)
128 result r = InitializeToDefault(DEFAULT_CAPACITY);
129 SysTryReturnVoidResult(NID_BASE, r == E_SUCCESS, E_OUT_OF_MEMORY, "Memory allocation failed.");
133 std::unique_ptr< int > pRefCntTemp(new (std::nothrow) int(1));
134 SysTryReturnVoidResult(NID_BASE, pRefCntTemp != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
136 std::unique_ptr< wchar_t[] > pStr(Tizen::Base::Utility::Utf8ToWcharN(pValue));
137 SysTryReturnVoidResult(NID_BASE, pStr != null, GetLastResult(), "[%ls] Propagating.", GetErrorMessage(GetLastResult()));
139 __pRefCount = pRefCntTemp.release();
140 __pValue = pStr.release();
141 __length = wcslen(__pValue);
142 __capacity = __length + DEFAULT_CAPACITY;
143 __pValue[__length] = '\0';
147 String::String(const String& value)
153 , __pStringImpl(null)
155 if (*(value.__pRefCount) != UNSHAREABLE)
157 Utility::_AtomicOperations::AtomicInc(value.__pRefCount);
158 __pRefCount = value.__pRefCount;
159 __pValue = value.__pValue;
160 __capacity = value.__capacity;
161 __length = value.__length;
162 __hash = value.__hash;
166 int length = wcslen(value.__pValue);
168 result r = InitializeToDefault(length + DEFAULT_CAPACITY);
169 SysTryReturnVoidResult(NID_BASE, r == E_SUCCESS, E_OUT_OF_MEMORY, "Memory allocation failed.");
171 wcsncpy(__pValue, value.__pValue, length);
172 __pValue[length] = '\0';
177 String::~String(void)
179 if (*__pRefCount == 1 || *__pRefCount == UNSHAREABLE)
186 Utility::_AtomicOperations::AtomicDec(__pRefCount);
192 String::operator [](int index) const
194 static wchar_t ch = -1;
195 SysTryReturn(NID_BASE, (index < __length && index >= 0), ch, E_OUT_OF_RANGE,
196 "[%s] The index(%d) MUST be greater than or equal to 0, and less than the length of this string(%d).",
197 GetErrorMessage(E_OUT_OF_RANGE), index, __length);
198 return __pValue[index];
202 String::operator [](int index)
204 static wchar_t ch = -1;
205 SysTryReturn(NID_BASE, (index < __length && index >= 0), ch, E_OUT_OF_RANGE,
206 "[%s] The index(%d) MUST be greater than or equal to 0, and less than the length of this string(%d).",
207 GetErrorMessage(E_OUT_OF_RANGE), index, __length);
209 result r = AboutToModify(__capacity, true);
210 SysTryReturn(NID_BASE, r == E_SUCCESS, ch, E_OUT_OF_MEMORY, "Memory allocation failed.");
213 return __pValue[index];
217 String::operator =(const wchar_t* pRhs)
231 String::operator =(const String& rhs)
245 String::operator +=(const wchar_t* pRhs)
258 String::operator +=(const String& rhs)
265 Append(rhs.__pValue);
271 operator +(const String& lhs, const String& rhs)
275 str.Append(rhs.__pValue);
281 String::operator ==(const String& rhs) const
283 if (__length != rhs.__length)
288 return(CompareTo(rhs) == 0);
292 String::operator !=(const String& rhs) const
294 return(!(*this).operator ==(rhs));
298 String::IsEmpty(void) const
300 return(__length == 0);
304 String::Append(wchar_t ch)
306 result r = Append(Character::ToString(ch));
307 SysTryReturn(NID_BASE, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
313 String::Append(char ch)
315 result r = Append((wchar_t) ch);
316 SysTryReturn(NID_BASE, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
322 String::Append(int i)
324 result r = Append(Integer::ToString(i));
325 SysTryReturn(NID_BASE, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
331 String::Append(short s)
333 result r = Append(Short::ToString(s));
334 SysTryReturn(NID_BASE, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
340 String::Append(long l)
342 result r = Append(Long::ToString(l));
343 SysTryReturn(NID_BASE, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
349 String::Append(long long ll)
351 result r = Append(LongLong::ToString(ll));
352 SysTryReturn(NID_BASE, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
358 String::Append(float f)
360 result r = Append(Float::ToString(f));
361 SysTryReturn(NID_BASE, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
367 String::Append(double d)
369 result r = Append(Double::ToString(d));
370 SysTryReturn(NID_BASE, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
376 String::Append(const wchar_t* p)
378 SysTryReturnResult(NID_BASE, p != null, E_INVALID_ARG, "p is null.");
380 int length = (wcslen(p) + __length);
382 if (*__pRefCount > 1 && *__pRefCount != UNSHAREABLE)
384 wchar_t* pValue = __pValue;
385 SysTryReturnResult(NID_BASE, AllocateCapacity(length) != false, E_OUT_OF_MEMORY, "Memory allocation failed.");
386 std::unique_ptr< int > pRefCntTemp(new (std::nothrow) int(1));
387 SysTryReturnResult(NID_BASE, pRefCntTemp != null, E_OUT_OF_MEMORY, "Memory allocation failed");
389 wcsncpy(__pValue, pValue, __length);
390 Utility::_AtomicOperations::AtomicDec(__pRefCount);
391 __pRefCount = pRefCntTemp.release();
394 result r = EnsureCapacity(length);
395 SysTryReturn(NID_BASE, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
397 wcsncpy((__pValue + __length), p, wcslen(p));
399 __pValue[length] = '\0';
407 String::Append(const String& str)
414 result r = Append(str.__pValue);
415 SysTryReturn(NID_BASE, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
428 String::Compare(const String& str0, const String& str1)
430 if (str0.__pValue == str1.__pValue)
435 return(wcscmp(str0.__pValue, str1.__pValue));
439 String::CompareTo(const String& str) const
441 if (__pValue == str.__pValue)
446 return(wcscmp(__pValue, str.__pValue));
450 String::EnsureCapacity(int minCapacity)
452 SysTryReturnResult(NID_BASE, minCapacity >= 0, E_INVALID_ARG, "The minCapacity(%d) MUST be greater than or equal to 0.",
455 if (minCapacity > __capacity)
457 SysTryReturnResult(NID_BASE, ExpandCapacity(minCapacity), E_OUT_OF_MEMORY, "Memory allocation failed.");
463 String::Equals(const Object& obj) const
465 const String* pOther = dynamic_cast< const String* >(&obj);
472 return(*this == *pOther);
476 String::Equals(const String& str, bool caseSensitive) const
480 return(*this == str);
484 if (__length != str.__length)
489 if (__pValue == str.__pValue)
494 if (wcscasecmp(__pValue, str.__pValue) == 0)
504 String::Format(int length, const wchar_t* pFormat, ...)
507 result r = E_SUCCESS;
509 SysTryReturnResult(NID_BASE, pFormat != null, E_INVALID_ARG, "The pFormat is null.");
510 SysTryReturnResult(NID_BASE, length >= 0, E_INVALID_ARG, "The length(%d) MUST be greater than or equal to 0.",
513 String tempStr(pFormat);
514 if (tempStr.IsEmpty() || length == 0)
520 // Check "%n" and "%hn"
521 r = tempStr.IndexOf(L"%n", 0, index);
522 SysTryReturnResult(NID_BASE, r == E_OBJ_NOT_FOUND, E_INVALID_ARG, "(%ls) is not supported format.", pFormat);
524 r = tempStr.IndexOf(L"%hn", 0, index);
525 SysTryReturnResult(NID_BASE, r == E_OBJ_NOT_FOUND, E_INVALID_ARG, "(%ls) is not supported format.", pFormat);
529 std::unique_ptr< wchar_t[] > pStr(new (std::nothrow) wchar_t[length + 1]);
530 SysTryReturnResult(NID_BASE, pStr != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
534 va_start(args, pFormat);
536 vswprintf(pStr.get(), length, tempStr.__pValue, args);
542 int len = wcslen(this->__pValue);
545 this->__pValue[len] = '\0';
546 this->__length = len;
550 this->__pValue[length - 1] = '\0';
551 this->__length = length - 1;
559 String::GetHashCode(void) const
565 wchar_t* pStr = __pValue;
566 for (int i = 0; i < __length; ++i)
568 hash = (hash << 5) - hash + (int) *pStr++;
581 String::GetCharAt(int indexAt, wchar_t& ret) const
583 SysTryReturnResult(NID_BASE, (indexAt < __length), E_OUT_OF_RANGE,
584 "The indexAt(%d) MUST be less than the length of this string(%d).", indexAt, __length);
585 SysTryReturnResult(NID_BASE, (indexAt >= 0), E_OUT_OF_RANGE,
586 "The indexAt(%d) MUST be greater than or equal to 0.", indexAt);
588 ret = __pValue[indexAt];
594 String::IndexOf(wchar_t ch, int startIndex, int& indexOf) const
596 SysTryReturnResult(NID_BASE, startIndex < __length, E_OUT_OF_RANGE,
597 "The startIndex(%d) MUST be less than the length of this string(%d).", startIndex, __length);
598 SysTryReturnResult(NID_BASE, startIndex >= 0, E_OUT_OF_RANGE,
599 "The startIndex(%d) MUST be greater than or equal to 0.", startIndex);
601 wchar_t* pBeg = __pValue + startIndex;
602 wchar_t* pFound = (wchar_t*) wcschr((const wchar_t*) pBeg, (wchar_t) ch);
608 return E_OBJ_NOT_FOUND;
611 indexOf = int(pFound - __pValue);
617 String::IndexOf(const String& str, int startIndex, int& indexOf) const
619 SysTryReturnResult(NID_BASE, startIndex < __length, E_OUT_OF_RANGE,
620 "The startIndex(%d) MUST be less than the length of this string(%d).", startIndex, __length);
621 SysTryReturnResult(NID_BASE, startIndex >= 0, E_OUT_OF_RANGE,
622 "The startIndex(%d) MUST be greater than or equal to 0.", startIndex);
626 indexOf = startIndex;
630 if (__length < str.__length)
633 return E_OBJ_NOT_FOUND;
638 p = (wchar_t*) wcsstr((__pValue + startIndex), str.__pValue);
643 return E_OBJ_NOT_FOUND;
646 indexOf = int(p - __pValue);
652 String::Insert(wchar_t ch, int indexAt)
654 SysTryReturnResult(NID_BASE,
655 indexAt >= 0 && indexAt <= __length, E_OUT_OF_RANGE,
656 "The indexAt(%d) MUST be greater than or equal to 0, and less than or equal to the length of this string(%d).",
659 result r = AboutToModify(__capacity);
660 SysTryReturnResult(NID_BASE, r == E_SUCCESS, E_OUT_OF_MEMORY, "Memory allocation failed.");
662 int length = (__length + 1);
664 r = EnsureCapacity(length);
665 SysTryReturn(NID_BASE, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
667 wmemmove((__pValue + indexAt + 1), (__pValue + indexAt), ((__length + 1) - indexAt));
669 __pValue[indexAt] = ch;
677 String::Insert(char ch, int indexAt)
679 wchar_t wideChar = (wchar_t) ch;
681 result r = Insert(wideChar, indexAt);
682 SysTryReturn(NID_BASE, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
688 String::Insert(short s, int indexAt)
690 result r = Insert(Short::ToString(s), indexAt);
691 SysTryReturn(NID_BASE, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
697 String::Insert(int i, int indexAt)
699 result r = Insert(Integer::ToString(i), indexAt);
700 SysTryReturn(NID_BASE, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
706 String::Insert(long l, int indexAt)
708 result r = Insert(Long::ToString(l), indexAt);
709 SysTryReturn(NID_BASE, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
715 String::Insert(long long ll, int indexAt)
717 result r = Insert(LongLong::ToString(ll), indexAt);
718 SysTryReturn(NID_BASE, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
724 String::Insert(float f, int indexAt)
726 result r = Insert(Float::ToString(f), indexAt);
727 SysTryReturn(NID_BASE, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
733 String::Insert(double d, int indexAt)
735 result r = Insert(Double::ToString(d), indexAt);
736 SysTryReturn(NID_BASE, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
741 String::Insert(const wchar_t* p, int indexAt)
743 SysTryReturnResult(NID_BASE, p != null, E_INVALID_ARG, "The p is null.");
744 SysTryReturnResult(NID_BASE,
745 indexAt >= 0 && indexAt <= __length, E_OUT_OF_RANGE,
746 "The indexAt(%d) MUST be greater than or equal to 0, and less than or equal to the length of this string(%d).",
749 int length = wcslen(p);
755 result r = AboutToModify(__capacity);
756 SysTryReturnResult(NID_BASE, r == E_SUCCESS, E_OUT_OF_MEMORY, "Memory allocation failed.");
758 r = EnsureCapacity(__length + length);
759 SysTryReturn(NID_BASE, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
761 wmemmove((__pValue + indexAt + length), (__pValue + indexAt), ((__length + 1) - indexAt));
762 wmemcpy((__pValue + indexAt), p, length);
770 String::Insert(const String& str, int indexAt)
772 result r = Insert(str.__pValue, indexAt);
773 SysTryReturn(NID_BASE, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
779 String::LastIndexOf(wchar_t ch, int startIndex, int& indexOf) const
781 SysTryReturnResult(NID_BASE,
782 startIndex >= 0 && startIndex < __length, E_OUT_OF_RANGE,
783 "The startIndex(%d) MUST be greater than or equal to 0, and less than the length of this string(%d).",
784 startIndex, __length);
786 wchar_t* pBeg = __pValue + startIndex;
787 wchar_t* pEnd = __pValue;
792 indexOf = int(pBeg - __pValue);
801 return E_OBJ_NOT_FOUND;
805 String::LastIndexOf(const String& str, int startIndex, int& indexOf) const
807 SysTryReturnResult(NID_BASE,
808 startIndex >= 0 && startIndex < __length, E_OUT_OF_RANGE,
809 "The startIndex(%d) MUST be greater than or equal to 0, and less than the length of this string(%d).",
810 startIndex, __length);
814 indexOf = startIndex;
818 if (__length < str.__length)
822 return E_OBJ_NOT_FOUND;
825 const wchar_t* pStr = str.__pValue;
827 int length = str.__length;
828 if (length > startIndex)
832 return E_OBJ_NOT_FOUND;
836 wchar_t* pBeg = __pValue + startIndex;
837 wchar_t* pEnd = __pValue;
841 if (wcsncmp(pBeg, pStr, length) == 0)
843 indexOf = (pBeg - __pValue);
850 return E_OBJ_NOT_FOUND;
854 String::Remove(int startIndex, int count)
856 SysTryReturnResult(NID_BASE,
857 startIndex >= 0 && startIndex < __length, E_OUT_OF_RANGE,
858 "The startIndex(%d) MUST be greater than or equal to 0, and less than the length of this string(%d).",
859 startIndex, __length);
860 int moveIndex = startIndex + count;
861 SysTryReturnResult(NID_BASE, moveIndex <= __length, E_OUT_OF_RANGE,
862 "The startIndex(%d) + count(%d) MUST be less than or equal to the length of this string(%d).",
863 startIndex, count, __length);
865 result r = AboutToModify(__capacity);
866 SysTryReturnResult(NID_BASE, r == E_SUCCESS, E_OUT_OF_MEMORY, "Memory allocation failed.");
868 wmemmove(__pValue + startIndex, __pValue + moveIndex, (__length - moveIndex) + 1);
870 __pValue[__length] = '\0';
877 String::Replace(wchar_t original, wchar_t replace)
879 result r = AboutToModify(__capacity);
880 SysTryReturnVoidResult(NID_BASE, r == E_SUCCESS, E_OUT_OF_MEMORY, "Memory allocation failed.");
882 for (int length = __length; length >= 0; --length)
884 if (__pValue[length] == original)
886 __pValue[length] = replace;
893 String::Replace(const String& org, const String& rep)
895 result r = Replace(org, rep, 0);
896 SysTryReturnResult(NID_BASE, r == E_SUCCESS, r, "Propagating.");
902 String::Replace(const String& org, const String& rep, int startIndex)
904 SysTryReturnResult(NID_BASE, org.__length > 0, E_INVALID_ARG, "The length of org MUST be greater than 0.");
906 SysTryReturnResult(NID_BASE,
907 startIndex >= 0 && startIndex < __length, E_OUT_OF_RANGE,
908 "The startIndex(%d) MUST be greater than or equal to 0, and less than the length of this string(%d).",
909 startIndex, __length);
916 int orgLen = org.__length;
917 int repLen = rep.__length;
918 if ((orgLen == __length) && (*this == org))
920 result r = AboutToModify(__capacity);
921 SysTryReturnResult(NID_BASE, r == E_SUCCESS, E_OUT_OF_MEMORY, "Memory allocation failed.");
923 r = EnsureCapacity(repLen);
924 SysTryReturnResult(NID_BASE, r == E_SUCCESS, r, "Propagating.");
926 wcsncpy(__pValue, rep.__pValue, repLen);
929 __pValue[__length] = '\0';
935 int matchedCount = 0;
937 wchar_t* pBeg = __pValue + startIndex;
938 wchar_t* pMatch = null;
940 while ((pMatch = wcsstr(pBeg, org.__pValue)) != null)
943 pBeg = pMatch + orgLen;
946 if (matchedCount > 0)
948 result r = AboutToModify(__capacity);
949 SysTryReturnResult(NID_BASE, r == E_SUCCESS, E_OUT_OF_MEMORY, "Memory allocation failed.");
951 const int newLength = (matchedCount * (repLen - orgLen)) + __length;
952 r = EnsureCapacity(newLength);
953 SysTryReturnResult(NID_BASE, r == E_SUCCESS, r, "Propagating.");
955 pBeg = __pValue + startIndex;
956 while ((pMatch = wcsstr(pBeg, org.__pValue)) != null)
958 int count = __length - (pMatch - __pValue) - orgLen;
959 wmemmove(pMatch + repLen, pMatch + orgLen, count);
960 wmemcpy(pMatch, rep.__pValue, repLen);
962 pBeg = pMatch + repLen;
963 pMatch[repLen + count] = '\0';
964 __length += (repLen - orgLen);
967 __length = newLength;
975 String::Reverse(void)
977 result r = AboutToModify(__capacity);
978 SysTryReturnVoidResult(NID_BASE, r == E_SUCCESS, E_OUT_OF_MEMORY, "Memory allocation failed.");
980 wchar_t* pBeg = __pValue;
981 wchar_t* pEnd = __pValue + __length - 1;
984 for (; pBeg < pEnd; ++pBeg, --pEnd)
994 String::SetCapacity(int capacity)
996 SysTryReturnResult(NID_BASE, capacity >= 0, E_INVALID_ARG,
997 "The capacity(%d) MUST be greater than or equal to 0.", capacity);
999 std::unique_ptr< wchar_t[] > pValue(new (std::nothrow) wchar_t[capacity + 1]);
1000 SysTryReturnResult(NID_BASE, pValue != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
1002 if (__pValue != null)
1004 if (__length < capacity)
1006 wmemcpy(pValue.get(), __pValue, (__length + 1));
1010 wmemcpy(pValue.get(), __pValue, capacity);
1011 pValue[capacity] = '\0';
1012 __length = capacity;
1016 if (*__pRefCount == 1)
1022 std::unique_ptr< int > pRefCntTemp(new (std::nothrow) int(1));
1023 SysTryReturnResult(NID_BASE, pRefCntTemp != null, E_OUT_OF_MEMORY, "Memory allocation failed");
1024 Utility::_AtomicOperations::AtomicDec(__pRefCount);
1025 __pRefCount = pRefCntTemp.release();
1029 __pValue = pValue.release();
1030 __capacity = capacity;
1036 String::SetCharAt(wchar_t ch, int indexAt)
1038 SysTryReturn(NID_BASE, indexAt >= 0 && indexAt < __length, E_OUT_OF_RANGE, E_OUT_OF_RANGE,
1039 "[%s] The indexAt(%d) MUST be greater than or equal to 0, and less then the length of this string(%d).",
1040 GetErrorMessage(E_OUT_OF_RANGE), indexAt, __length);
1042 result r = AboutToModify(__capacity);
1043 SysTryReturnResult(NID_BASE, r == E_SUCCESS, E_OUT_OF_MEMORY, "Memory allocation failed.");
1045 __pValue[indexAt] = ch;
1052 String::SetLength(int newLength)
1054 SysTryReturnResult(NID_BASE, newLength >= 0, E_INVALID_ARG, "The newLength(%d) MUST be greater than or equal to 0.",
1057 result r = AboutToModify(__capacity);
1058 SysTryReturnResult(NID_BASE, r == E_SUCCESS, E_OUT_OF_MEMORY, "Memory allocation failed.");
1060 static const wchar_t SPACE = 0x0020;
1062 r = EnsureCapacity(newLength);
1063 SysTryReturn(NID_BASE, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
1065 if (newLength > __length)
1067 wmemset(__pValue + __length, SPACE, newLength - __length);
1070 __pValue[newLength] = '\0';
1071 __length = newLength;
1078 String::SubString(int startIndex, String& out) const
1080 SysTryReturnResult(NID_BASE,
1081 startIndex >= 0 && startIndex < __length, E_OUT_OF_RANGE,
1082 "The startIndex(%d) MUST be greater than or equal to 0, and less than the length of this string(%d).",
1083 startIndex, __length);
1085 out = __pValue + startIndex;
1091 String::SubString(int startIndex, int length, String& out) const
1093 SysTryReturnResult(NID_BASE,
1094 startIndex >= 0 && startIndex < __length, E_OUT_OF_RANGE,
1095 "The startIndex(%d) MUST be greater than or equal to 0, and less than the length of this string(%d).",
1096 startIndex, __length);
1097 SysTryReturnResult(NID_BASE, length >= 0, E_OUT_OF_RANGE, "The length(%d) MUST be greater than 0.", length);
1098 SysTryReturnResult(NID_BASE, startIndex + length <= __length, E_OUT_OF_RANGE,
1099 "The startIndex(%d) + length(%d) MUST be less than or equal to the length of this string(%d).",
1100 startIndex, length, __length);
1104 std::unique_ptr< wchar_t[] > pTemp(new (std::nothrow) wchar_t[length + 1]);
1105 SysTryReturnResult(NID_BASE, pTemp != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
1106 wcsncpy(pTemp.get(), __pValue + startIndex, length);
1107 pTemp[length] = '\0';
1111 else if (length == 0)
1120 String::StartsWith(const String& str, int startIndex) const
1122 SysTryReturn(NID_BASE, startIndex >= 0 && startIndex < __length, false, E_OUT_OF_RANGE,
1123 "[%s] The startIndex(%d) MUST be greater than or equal to 0, and less than the length of this string(%d).",
1124 GetErrorMessage(E_OUT_OF_RANGE), startIndex, __length);
1125 SysTryReturn(NID_BASE, str.__length > 0, false, E_INVALID_ARG,
1126 "[%s] Invalid argument is used. The length of str(%d) MUST be greater than 0.",
1127 GetErrorMessage(E_INVALID_ARG), str.__length);
1129 if (str.__length > __length)
1134 if ((wcsncmp(__pValue + startIndex, str.__pValue, str.__length) == 0))
1143 String::EndsWith(const String& str) const
1150 int strLen = str.__length;
1151 SysTryReturn(NID_BASE, strLen > 0, false, E_INVALID_ARG,
1152 "[%s] Invalid argument is used. The length of str(%d) MUST be greater than 0.", GetErrorMessage(E_INVALID_ARG), strLen);
1154 int curLen = __length;
1155 if (strLen > curLen || curLen == 0)
1159 else if (wcscmp(__pValue + (curLen - strLen), str.__pValue) == 0)
1168 String::ToLower(String& out) const
1170 String str(__length + 1);
1172 wchar_t* pDst = str.__pValue;
1173 wchar_t* pSrc = __pValue;
1175 for (; *pSrc != 0; ++pSrc, ++pDst)
1177 *pDst = Character::ToLower(*pSrc);
1182 str.__length = __length;
1189 String::ToLowerCase(String& out) const
1191 String str(__length + 1);
1193 wchar_t* pDst = str.__pValue;
1194 wchar_t* pSrc = __pValue;
1196 for (; *pSrc != 0; ++pSrc, ++pDst)
1198 *pDst = Character::ToLowerCase(*pSrc);
1203 str.__length = __length;
1210 String::ToUpper(String& out) const
1212 String str(__length + 1);
1214 wchar_t* pDst = str.__pValue;
1215 wchar_t* pSrc = __pValue;
1217 for (; *pSrc != 0; ++pSrc, ++pDst)
1219 *pDst = Character::ToUpper(*pSrc);
1224 str.__length = __length;
1231 String::ToUpperCase(String& out) const
1233 String str(__length + 1);
1235 wchar_t* pDst = str.__pValue;
1236 wchar_t* pSrc = __pValue;
1237 for (; *pSrc != 0; ++pSrc, ++pDst)
1239 *pDst = Character::ToUpperCase(*pSrc);
1244 str.__length = __length;
1251 String::ToLower(void)
1253 result r = AboutToModify(__capacity);
1254 SysTryReturnVoidResult(NID_BASE, r == E_SUCCESS, E_OUT_OF_MEMORY, "Memory allocation failed.");
1256 String str(__length + 1);
1258 wchar_t* pDst = str.__pValue;
1259 wchar_t* pSrc = __pValue;
1261 for (; *pSrc != 0; ++pSrc, ++pDst)
1263 *pDst = Character::ToLower(*pSrc);
1266 wcsncpy(__pValue, str.__pValue, __length);
1267 __pValue[__length] = '\0';
1272 String::ToLowerCase(void)
1274 result r = AboutToModify(__capacity);
1275 SysTryReturnVoidResult(NID_BASE, r == E_SUCCESS, E_OUT_OF_MEMORY, "Memory allocation failed.");
1277 String str(__length + 1);
1279 wchar_t* pDst = str.__pValue;
1280 wchar_t* pSrc = __pValue;
1282 for (; *pSrc != 0; ++pSrc, ++pDst)
1284 *pDst = Character::ToLowerCase(*pSrc);
1287 wcsncpy(__pValue, str.__pValue, __length);
1288 __pValue[__length] = '\0';
1293 String::ToUpper(void)
1295 result r = AboutToModify(__capacity);
1296 SysTryReturnVoidResult(NID_BASE, r == E_SUCCESS, E_OUT_OF_MEMORY, "Memory allocation failed.");
1298 String str(__length + 1);
1300 wchar_t* pDst = str.__pValue;
1301 wchar_t* pSrc = __pValue;
1303 for (; *pSrc != 0; ++pSrc, ++pDst)
1305 *pDst = Character::ToUpper(*pSrc);
1308 wcsncpy(__pValue, str.__pValue, __length);
1309 __pValue[__length] = '\0';
1314 String::ToUpperCase(void)
1316 result r = AboutToModify(__capacity);
1317 SysTryReturnVoidResult(NID_BASE, r == E_SUCCESS, E_OUT_OF_MEMORY, "Memory allocation failed.");
1319 String str(__length + 1);
1321 wchar_t* pDst = str.__pValue;
1322 wchar_t* pSrc = __pValue;
1324 for (; *pSrc != 0; ++pSrc, ++pDst)
1326 *pDst = Character::ToUpperCase(*pSrc);
1329 wcsncpy(__pValue, str.__pValue, __length);
1330 __pValue[__length] = '\0';
1343 int lastIndex = __length;
1345 const wchar_t* pStr = __pValue;
1347 while ((startIndex < lastIndex) && (*(pStr + startIndex) <= L' '))
1352 while ((startIndex < lastIndex) && (*(pStr + lastIndex - 1) <= L' '))
1357 bool trimRight = lastIndex < __length;
1358 bool trimLeft = startIndex > 0;
1360 if (!trimRight && !trimLeft) // nothing to trim
1365 result r = AboutToModify(__capacity);
1366 SysTryReturnVoidResult(NID_BASE, r == E_SUCCESS, E_OUT_OF_MEMORY, "Memory allocation failed");
1370 Remove(lastIndex, __length - lastIndex);
1375 Remove(0, startIndex);
1380 String::GetCapacity(void) const
1382 return __capacity; // REMARK: the actual allocated size of buffer is __capacity + 1
1386 String::GetLength(void) const
1392 String::GetPointer(void) const
1399 String::Contains(const String& str) const
1401 SysTryReturn(NID_BASE, str.__length > 0, false, E_INVALID_ARG,
1402 "[%s] Invalid argument is used. The length of str(%d) MUST be greater than 0.",
1403 GetErrorMessage(E_INVALID_ARG), str.__length);
1409 else if ((__length == str.__length) && (*this == str))
1414 return (wcsstr(__pValue, str.__pValue) != null);
1418 String::AllocateCapacity(int capacity)
1420 __pValue = new (std::nothrow) wchar_t[capacity + 1]; // + 1 for null character
1421 if (__pValue == null)
1423 SysLogException(NID_BASE, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1428 __pValue[capacity] = '\0';
1429 __capacity = capacity;
1435 String::ExpandCapacity(int minCapacity)
1437 int capacity = (__capacity ? (4 * (((int) (GROWTH_FACTOR * __capacity) - 1) / 4 + 1)) : 0); // nearest multiple of 4
1439 if (minCapacity > capacity)
1441 capacity = minCapacity;
1444 std::unique_ptr< wchar_t[] > pNewValue(new (std::nothrow) wchar_t[capacity + 1]); // + 1 for null character
1445 SysTryReturn(NID_BASE, pNewValue != null, false, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.",
1446 GetErrorMessage(E_OUT_OF_MEMORY));
1448 if (__pValue != null)
1450 wmemcpy(pNewValue.get(), __pValue, __length);
1451 pNewValue[__length] = '\0';
1453 if (*__pRefCount == 1)
1459 std::unique_ptr< int > pRefCntTemp(new (std::nothrow) int(1));
1460 SysTryReturn(NID_BASE, pRefCntTemp != null, false, E_OUT_OF_MEMORY, "[%s] Memory allocation failed",
1461 GetErrorMessage(E_OUT_OF_MEMORY));
1462 Utility::_AtomicOperations::AtomicDec(__pRefCount);
1463 __pRefCount = pRefCntTemp.release();
1466 __pValue = pNewValue.release();
1467 __pValue[capacity] = '\0';
1468 __capacity = capacity;
1474 String::InitializeToDefault(int capacity)
1476 std::unique_ptr< int > pRefCntTemp(new (std::nothrow) int(1));
1477 SysTryReturnResult(NID_BASE, pRefCntTemp != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
1478 SysTryReturnResult(NID_BASE, AllocateCapacity(capacity) != false, E_OUT_OF_MEMORY, "Memory allocation failed.");
1480 __pRefCount = pRefCntTemp.release();
1485 String::AboutToModify(int capacity, bool isUnshareable)
1487 if (*__pRefCount > 1 && *__pRefCount != UNSHAREABLE)
1489 wchar_t* pValue = __pValue;
1490 std::unique_ptr< int > pRefCntTemp(new (std::nothrow) int(1));
1491 SysTryReturnResult(NID_BASE, pRefCntTemp != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
1492 SysTryReturnResult(NID_BASE, AllocateCapacity(capacity), E_OUT_OF_MEMORY, "Memory allocation failed.");
1494 wcsncpy(__pValue, pValue, __length);
1495 __pValue[__length] = '\0';
1497 Utility::_AtomicOperations::AtomicDec(__pRefCount);
1498 __pRefCount = pRefCntTemp.release();
1503 *__pRefCount = UNSHAREABLE;
1510 String::Swap(String& str)
1512 std::swap(__capacity, str.__capacity);
1513 std::swap(__length, str.__length);
1514 std::swap(__hash, str.__hash);
1515 std::swap(__pRefCount, str.__pRefCount);
1516 std::swap(__pValue, str.__pValue);
1517 std::swap(__pStringImpl, str.__pStringImpl);