source: trunk/src/stl/string.cc @ 395

Last change on this file since 395 was 395, checked in by epyon, 10 years ago
  • bulk update copyright update include guards cleanup core/common.hh -> common.hh minor cleanups
File size: 4.1 KB
Line 
1// Copyright (C) 2015 ChaosForge Ltd
2// http://chaosforge.org/
3//
4// This file is part of Nova libraries.
5// For conditions of distribution and use, see copying.txt file in root folder.
6//
7// TODO: speedup conversion by doing divisions by 100
8
9#include "nv/stl/string.hh"
10
11#include <cstdio>
12#include <cstdlib>
13#include <fstream> // for slurp only
14
15using namespace nv;
16
17static const double s_power_10[] = { 1, 10, 100, 1000, 10000, 100000, 1000000,
1810000000, 100000000, 1000000000 };
19
20std::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
29static 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
40nv::size_t nv::sint32_to_buffer( sint32 n, char* str )
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 );
52        return (nv::size_t)( s - str );
53}
54
55nv::size_t nv::sint64_to_buffer( sint64 n, char* str )
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 );
67        return (nv::size_t)( s - str );
68}
69
70nv::size_t nv::uint32_to_buffer( uint32 n, char* str )
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 );
80        return (nv::size_t)( s - str );
81}
82
83nv::size_t nv::uint64_to_buffer( uint64 n, char* str )
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
96nv::size_t nv::f32_to_buffer( f32 n, char* str )
97{
98#if NV_COMPILER == NV_MSVC
99        int result = sprintf_s( str, 64, "%.*g", 6, n );
100#else
101        int result = snprintf( str, 64, "%.*g", 6, n );
102#endif
103        return result > 0 ? ( nv::size_t )result : 0;
104}
105
106nv::size_t nv::f64_to_buffer( f64 n, char* str )
107{
108#if NV_COMPILER == NV_MSVC
109        int result = sprintf_s( str, 64, "%.*g", 6, n );
110#else
111        int result = snprintf( str, 64, "%.*g", 6, n );
112#endif
113        return result > 0 ? ( nv::size_t )result : 0;
114}
115
116sint32 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
137sint64 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
157uint32 buffer_to_uint32( const char* s, char** end )
158{
159        while ( *s == ' ' ) ++s;
160        uint32 result = 0;
161        while ( *s >= '0' && *s <= '9' )
162        {
163                result = ( result * 10 ) + (uint32)( *s - '0' );
164                ++s;
165        }
166        if ( end != nullptr ) *end = (char*)s;
167        return result;
168}
169
170uint64 buffer_to_uint64( const char* s, char** end )
171{
172        while ( *s == ' ' ) ++s;
173        uint64 result = 0;
174        while ( *s >= '0' && *s <= '9' )
175        {
176                result = ( result * 10 ) + (uint32)( *s - '0' );
177                ++s;
178        }
179        if ( end != nullptr ) *end = (char*)s;
180        return result;
181}
182
183float buffer_to_f32( const char* s, char** end )
184{
185        return strtof( s, end );
186}
187
188double buffer_to_f64( const char* s, char** end )
189{
190        return strtod( s, end );
191}
Note: See TracBrowser for help on using the repository browser.