Skip to content

Commit

Permalink
Cxx: Add to_string_4 with floatStyle argument
Browse files Browse the repository at this point in the history
  • Loading branch information
agdavydov81 committed Apr 5, 2024
1 parent 394013d commit 4dc12a4
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 11 deletions.
1 change: 1 addition & 0 deletions native/src/NativeImpl.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ OPN(tryParse, dfp64_try_parse(str, exception), const char* str, uint32* exceptio
OPNR(to_string, const char*, dfp64_to_string(x), BID_UINT64 x)
OPNR(to_string_2, const char*, dfp64_to_string_2(x, decimalMark), BID_UINT64 x, char decimalMark)
OPNR(to_string_3, const char*, dfp64_to_string_3(x, decimalMark, buffer512), BID_UINT64 x, char decimalMark, char* buffer512)
OPNR(to_string_4, const char*, dfp64_to_string_4(x, decimalMark, buffer512, floatStyle), BID_UINT64 x, char decimalMark, char* buffer512, bool floatStyle)

OPNR(to_scientific_string, const char*, dfp64_to_scientific_string(x), BID_UINT64 x)
OPNR(to_scientific_string_2, const char*, dfp64_to_scientific_string_2(x, decimalMark), BID_UINT64 x, char decimalMark)
Expand Down
38 changes: 27 additions & 11 deletions native/src/NativeImplToString.c
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,10 @@ const char* dfp64_to_string_2(BID_UINT64 value, char decimalMark) {
}

const char* dfp64_to_string_3(BID_UINT64 value, char decimalMark, char* buffer512) {
return dfp64_to_string_4(value, decimalMark, buffer512, false);
}

const char* dfp64_to_string_4(BID_UINT64 value, char decimalMark, char* buffer512, bool floatStyle) {
if (isNull(value))
return "null";

Expand All @@ -136,9 +140,13 @@ const char* dfp64_to_string_3(BID_UINT64 value, char decimalMark, char* buffer51

if (partsCoefficient == 0) { // return "0" + decimalMark + "0";
buffer512[0] = '0';
buffer512[1] = decimalMark;
buffer512[2] = '0';
buffer512[3] = '\0';
if (!floatStyle) {
buffer512[1] = '\0';
} else {
buffer512[1] = decimalMark;
buffer512[2] = '0';
buffer512[3] = '\0';
}
return buffer512;
}

Expand All @@ -148,8 +156,10 @@ const char* dfp64_to_string_3(BID_UINT64 value, char decimalMark, char* buffer51

if (exponent >= 0) {
int bi = bufferMinLength;
buffer512[--bi] = '0';
buffer512[--bi] = decimalMark;
if (floatStyle) {
buffer512[--bi] = '0';
buffer512[--bi] = decimalMark;
}
for (int i = 0; i < exponent; ++i)
buffer512[--bi] = '0';

Expand All @@ -161,7 +171,7 @@ const char* dfp64_to_string_3(BID_UINT64 value, char decimalMark, char* buffer51
while (buffer512[bi] == '0')
++bi;

if (value < 0)
if (partsSignMask)
buffer512[--bi] = '-';

return buffer512 + bi;
Expand Down Expand Up @@ -198,15 +208,21 @@ const char* dfp64_to_string_3(BID_UINT64 value, char decimalMark, char* buffer51
while (buffer512[bi] == '0')
++bi;

if (value < 0)
if (partsSignMask)
buffer512[--bi] = '-';

int be = bufferMinLength;
while (buffer512[be - 1] == '0')
--be;

if (buffer512[be - 1] == decimalMark && be < bufferMinLength)
buffer512[be++] = '0';
if (buffer512[be - 1] == decimalMark) {
if (!floatStyle) {
--be;
}
else if (be < bufferMinLength) {
buffer512[be++] = '0';
}
}

buffer512[be] = '\0';
return buffer512 + bi;
Expand All @@ -228,7 +244,7 @@ const char* dfp64_to_string_3(BID_UINT64 value, char decimalMark, char* buffer51
buffer512[--bi] = decimalMark;
buffer512[--bi] = '0';

if (value < 0)
if (partsSignMask)
buffer512[--bi] = '-';

int be = bufferMinLength;
Expand Down Expand Up @@ -291,7 +307,7 @@ const char* dfp64_to_scientific_string_3(BID_UINT64 value, char decimalMark, cha
buffer512[bi] = buffer512[bi + 1];
buffer512[bi + 1] = decimalMark;

if (value < 0)
if (partsSignMask)
buffer512[--bi] = '-';

buffer512[be++] = 'e';
Expand Down
1 change: 1 addition & 0 deletions native/src/NativeImplToString.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
BID_EXTERN_C const char * dfp64_to_string(BID_UINT64 value);
BID_EXTERN_C const char * dfp64_to_string_2(BID_UINT64 value, char decimalMark);
BID_EXTERN_C const char * dfp64_to_string_3(BID_UINT64 value, char decimalMark, char* buffer512);
BID_EXTERN_C const char * dfp64_to_string_4(BID_UINT64 value, char decimalMark, char* buffer512, bool floatStyle);

BID_EXTERN_C const char * dfp64_to_scientific_string(BID_UINT64 value);
BID_EXTERN_C const char * dfp64_to_scientific_string_2(BID_UINT64 value, char decimalMark);
Expand Down

0 comments on commit 4dc12a4

Please sign in to comment.