From 448ad42961ae22db966c10344609c5d3b0a3ecf6 Mon Sep 17 00:00:00 2001 From: Tomasz Sowa Date: Wed, 3 Mar 2021 02:24:30 +0100 Subject: [PATCH] added: Toa() methods for converting to a string (new file convert/inttostr.cpp): template void Toa(unsigned long long value, StringType & res, bool clear_string = true, int base = 10); template void Toa(long long value, StringType & res, bool clear_string = true, int base = 10); template void Toa(unsigned long value, StringType & res, bool clear_string = true, int base = 10); template void Toa(long value, StringType & res, bool clear_string = true, int base = 10); template void Toa(unsigned int value, StringType & res, bool clear_string = true, int base = 10); template void Toa(int value, StringType & res, bool clear_string = true, int base = 10); template void Toa(unsigned short value, StringType & res, bool clear_string = true, int base = 10); template void Toa(short value, StringType & res, bool clear_string = true, int base = 10); std::wstring Toa(unsigned long long value, int base); std::wstring Toa(long long value, int base); std::wstring Toa(unsigned long value, int base); std::wstring Toa(long value, int base); std::wstring Toa(unsigned int value, int base); std::wstring Toa(int value, int base); std::wstring Toa(unsigned short value, int base); std::wstring Toa(short value, int base); --- convert/Makefile.dep | 1 + convert/Makefile.o.dep | 2 +- convert/inttostr.cpp | 100 +++++++++++++++++++++++++++++++++++++++++ convert/inttostr.h | 97 ++++++++++++++++++++++++++++++++++++++- 4 files changed, 198 insertions(+), 2 deletions(-) create mode 100644 convert/inttostr.cpp diff --git a/convert/Makefile.dep b/convert/Makefile.dep index de285f0..35b219c 100644 --- a/convert/Makefile.dep +++ b/convert/Makefile.dep @@ -1,4 +1,5 @@ # DO NOT DELETE +inttostr.o: inttostr.h misc.o: misc.h text.h text.o: text.h diff --git a/convert/Makefile.o.dep b/convert/Makefile.o.dep index 9fe3548..70b7e42 100644 --- a/convert/Makefile.o.dep +++ b/convert/Makefile.o.dep @@ -1 +1 @@ -o = misc.o text.o \ No newline at end of file +o = inttostr.o misc.o text.o \ No newline at end of file diff --git a/convert/inttostr.cpp b/convert/inttostr.cpp new file mode 100644 index 0000000..b4ae639 --- /dev/null +++ b/convert/inttostr.cpp @@ -0,0 +1,100 @@ +/* + * This file is a part of PikoTools + * and is distributed under the (new) BSD licence. + * Author: Tomasz Sowa + */ + +/* + * Copyright (c) 2021, Tomasz Sowa + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * * Neither the name Tomasz Sowa nor the names of contributors to this + * project may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF + * THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "inttostr.h" + + +namespace PT +{ + + +std::wstring Toa(unsigned long long value, int base) +{ + std::wstring res; + Toa(value, res, false, base); + + return res; +} + + +std::wstring Toa(long long value, int base) +{ + std::wstring res; + Toa(value, res, false, base); + + return res; +} + + +std::wstring Toa(unsigned long value, int base) +{ + return Toa(static_cast(value), base); +} + + +std::wstring Toa(long value, int base) +{ + return Toa(static_cast(value), base); +} + + +std::wstring Toa(unsigned int value, int base) +{ + return Toa(static_cast(value), base); +} + + +std::wstring Toa(int value, int base) +{ + return Toa(static_cast(value), base); +} + + +std::wstring Toa(unsigned short value, int base) +{ + return Toa(static_cast(value), base); +} + + +std::wstring Toa(short value, int base) +{ + return Toa(static_cast(value), base); +} + + +} + diff --git a/convert/inttostr.h b/convert/inttostr.h index c6a4dea..78e7d58 100644 --- a/convert/inttostr.h +++ b/convert/inttostr.h @@ -5,7 +5,7 @@ */ /* - * Copyright (c) 2012-2017, Tomasz Sowa + * Copyright (c) 2012-2021, Tomasz Sowa * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -38,6 +38,7 @@ #ifndef headerfile_picotools_convert_inttostr #define headerfile_picotools_convert_inttostr +#include @@ -188,6 +189,100 @@ bool Toa(short value, CharType * buffer, size_t buf_len, int base = 10, size_t * + + +template +void Toa(unsigned long long value, StringType & res, bool clear_string = true, int base = 10) +{ + typename StringType::value_type buffer[50]; + size_t buffer_len = sizeof(buffer) / sizeof(wchar_t); + + if( clear_string ) + res.clear(); + + /* + * the size of the buffer is sufficient so the status should always be true + */ + size_t len_out; + Toa(value, buffer, buffer_len, base, &len_out); + res.append(buffer, len_out); +} + + +template +void Toa(long long value, StringType & res, bool clear_string = true, int base = 10) +{ + typename StringType::value_type buffer[50]; + size_t buffer_len = sizeof(buffer) / sizeof(wchar_t); + + if( clear_string ) + res.clear(); + + /* + * the size of the buffer is sufficient so the status should always be true + */ + size_t len_out; + Toa(value, buffer, buffer_len, base, &len_out); + res.append(buffer, len_out); +} + + +template +void Toa(unsigned long value, StringType & res, bool clear_string = true, int base = 10) +{ + Toa(static_cast(value), res, clear_string, base); +} + + +template +void Toa(long value, StringType & res, bool clear_string = true, int base = 10) +{ + Toa(static_cast(value), res, clear_string, base); +} + + +template +void Toa(unsigned int value, StringType & res, bool clear_string = true, int base = 10) +{ + Toa(static_cast(value), res, clear_string, base); +} + + +template +void Toa(int value, StringType & res, bool clear_string = true, int base = 10) +{ + Toa(static_cast(value), res, clear_string, base); +} + + +template +void Toa(unsigned short value, StringType & res, bool clear_string = true, int base = 10) +{ + Toa(static_cast(value), res, clear_string, base); +} + + +template +void Toa(short value, StringType & res, bool clear_string = true, int base = 10) +{ + Toa(static_cast(value), res, clear_string, base); +} + + + +std::wstring Toa(unsigned long long value, int base = 10); +std::wstring Toa(long long value, int base = 10); +std::wstring Toa(unsigned long value, int base = 10); +std::wstring Toa(long value, int base = 10); +std::wstring Toa(unsigned int value, int base = 10); +std::wstring Toa(int value, int base = 10); +std::wstring Toa(unsigned short value, int base = 10); +std::wstring Toa(short value, int base = 10); + + + + + }