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

Merge pull request #2090 from m910q/integer-tostring-optimize

Avoid recalc of string length in integer ToString()
This commit is contained in:
Brian Fiete 2025-01-14 12:59:11 -08:00 committed by GitHub
commit 851dda7f74
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 9 additions and 11 deletions

View file

@ -66,7 +66,7 @@ namespace System
public override void ToString(String strBuffer) public override void ToString(String strBuffer)
{ {
// Dumb, make better. // Dumb, make better.
char8[] strChars = scope:: char8[16]; char8[16] strChars = ?;
int32 char8Idx = 14; int32 char8Idx = 14;
int32 valLeft = (int32)this; int32 valLeft = (int32)this;
bool isNeg = true; bool isNeg = true;
@ -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

@ -78,29 +78,27 @@ namespace System
public override void ToString(String strBuffer) public override void ToString(String strBuffer)
{ {
// Dumb, make better. // Dumb, make better.
char8[] strChars = scope:: char8[22]; char8[22] strChars = ?;
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

@ -61,7 +61,7 @@ namespace System
public override void ToString(String strBuffer) public override void ToString(String strBuffer)
{ {
// Dumb, make better. // Dumb, make better.
char8[] strChars = scope:: char8[16]; char8[16] strChars = ?;
int32 char8Idx = 14; int32 char8Idx = 14;
uint32 valLeft = (uint32)this; uint32 valLeft = (uint32)this;
while (valLeft > 0) while (valLeft > 0)
@ -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

@ -73,7 +73,7 @@ namespace System
public override void ToString(String strBuffer) public override void ToString(String strBuffer)
{ {
// Dumb, make better. // Dumb, make better.
char8[] strChars = scope:: char8[22]; char8[22] strChars = ?;
int32 char8Idx = 20; int32 char8Idx = 20;
uint64 valLeft = (uint64)this; uint64 valLeft = (uint64)this;
while (valLeft > 0) while (valLeft > 0)
@ -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)