stdlib: Refactor toString with radix for Int and Long
This commit is contained in:
@@ -14,6 +14,7 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include <limits.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
@@ -24,15 +25,48 @@
|
|||||||
#include "KString.h"
|
#include "KString.h"
|
||||||
#include "Types.h"
|
#include "Types.h"
|
||||||
|
|
||||||
constexpr int MIN_RADIX = 2;
|
namespace {
|
||||||
constexpr int MAX_RADIX = 36;
|
|
||||||
|
|
||||||
char int_to_digit(uint32_t value) {
|
constexpr int MIN_RADIX = 2;
|
||||||
|
constexpr int MAX_RADIX = 36;
|
||||||
|
|
||||||
|
char int_to_digit(uint32_t value) {
|
||||||
if (value < 10) {
|
if (value < 10) {
|
||||||
return '0' + value;
|
return '0' + value;
|
||||||
} else {
|
} else {
|
||||||
return 'a' + (value - 10);
|
return 'a' + (value - 10);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T> OBJ_GETTER(Kotlin_toStringRadix, T value, KInt radix) {
|
||||||
|
if (value == 0) {
|
||||||
|
RETURN_RESULT_OF(CreateStringFromCString, "0");
|
||||||
|
}
|
||||||
|
if (radix < MIN_RADIX || radix > MAX_RADIX) {
|
||||||
|
radix = 10;
|
||||||
|
}
|
||||||
|
char cstring[sizeof(T) * CHAR_BIT + 1];
|
||||||
|
bool negative = (value < 0);
|
||||||
|
if (!negative) {
|
||||||
|
value = -value;
|
||||||
|
}
|
||||||
|
|
||||||
|
int32_t length = 0;
|
||||||
|
while (value < 0) {
|
||||||
|
cstring[length++] = int_to_digit(-(value % radix));
|
||||||
|
value /= radix;
|
||||||
|
}
|
||||||
|
if (negative) {
|
||||||
|
cstring[length++] = '-';
|
||||||
|
}
|
||||||
|
for (int i = 0, j = length - 1; i < j; i++, j--) {
|
||||||
|
char tmp = cstring[i];
|
||||||
|
cstring[i] = cstring[j];
|
||||||
|
cstring[j] = tmp;
|
||||||
|
}
|
||||||
|
cstring[length] = '\0';
|
||||||
|
RETURN_RESULT_OF(CreateStringFromCString, cstring);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
extern "C" {
|
extern "C" {
|
||||||
@@ -71,35 +105,7 @@ OBJ_GETTER(Kotlin_Int_toString, KInt value) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
OBJ_GETTER(Kotlin_Int_toStringRadix, KInt value, KInt radix) {
|
OBJ_GETTER(Kotlin_Int_toStringRadix, KInt value, KInt radix) {
|
||||||
if (value == 0) {
|
RETURN_RESULT_OF(Kotlin_toStringRadix<KInt>, value, radix)
|
||||||
RETURN_RESULT_OF(CreateStringFromCString, "0");
|
|
||||||
}
|
|
||||||
if (radix < MIN_RADIX || radix > MAX_RADIX) {
|
|
||||||
radix = 10;
|
|
||||||
}
|
|
||||||
// TODO: maye not fit for smaller radices.
|
|
||||||
char cstring[32];
|
|
||||||
bool negative = (value < 0);
|
|
||||||
// Calculate in negative values because MIN_VALUE * -1 == MIN_VALUE
|
|
||||||
if (!negative) {
|
|
||||||
value = -value;
|
|
||||||
}
|
|
||||||
|
|
||||||
int32_t length = 0;
|
|
||||||
while (value < 0) {
|
|
||||||
cstring[length++] = int_to_digit(-(value % radix));
|
|
||||||
value /= radix;
|
|
||||||
}
|
|
||||||
if (negative) {
|
|
||||||
cstring[length++] = '-';
|
|
||||||
}
|
|
||||||
for (int i = 0, j = length - 1; i < j; i++, j--) {
|
|
||||||
char tmp = cstring[i];
|
|
||||||
cstring[i] = cstring[j];
|
|
||||||
cstring[j] = tmp;
|
|
||||||
}
|
|
||||||
cstring[length] = '\0';
|
|
||||||
RETURN_RESULT_OF(CreateStringFromCString, cstring);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
OBJ_GETTER(Kotlin_Long_toString, KLong value) {
|
OBJ_GETTER(Kotlin_Long_toString, KLong value) {
|
||||||
@@ -109,35 +115,7 @@ OBJ_GETTER(Kotlin_Long_toString, KLong value) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
OBJ_GETTER(Kotlin_Long_toStringRadix, KLong value, KInt radix) {
|
OBJ_GETTER(Kotlin_Long_toStringRadix, KLong value, KInt radix) {
|
||||||
if (value == 0) {
|
RETURN_RESULT_OF(Kotlin_toStringRadix<KLong>, value, radix)
|
||||||
RETURN_RESULT_OF(CreateStringFromCString, "0");
|
|
||||||
}
|
|
||||||
if (radix < MIN_RADIX || radix > MAX_RADIX) {
|
|
||||||
radix = 10;
|
|
||||||
}
|
|
||||||
// TODO: maye not fit for smaller radices.
|
|
||||||
char cstring[64];
|
|
||||||
bool negative = (value < 0);
|
|
||||||
// Calculate in negative values because MIN_VALUE * -1 == MIN_VALUE
|
|
||||||
if (!negative) {
|
|
||||||
value = -value;
|
|
||||||
}
|
|
||||||
|
|
||||||
int32_t length = 0;
|
|
||||||
while (value < 0) {
|
|
||||||
cstring[length++] = int_to_digit(-(value % radix));
|
|
||||||
value /= radix;
|
|
||||||
}
|
|
||||||
if (negative) {
|
|
||||||
cstring[length++] = '-';
|
|
||||||
}
|
|
||||||
for (int i = 0, j = length - 1; i < j; i++, j--) {
|
|
||||||
char tmp = cstring[i];
|
|
||||||
cstring[i] = cstring[j];
|
|
||||||
cstring[j] = tmp;
|
|
||||||
}
|
|
||||||
cstring[length] = '\0';
|
|
||||||
RETURN_RESULT_OF(CreateStringFromCString, cstring);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: use David Gay's dtoa() here instead. It's *very* big and ugly.
|
// TODO: use David Gay's dtoa() here instead. It's *very* big and ugly.
|
||||||
|
|||||||
Reference in New Issue
Block a user