Skip to content
Open
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
110 changes: 70 additions & 40 deletions api/String.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,59 +85,82 @@ String::String(char c)
String::String(unsigned char value, unsigned char base)
{
init();
char buf[1 + 8 * sizeof(unsigned char)];
utoa(value, buf, base);
*this = buf;
if (!reserve(1 + 8 * sizeof(unsigned char))) {
invalidate();
return;
}
utoa(value, buffer, base);
len = strlen(buffer);
}

String::String(int value, unsigned char base)
{
init();
char buf[2 + 8 * sizeof(int)];
itoa(value, buf, base);
*this = buf;
if (!reserve(1 + 8 * sizeof(int))) {
invalidate();
return;
}
itoa(value, buffer, base);
len = strlen(buffer);
}

String::String(unsigned int value, unsigned char base)
{
init();
char buf[1 + 8 * sizeof(unsigned int)];
utoa(value, buf, base);
*this = buf;
if (!reserve(1 + 8 * sizeof(unsigned int))) {
invalidate();
return;
}
utoa(value, buffer, base);
len = strlen(buffer);
}

String::String(long value, unsigned char base)
{
init();
char buf[2 + 8 * sizeof(long)];
ltoa(value, buf, base);
*this = buf;
if (!reserve(1 + 8 * sizeof(long))) {
invalidate();
return;
}
ltoa(value, buffer, base);
len = strlen(buffer);
}

String::String(unsigned long value, unsigned char base)
{
init();
char buf[1 + 8 * sizeof(unsigned long)];
ultoa(value, buf, base);
*this = buf;
if (!reserve(1 + 8 * sizeof(unsigned long))) {
invalidate();
return;
}
ultoa(value, buffer, base);
len = strlen(buffer);
}

String::String(float value, unsigned char decimalPlaces)
{
static size_t const FLOAT_BUF_SIZE = FLT_MAX_10_EXP + FLT_MAX_DECIMAL_PLACES + 1 /* '-' */ + 1 /* '.' */ + 1 /* '\0' */;
init();
char buf[FLOAT_BUF_SIZE];
decimalPlaces = min(decimalPlaces, FLT_MAX_DECIMAL_PLACES);
*this = dtostrf(value, (decimalPlaces + 2), decimalPlaces, buf);
if (!reserve(FLOAT_BUF_SIZE - 1)) {
invalidate();
return;
}
dtostrf(value, (decimalPlaces + 2), decimalPlaces, buffer);
len = strlen(buffer);
}

String::String(double value, unsigned char decimalPlaces)
{
static size_t const DOUBLE_BUF_SIZE = DBL_MAX_10_EXP + DBL_MAX_DECIMAL_PLACES + 1 /* '-' */ + 1 /* '.' */ + 1 /* '\0' */;
init();
char buf[DOUBLE_BUF_SIZE];
decimalPlaces = min(decimalPlaces, DBL_MAX_DECIMAL_PLACES);
*this = dtostrf(value, (decimalPlaces + 2), decimalPlaces, buf);
if (!reserve(DOUBLE_BUF_SIZE - 1)) {
invalidate();
return;
}
dtostrf(value, (decimalPlaces + 2), decimalPlaces, buffer);
len = strlen(buffer);
}

String::~String()
Expand Down Expand Up @@ -293,51 +316,58 @@ bool String::concat(char c)

bool String::concat(unsigned char num)
{
char buf[1 + 3 * sizeof(unsigned char)];
itoa(num, buf, 10);
return concat(buf);
if (!reserve(len + 1 + 8 * sizeof(unsigned char))) return false;
itoa(num, buffer + len, 10);
len += strlen(buffer + len);
return true;
}

bool String::concat(int num)
{
char buf[2 + 3 * sizeof(int)];
itoa(num, buf, 10);
return concat(buf);
if (!reserve(len + 2 + 8 * sizeof(int))) return false;
itoa(num, buffer + len, 10);
len += strlen(buffer + len);
return true;
}

bool String::concat(unsigned int num)
{
char buf[1 + 3 * sizeof(unsigned int)];
utoa(num, buf, 10);
return concat(buf);
if (!reserve(len + 1 + 8 * sizeof(unsigned int))) return false;
utoa(num, buffer + len, 10);
len += strlen(buffer + len);
return true;
}

bool String::concat(long num)
{
char buf[2 + 3 * sizeof(long)];
ltoa(num, buf, 10);
return concat(buf);
if (!reserve(len + 2 + 8 * sizeof(long))) return false;
ltoa(num, buffer + len, 10);
len += strlen(buffer + len);
return true;
}

bool String::concat(unsigned long num)
{
char buf[1 + 3 * sizeof(unsigned long)];
ultoa(num, buf, 10);
return concat(buf);
if (!reserve(len + 1 + 8 * sizeof(unsigned long))) return false;
ultoa(num, buffer + len, 10);
len += strlen(buffer + len);
return true;
}

