tools

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

string.cpp (2603B)


      1 // Copyright (c) 2020 Morel BĂ©renger
      2 // 
      3 // This software is provided 'as-is', without any express or implied
      4 // warranty. In no event will the authors be held liable for any damages
      5 // arising from the use of this software.
      6 // 
      7 // Permission is granted to anyone to use this software for any purpose,
      8 // including commercial applications, and to alter it and redistribute it
      9 // freely, subject to the following restrictions:
     10 // 
     11 // 1. The origin of this software must not be misrepresented; you must not
     12 //    claim that you wrote the original software. If you use this software
     13 //    in a product, an acknowledgment in the product documentation would be
     14 //    appreciated but is not required.
     15 // 2. Altered source versions must be plainly marked as such, and must not be
     16 //    misrepresented as being the original software.
     17 // 3. This notice may not be removed or altered from any source distribution.
     18 
     19 #include <utility>
     20 
     21 #include <string.h>
     22 #include <stddef.h>
     23 #include <stdint.h>
     24 #include <stdlib.h>
     25 #include <stdio.h>
     26 #include <assert.h>
     27 #include <wchar.h>
     28 
     29 #include "vector.hpp"
     30 #include "string.hpp"
     31 #include "memory.hpp"
     32 
     33 string make_string( char const* str )
     34 {
     35 	string ret;
     36 	ret.assign( str, str + strlen( str ) );
     37 	return ret;
     38 }
     39 
     40 string make_string( wchar_t const* str )
     41 {
     42 	string ret;
     43 	size_t str_sz = wcslen( str );
     44 	unique_res<char*,nullptr,generic_free> temp( static_cast<char*>(  malloc( str_sz +1 ) ) );
     45 	size_t sz = wcstombs( temp.get(), str, str_sz );
     46 	assert( sz == str_sz );
     47 	ret.assign( temp.get(), temp.get() + sz );
     48 	return ret;
     49 }
     50 
     51 string to_string( int32_t i )
     52 {
     53 	int n = snprintf( nullptr, 0, "%d", i );
     54 	assert( n >= 0 );
     55 	++n;
     56 	size_t sz = static_cast<size_t>( n );
     57 	string ret;
     58 	if( ret.resize( sz ) )
     59 	{
     60 		return ret;
     61 	}
     62 	snprintf( ret.data(), sz, "%d", i );
     63 	ret.pop_back();
     64 	return ret;
     65 }
     66 
     67 string to_string( uint32_t i )
     68 {
     69 	int n = snprintf( nullptr, 0, "%u", i );
     70 	assert( n >= 0 );
     71 	++n;
     72 	size_t sz = static_cast<size_t>( n );
     73 	string ret;
     74 	if( ret.resize( sz ) )
     75 	{
     76 		return ret;
     77 	}
     78 	snprintf( ret.data(), sz, "%u", i );
     79 	ret.pop_back();
     80 	return ret;
     81 }
     82 
     83 string& operator+=( string& lhs, char const* rhs )
     84 {
     85 	append( lhs, rhs, rhs + strlen( rhs ) );
     86 	return lhs;
     87 }
     88 
     89 string& operator+=( string& lhs, char rhs )
     90 {
     91 	append( lhs, rhs );
     92 	return lhs;
     93 }
     94 
     95 string& operator+=( string& lhs, string const& rhs )
     96 {
     97 	append( lhs, rhs );
     98 	return lhs;
     99 }
    100 
    101 string& operator+=( string& lhs, string && rhs )
    102 {
    103 	append( lhs, rhs );
    104 	return lhs;
    105 }
    106 
    107 string operator+( char const* lhs, string const& rhs )
    108 {
    109 	string ret;
    110 	ret.assign( lhs, lhs + strlen( lhs ) );
    111 	ret += rhs;
    112 	return ret;
    113 }