Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Use itoa from samd core to replace use of deprecated sprintf call#211

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
jboynes wants to merge2 commits intoarduino:master
base:master
Choose a base branch
Loading
fromjboynes:undeprecate_itoa
Open
Changes fromall 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
149 changes: 105 additions & 44 deletionstest/src/itoa.cpp
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,123 @@
/*
* Copyright (c) 2020 Arduino. All rights reserved.
*/
Copyright (c) 2014 Arduino LLC. All right reserved.

/**************************************************************************************
* INCLUDE
**************************************************************************************/
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.

#include <api/itoa.h>
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.

#include <string>
#include <stdexcept>
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
*/

#include <stdio.h>
#ifdef __cplusplus
extern "C" {
#endif

/**************************************************************************************
* FUNCTION IMPLEMENTATION
**************************************************************************************/

std::string radixToFmtString(int const radix)
char* ltoa( long value, char *string, int radix )
{
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.");
}
char tmp[33];
char *tp = tmp;
unsigned long i;
unsigned long v;
int sign;
char *sp;

char * itoa(int 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;
if ( string == nullptr )
{
return nullptr ;
}

if (radix > 36 || radix <= 1)
{
return nullptr ;
}

sign = (radix == 10 && value < 0);
if (sign)
{
v = -value;
}
else
{
v = (unsigned long)value;
}

while (v || tp == tmp)
{
i = v % radix;
v = v / radix;
if (i < 10)
*tp++ = static_cast<char>(i+'0');
else
*tp++ = static_cast<char>(i + 'a' - 10);
}

sp = string;

if (sign)
*sp++ = '-';
while (tp > tmp)
*sp++ = *--tp;
*sp = 0;

return string;
}

char * ltoa(long value, char * str, int radix)
char* ultoa( unsignedlong value, char *string, 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 tmp[33];
char *tp = tmp;
unsigned long i;
unsigned long v = value;
char *sp;

if ( string == nullptr )
{
return nullptr;
}

if (radix > 36 || radix <= 1)
{
return nullptr;
}

while (v || tp == tmp)
{
i = v % radix;
v = v / radix;
if (i < 10)
*tp++ = static_cast<char>(i+'0');
else
*tp++ = static_cast<char>(i + 'a' - 10);
}

sp = string;


while (tp > tmp)
*sp++ = *--tp;
*sp = 0;

return string;
}

char * utoa(unsignedvalue, char *str, int radix)
char* itoa( intvalue, char *string, 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 ltoa( value, string, radix ) ;
}

char * ultoa(unsignedlong value, char * str, int radix)
char* utoa(unsignedint value, char *string, 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( value, string, radix ) ;
}

#ifdef __cplusplus
} // extern "C"
#endif

[8]ページ先頭

©2009-2025 Movatter.jp