[365] | 1 | // Copyright (C) 2015 ChaosForge Ltd
|
---|
| 2 | // http://chaosforge.org/
|
---|
| 3 | //
|
---|
[395] | 4 | // This file is part of Nova libraries.
|
---|
| 5 | // For conditions of distribution and use, see copying.txt file in root folder.
|
---|
[365] | 6 | //
|
---|
| 7 | // TODO: speedup conversion by doing divisions by 100
|
---|
| 8 |
|
---|
[368] | 9 | #include "nv/stl/string.hh"
|
---|
[365] | 10 |
|
---|
| 11 | #include <cstdio>
|
---|
| 12 | #include <cstdlib>
|
---|
[378] | 13 | #include <fstream> // for slurp only
|
---|
[365] | 14 |
|
---|
| 15 | using namespace nv;
|
---|
| 16 |
|
---|
[402] | 17 | //static const double s_power_10[] = { 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000 };
|
---|
[365] | 18 |
|
---|
[378] | 19 | std::string nv::slurp( const std::string& filename )
|
---|
| 20 | {
|
---|
| 21 | std::ifstream input( filename );
|
---|
| 22 | // if ( !input.is_open() ) NV_THROW( std::runtime_error, "File "+filename+" not found!");
|
---|
| 23 | std::stringstream sstr;
|
---|
| 24 | while ( input >> sstr.rdbuf() );
|
---|
| 25 | return sstr.str();
|
---|
| 26 | }
|
---|
| 27 |
|
---|
[365] | 28 | static inline void string_reverse( char* begin, char* end )
|
---|
| 29 | {
|
---|
| 30 | char temp;
|
---|
| 31 | while ( end > begin )
|
---|
| 32 | {
|
---|
| 33 | temp = *end;
|
---|
| 34 | *end-- = *begin;
|
---|
| 35 | *begin++ = temp;
|
---|
| 36 | }
|
---|
| 37 | }
|
---|
| 38 |
|
---|
[374] | 39 | nv::size_t nv::sint32_to_buffer( sint32 n, char* str )
|
---|
[365] | 40 | {
|
---|
| 41 | char* s = str;
|
---|
[402] | 42 | uint32 abs = static_cast< uint32 >( n < 0 ? -n : n );
|
---|
[365] | 43 | do
|
---|
| 44 | {
|
---|
[402] | 45 | *s++ = static_cast<char>( '0' + ( abs % 10 ) );
|
---|
[365] | 46 | abs /= 10;
|
---|
| 47 | } while ( abs > 0 );
|
---|
| 48 | if ( n < 0 ) *s++ = '-';
|
---|
| 49 | *s = '\0';
|
---|
| 50 | string_reverse( str, s - 1 );
|
---|
[402] | 51 | return static_cast<nv::size_t>( s - str );
|
---|
[365] | 52 | }
|
---|
| 53 |
|
---|
[374] | 54 | nv::size_t nv::sint64_to_buffer( sint64 n, char* str )
|
---|
[365] | 55 | {
|
---|
| 56 | char* s = str;
|
---|
[402] | 57 | uint64 abs = static_cast< uint64 >( n < 0 ? -n : n );
|
---|
[365] | 58 | do
|
---|
| 59 | {
|
---|
[402] | 60 | *s++ = static_cast<char>( '0' + ( abs % 10 ) );
|
---|
[365] | 61 | abs /= 10;
|
---|
| 62 | } while ( abs > 0 );
|
---|
| 63 | if ( n < 0 ) *s++ = '-';
|
---|
| 64 | *s = '\0';
|
---|
| 65 | string_reverse( str, s - 1 );
|
---|
[402] | 66 | return static_cast<nv::size_t>( s - str );
|
---|
[365] | 67 | }
|
---|
| 68 |
|
---|
[374] | 69 | nv::size_t nv::uint32_to_buffer( uint32 n, char* str )
|
---|
[365] | 70 | {
|
---|
| 71 | char* s = str;
|
---|
| 72 | do
|
---|
| 73 | {
|
---|
[402] | 74 | *s++ = static_cast<char>( '0' + ( n % 10 ) );
|
---|
[365] | 75 | n /= 10;
|
---|
| 76 | } while ( n > 0 );
|
---|
| 77 | *s = '\0';
|
---|
| 78 | string_reverse( str, s - 1 );
|
---|
[402] | 79 | return static_cast<nv::size_t>( s - str );
|
---|
[365] | 80 | }
|
---|
| 81 |
|
---|
[374] | 82 | nv::size_t nv::uint64_to_buffer( uint64 n, char* str )
|
---|
[365] | 83 | {
|
---|
| 84 | char* s = str;
|
---|
| 85 | do
|
---|
| 86 | {
|
---|
[402] | 87 | *s++ = static_cast<char>( '0' + ( n % 10 ) );
|
---|
[365] | 88 | n /= 10;
|
---|
| 89 | } while ( n > 0 );
|
---|
| 90 | *s = '\0';
|
---|
| 91 | string_reverse( str, s - 1 );
|
---|
[402] | 92 | return static_cast<nv::size_t>( s - str );
|
---|
[365] | 93 | }
|
---|
| 94 |
|
---|
[374] | 95 | nv::size_t nv::f32_to_buffer( f32 n, char* str )
|
---|
[365] | 96 | {
|
---|
| 97 | #if NV_COMPILER == NV_MSVC
|
---|
[380] | 98 | int result = sprintf_s( str, 64, "%.*g", 6, n );
|
---|
[365] | 99 | #else
|
---|
[380] | 100 | int result = snprintf( str, 64, "%.*g", 6, n );
|
---|
[365] | 101 | #endif
|
---|
[402] | 102 | return static_cast<nv::size_t>( result > 0 ? result : 0 );
|
---|
[365] | 103 | }
|
---|
| 104 |
|
---|
[374] | 105 | nv::size_t nv::f64_to_buffer( f64 n, char* str )
|
---|
[365] | 106 | {
|
---|
| 107 | #if NV_COMPILER == NV_MSVC
|
---|
[380] | 108 | int result = sprintf_s( str, 64, "%.*g", 6, n );
|
---|
[365] | 109 | #else
|
---|
[380] | 110 | int result = snprintf( str, 64, "%.*g", 6, n );
|
---|
[365] | 111 | #endif
|
---|
[402] | 112 | return static_cast<nv::size_t>( result > 0 ? result : 0 );
|
---|
[365] | 113 | }
|
---|
| 114 |
|
---|
[402] | 115 | sint32 nv::buffer_to_sint32( const char* str, char** end )
|
---|
[365] | 116 | {
|
---|
| 117 | const char* s = str;
|
---|
| 118 | while ( *s == ' ' ) ++s;
|
---|
| 119 | sint32 result = 0;
|
---|
| 120 | bool positive = true;
|
---|
| 121 | switch ( *s )
|
---|
| 122 | {
|
---|
| 123 | case '-': ++s; positive = false; break;
|
---|
| 124 | case '+': ++s; break;
|
---|
| 125 | default: break;
|
---|
| 126 | }
|
---|
| 127 | while ( *s >= '0' && *s <= '9' )
|
---|
| 128 | {
|
---|
| 129 | result = ( result * 10 ) + ( *s - '0' );
|
---|
| 130 | ++s;
|
---|
| 131 | }
|
---|
[402] | 132 | if ( end != nullptr ) *end = const_cast<char*>( s );
|
---|
[365] | 133 | return positive ? result : -result;
|
---|
| 134 | }
|
---|
| 135 |
|
---|
[402] | 136 | sint64 nv::buffer_to_sint64( const char* s, char** end )
|
---|
[365] | 137 | {
|
---|
| 138 | while ( *s == ' ' ) ++s;
|
---|
| 139 | sint64 result = 0;
|
---|
| 140 | bool positive = true;
|
---|
| 141 | switch ( *s )
|
---|
| 142 | {
|
---|
| 143 | case '-': ++s; positive = false; break;
|
---|
| 144 | case '+': ++s; break;
|
---|
| 145 | default: break;
|
---|
| 146 | }
|
---|
| 147 | while ( *s >= '0' && *s <= '9' )
|
---|
| 148 | {
|
---|
| 149 | result = ( result * 10 ) + ( *s - '0' );
|
---|
| 150 | ++s;
|
---|
| 151 | }
|
---|
[402] | 152 | if ( end != nullptr ) *end = const_cast<char*>( s );
|
---|
[365] | 153 | return positive ? result : -result;
|
---|
| 154 | }
|
---|
| 155 |
|
---|
[402] | 156 | uint32 nv::buffer_to_uint32( const char* s, char** end )
|
---|
[365] | 157 | {
|
---|
| 158 | while ( *s == ' ' ) ++s;
|
---|
| 159 | uint32 result = 0;
|
---|
| 160 | while ( *s >= '0' && *s <= '9' )
|
---|
| 161 | {
|
---|
[402] | 162 | result = ( result * 10 ) + static_cast<uint32>( *s - '0' );
|
---|
[365] | 163 | ++s;
|
---|
| 164 | }
|
---|
[402] | 165 | if ( end != nullptr ) *end = const_cast<char*>( s );
|
---|
[365] | 166 | return result;
|
---|
| 167 | }
|
---|
| 168 |
|
---|
[402] | 169 | uint64 nv::buffer_to_uint64( const char* s, char** end )
|
---|
[365] | 170 | {
|
---|
| 171 | while ( *s == ' ' ) ++s;
|
---|
| 172 | uint64 result = 0;
|
---|
| 173 | while ( *s >= '0' && *s <= '9' )
|
---|
| 174 | {
|
---|
[402] | 175 | result = ( result * 10 ) + static_cast<uint32>( *s - '0' );
|
---|
[365] | 176 | ++s;
|
---|
| 177 | }
|
---|
[402] | 178 | if ( end != nullptr ) *end = const_cast<char*>( s );
|
---|
[365] | 179 | return result;
|
---|
| 180 | }
|
---|
| 181 |
|
---|
[402] | 182 | float nv::buffer_to_f32( const char* s, char** end )
|
---|
[365] | 183 | {
|
---|
| 184 | return strtof( s, end );
|
---|
| 185 | }
|
---|
| 186 |
|
---|
[402] | 187 | double nv::buffer_to_f64( const char* s, char** end )
|
---|
[365] | 188 | {
|
---|
| 189 | return strtod( s, end );
|
---|
| 190 | }
|
---|