// Copyright (C) 2015 ChaosForge Ltd // http://chaosforge.org/ // // This file is part of NV Libraries. // For conditions of distribution and use, see copyright notice in nv.hh // // TODO: speedup conversion by doing divisions by 100 #include "nv/stl/string.hh" #include #include using namespace nv; static const double s_power_10[] = { 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000 }; static inline void string_reverse( char* begin, char* end ) { char temp; while ( end > begin ) { temp = *end; *end-- = *begin; *begin++ = temp; } } size_t nv::sint32_to_buffer( sint32 n, char* str ) { char* s = str; uint32 abs = ( n < 0 ) ? (uint32)( -n ) : (uint32)( n ); do { *s++ = (char)( '0' + ( abs % 10 ) ); abs /= 10; } while ( abs > 0 ); if ( n < 0 ) *s++ = '-'; *s = '\0'; string_reverse( str, s - 1 ); return (size_t)( s - str ); } size_t nv::sint64_to_buffer( sint64 n, char* str ) { char* s = str; uint64 abs = ( n < 0 ) ? (uint64)( -n ) : (uint64)( n ); do { *s++ = (char)( '0' + ( abs % 10 ) ); abs /= 10; } while ( abs > 0 ); if ( n < 0 ) *s++ = '-'; *s = '\0'; string_reverse( str, s - 1 ); return (size_t)( s - str ); } size_t nv::uint32_to_buffer( uint32 n, char* str ) { char* s = str; do { *s++ = (char)( '0' + ( n % 10 ) ); n /= 10; } while ( n > 0 ); *s = '\0'; string_reverse( str, s - 1 ); return (size_t)( s - str ); } size_t nv::uint64_to_buffer( uint64 n, char* str ) { char* s = str; do { *s++ = (char)( '0' + ( n % 10 ) ); n /= 10; } while ( n > 0 ); *s = '\0'; string_reverse( str, s - 1 ); return (size_t)( s - str ); } size_t nv::f32_to_buffer( f32 n, char* str ) { #if NV_COMPILER == NV_MSVC sprintf_s( str, 64, "%.*g", 6, n ); #else snprintf( str, 64, "%.*g", 6, n ); #endif sprintf( str, "%g", n ); return strlen( str ); } size_t nv::f64_to_buffer( f64 n, char* str ) { #if NV_COMPILER == NV_MSVC sprintf_s( str, 64, "%.*g", 6, n ); #else snprintf( str, 64, "%.*g", 6, n ); #endif return strlen( str ); } sint32 buffer_to_sint32( const char* str, char** end ) { const char* s = str; while ( *s == ' ' ) ++s; sint32 result = 0; bool positive = true; switch ( *s ) { case '-': ++s; positive = false; break; case '+': ++s; break; default: break; } while ( *s >= '0' && *s <= '9' ) { result = ( result * 10 ) + ( *s - '0' ); ++s; } if ( end != nullptr ) *end = (char*)s; return positive ? result : -result; } sint64 buffer_to_sint64( const char* s, char** end ) { while ( *s == ' ' ) ++s; sint64 result = 0; bool positive = true; switch ( *s ) { case '-': ++s; positive = false; break; case '+': ++s; break; default: break; } while ( *s >= '0' && *s <= '9' ) { result = ( result * 10 ) + ( *s - '0' ); ++s; } if ( end != nullptr ) *end = (char*)s; return positive ? result : -result; } uint32 buffer_to_uint32( const char* s, char** end ) { while ( *s == ' ' ) ++s; uint32 result = 0; while ( *s >= '0' && *s <= '9' ) { result = ( result * 10 ) + ( *s - '0' ); ++s; } if ( end != nullptr ) *end = (char*)s; return result; } uint64 buffer_to_uint64( const char* s, char** end ) { while ( *s == ' ' ) ++s; uint64 result = 0; while ( *s >= '0' && *s <= '9' ) { result = ( result * 10 ) + ( *s - '0' ); ++s; } if ( end != nullptr ) *end = (char*)s; return result; } float buffer_to_f32( const char* s, char** end ) { return strtof( s, end ); } double buffer_to_f64( const char* s, char** end ) { return strtod( s, end ); }