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

Last change on this file since 428 was 402, checked in by epyon, 10 years ago
  • cleanups of clang warnings (gotta love them all)
  • only nv-core for now (this will take a while for the whole source)
  • mainly C++ casts instead of C-style casts, but also a few bugs fixed!
File size: 4.3 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
17//static const double s_power_10[] = { 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000 };
18
19std::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
28static 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
39nv::size_t nv::sint32_to_buffer( sint32 n, char* str )
40{
41        char* s = str;
42        uint32 abs = static_cast< uint32 >( n < 0 ? -n : n );
43        do
44        {
45                *s++ = static_cast<char>( '0' + ( abs % 10 ) );
46                abs /= 10;
47        } while ( abs > 0 );
48        if ( n < 0 ) *s++ = '-';
49        *s = '\0';
50        string_reverse( str, s - 1 );
51        return static_cast<nv::size_t>( s - str );
52}
53
54nv::size_t nv::sint64_to_buffer( sint64 n, char* str )
55{
56        char* s = str;
57        uint64 abs = static_cast< uint64 >( n < 0 ? -n : n );
58        do
59        {
60                *s++ = static_cast<char>( '0' + ( abs % 10 ) );
61                abs /= 10;
62        } while ( abs > 0 );
63        if ( n < 0 ) *s++ = '-';
64        *s = '\0';
65        string_reverse( str, s - 1 );
66        return static_cast<nv::size_t>( s - str );
67}
68
69nv::size_t nv::uint32_to_buffer( uint32 n, char* str )
70{
71        char* s = str;
72        do
73        {
74                *s++ = static_cast<char>( '0' + ( n % 10 ) );
75                n /= 10;
76        } while ( n > 0 );
77        *s = '\0';
78        string_reverse( str, s - 1 );
79        return static_cast<nv::size_t>( s - str );
80}
81
82nv::size_t nv::uint64_to_buffer( uint64 n, char* str )
83{
84        char* s = str;
85        do
86        {
87                *s++ = static_cast<char>( '0' + ( n % 10 ) );
88                n /= 10;
89        } while ( n > 0 );
90        *s = '\0';
91        string_reverse( str, s - 1 );
92        return static_cast<nv::size_t>( s - str );
93}
94
95nv::size_t nv::f32_to_buffer( f32 n, char* str )
96{
97#if NV_COMPILER == NV_MSVC
98        int result = sprintf_s( str, 64, "%.*g", 6, n );
99#else
100        int result = snprintf( str, 64, "%.*g", 6, n );
101#endif
102        return static_cast<nv::size_t>( result > 0 ? result : 0 );
103}
104
105nv::size_t nv::f64_to_buffer( f64 n, char* str )
106{
107#if NV_COMPILER == NV_MSVC
108        int result = sprintf_s( str, 64, "%.*g", 6, n );
109#else
110        int result = snprintf( str, 64, "%.*g", 6, n );
111#endif
112        return static_cast<nv::size_t>( result > 0 ? result : 0 );
113}
114
115sint32 nv::buffer_to_sint32( const char* str, char** end )
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        }
132        if ( end != nullptr ) *end = const_cast<char*>( s );
133        return positive ? result : -result;
134}
135
136sint64 nv::buffer_to_sint64( const char* s, char** end )
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        }
152        if ( end != nullptr ) *end = const_cast<char*>( s );
153        return positive ? result : -result;
154}
155
156uint32 nv::buffer_to_uint32( const char* s, char** end )
157{
158        while ( *s == ' ' ) ++s;
159        uint32 result = 0;
160        while ( *s >= '0' && *s <= '9' )
161        {
162                result = ( result * 10 ) + static_cast<uint32>( *s - '0' );
163                ++s;
164        }
165        if ( end != nullptr ) *end = const_cast<char*>( s );
166        return result;
167}
168
169uint64 nv::buffer_to_uint64( const char* s, char** end )
170{
171        while ( *s == ' ' ) ++s;
172        uint64 result = 0;
173        while ( *s >= '0' && *s <= '9' )
174        {
175                result = ( result * 10 ) + static_cast<uint32>( *s - '0' );
176                ++s;
177        }
178        if ( end != nullptr ) *end = const_cast<char*>( s );
179        return result;
180}
181
182float nv::buffer_to_f32( const char* s, char** end )
183{
184        return strtof( s, end );
185}
186
187double nv::buffer_to_f64( const char* s, char** end )
188{
189        return strtod( s, end );
190}
Note: See TracBrowser for help on using the repository browser.