bool String::concat(float num)
{
char buf[20];
char* string = dtostrf(num, 4, 2, buf);
return concat(string);
if (!reserve(len + FLT_MAX_10_EXP + FLT_MAX_DECIMAL_PLACES + 1 /* '-' */ + 1 /* '.' */)) return false;
dtostrf(num, 4, 2, buffer + len);
len += strlen(buffer + len);
return true;
}

bool String::concat(double num)
{
char buf[20];
char* string = dtostrf(num, 4, 2, buf);
return concat(string);
if (!reserve(len + DBL_MAX_10_EXP + DBL_MAX_DECIMAL_PLACES + 1 /* '-' */ + 1 /* '.' */)) return false;
dtostrf(num, 4, 2, buffer + len);
len += strlen(buffer + len);
return true;
}

bool String::concat(const __FlashStringHelper * str)
Expand Down
45 changes: 23 additions & 22 deletions api/deprecated-avr-comp/avr/dtostrf.c.impl
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
/*
dtostrf - Emulation for dtostrf function from avr-libc
Copyright (c) 2016 Arduino LLC. All rights reserved.

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
dtostrf - Emulation for dtostrf function from avr-libc
Copyright (c) 2016 Arduino LLC. All rights reserved.

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/

// This is a default implementation for dtostrf function.
Expand All @@ -29,12 +29,13 @@
char *dtostrf (double val, signed char width, unsigned char prec, char *sout) {
asm(".global _printf_float");

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
char fmt[20];
sprintf(fmt, "%%%d.%df", width, prec);
sprintf(sout, fmt, val);
int len;
snprintf(fmt, sizeof(fmt), "%%%d.%df", width, prec);
/* sout must be large enough to hold the result (the caller's
* responsibility, as with the original sprintf). Measure the output
* first so the bounded call below can never truncate it. */
len = snprintf(NULL, 0, fmt, val);
snprintf(sout, len + 1, fmt, val);
return sout;
#pragma GCC diagnostic pop
}

94 changes: 58 additions & 36 deletions test/src/itoa.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,56 +9,78 @@
**************************************************************************************/

#include <api/itoa.h>

#include <string>
#include <stdexcept>

#include <stdio.h>

#include <string.h>
/**************************************************************************************
* FUNCTION IMPLEMENTATION
**************************************************************************************/

std::string radixToFmtString(int const radix)
/* Reverse the string s in place (K&R, "The C Programming Language", 2nd ed.). */
static void reverse(char * s)
{
if (radix == 8) return std::string("%o");
else if (radix == 10) return std::string("%d");
else if (radix == 16) return std::string("%X");
else throw std::runtime_error("Invalid radix.");
size_t i = 0;
size_t j = strlen(s);
if (j == 0) {
return;
}
j--;
while (i < j) {
char const c = s[i];
s[i] = s[j];
s[j] = c;
i++;
j--;
}
}

char * itoa(int value, char * str, int radix)
/* Convert an unsigned value to a NUL-terminated string in the given radix
* (2..36) and write it into str. The caller must provide a buffer large
* enough for the result. Digits above 9 use lower-case letters, matching
* avr-libc itoa. */
static char * ultoa_impl(unsigned long value, char * str, int radix)
{
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
sprintf(str, radixToFmtString(radix).c_str(), value);
#pragma GCC diagnostic pop
static char const digits[] = "0123456789abcdefghijklmnopqrstuvwxyz";
char * p = str;
if (radix < 2 || radix > 36) {
str[0] = '\0';
return str;
}
do {
*p++ = digits[value % (unsigned long)radix];
value /= (unsigned long)radix;
} while (value > 0);
*p = '\0';
reverse(str);
return str;
}

char * itoa(int value, char * str, int radix)
{
char * out = str;
if (value < 0 && radix == 10) {
/* Only base 10 gets a '-' sign (avr-libc behaviour). Negate in
* unsigned arithmetic so INT_MIN doesn't overflow. */
*str++ = '-';
ultoa_impl((unsigned long)(-(value + 1)) + 1UL, str, radix);
} else {
ultoa_impl((unsigned long)value, str, radix);
}
return out;
}
char * ltoa(long value, char * str, int radix)
{
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
sprintf(str, radixToFmtString(radix).c_str(), value);
#pragma GCC diagnostic pop
return str;
char * out = str;
if (value < 0 && radix == 10) {
/* Only base 10 gets a '-' sign (avr-libc behaviour). Negate in
* unsigned arithmetic so LONG_MIN doesn't overflow. */
*str++ = '-';
ultoa_impl((unsigned long)(-(value + 1)) + 1UL, str, radix);
} else {
ultoa_impl((unsigned long)value, str, radix);
}
return out;
}

char * utoa(unsigned value, char *str, int radix)
{
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
sprintf(str, radixToFmtString(radix).c_str(), value);
#pragma GCC diagnostic pop
return str;
return ultoa_impl((unsigned long)value, str, radix);
}

char * ultoa(unsigned long value, char * str, int radix)
{
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
sprintf(str, radixToFmtString(radix).c_str(), value);
#pragma GCC diagnostic pop
return str;
return ultoa_impl(value, str, radix);
}
Loading