fix ltoa_internal: fix #948 (#949)

* fix ltoa_internal

* fix hex
This commit is contained in:
Matthew Kennedy 2019-09-23 05:45:45 -07:00 committed by rusefi
parent 8c108f8362
commit b4e176a9cb
1 changed files with 28 additions and 17 deletions

View File

@ -129,26 +129,37 @@ int atoi(const char *string) {
static char todofixthismesswithcopy[100]; static char todofixthismesswithcopy[100];
/** static char *ltoa_internal(char *p, uint32_t num, unsigned radix) {
* WARNING: due to implementation details specific buffer should be at least size of '_MAX_FILLER' constexpr int bufferLength = 10;
*/
static char *ltoa_internal(char *p, long num, unsigned radix) { char buffer[bufferLength];
int i;
char *q;
q = p + _MAX_FILLER; size_t idx = bufferLength - 1;
do {
i = (int) (num % radix); // First, we write from right-to-left so that we don't have to compute
i += '0'; // log(num)/log(radix)
if (i > '9') do
i += 'A' - '0' - 10; {
*--q = i; auto digit = num % radix;
// Digits 0-9 -> '0'-'9'
// Digits 10-15 -> 'a'-'f'
char c = digit < 10
? digit + '0'
: digit + 'a' - 10;
// Write this digit in to the buffer
buffer[idx] = c;
idx--;
} while ((num /= radix) != 0); } while ((num /= radix) != 0);
idx++;
i = (int) (p + _MAX_FILLER - q); // Now, we copy characters in to place in the final buffer
do { while (idx < bufferLength)
*p++ = *q++; {
} while (--i); *p++ = buffer[idx++];
}
return p; return p;
} }