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

Last change on this file since 368 was 368, checked in by epyon, 10 years ago
  • massive restructuring
  • detail::data_base class for container/reference class base
File size: 3.6 KB
RevLine 
[365]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
[368]9#include "nv/stl/string.hh"
[365]10
11#include <cstdio>
12#include <cstdlib>
13
14using namespace nv;
15
16static const double s_power_10[] = { 1, 10, 100, 1000, 10000, 100000, 1000000,
1710000000, 100000000, 1000000000 };
18
19static inline void string_reverse( char* begin, char* end )
20{
21        char temp;
22        while ( end > begin )
23        {
24                temp = *end;
25                *end-- = *begin;
26                *begin++ = temp;
27        }
28}
29
30size_t nv::sint32_to_buffer( sint32 n, char* str )
31{
32        char* s = str;
33        uint32 abs = ( n < 0 ) ? (uint32)( -n ) : (uint32)( n );
34        do
35        {
36                *s++ = (char)( '0' + ( abs % 10 ) );
37                abs /= 10;
38        } while ( abs > 0 );
39        if ( n < 0 ) *s++ = '-';
40        *s = '\0';
41        string_reverse( str, s - 1 );
42        return (size_t)( s - str );
43}
44
45size_t nv::sint64_to_buffer( sint64 n, char* str )
46{
47        char* s = str;
48        uint64 abs = ( n < 0 ) ? (uint64)( -n ) : (uint64)( n );
49        do
50        {
51                *s++ = (char)( '0' + ( abs % 10 ) );
52                abs /= 10;
53        } while ( abs > 0 );
54        if ( n < 0 ) *s++ = '-';
55        *s = '\0';
56        string_reverse( str, s - 1 );
57        return (size_t)( s - str );
58}
59
60size_t nv::uint32_to_buffer( uint32 n, char* str )
61{
62        char* s = str;
63        do
64        {
65                *s++ = (char)( '0' + ( n % 10 ) );
66                n /= 10;
67        } while ( n > 0 );
68        *s = '\0';
69        string_reverse( str, s - 1 );
70        return (size_t)( s - str );
71}
72
73size_t nv::uint64_to_buffer( uint64 n, char* str )
74{
75        char* s = str;
76        do
77        {
78                *s++ = (char)( '0' + ( n % 10 ) );
79                n /= 10;
80        } while ( n > 0 );
81        *s = '\0';
82        string_reverse( str, s - 1 );
83        return (size_t)( s - str );
84}
85
86size_t nv::f32_to_buffer( f32 n, char* str )
87{
88#if NV_COMPILER == NV_MSVC
89        sprintf_s( str, 64, "%.*g", 6, n );
90#else
[367]91        snprintf( str, 64, "%.*g", 6, n );
[365]92#endif
93        sprintf( str, "%g", n );
94        return strlen( str );
95}
96
97size_t nv::f64_to_buffer( f64 n, char* str )
98{
99#if NV_COMPILER == NV_MSVC
100        sprintf_s( str, 64, "%.*g", 6, n );
101#else
[367]102        snprintf( str, 64, "%.*g", 6, n );
[365]103#endif
104        return strlen( str );
105}
106
107sint32 buffer_to_sint32( const char* str, char** end )
108{
109        const char* s = str;
110        while ( *s == ' ' ) ++s;
111        sint32 result = 0;
112        bool positive = true;
113        switch ( *s )
114        {
115        case '-': ++s; positive = false; break;
116        case '+': ++s; break;
117        default: break;
118        }
119        while ( *s >= '0' && *s <= '9' )
120        {
121                result = ( result * 10 ) + ( *s - '0' );
122                ++s;
123        }
124        if ( end != nullptr ) *end = (char*)s;
125        return positive ? result : -result;
126}
127
128sint64 buffer_to_sint64( const char* s, char** end )
129{
130        while ( *s == ' ' ) ++s;
131        sint64 result = 0;
132        bool positive = true;
133        switch ( *s )
134        {
135        case '-': ++s; positive = false; break;
136        case '+': ++s; break;
137        default: break;
138        }
139        while ( *s >= '0' && *s <= '9' )
140        {
141                result = ( result * 10 ) + ( *s - '0' );
142                ++s;
143        }
144        if ( end != nullptr ) *end = (char*)s;
145        return positive ? result : -result;
146}
147
148uint32 buffer_to_uint32( const char* s, char** end )
149{
150        while ( *s == ' ' ) ++s;
151        uint32 result = 0;
152        while ( *s >= '0' && *s <= '9' )
153        {
154                result = ( result * 10 ) + ( *s - '0' );
155                ++s;
156        }
157        if ( end != nullptr ) *end = (char*)s;
158        return result;
159}
160
161uint64 buffer_to_uint64( const char* s, char** end )
162{
163        while ( *s == ' ' ) ++s;
164        uint64 result = 0;
165        while ( *s >= '0' && *s <= '9' )
166        {
167                result = ( result * 10 ) + ( *s - '0' );
168                ++s;
169        }
170        if ( end != nullptr ) *end = (char*)s;
171        return result;
172}
173
174float buffer_to_f32( const char* s, char** end )
175{
176        return strtof( s, end );
177}
178
179double buffer_to_f64( const char* s, char** end )
180{
181        return strtod( s, end );
182}
Note: See TracBrowser for help on using the repository browser.