1
0
Fork 0
mirror of https://github.com/beefytech/Beef.git synced 2025-06-09 20:12:21 +02:00

Avoid recalc of string length in integer ToString

This commit is contained in:
ESH 2025-01-13 20:17:04 +01:00
parent 96c0a44a7e
commit 84b2f22846
4 changed files with 5 additions and 7 deletions

View file

@ -86,7 +86,7 @@ namespace System
if (isNeg) if (isNeg)
strChars[char8Idx--] = '-'; strChars[char8Idx--] = '-';
char8* char8Ptr = &strChars[char8Idx + 1]; char8* char8Ptr = &strChars[char8Idx + 1];
strBuffer.Append(char8Ptr); strBuffer.Append(char8Ptr, 14 - char8Idx);
} }
void ToString(String strBuffer, int minNumerals) void ToString(String strBuffer, int minNumerals)

View file

@ -82,25 +82,23 @@ namespace System
int32 char8Idx = 20; int32 char8Idx = 20;
int64 valLeft = (int64)this; int64 valLeft = (int64)this;
bool isNeg = true; bool isNeg = true;
int minNumeralsLeft = 0;
if (valLeft >= 0) if (valLeft >= 0)
{ {
valLeft = -valLeft; valLeft = -valLeft;
isNeg = false; isNeg = false;
} }
while ((valLeft < 0) || (minNumeralsLeft > 0)) while (valLeft < 0)
{ {
strChars[char8Idx] = (char8)('0' &- (valLeft % 10)); strChars[char8Idx] = (char8)('0' &- (valLeft % 10));
valLeft /= 10; valLeft /= 10;
char8Idx--; char8Idx--;
minNumeralsLeft--;
} }
if (char8Idx == 20) if (char8Idx == 20)
strChars[char8Idx--] = '0'; strChars[char8Idx--] = '0';
if (isNeg) if (isNeg)
strChars[char8Idx--] = '-'; strChars[char8Idx--] = '-';
char8* char8Ptr = &strChars[char8Idx + 1]; char8* char8Ptr = &strChars[char8Idx + 1];
strBuffer.Append(char8Ptr); strBuffer.Append(char8Ptr, 20 - char8Idx);
} }
public static Result<int64, ParseError> Parse(StringView val, NumberStyles style = .Number, CultureInfo cultureInfo = null) public static Result<int64, ParseError> Parse(StringView val, NumberStyles style = .Number, CultureInfo cultureInfo = null)

View file

@ -73,7 +73,7 @@ namespace System
if (char8Idx == 14) if (char8Idx == 14)
strChars[char8Idx--] = '0'; strChars[char8Idx--] = '0';
char8* char8Ptr = &strChars[char8Idx + 1]; char8* char8Ptr = &strChars[char8Idx + 1];
strBuffer.Append(char8Ptr); strBuffer.Append(char8Ptr, 14 - char8Idx);
} }
void ToString(String strBuffer, int minNumerals) void ToString(String strBuffer, int minNumerals)

View file

@ -85,7 +85,7 @@ namespace System
if (char8Idx == 20) if (char8Idx == 20)
strChars[char8Idx--] = '0'; strChars[char8Idx--] = '0';
char8* char8Ptr = &strChars[char8Idx + 1]; char8* char8Ptr = &strChars[char8Idx + 1];
strBuffer.Append(char8Ptr); strBuffer.Append(char8Ptr, 20 - char8Idx);
} }
public static Result<uint64, ParseError> Parse(StringView val, NumberStyles style = .Number, CultureInfo cultureInfo = null) public static Result<uint64, ParseError> Parse(StringView val, NumberStyles style = .Number, CultureInfo cultureInfo = null)