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