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

Last change on this file since 378 was 378, checked in by epyon, 10 years ago
  • important fix in reverse_iterator operators
  • std::string functions removed or converted to string_ref in string.hh
  • capi.hh - more function forwards and usage of them instead of libc
  • more std removal
  • assert fix
File size: 3.9 KB
Line 
1// Copyright (C) 2015 ChaosForge Ltd
2// http://chaosforge.org/
3//
4// This file is part of NV Libraries.
5// For conditions of distribution and use, see copyright notice in nv.hh
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 (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 (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 (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        sprintf_s( str, 64, "%.*g", 6, n );
100#else
101        snprintf( str, 64, "%.*g", 6, n );
102#endif
103        sprintf( str, "%g", n );
104        return strlen( str );
105}
106
107nv::size_t nv::f64_to_buffer( f64 n, char* str )
108{
109#if NV_COMPILER == NV_MSVC
110        sprintf_s( str, 64, "%.*g", 6, n );
111#else
112        snprintf( str, 64, "%.*g", 6, n );
113#endif
114        return strlen( str );
115}
116
117sint32 buffer_to_sint32( const char* str, char** end )
118{
119        const char* s = str;
120        while ( *s == ' ' ) ++s;
121        sint32 result = 0;
122        bool positive = true;
123        switch ( *s )
124        {
125        case '-': ++s; positive = false; break;
126        case '+': ++s; break;
127        default: break;
128        }
129        while ( *s >= '0' && *s <= '9' )
130        {
131                result = ( result * 10 ) + ( *s - '0' );
132                ++s;
133        }
134        if ( end != nullptr ) *end = (char*)s;
135        return positive ? result : -result;
136}
137
138sint64 buffer_to_sint64( const char* s, char** end )
139{
140        while ( *s == ' ' ) ++s;
141        sint64 result = 0;
142        bool positive = true;
143        switch ( *s )
144        {
145        case '-': ++s; positive = false; break;
146        case '+': ++s; break;
147        default: break;
148        }
149        while ( *s >= '0' && *s <= '9' )
150        {
151                result = ( result * 10 ) + ( *s - '0' );
152                ++s;
153        }
154        if ( end != nullptr ) *end = (char*)s;
155        return positive ? result : -result;
156}
157
158uint32 buffer_to_uint32( const char* s, char** end )
159{
160        while ( *s == ' ' ) ++s;
161        uint32 result = 0;
162        while ( *s >= '0' && *s <= '9' )
163        {
164                result = ( result * 10 ) + ( *s - '0' );
165                ++s;
166        }
167        if ( end != nullptr ) *end = (char*)s;
168        return result;
169}
170
171uint64 buffer_to_uint64( const char* s, char** end )
172{
173        while ( *s == ' ' ) ++s;
174        uint64 result = 0;
175        while ( *s >= '0' && *s <= '9' )
176        {
177                result = ( result * 10 ) + ( *s - '0' );
178                ++s;
179        }
180        if ( end != nullptr ) *end = (char*)s;
181        return result;
182}
183
184float buffer_to_f32( const char* s, char** end )
185{
186        return strtof( s, end );
187}
188
189double buffer_to_f64( const char* s, char** end )
190{
191        return strtod( s, end );
192}
Note: See TracBrowser for help on using the repository browser.