Changeset 435


Ignore:
Timestamp:
07/22/15 21:00:30 (10 years ago)
Author:
epyon
Message:
  • short_string implementation - first usages
Location:
trunk
Files:
1 added
14 edited

Legend:

Unmodified
Added
Removed
  • trunk/nv/core/logging.hh

    r406 r435  
    4040                void log_append( string_view ref )
    4141                {
    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;
    4445                }
    4546                void log_append( uint32 u )
    4647                {
    47                         m_pos += uint32_to_buffer( u, m_pos );
     48                        m_pos += uint32_to_buffer( array_ref< char >( m_pos, space_left() ), u );
    4849                }
    4950                void log_append( sint32 s )
    5051                {
    51                         m_pos += sint32_to_buffer( s, m_pos );
     52                        m_pos += sint32_to_buffer( array_ref< char >( m_pos, space_left() ), s );
    5253                }
    5354                void log_append( uint64 u )
    5455                {
    55                         m_pos += uint64_to_buffer( u, m_pos );
     56                        m_pos += uint64_to_buffer( array_ref< char >( m_pos, space_left() ), u );
    5657                }
    5758                void log_append( sint64 s )
    5859                {
    59                         m_pos += sint64_to_buffer( s, m_pos );
     60                        m_pos += sint64_to_buffer( array_ref< char >( m_pos, space_left() ), s );
    6061                }
    6162                void log_append( f32 f )
    6263                {
    63                         m_pos += f32_to_buffer( f, m_pos );
     64                        m_pos += f32_to_buffer( array_ref< char >( m_pos, space_left() ), f );
    6465                }
    6566                static bool can_log( log_level level )
     
    8283                virtual ~logger_base() {}
    8384        protected:
     85                inline size_t space_left() const
     86                {
     87                        return 1024 - static_cast< size_t >( m_pos - m_message );
     88                }
     89
    8490                char m_message[1024];
    8591                char* m_pos;
  • trunk/nv/stl/container/contiguous_storage.hh

    r406 r435  
    4949                ~static_storage() = default;
    5050        protected:
    51                 alignas( T ) unsigned char m_data[ N * sizeof( N ) ];
     51                alignas( T ) unsigned char m_data[ N * sizeof( T ) ];
    5252        };
    5353       
  • trunk/nv/stl/container/fixed_storage.hh

    r434 r435  
    5050                }
    5151
    52                 static constexpr size_type max_size() { return N; }
    53                 static constexpr size_type capacity() { return N; }
    54                 static constexpr size_type size() { 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; }
    5555                static constexpr bool empty() { return N == 0; }
    56                 static constexpr size_type raw_size() { return sizeof( value_type ) * N; }
     56                static constexpr size_t raw_size() { return sizeof( value_type ) * N; }
    5757
    5858                operator array_ref< value_type >() { return array_ref< value_type >( Storage::data(), N ); }
  • trunk/nv/stl/container/growing_storage.hh

    r434 r435  
    3333        };
    3434
     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
    3568        template <
    3669                typename Storage,
    3770                typename InitializePolicy = policy_initialize_standard,
     71                typename SizeType = size_t,
     72                size_t Capacity = 0,
    3873                typename NextCapacity = default_next_capacity< size_t >
    3974        >
     
    4176        {
    4277        public:
     78                typedef detail::growing_storage_size< SizeType, Capacity > size_impl_type;
    4379                typedef typename Storage::value_type value_type;
    4480                typedef size_t                       size_type;
     81                typedef SizeType                     size_store_type;
    4582                typedef value_type*                  iterator;
    4683                typedef const value_type*            const_iterator;
     
    4885                static constexpr bool is_fixed = false;
    4986
    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; }
    5390                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; }
    5592
    5693                operator array_ref< value_type >() { return array_ref< value_type >( Storage::data(), size() ); }
    5794                operator array_view< value_type >() const { return array_view< value_type >( Storage::data(), size() ); }
    5895
    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 ); }
    63100
    64101                // prevent copying
     
    68105                // allow move
    69106                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();
    74110                }
    75111
    76112                inline growing_storage& operator=( growing_storage&& other )
    77113                {
    78                         if ( m_capacity > 0 ) Storage::reallocate( 0, false );
     114                        Storage::reallocate( 0, false );
    79115                        m_size = other.m_size;
    80                         m_capacity = other.m_capacity;
    81116                        Storage::operator=( nv::move( other ) );
    82                         other.m_size = 0;
    83                         other.m_capacity = 0;
     117                        other.m_size = size_impl_type();
    84118                        return *this;
    85119                }
     
    108142                        if ( m_size == 0 ) return;
    109143                        InitializePolicy::destroy( Storage::data() + m_size - 1 );
    110                         m_size--;
     144                        m_size.size( m_size - 1 );
    111145                }
    112146
     
    119153                        if ( ( position + 1 ) < iend )
    120154                                raw_alias_copy( position + 1, iend, position );
    121                         m_size--;
     155                        m_size.size( m_size - 1 );
    122156                        return position;
    123157                }
     
    195229                                        InitializePolicy::copy( ptr, ptr + sz, Storage::data() );
    196230                        }
    197                         else m_size = 0;
     231                        else m_size.size( 0 );
    198232                }
    199233
     
    220254                        {
    221255                                InitializePolicy::destroy( Storage::data(), Storage::data() + m_size );
    222                                 m_size = 0;
     256                                m_size = size_impl_type();
    223257                        }
    224258                }
     
    248282                }
    249283
     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
    250301                // 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 );
    264311                        return true;
    265312                }
     313
    266314                // TODO: return type error checking
    267315                bool try_reserve( size_t new_capacity, bool copy_needed )
    268316                {
    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 ) )
    272329                                {
    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 );
    287331                                        return true;
    288332                                }
    289333                                return false;
    290334                        }
    291                         m_size = new_size;
     335                        m_size.size( new_size );
    292336                        return true;
    293337                }
    294338        protected:
    295                 size_type m_size;
    296                 size_type m_capacity;
    297 
     339                size_impl_type m_size;
    298340        };
    299341
  • trunk/nv/stl/container/sized_storage.hh

    r434 r435  
    2222        template <
    2323                typename Storage,
    24                 typename InitializePolicy = policy_initialize_standard
     24                typename InitializePolicy = policy_initialize_standard,
     25                typename SizeType = size_t
    2526        >
    2627        class sized_storage : public Storage
     
    2829        public:
    2930                typedef size_t                       size_type;
     31                typedef SizeType                     size_store_type;
    3032                typedef typename Storage::value_type value_type;
    3133
     
    3335
    3436                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; }
    3739                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; }
    3941
    4042                operator array_ref< value_type >() { return array_ref< value_type >( Storage::data(), m_size ); }
     
    160162                }
    161163        protected:
    162                 size_type m_size;
     164                size_store_type m_size;
    163165        };
    164166
  • trunk/nv/stl/memory.hh

    r434 r435  
    6060                constexpr array_ref( value_type* a_data, size_type a_size )
    6161                        : 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 ) {}
    6265
    6366                void assign( value_type* a_data, size_type a_size )
     
    130133                constexpr array_view( const array_ref<T>& view )
    131134                        : 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 ) {}
    132138
    133139                void assign( const value_type* a_data, size_type a_size )
  • trunk/nv/stl/string.hh

    r433 r435  
    7676        };
    7777
    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 
    9178        inline string_view trimmed( const string_view& str )
    9279        {
  • trunk/nv/stl/string/common.hh

    r433 r435  
    3737        struct is_string_base : detail::is_string_base_impl< T > {};
    3838
     39
    3940}
    4041
  • trunk/nv/stl/string/short_string.hh

    r434 r435  
    1717{
    1818        template < typename Storage >
    19         class string_buffer_ops : Storage
     19        class string_buffer_base : public string_base< Storage >
    2020        {
     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;
    2126
     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;
    22146        };
    23147
    24         template < typename T, size_t N >
     148        template < size_t N >
    25149        using short_string =
    26                 string_buffer_ops<
     150                string_buffer_base<
    27151                        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 > > >;
    31156
    32         template < typename T >
    33         using buffer_string =
    34                 string_buffer_ops<
     157        using string_buffer =
     158                string_buffer_base<
    35159                        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 >;
    39169
    40170
  • trunk/nv/stl/string/string_base.hh

    r433 r435  
    1414namespace nv
    1515{
     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 );
    1630       
    1731        // string base class - will become a base for a string class later
     
    6983        protected:
    7084                using base_type::base_type;
    71                 constexpr string_base() : base_type() {}
    72                 constexpr string_base( pointer str, size_type len ) : base_type( str, len ) {}
    7385
    7486                template < typename ReverseIterator >
  • trunk/src/gui/gui_gfx_renderer.cc

    r433 r435  
    147147        size_t wsize = m_atlas.get_depth() * 4 * 4;
    148148        uint8* wfill = new uint8[m_atlas.get_depth() * 4 * 4];
    149         std::fill( wfill, wfill + wsize, 255 );
     149        raw_fill( wfill, wfill + wsize, 255 );
    150150        white.pos = ivec2();
    151151        m_atlas.set_region( white, wfill );
     
    189189nv::size_t gfx_renderer::load_font( const string_view& filename, nv::size_t size )
    190190{
    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 );
    195193        auto i = m_font_names.find( id );
    196194        if ( i != m_font_names.end() )
  • trunk/src/lua/lua_path.cc

    r399 r435  
    8888                {
    8989                        *current++ = '[';
    90                         current += uint32_to_buffer( e.value, current );
     90                        current += uint32_to_buffer( array_ref< char >( current, current - start ), e.value );
    9191                        *current++ = ']';
    9292                        dot = false;
  • trunk/src/lua/lua_raw.cc

    r406 r435  
    2626        default : break;
    2727        }
    28         char buffer[64];
     28        char buffer_data[64];
     29        nv::array_ref< char > buffer( buffer_data, 64 );
    2930        if ( type == LUA_TLIGHTUSERDATA || type == LUA_TUSERDATA )
    3031        {
    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 );
    3334        }
    3435        else if ( type == LUA_TNUMBER )
    3536        {
    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 );
    3839        }
    3940        return "UNKNOWN!";
  • trunk/src/stl/string.cc

    r402 r435  
    3737}
    3838
    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         {
     39nv::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;  }
    4548                *s++ = static_cast<char>( '0' + ( abs % 10 ) );
    4649                abs /= 10;
    4750        } 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
     61nv::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; }
    6070                *s++ = static_cast<char>( '0' + ( abs % 10 ) );
    6171                abs /= 10;
    6272        } 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
     83nv::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; }
    7491                *s++ = static_cast<char>( '0' + ( n % 10 ) );
    7592                n /= 10;
    7693        } while ( n > 0 );
    7794        *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
     99nv::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; }
    87107                *s++ = static_cast<char>( '0' + ( n % 10 ) );
    88108                n /= 10;
    89109        } while ( n > 0 );
    90110        *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
     115nv::size_t nv::f32_to_buffer( array_ref< char > buffer, f32 n )
    96116{
    97117#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 );
    99119#else
    100         int result = snprintf( str, 64, "%.*g", 6, n );
     120        int result = snprintf( buffer.data(), buffer.size(), "%.*g", 6, n );
    101121#endif
    102122        return static_cast<nv::size_t>( result > 0 ? result : 0 );
    103123}
    104124
    105 nv::size_t nv::f64_to_buffer( f64 n, char* str )
     125nv::size_t nv::f64_to_buffer( array_ref< char > buffer, f64 n )
    106126{
    107127#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 );
    109129#else
    110         int result = snprintf( str, 64, "%.*g", 6, n );
     130        int result = snprintf( buffer.data(), buffer.size(), "%.*g", 6, n );
    111131#endif
    112132        return static_cast<nv::size_t>( result > 0 ? result : 0 );
Note: See TracChangeset for help on using the changeset viewer.