Changeset 435
- Timestamp:
- 07/22/15 21:00:30 (10 years ago)
- Location:
- trunk
- Files:
-
- 1 added
- 14 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/nv/core/logging.hh
r406 r435 40 40 void log_append( string_view ref ) 41 41 { 42 nvmemcpy( m_pos, ref.data(), ref.size() ); 43 m_pos += ref.size(); 42 size_t count = ::nv::min< size_t >( ref.size(), space_left()-1 ); 43 nvmemcpy( m_pos, ref.data(), count ); 44 m_pos += count; 44 45 } 45 46 void log_append( uint32 u ) 46 47 { 47 m_pos += uint32_to_buffer( u, m_pos);48 m_pos += uint32_to_buffer( array_ref< char >( m_pos, space_left() ), u ); 48 49 } 49 50 void log_append( sint32 s ) 50 51 { 51 m_pos += sint32_to_buffer( s, m_pos );52 m_pos += sint32_to_buffer( array_ref< char >( m_pos, space_left() ), s ); 52 53 } 53 54 void log_append( uint64 u ) 54 55 { 55 m_pos += uint64_to_buffer( u, m_pos);56 m_pos += uint64_to_buffer( array_ref< char >( m_pos, space_left() ), u ); 56 57 } 57 58 void log_append( sint64 s ) 58 59 { 59 m_pos += sint64_to_buffer( s, m_pos );60 m_pos += sint64_to_buffer( array_ref< char >( m_pos, space_left() ), s ); 60 61 } 61 62 void log_append( f32 f ) 62 63 { 63 m_pos += f32_to_buffer( f, m_pos);64 m_pos += f32_to_buffer( array_ref< char >( m_pos, space_left() ), f ); 64 65 } 65 66 static bool can_log( log_level level ) … … 82 83 virtual ~logger_base() {} 83 84 protected: 85 inline size_t space_left() const 86 { 87 return 1024 - static_cast< size_t >( m_pos - m_message ); 88 } 89 84 90 char m_message[1024]; 85 91 char* m_pos; -
trunk/nv/stl/container/contiguous_storage.hh
r406 r435 49 49 ~static_storage() = default; 50 50 protected: 51 alignas( T ) unsigned char m_data[ N * sizeof( N) ];51 alignas( T ) unsigned char m_data[ N * sizeof( T ) ]; 52 52 }; 53 53 -
trunk/nv/stl/container/fixed_storage.hh
r434 r435 50 50 } 51 51 52 static constexpr size_t ypemax_size() { return N; }53 static constexpr size_t ypecapacity() { return N; }54 static constexpr size_t ypesize() { return N; }52 static constexpr size_t max_size() { return N; } 53 static constexpr size_t capacity() { return N; } 54 static constexpr size_t size() { return N; } 55 55 static constexpr bool empty() { return N == 0; } 56 static constexpr size_t yperaw_size() { return sizeof( value_type ) * N; }56 static constexpr size_t raw_size() { return sizeof( value_type ) * N; } 57 57 58 58 operator array_ref< value_type >() { return array_ref< value_type >( Storage::data(), N ); } -
trunk/nv/stl/container/growing_storage.hh
r434 r435 33 33 }; 34 34 35 namespace detail 36 { 37 template < typename SizeType, size_t Capacity > 38 struct growing_storage_size 39 { 40 static constexpr size_t max_size() { return Capacity; } 41 static constexpr size_t capacity() { return Capacity; } 42 constexpr size_t size() const { return m_size; } 43 44 inline void size( size_t new_size ) { m_size = static_cast< SizeType >( new_size ); } 45 46 operator size_t( ) const { return m_size; } 47 48 SizeType m_size = 0; 49 }; 50 51 template < typename SizeType > 52 struct growing_storage_size< SizeType, 0 > 53 { 54 static constexpr size_t max_size() { return size_t( 0x80000000 ); } 55 constexpr size_t capacity() const { return m_capacity; } 56 constexpr size_t size() const { return m_size; } 57 58 inline void capacity( size_t new_capacity ) { m_capacity = static_cast< SizeType >( new_capacity ); } 59 inline void size( size_t new_size ) { m_size = static_cast< SizeType >( new_size ); } 60 61 operator size_t() const { return m_size; } 62 63 SizeType m_size = 0; 64 SizeType m_capacity = 0; 65 }; 66 } 67 35 68 template < 36 69 typename Storage, 37 70 typename InitializePolicy = policy_initialize_standard, 71 typename SizeType = size_t, 72 size_t Capacity = 0, 38 73 typename NextCapacity = default_next_capacity< size_t > 39 74 > … … 41 76 { 42 77 public: 78 typedef detail::growing_storage_size< SizeType, Capacity > size_impl_type; 43 79 typedef typename Storage::value_type value_type; 44 80 typedef size_t size_type; 81 typedef SizeType size_store_type; 45 82 typedef value_type* iterator; 46 83 typedef const value_type* const_iterator; … … 48 85 static constexpr bool is_fixed = false; 49 86 50 static constexpr size_type max_size() { return size_ type( 0x80000000); }51 constexpr size_t capacity() const { return m_capacity; }52 constexpr size_t size() const { return m_size; }87 static constexpr size_type max_size() { return size_impl_type::max_size(); } 88 constexpr size_type capacity() const { return m_size.capacity(); } 89 constexpr size_type size() const { return m_size; } 53 90 constexpr bool empty() const { return m_size == 0; } 54 constexpr size_t raw_size() const { return sizeof( value_type ) * m_size; }91 constexpr size_type raw_size() const { return sizeof( value_type ) * m_size; } 55 92 56 93 operator array_ref< value_type >() { return array_ref< value_type >( Storage::data(), size() ); } 57 94 operator array_view< value_type >() const { return array_view< value_type >( Storage::data(), size() ); } 58 95 59 inline growing_storage() : m_size( 0 ), m_capacity( 0 ){}60 inline explicit growing_storage( size_type new_size ) : m_size( 0 ), m_capacity( 0 ){ resize( new_size ); }61 inline explicit growing_storage( default_init ) : m_size( 0 ), m_capacity( 0 ){ resize( default_init() ); }62 inline growing_storage( size_type new_size, const value_type& v ) : m_size( 0 ), m_capacity( 0 ){ resize( new_size, v ); }96 inline growing_storage() {} 97 inline explicit growing_storage( size_type new_size ) { resize( new_size ); } 98 inline explicit growing_storage( default_init ) { resize( default_init() ); } 99 inline growing_storage( size_type new_size, const value_type& v ) { resize( new_size, v ); } 63 100 64 101 // prevent copying … … 68 105 // allow move 69 106 growing_storage( growing_storage&& other ) 70 : Storage( nv::move( other ) ), m_size( other.m_size ), m_capacity( other.m_capacity ) 71 { 72 other.m_size = 0; 73 other.m_capacity = 0; 107 : Storage( nv::move( other ) ), m_size( other.m_size ) 108 { 109 other.m_size = size_impl_type(); 74 110 } 75 111 76 112 inline growing_storage& operator=( growing_storage&& other ) 77 113 { 78 if ( m_capacity > 0 )Storage::reallocate( 0, false );114 Storage::reallocate( 0, false ); 79 115 m_size = other.m_size; 80 m_capacity = other.m_capacity;81 116 Storage::operator=( nv::move( other ) ); 82 other.m_size = 0; 83 other.m_capacity = 0; 117 other.m_size = size_impl_type(); 84 118 return *this; 85 119 } … … 108 142 if ( m_size == 0 ) return; 109 143 InitializePolicy::destroy( Storage::data() + m_size - 1 ); 110 m_size --;144 m_size.size( m_size - 1 ); 111 145 } 112 146 … … 119 153 if ( ( position + 1 ) < iend ) 120 154 raw_alias_copy( position + 1, iend, position ); 121 m_size --;155 m_size.size( m_size - 1 ); 122 156 return position; 123 157 } … … 195 229 InitializePolicy::copy( ptr, ptr + sz, Storage::data() ); 196 230 } 197 else m_size = 0;231 else m_size.size( 0 ); 198 232 } 199 233 … … 220 254 { 221 255 InitializePolicy::destroy( Storage::data(), Storage::data() + m_size ); 222 m_size = 0;256 m_size = size_impl_type(); 223 257 } 224 258 } … … 248 282 } 249 283 284 template< size_t C = Capacity > 285 enable_if_t< C != 0, bool > try_reallocate( size_t, bool ) 286 { 287 return false; 288 } 289 290 template< size_t C = Capacity > 291 enable_if_t< C == 0, bool > try_reallocate( size_t new_capacity, bool copy_needed ) 292 { 293 if ( new_capacity > 0 && Storage::reallocate( new_capacity, copy_needed ) ) 294 { 295 m_size.capacity( new_capacity ); 296 return true; 297 } 298 else return false; 299 } 300 250 301 // TODO: return type error checking 251 bool try_grow( size_t amount ) 252 { 253 size_type new_size = amount + m_size; 254 if ( new_size > m_capacity ) 255 { 256 size_type new_capacity = NextCapacity::get( new_size - m_capacity, m_capacity, max_size() ); 257 if ( new_capacity > 0 && Storage::reallocate( new_capacity, true ) ) 258 { 259 m_capacity = new_capacity; 260 } 261 else return false; 262 } 263 m_size = new_size; 302 bool try_grow( size_t amount, size_t extra = 0 ) 303 { 304 size_type new_size = amount + m_size + extra; 305 if ( new_size > m_size.capacity() ) 306 { 307 size_type new_capacity = NextCapacity::get( new_size - m_size.capacity(), m_size.capacity(), max_size() ); 308 if ( !try_reallocate( new_capacity, true ) ) return false; 309 } 310 m_size.size( new_size - extra ); 264 311 return true; 265 312 } 313 266 314 // TODO: return type error checking 267 315 bool try_reserve( size_t new_capacity, bool copy_needed ) 268 316 { 269 if ( new_capacity > m_capacity ) 270 { 271 if ( new_capacity > 0 && Storage::reallocate( new_capacity, copy_needed ) ) 317 if ( new_capacity > m_size.capacity() ) 318 { 319 return try_reallocate( new_capacity, copy_needed ); 320 } 321 return true; 322 } 323 // TODO: return type error checking 324 bool try_resize( size_t new_size, bool copy_needed, size_t extra = 0 ) 325 { 326 if ( new_size > m_size ) 327 { 328 if ( try_reserve( new_size + extra, copy_needed ) ) 272 329 { 273 m_capacity = new_capacity; 274 } 275 else return false; 276 } 277 return true; 278 } 279 // TODO: return type error checking 280 bool try_resize( size_t new_size, bool copy_needed ) 281 { 282 if ( new_size > m_size ) 283 { 284 if ( try_reserve( new_size, copy_needed ) ) 285 { 286 m_size = new_size; 330 m_size.size( new_size ); 287 331 return true; 288 332 } 289 333 return false; 290 334 } 291 m_size = new_size;335 m_size.size( new_size ); 292 336 return true; 293 337 } 294 338 protected: 295 size_type m_size; 296 size_type m_capacity; 297 339 size_impl_type m_size; 298 340 }; 299 341 -
trunk/nv/stl/container/sized_storage.hh
r434 r435 22 22 template < 23 23 typename Storage, 24 typename InitializePolicy = policy_initialize_standard 24 typename InitializePolicy = policy_initialize_standard, 25 typename SizeType = size_t 25 26 > 26 27 class sized_storage : public Storage … … 28 29 public: 29 30 typedef size_t size_type; 31 typedef SizeType size_store_type; 30 32 typedef typename Storage::value_type value_type; 31 33 … … 33 35 34 36 static constexpr size_type max_size() { return size_type( 0x80000000 ); } 35 constexpr size_t capacity() const { return m_size; }36 constexpr size_t size() const { return m_size; }37 constexpr size_type capacity() const { return m_size; } 38 constexpr size_type size() const { return m_size; } 37 39 constexpr bool empty() const { return m_size == 0; } 38 constexpr size_t raw_size() const { return sizeof( value_type ) * m_size; }40 constexpr size_type raw_size() const { return sizeof( value_type ) * m_size; } 39 41 40 42 operator array_ref< value_type >() { return array_ref< value_type >( Storage::data(), m_size ); } … … 160 162 } 161 163 protected: 162 size_ type m_size;164 size_store_type m_size; 163 165 }; 164 166 -
trunk/nv/stl/memory.hh
r434 r435 60 60 constexpr array_ref( value_type* a_data, size_type a_size ) 61 61 : m_data( a_data ), m_size( a_size ) {} 62 template< size_t N > 63 constexpr explicit array_ref( value_type( &a_data )[N] ) 64 : m_data( a_data ), m_size( N ) {} 62 65 63 66 void assign( value_type* a_data, size_type a_size ) … … 130 133 constexpr array_view( const array_ref<T>& view ) 131 134 : m_data( view.data() ), m_size( view.size() ) {} 135 template< size_t N > 136 constexpr explicit array_view( const value_type( &a_data )[N] ) 137 : m_data( a_data ), m_size( N ) {} 132 138 133 139 void assign( const value_type* a_data, size_type a_size ) -
trunk/nv/stl/string.hh
r433 r435 76 76 }; 77 77 78 size_t sint32_to_buffer( sint32 n, char* str );79 size_t sint64_to_buffer( sint64 n, char* str );80 size_t uint32_to_buffer( uint32 n, char* str );81 size_t uint64_to_buffer( uint64 n, char* str );82 size_t f32_to_buffer( f32 n, char* str );83 size_t f64_to_buffer( f64 n, char* str );84 sint32 buffer_to_sint32( const char* str, char** end );85 sint64 buffer_to_sint64( const char* s, char** end );86 uint32 buffer_to_uint32( const char* s, char** end );87 uint64 buffer_to_uint64( const char* s, char** end );88 float buffer_to_f32( const char* s, char** end );89 double buffer_to_f64( const char* s, char** end );90 91 78 inline string_view trimmed( const string_view& str ) 92 79 { -
trunk/nv/stl/string/common.hh
r433 r435 37 37 struct is_string_base : detail::is_string_base_impl< T > {}; 38 38 39 39 40 } 40 41 -
trunk/nv/stl/string/short_string.hh
r434 r435 17 17 { 18 18 template < typename Storage > 19 class string_buffer_ ops : Storage19 class string_buffer_base : public string_base< Storage > 20 20 { 21 public: 22 typedef string_buffer_base< Storage > this_type; 23 typedef string_base< Storage > base_type; 24 typedef typename Storage::iterator iterator; 25 typedef typename Storage::const_iterator const_iterator; 21 26 27 constexpr string_buffer_base() : base_type() {} 28 inline string_buffer_base( const char* data, size_t sz ) 29 : base_type() 30 { 31 append( data, sz ); 32 } 33 34 inline explicit string_buffer_base( const string_view& s ) : this_type( s.data(), s.size() ) {} 35 template < typename S > 36 inline string_buffer_base( const string_base<S>& rhs ) : this_type( rhs.data(), rhs.size() ) {} 37 inline string_buffer_base( string_buffer_base&& copy ) = default; 38 inline string_buffer_base& operator=( string_buffer_base&& other ) = default; 39 inline string_buffer_base& operator=( const string_view& other ) 40 { 41 clear(); 42 append( other ); 43 } 44 45 size_t append( const char* data, size_t sz ) 46 { 47 size_t pos = size(); 48 size_t amount = expand_by( sz ); 49 raw_copy_n( data, amount, this->begin() + pos ); 50 return amount; 51 } 52 53 size_t append( const_iterator first, const_iterator last ) 54 { 55 return append( first, last - first ); 56 } 57 58 size_t append( const string_view& s ) 59 { 60 return append( s.data(), s.size() ); 61 } 62 63 template < typename S > 64 size_t append( const string_base<S>& s ) 65 { 66 return append( s.data(), s.size() ); 67 } 68 69 size_t append( sint32 s ) 70 { 71 char buffer[8]; 72 size_t result = sint32_to_buffer( array_ref< char >( buffer ), s ); 73 return ( result > 0 ? append( buffer.data(), result ) : 0 ); 74 } 75 76 size_t append( uint32 u ) 77 { 78 char buffer[8]; 79 size_t result = uint32_to_buffer( array_ref< char >( buffer ), u ); 80 return ( result > 0 ? append( buffer, result ) : 0 ); 81 } 82 83 size_t append( sint64 s ) 84 { 85 char buffer[16]; 86 size_t result = sint64_to_buffer( array_ref< char >( buffer ), s ); 87 return ( result > 0 ? append( buffer, result ) : 0 ); 88 } 89 90 size_t append( uint64 u ) 91 { 92 char buffer[16]; 93 size_t result = uint64_to_buffer( array_ref< char >( buffer ), u ); 94 return ( result > 0 ? append( buffer, result ) : 0 ); 95 } 96 97 size_t append( f32 u ) 98 { 99 char buffer[64]; 100 size_t result = uint64_to_buffer( array_ref< char >( buffer ), u ); 101 return ( result > 0 ? append( buffer, result ) : 0 ); 102 } 103 104 size_t append( f64 s ) 105 { 106 char buffer[64]; 107 size_t result = uint64_to_buffer( array_ref< char >( buffer ), u ); 108 return ( result > 0 ? append( buffer, result ) : 0 ); 109 } 110 111 void reserve( size_type new_capacity ) 112 { 113 Storage::reserve( new_capacity + 1, true ); 114 this->data()[this->size()] = 0; 115 } 116 117 void resize( size_type new_size ) 118 { 119 Storage::reserve( new_size + 1, true ); 120 Storage::resize( min( new_size, capacity() - 1 ) ); 121 this->data()[this->size()] = 0; 122 } 123 124 protected: 125 126 size_t expand_by( size_t sz ) 127 { 128 size_t result = 0; 129 if ( Storage::try_grow( sz, 1 ) ) 130 result = sz; 131 else 132 { 133 result = capacity() - size() - 1; 134 Storage::try_resize( capacity() - 1, true, 1 ); 135 } 136 this->data()[this->size()] = 0; 137 return result; 138 } 139 private: // blocked, because not taking care of trailing zero 140 using Storage::assign; 141 using Storage::insert; 142 using Storage::erase; 143 using Storage::pop_back; 144 using Storage::push_back; 145 using Storage::emplace_back; 22 146 }; 23 147 24 template < typename T,size_t N >148 template < size_t N > 25 149 using short_string = 26 string_buffer_ ops<150 string_buffer_base< 27 151 random_access < 28 string_base< 29 growing_storage< 30 static_storage< T, N > > > > >; 152 growing_storage< 153 static_storage< char, N >, 154 policy_initialize_never, 155 uint8, N > > >; 31 156 32 template < typename T > 33 using buffer_string = 34 string_buffer_ops< 157 using string_buffer = 158 string_buffer_base< 35 159 random_access < 36 string_base< 37 growing_storage< 38 dynamic_storage< T > > > > >; 160 growing_storage< 161 dynamic_storage< char >, 162 policy_initialize_never, 163 uint16 > > >; 164 165 using string32 = short_string< 31 >; 166 using string64 = short_string< 63 >; 167 using string128 = short_string< 127 >; 168 using string256 = short_string< 255 >; 39 169 40 170 -
trunk/nv/stl/string/string_base.hh
r433 r435 14 14 namespace nv 15 15 { 16 17 size_t sint32_to_buffer( array_ref< char > buffer, sint32 n ); 18 size_t sint64_to_buffer( array_ref< char > buffer, sint64 n ); 19 size_t uint32_to_buffer( array_ref< char > buffer, uint32 n ); 20 size_t uint64_to_buffer( array_ref< char > buffer, uint64 n ); 21 size_t f32_to_buffer( array_ref< char > buffer, f32 n ); 22 size_t f64_to_buffer( array_ref< char > buffer, f64 n ); 23 24 sint32 buffer_to_sint32( const char* str, char** end ); 25 sint64 buffer_to_sint64( const char* s, char** end ); 26 uint32 buffer_to_uint32( const char* s, char** end ); 27 uint64 buffer_to_uint64( const char* s, char** end ); 28 float buffer_to_f32( const char* s, char** end ); 29 double buffer_to_f64( const char* s, char** end ); 16 30 17 31 // string base class - will become a base for a string class later … … 69 83 protected: 70 84 using base_type::base_type; 71 constexpr string_base() : base_type() {}72 constexpr string_base( pointer str, size_type len ) : base_type( str, len ) {}73 85 74 86 template < typename ReverseIterator > -
trunk/src/gui/gui_gfx_renderer.cc
r433 r435 147 147 size_t wsize = m_atlas.get_depth() * 4 * 4; 148 148 uint8* wfill = new uint8[m_atlas.get_depth() * 4 * 4]; 149 std::fill( wfill, wfill + wsize, 255 );149 raw_fill( wfill, wfill + wsize, 255 ); 150 150 white.pos = ivec2(); 151 151 m_atlas.set_region( white, wfill ); … … 189 189 nv::size_t gfx_renderer::load_font( const string_view& filename, nv::size_t size ) 190 190 { 191 std::string id_name( filename.data(), filename.size() ); 192 char buffer[8]; size_t len = nv::sint32_to_buffer( sint32( size ), buffer ); 193 id_name.append( std::string( buffer, len ) ); 194 string_view id( id_name.c_str(), id_name.size() ); 191 string128 id( filename ); 192 id.append( size ); 195 193 auto i = m_font_names.find( id ); 196 194 if ( i != m_font_names.end() ) -
trunk/src/lua/lua_path.cc
r399 r435 88 88 { 89 89 *current++ = '['; 90 current += uint32_to_buffer( e.value, current);90 current += uint32_to_buffer( array_ref< char >( current, current - start ), e.value ); 91 91 *current++ = ']'; 92 92 dot = false; -
trunk/src/lua/lua_raw.cc
r406 r435 26 26 default : break; 27 27 } 28 char buffer[64]; 28 char buffer_data[64]; 29 nv::array_ref< char > buffer( buffer_data, 64 ); 29 30 if ( type == LUA_TLIGHTUSERDATA || type == LUA_TUSERDATA ) 30 31 { 31 size_t l = nv::uint64_to_buffer( nv::uint64( lua_touserdata( L, idx ) ), buffer);32 return std::string( buffer , l );32 size_t l = nv::uint64_to_buffer( buffer, nv::uint64( lua_touserdata( L, idx ) ) ); 33 return std::string( buffer_data, l ); 33 34 } 34 35 else if ( type == LUA_TNUMBER ) 35 36 { 36 size_t l = nv::f64_to_buffer( lua_tonumber( L, idx ), buffer);37 return std::string( buffer , l );37 size_t l = nv::f64_to_buffer( buffer, lua_tonumber( L, idx ) ); 38 return std::string( buffer_data, l ); 38 39 } 39 40 return "UNKNOWN!"; -
trunk/src/stl/string.cc
r402 r435 37 37 } 38 38 39 nv::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 { 39 nv::size_t nv::sint32_to_buffer( array_ref< char > buffer, sint32 n ) 40 { 41 if ( buffer.size() < 2 ) return 0; 42 char* last = buffer.end() - 1; 43 char* s = buffer.begin(); 44 uint32 abs = static_cast<uint32>( n < 0 ? -n : n ); 45 do 46 { 47 if ( s == last ) { *buffer.begin() = '\0'; return 0; } 45 48 *s++ = static_cast<char>( '0' + ( abs % 10 ) ); 46 49 abs /= 10; 47 50 } 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 54 nv::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 { 51 if ( n < 0 ) 52 { 53 if ( s == last ) { *buffer.begin() = '\0'; return 0; } 54 *s++ = '-'; 55 } 56 *s = '\0'; 57 string_reverse( buffer.begin(), s - 1 ); 58 return static_cast<nv::size_t>( s - buffer.begin() ); 59 } 60 61 nv::size_t nv::sint64_to_buffer( array_ref< char > buffer, sint64 n ) 62 { 63 if ( buffer.size() < 2 ) return 0; 64 char* last = buffer.end() - 1; 65 char* s = buffer.begin(); 66 uint64 abs = static_cast<uint64>( n < 0 ? -n : n ); 67 do 68 { 69 if ( s == last ) { *buffer.begin() = '\0'; return 0; } 60 70 *s++ = static_cast<char>( '0' + ( abs % 10 ) ); 61 71 abs /= 10; 62 72 } 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 69 nv::size_t nv::uint32_to_buffer( uint32 n, char* str ) 70 { 71 char* s = str; 72 do 73 { 73 if ( n < 0 ) 74 { 75 if ( s == last ) { *buffer.begin() = '\0'; return 0; } 76 *s++ = '-'; 77 } 78 *s = '\0'; 79 string_reverse( buffer.begin(), s - 1 ); 80 return static_cast<nv::size_t>( s - buffer.begin() ); 81 } 82 83 nv::size_t nv::uint32_to_buffer( array_ref< char > buffer, uint32 n ) 84 { 85 if ( buffer.size() < 2 ) return 0; 86 char* last = buffer.end() - 1; 87 char* s = buffer.begin(); 88 do 89 { 90 if ( s == last ) { *buffer.begin() = '\0'; return 0; } 74 91 *s++ = static_cast<char>( '0' + ( n % 10 ) ); 75 92 n /= 10; 76 93 } while ( n > 0 ); 77 94 *s = '\0'; 78 string_reverse( str, s - 1 ); 79 return static_cast<nv::size_t>( s - str ); 80 } 81 82 nv::size_t nv::uint64_to_buffer( uint64 n, char* str ) 83 { 84 char* s = str; 85 do 86 { 95 string_reverse( buffer.begin(), s - 1 ); 96 return static_cast<nv::size_t>( s - buffer.begin() ); 97 } 98 99 nv::size_t nv::uint64_to_buffer( array_ref< char > buffer, uint64 n ) 100 { 101 if ( buffer.size() < 2 ) return 0; 102 char* last = buffer.end() - 1; 103 char* s = buffer.begin(); 104 do 105 { 106 if ( s == last ) { *buffer.begin() = '\0'; return 0; } 87 107 *s++ = static_cast<char>( '0' + ( n % 10 ) ); 88 108 n /= 10; 89 109 } while ( n > 0 ); 90 110 *s = '\0'; 91 string_reverse( str, s - 1 );92 return static_cast<nv::size_t>( s - str);93 } 94 95 nv::size_t nv::f32_to_buffer( f32 n, char* str)111 string_reverse( buffer.begin(), s - 1 ); 112 return static_cast<nv::size_t>( s - buffer.begin() ); 113 } 114 115 nv::size_t nv::f32_to_buffer( array_ref< char > buffer, f32 n ) 96 116 { 97 117 #if NV_COMPILER == NV_MSVC 98 int result = sprintf_s( str, 64, "%.*g", 6, n );118 int result = sprintf_s( buffer.data(), buffer.size(), "%.*g", 6, n ); 99 119 #else 100 int result = snprintf( str, 64, "%.*g", 6, n );120 int result = snprintf( buffer.data(), buffer.size(), "%.*g", 6, n ); 101 121 #endif 102 122 return static_cast<nv::size_t>( result > 0 ? result : 0 ); 103 123 } 104 124 105 nv::size_t nv::f64_to_buffer( f64 n, char* str)125 nv::size_t nv::f64_to_buffer( array_ref< char > buffer, f64 n ) 106 126 { 107 127 #if NV_COMPILER == NV_MSVC 108 int result = sprintf_s( str, 64, "%.*g", 6, n );128 int result = sprintf_s( buffer.data(), buffer.size(), "%.*g", 6, n ); 109 129 #else 110 int result = snprintf( str, 64, "%.*g", 6, n );130 int result = snprintf( buffer.data(), buffer.size(), "%.*g", 6, n ); 111 131 #endif 112 132 return static_cast<nv::size_t>( result > 0 ? result : 0 );
Note: See TracChangeset
for help on using the changeset viewer.