Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 2 additions & 52 deletions cores/esp8266/core_esp8266_noniso.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,62 +29,12 @@
#include <math.h>
#include "stdlib_noniso.h"

void reverse(char* begin, char* end) {
char *is = begin;
char *ie = end - 1;
while(is < ie) {
char tmp = *ie;
*ie = *is;
*is = tmp;
++is;
--ie;
}
}

char* ltoa(long value, char* result, int base) {
if(base < 2 || base > 16) {
*result = 0;
return result;
}

char* out = result;
long quotient = abs(value);

do {
const long tmp = quotient / base;
*out = "0123456789abcdef"[quotient - (tmp * base)];
++out;
quotient = tmp;
} while(quotient);

// Apply negative sign
if(value < 0)
*out++ = '-';

reverse(result, out);
*out = 0;
return result;
return itoa((int)value, result, base);
}

char* ultoa(unsigned long value, char* result, int base) {
if(base < 2 || base > 16) {
*result = 0;
return result;
}

char* out = result;
unsigned long quotient = value;

do {
const unsigned long tmp = quotient / base;
*out = "0123456789abcdef"[quotient - (tmp * base)];
++out;
quotient = tmp;
} while(quotient);

reverse(result, out);
*out = 0;
return result;
return utoa((unsigned int)value, result, base);
}

char * dtostrf(double number, signed char width, unsigned char prec, char *s) {
Expand Down
14 changes: 14 additions & 0 deletions tests/host/common/noniso.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,17 @@
#include "stdlib_noniso.h"


void reverse(char* begin, char* end) {
char *is = begin;
char *ie = end - 1;
while(is < ie) {
char tmp = *ie;
*ie = *is;
*is = tmp;
++is;
--ie;
}
}

char* utoa(unsigned value, char* result, int base) {
if(base < 2 || base > 16) {
Expand Down Expand Up @@ -49,6 +60,9 @@ char* itoa(int value, char* result, int base) {
*result = 0;
return result;
}
if (base != 10) {
return utoa((unsigned)value, result, base);
}

char* out = result;
int quotient = abs(value);
Expand Down
9 changes: 9 additions & 0 deletions tests/host/core/test_string.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,15 @@ TEST_CASE("String concantenation", "[core][String]")
str = "clean";
REQUIRE(str.concat(str) == true);
REQUIRE(str == "cleanclean");
// non-decimal negative #s should be as if they were unsigned
str = String((int)-100, 16);
REQUIRE(str == "ffffff9c");
str = String((long)-101, 16);
REQUIRE(str == "ffffff9b");
str = String((int)-100, 10);
REQUIRE(str == "-100");
str = String((long)-100, 10);
REQUIRE(str == "-100");
}

TEST_CASE("String comparison", "[core][String]")
Expand Down