source: trunk/src/core/string.cc @ 365

Last change on this file since 365 was 365, checked in by epyon, 10 years ago
  • overhaul of logging: no longer stream operated no longer using STL no memory allocation shorthand macros fast file and console I/O
File size: 3.7 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
9#include "nv/core/string.hh"
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        if ( n < 0 ) *s++ = '-';
69        *s = '\0';
70        string_reverse( str, s - 1 );
71        return (size_t)( s - str );
72}
73
74size_t nv::uint64_to_buffer( uint64 n, char* str )
75{
76        char* s = str;
77        do
78        {
79                *s++ = (char)( '0' + ( n % 10 ) );
80                n /= 10;
81        } while ( n > 0 );
82        if ( n < 0 ) *s++ = '-';
83        *s = '\0';
84        string_reverse( str, s - 1 );
85        return (size_t)( s - str );
86}
87
88size_t nv::f32_to_buffer( f32 n, char* str )
89{
90#if NV_COMPILER == NV_MSVC
91        sprintf_s( str, 64, "%.*g", 6, n );
92#else
93        snprintf( buffer, 64, "%.*g", 6, n );
94#endif
95        sprintf( str, "%g", n );
96        return strlen( str );
97}
98
99size_t nv::f64_to_buffer( f64 n, char* str )
100{
101#if NV_COMPILER == NV_MSVC
102        sprintf_s( str, 64, "%.*g", 6, n );
103#else
104        snprintf( buffer, 64, "%.*g", 6, n );
105#endif
106        return strlen( str );
107}
108
109sint32 buffer_to_sint32( const char* str, char** end )
110{
111        const char* s = str;
112        while ( *s == ' ' ) ++s;
113        sint32 result = 0;
114        bool positive = true;
115        switch ( *s )
116        {
117        case '-': ++s; positive = false; break;
118        case '+': ++s; break;
119        default: break;
120        }
121        while ( *s >= '0' && *s <= '9' )
122        {
123                result = ( result * 10 ) + ( *s - '0' );
124                ++s;
125        }
126        if ( end != nullptr ) *end = (char*)s;
127        return positive ? result : -result;
128}
129
130sint64 buffer_to_sint64( const char* s, char** end )
131{
132        while ( *s == ' ' ) ++s;
133        sint64 result = 0;
134        bool positive = true;
135        switch ( *s )
136        {
137        case '-': ++s; positive = false; break;
138        case '+': ++s; break;
139        default: break;
140        }
141        while ( *s >= '0' && *s <= '9' )
142        {
143                result = ( result * 10 ) + ( *s - '0' );
144                ++s;
145        }
146        if ( end != nullptr ) *end = (char*)s;
147        return positive ? result : -result;
148}
149
150uint32 buffer_to_uint32( const char* s, char** end )
151{
152        while ( *s == ' ' ) ++s;
153        uint32 result = 0;
154        while ( *s >= '0' && *s <= '9' )
155        {
156                result = ( result * 10 ) + ( *s - '0' );
157                ++s;
158        }
159        if ( end != nullptr ) *end = (char*)s;
160        return result;
161}
162
163uint64 buffer_to_uint64( const char* s, char** end )
164{
165        while ( *s == ' ' ) ++s;
166        uint64 result = 0;
167        while ( *s >= '0' && *s <= '9' )
168        {
169                result = ( result * 10 ) + ( *s - '0' );
170                ++s;
171        }
172        if ( end != nullptr ) *end = (char*)s;
173        return result;
174}
175
176float buffer_to_f32( const char* s, char** end )
177{
178        return strtof( s, end );
179}
180
181double buffer_to_f64( const char* s, char** end )
182{
183        return strtod( s, end );
184}
Note: See TracBrowser for help on using the repository browser.