stdlib: Refactor toString with radix for Int and Long

This commit is contained in:
Ilya Matveev
2017-04-21 11:35:42 +07:00
committed by ilmat192
parent 0cb7e5ba72
commit e9d6013efd
+39 -61
View File
@@ -14,6 +14,7 @@
* limitations under the License.
*/
#include <limits.h>
#include <stdio.h>
#include <string.h>
@@ -24,15 +25,48 @@
#include "KString.h"
#include "Types.h"
constexpr int MIN_RADIX = 2;
constexpr int MAX_RADIX = 36;
namespace {
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) {
return '0' + value;
} else {
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" {
@@ -71,35 +105,7 @@ OBJ_GETTER(Kotlin_Int_toString, KInt value) {
}
OBJ_GETTER(Kotlin_Int_toStringRadix, KInt value, KInt radix) {
if (value == 0) {
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);
RETURN_RESULT_OF(Kotlin_toStringRadix<KInt>, value, radix)
}
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) {
if (value == 0) {
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);
RETURN_RESULT_OF(Kotlin_toStringRadix<KLong>, value, radix)
}
// TODO: use David Gay's dtoa() here instead. It's *very* big and ugly.