void
String::Reverse(void)
{
- if (*__pRefCount > 0)
+ if (*__pRefCount > 1)
{
result r = CopyOnWrite(__capacity);
SysTryReturnVoidResult(NID_BASE, r == E_SUCCESS, E_OUT_OF_MEMORY, "Memory allocation failed.");
void
String::ToLowerCase(void)
{
- if (*__pRefCount > 0)
+ if (*__pRefCount > 1)
{
result r = CopyOnWrite(__capacity);
SysTryReturnVoidResult(NID_BASE, r == E_SUCCESS, E_OUT_OF_MEMORY, "Memory allocation failed.");
--lastIndex;
}
- // trim right
- if (lastIndex < __length)
+ bool trimRight = lastIndex < __length;
+ bool trimLeft = startIndex > 0;
+
+ if (!trimRight && !trimLeft) // nothing to trim
+ {
+ return;
+ }
+
+ if (*__pRefCount > 1)
+ {
+ result r = CopyOnWrite(__capacity);
+ SysTryReturnVoidResult(NID_BASE, r == E_SUCCESS, E_OUT_OF_MEMORY, "Memory allocation failed");
+ }
+
+ if (trimRight)
{
Remove(lastIndex, __length - lastIndex);
}
- // trim left
- if (startIndex > 0)
+ if (trimLeft)
{
Remove(0, startIndex);
}