tools

various tools
git clone git://deadbeef.fr/tools.git
Log | Files | Refs | README | LICENSE

commit 869354f878ec5eea53d6448a2541f94caae1b72d
parent 3d499e3c0412d4fb75ee7e095644eca356017dfc
Author: Morel BĂ©renger <berengermorel76@gmail.com>
Date:   Wed, 12 Aug 2020 20:56:40 +0200

typedef vector<char> string + helpers

Diffstat:
Abtl/src/string.cpp | 94+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Abtl/src/string.hpp | 28++++++++++++++++++++++++++++
2 files changed, 122 insertions(+), 0 deletions(-)

diff --git a/btl/src/string.cpp b/btl/src/string.cpp @@ -0,0 +1,94 @@ +#include <utility> +#include <memory> + +#include <string.h> +#include <stddef.h> +#include <stdint.h> +#include <stdlib.h> +#include <stdio.h> +#include <assert.h> + +#include "vector.hpp" +#include "string.hpp" + +string make_string( char const* str ) +{ + string ret; + ret.assign( str, str + strlen( str ) ); + return ret; +} + +string make_string( wchar_t const* str ) +{ + string ret; + size_t str_sz = wcslen( str ); + std::unique_ptr<char> temp( static_cast<char*>( malloc( str_sz +1 ) ) ); + size_t sz = wcstombs( temp.get(), str, str_sz ); + assert( sz == str_sz ); + ret.assign( temp.get(), temp.get() + sz ); + return ret; +} + +string to_string( int32_t i ) +{ + int n = snprintf( nullptr, 0, "%d", i ); + assert( n >= 0 ); + ++n; + size_t sz = static_cast<size_t>( n ); + string ret; + if( ret.resize( sz ) ) + { + return ret; + } + snprintf( ret.data(), sz, "%d", i ); + ret.pop_back(); + return ret; +} + +string to_string( uint32_t i ) +{ + int n = snprintf( nullptr, 0, "%u", i ); + assert( n >= 0 ); + ++n; + size_t sz = static_cast<size_t>( n ); + string ret; + if( ret.resize( sz ) ) + { + return ret; + } + snprintf( ret.data(), sz, "%u", i ); + ret.pop_back(); + return ret; +} + +string& operator+=( string& lhs, char const* rhs ) +{ + append( lhs, rhs, rhs + strlen( rhs ) ); + return lhs; +} + +string& operator+=( string& lhs, char rhs ) +{ + append( lhs, rhs ); + return lhs; +} + +string& operator+=( string& lhs, string const& rhs ) +{ + append( lhs, rhs ); + return lhs; +} + +string& operator+=( string& lhs, string && rhs ) +{ + append( lhs, rhs ); + return lhs; +} + +string operator+( char const* lhs, string const& rhs ) +{ + string ret; + ret.assign( lhs, lhs + strlen( lhs ) ); + ret += rhs; + return ret; +} diff --git a/btl/src/string.hpp b/btl/src/string.hpp @@ -0,0 +1,28 @@ +#ifndef STRING_HPP +#define STRING_HPP + +typedef vector<char> string; + +string make_string( char const* str ); +string make_string( wchar_t const* str ); + +string to_string( int32_t i ); +string to_string( uint32_t i ); + +string& operator+=( string& lhs, char const* rhs ); +string& operator+=( string& lhs, char rhs ); +string& operator+=( string& lhs, string const& rhs ); +string& operator+=( string& lhs, string && rhs ); + +//TODO: make a template +template <typename T> +string operator+( string const& lhs, T rhs ) +{ + string ret( lhs ); + ret += rhs; + return ret; +} + +string operator+( char const* lhs, string const& rhs ); + +#endif