1
0
Fork 0
mirror of https://github.com/beefytech/Beef.git synced 2025-06-14 14:24:10 +02:00

Merge pull request #644 from thibmo/uint_string_format_fix

Fix formatting issue for UInt32, UInt16, UInt8 and UInt
This commit is contained in:
Brian Fiete 2020-10-30 13:10:57 -07:00 committed by GitHub
commit 72d48c7cfa
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 43 deletions

View file

@ -1735,14 +1735,14 @@ namespace System
public static void NumberToString (StringView format, uint32 value, IFormatProvider fp, String outString) public static void NumberToString (StringView format, uint32 value, IFormatProvider fp, String outString)
{ {
NumberFormatter inst = GetInstance!(fp); NumberFormatter inst = GetInstance!(fp);
inst.Init (format, value, Int32DefPrecision); inst.Init (format, value, UInt32DefPrecision);
inst.IntegerToString(format, fp, outString); inst.IntegerToString(format, fp, outString);
} }
public static void NumberToString (StringView format, int32 value, IFormatProvider fp, String outString) public static void NumberToString (StringView format, int32 value, IFormatProvider fp, String outString)
{ {
NumberFormatter inst = GetInstance!(fp); NumberFormatter inst = GetInstance!(fp);
inst.Init (format, value, UInt32DefPrecision); inst.Init (format, value, Int32DefPrecision);
inst.IntegerToString (format, fp, outString); inst.IntegerToString (format, fp, outString);
} }

View file

@ -54,56 +54,40 @@ 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[] strChars = scope:: char8[16];
int32 char8Idx = 14; int32 char8Idx = 14;
int32 valLeft = (int32)this; uint32 valLeft = (uint32)this;
bool isNeg = false; while (valLeft > 0)
if (valLeft < 0)
{ {
valLeft = -valLeft; strChars[char8Idx] = (char8)('0' + (valLeft % 10));
isNeg = true; valLeft /= 10;
char8Idx--;
} }
while (valLeft > 0) if (char8Idx == 14)
{ strChars[char8Idx--] = '0';
strChars[char8Idx] = (char8)('0' + (valLeft % 10)); char8* char8Ptr = &strChars[char8Idx + 1];
valLeft /= 10; strBuffer.Append(char8Ptr);
char8Idx--;
}
if (char8Idx == 14)
strChars[char8Idx--] = '0';
if (isNeg)
strChars[char8Idx--] = '-';
char8* char8Ptr = &strChars[char8Idx + 1];
strBuffer.Append(char8Ptr);
} }
void ToString(String strBuffer, int minNumerals) void ToString(String strBuffer, int minNumerals)
{ {
// Dumb, make better. // Dumb, make better.
char8[] strChars = scope:: char8[16]; char8[] strChars = scope:: char8[16];
int32 char8Idx = 14; int32 char8Idx = 14;
int32 valLeft = (int32)this; uint32 valLeft = (uint32)this;
bool isNeg = false;
int minNumeralsLeft = minNumerals; int minNumeralsLeft = minNumerals;
if (valLeft < 0) while ((valLeft > 0) || (minNumeralsLeft > 0))
{ {
valLeft = -valLeft; strChars[char8Idx] = (char8)('0' + (valLeft % 10));
isNeg = true; valLeft /= 10;
} char8Idx--;
while ((valLeft > 0) || (minNumeralsLeft > 0))
{
strChars[char8Idx] = (char8)('0' + (valLeft % 10));
valLeft /= 10;
char8Idx--;
minNumeralsLeft--; minNumeralsLeft--;
} }
if (char8Idx == 14) if (char8Idx == 14)
strChars[char8Idx--] = '0'; strChars[char8Idx--] = '0';
if (isNeg) char8* char8Ptr = &strChars[char8Idx + 1];
strChars[char8Idx--] = '-'; strBuffer.Append(char8Ptr);
char8* char8Ptr = &strChars[char8Idx + 1];
strBuffer.Append(char8Ptr);
} }
public void ToString(String outString, String format, IFormatProvider formatProvider) public void ToString(String outString, String format, IFormatProvider formatProvider)