From 0540deb5ade9677087e59fd46bffce6493810b48 Mon Sep 17 00:00:00 2001 From: Baptiste Wicht Date: Mon, 1 Aug 2016 13:49:40 +0200 Subject: [PATCH] Prepare to_raw_string --- tstl/include/string.hpp | 77 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) diff --git a/tstl/include/string.hpp b/tstl/include/string.hpp index d543e81a..c9aa6b4b 100644 --- a/tstl/include/string.hpp +++ b/tstl/include/string.hpp @@ -627,6 +627,83 @@ inline std::string to_string(const int32_t& value){ return to_string(static_cast(value)); } +template +void to_raw_string(const T& value, char* buffer, size_t n); + +template<> +inline void to_raw_string(const uint64_t& value, char* buffer, size_t n){ + if(n < 20){ + //TODO Print an error ? + return; + } + + if(value == 0){ + buffer[0] = '0'; + buffer[1] = '\0'; + return; + } + + std::string s; + + char int_buffer[20]; + int i = 0; + auto rem = value; + + while(rem != 0){ + int_buffer[i++] = '0' + rem % 10; + rem /= 10; + } + + --i; + + size_t j = 0; + for(; i >= 0; --i){ + buffer[j++] = int_buffer[i]; + } + + buffer[j] = '\0'; +} + +template<> +inline void to_raw_string(const int64_t& value, char* buffer, size_t n){ + if(value < 0){ + *buffer = '-'; + to_raw_string(static_cast(value), buffer + 1, n - 1); + } else { + to_raw_string(static_cast(value), buffer, n); + } +} + +template<> +inline void to_raw_string(const uint8_t& value, char* buffer, size_t n){ + to_raw_string(static_cast(value), buffer, n); +} + +template<> +inline void to_raw_string(const uint16_t& value, char* buffer, size_t n){ + to_raw_string(static_cast(value), buffer, n); +} + +template<> +inline void to_raw_string(const uint32_t& value, char* buffer, size_t n){ + to_raw_string(static_cast(value), buffer, n); +} + +template<> +inline void to_raw_string(const int8_t& value, char* buffer, size_t n){ + to_raw_string(static_cast(value), buffer, n); +} + +template<> +inline void to_raw_string(const int16_t& value, char* buffer, size_t n){ + to_raw_string(static_cast(value), buffer, n); +} + +template<> +inline void to_raw_string(const int32_t& value, char* buffer, size_t n){ + to_raw_string(static_cast(value), buffer, n); +} + } //end of namespace std #endif