diff --git a/BeefLibs/corlib/src/String.bf b/BeefLibs/corlib/src/String.bf index 575831de..473653cd 100644 --- a/BeefLibs/corlib/src/String.bf +++ b/BeefLibs/corlib/src/String.bf @@ -682,6 +682,13 @@ namespace System return (bumpSize > minSize) ? bumpSize : minSize; } + [Inline] + void CalculatedReserve(int newSize) + { + if (newSize > AllocSize) + Realloc(CalcNewSize(newSize)); + } + void Realloc(int newSize) { Debug.Assert(AllocSize > 0, "String has been frozen"); @@ -815,8 +822,7 @@ namespace System if (bytes <= 0) return null; int count = bytes; - if (mLength + count >= AllocSize) - Realloc(CalcNewSize(mLength + count + 1)); + CalculatedReserve(mLength + count + 1); char8* ptr = Ptr + mLength; mLength += (int_strsize)bytes; return ptr; @@ -851,19 +857,17 @@ namespace System public void Append(char8 c) { - if (mLength + 1 > AllocSize) - Realloc(CalcNewSize(mLength + 1)); + CalculatedReserve(mLength + 1); let ptr = Ptr; ptr[mLength++] = c; } public void Append(char8 c, int count) { - if (count == 0) + if (count <= 0) return; - if (mLength + count > AllocSize) - Realloc(CalcNewSize(mLength + count)); + CalculatedReserve(mLength + count); let ptr = Ptr; for (int_strsize i = 0; i < count; i++) ptr[mLength++] = c; @@ -871,32 +875,31 @@ namespace System public void Append(char32 c) { - let ptr = Ptr; if (c < (char32)0x80) { - if (mLength + 1 > AllocSize) - Realloc(CalcNewSize(mLength + 1)); + CalculatedReserve(mLength + 1); + let ptr = Ptr; ptr[mLength++] = (char8)c; } else if (c < (char32)0x800) { - if (mLength + 2 > AllocSize) - Realloc(CalcNewSize(mLength + 2)); + CalculatedReserve(mLength + 2); + let ptr = Ptr; ptr[mLength++] = (char8)(c>>6) | (char8)0xC0; ptr[mLength++] = (char8)(c & (char8)0x3F) | (char8)0x80; } else if (c < (char32)0x10000) { - if (mLength + 3 > AllocSize) - Realloc(CalcNewSize(mLength + 3)); + CalculatedReserve(mLength + 3); + let ptr = Ptr; ptr[mLength++] = (char8)(c>>12) | (char8)0xE0; ptr[mLength++] = (char8)((c>>6) & (char8)0x3F) | (char8)0x80; ptr[mLength++] = (char8)(c & (char8)0x3F) | (char8)0x80; } else if (c < (char32)0x110000) { - if (mLength + 4 > AllocSize) - Realloc(CalcNewSize(mLength + 4)); + CalculatedReserve(mLength + 4); + let ptr = Ptr; ptr[mLength++] = (char8)((c>>18) | (char8)0xF0); ptr[mLength++] = (char8)((c>>12) & (char8)0x3F) | (char8)0x80; ptr[mLength++] = (char8)((c>>6) & (char8)0x3F) | (char8)0x80; @@ -906,7 +909,7 @@ namespace System public void Append(char32 c, int count) { - if (count == 0) + if (count <= 0) return; if (count == 1) @@ -917,8 +920,7 @@ namespace System int encodedLen = UTF8.GetEncodedLength(c); - if (mLength + count * encodedLen > AllocSize) - Realloc(CalcNewSize(mLength + count * encodedLen)); + CalculatedReserve(mLength + count * encodedLen); let ptr = Ptr; for (int_strsize i = 0; i < count; i++) @@ -953,8 +955,7 @@ namespace System int allocSize = AllocSize; if ((allocSize == mLength) || (Ptr[mLength] != 0)) { - if (mLength >= allocSize) - Realloc(CalcNewSize(mLength + 1)); + CalculatedReserve(mLength + 1); Ptr[mLength] = 0; } } @@ -1457,8 +1458,7 @@ namespace System int_strsize length = (int_strsize)addString.Length; int_strsize newLength = mLength + length; - if (newLength > AllocSize) - Realloc(CalcNewSize(newLength)); + CalculatedReserve(newLength); let moveChars = mLength - idx; let ptr = Ptr; @@ -1473,8 +1473,7 @@ namespace System Contract.Requires(idx >= 0); let newLength = mLength + 1; - if (newLength > AllocSize) - Realloc(CalcNewSize(newLength)); + CalculatedReserve(newLength); let moveChars = mLength - idx; let ptr = Ptr; @@ -1492,8 +1491,7 @@ namespace System return; let newLength = mLength + (int_strsize)count; - if (newLength > AllocSize) - Realloc(CalcNewSize(newLength)); + CalculatedReserve(newLength); let moveChars = mLength - idx; let ptr = Ptr; @@ -1509,11 +1507,10 @@ namespace System Contract.Requires(idx >= 0); let moveChars = mLength - idx; - let ptr = Ptr; if (c < (char32)0x80) { - if (mLength + 1 > AllocSize) - Realloc(CalcNewSize(mLength + 1)); + CalculatedReserve(mLength + 1); + let ptr = Ptr; if (moveChars > 0) Internal.MemMove(ptr + idx + 1, ptr + idx, moveChars); ptr[idx] = (char8)c; @@ -1521,8 +1518,8 @@ namespace System } else if (c < (char32)0x800) { - if (mLength + 2 > AllocSize) - Realloc(CalcNewSize(mLength + 2)); + CalculatedReserve(mLength + 2); + let ptr = Ptr; if (moveChars > 0) Internal.MemMove(ptr + idx + 2, ptr + idx, moveChars); ptr[idx] = (char8)(c>>6) | (char8)0xC0; @@ -1531,8 +1528,8 @@ namespace System } else if (c < (char32)0x10000) { - if (mLength + 3 > AllocSize) - Realloc(CalcNewSize(mLength + 3)); + CalculatedReserve(mLength + 3); + let ptr = Ptr; if (moveChars > 0) Internal.MemMove(ptr + idx + 3, ptr + idx, moveChars); ptr[idx] = (char8)(c>>12) | (char8)0xE0; @@ -1542,8 +1539,8 @@ namespace System } else if (c < (char32)0x110000) { - if (mLength + 4 > AllocSize) - Realloc(CalcNewSize(mLength + 4)); + CalculatedReserve(mLength + 4); + let ptr = Ptr; if (moveChars > 0) Internal.MemMove(ptr + idx + 4, ptr + idx, moveChars); ptr[idx] = (char8)((c>>18) | (char8)0xF0); @@ -1569,8 +1566,7 @@ namespace System let encodedLen = UTF8.GetEncodedLength(c); let newLength = mLength + (int_strsize)(count * encodedLen); - if (newLength > AllocSize) - Realloc(CalcNewSize(newLength)); + CalculatedReserve(newLength); let moveChars = mLength - idx; let ptr = Ptr; @@ -1897,8 +1893,7 @@ namespace System int destLength = mLength + moveOffset * replaceEntries.Count; int needSize = destLength; - if (needSize > AllocSize) - Realloc((int_strsize)needSize); + CalculatedReserve(needSize); let replacePtr = replace.Ptr; let ptr = Ptr;