1
0
Fork 0
mirror of https://github.com/beefytech/Beef.git synced 2025-06-11 12:54:15 +02:00

Merge remote-tracking branch 'origin/master'

This commit is contained in:
Brian Fiete 2020-03-26 05:52:27 -07:00
commit 55a8c12a7c

View file

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