From b9af4ee8dfcbacf4070be7f42d0e978d050b1ccf Mon Sep 17 00:00:00 2001 From: Petr Benes Date: Wed, 26 Dec 2018 21:22:09 +0100 Subject: [PATCH] mini::buffer_ref & mini::string_ref improvements --- mini-tor.vcxproj | 2 +- mini-tor.vcxproj.filters | 6 +- mini/buffer_ref.h | 25 +++---- mini/buffer_ref.inl | 29 +++----- mini/io/path.cpp | 6 +- mini/io/path.h | 6 +- mini/net/detail/ssl_context.h | 2 +- mini/net/ssl_socket.h | 2 +- mini/net/ssl_stream.h | 2 +- mini/string.h | 4 +- mini/string_ref.h | 90 ++++++++++--------------- mini/{string_ref.cpp => string_ref.inl} | 59 +++------------- mini/win32/api_set.h | 4 +- 13 files changed, 79 insertions(+), 158 deletions(-) rename mini/{string_ref.cpp => string_ref.inl} (81%) diff --git a/mini-tor.vcxproj b/mini-tor.vcxproj index aa84c43..6301cbd 100644 --- a/mini-tor.vcxproj +++ b/mini-tor.vcxproj @@ -1323,7 +1323,6 @@ - @@ -1500,6 +1499,7 @@ + diff --git a/mini-tor.vcxproj.filters b/mini-tor.vcxproj.filters index 2b5fbcf..ec1c350 100644 --- a/mini-tor.vcxproj.filters +++ b/mini-tor.vcxproj.filters @@ -213,9 +213,6 @@ Source Files\mini - - Source Files\mini - Source Files\mini\tor\parsers @@ -739,6 +736,9 @@ Source Files\mini\collections + + Source Files\mini + diff --git a/mini/buffer_ref.h b/mini/buffer_ref.h index 03183a6..8dfd68e 100644 --- a/mini/buffer_ref.h +++ b/mini/buffer_ref.h @@ -47,15 +47,11 @@ class buffer_ref const buffer_ref& other ) = default; - constexpr buffer_ref( - buffer_ref&& other - ) = default; - constexpr buffer_ref( std::nullptr_t ); - buffer_ref( + constexpr buffer_ref( std::initializer_list values ); @@ -87,21 +83,16 @@ class buffer_ref // assign operators. // - buffer_ref& + constexpr buffer_ref& operator=( const buffer_ref& other ); - buffer_ref& - operator=( - buffer_ref&& other - ); - // // swap. // - void + constexpr void swap( buffer_ref& other ); @@ -120,7 +111,7 @@ class buffer_ref size_type index ) const; - const value_type* + constexpr const value_type* get_buffer( void ) const; @@ -129,12 +120,12 @@ class buffer_ref // iterators. // - const_iterator + constexpr const_iterator begin( void ) const; - const_iterator + constexpr const_iterator end( void ) const; @@ -143,12 +134,12 @@ class buffer_ref // capacity. // - bool + constexpr bool is_empty( void ) const; - size_type + constexpr size_type get_size( void ) const; diff --git a/mini/buffer_ref.inl b/mini/buffer_ref.inl index 5d6ef2b..e38183b 100644 --- a/mini/buffer_ref.inl +++ b/mini/buffer_ref.inl @@ -34,7 +34,7 @@ constexpr buffer_ref::buffer_ref( template < typename T > -buffer_ref::buffer_ref( +constexpr buffer_ref::buffer_ref( std::initializer_list values ) : _begin((value_type*)values.begin()) @@ -82,7 +82,7 @@ constexpr buffer_ref::buffer_ref( template < typename T > -buffer_ref& +constexpr buffer_ref& buffer_ref::operator=( const buffer_ref& other ) @@ -93,19 +93,6 @@ buffer_ref::operator=( return *this; } -template < - typename T -> -buffer_ref& -buffer_ref::operator=( - buffer_ref&& other - ) -{ - swap(other); - - return *this; -} - // // swap. // @@ -113,7 +100,7 @@ buffer_ref::operator=( template < typename T > -void +constexpr void buffer_ref::swap( buffer_ref& other ) @@ -151,7 +138,7 @@ buffer_ref::at( template < typename T > -const typename buffer_ref::value_type* +constexpr const typename buffer_ref::value_type* buffer_ref::get_buffer( void ) const @@ -167,7 +154,7 @@ template < typename T > typename buffer_ref::const_iterator -buffer_ref::begin( +constexpr buffer_ref::begin( void ) const { @@ -178,7 +165,7 @@ template < typename T > typename buffer_ref::const_iterator -buffer_ref::end( +constexpr buffer_ref::end( void ) const { @@ -192,7 +179,7 @@ buffer_ref::end( template < typename T > -bool +constexpr bool buffer_ref::is_empty( void ) const @@ -203,7 +190,7 @@ buffer_ref::is_empty( template < typename T > -typename buffer_ref::size_type +constexpr typename buffer_ref::size_type buffer_ref::get_size( void ) const diff --git a/mini/io/path.cpp b/mini/io/path.cpp index 01164e1..77b5629 100644 --- a/mini/io/path.cpp +++ b/mini/io/path.cpp @@ -61,7 +61,7 @@ path::get_file_name( if (name_offset == string_ref::not_found || (name_offset + 1) >= p.get_size()) { - return string_ref::empty; + return string_ref(); } return p.substring(name_offset + 1); @@ -96,7 +96,7 @@ path::get_filename_without_extension( if (name_offset == string_ref::not_found || (name_offset + 1) >= p.get_size()) { - return string_ref::empty; + return string_ref(); } string_ref result = p.substring(name_offset + 1); @@ -118,7 +118,7 @@ path::get_extension( return file_name.substring(extension_offset); } - return string_ref::empty; + return string_ref(); } } diff --git a/mini/io/path.h b/mini/io/path.h index 255c180..d5b124f 100644 --- a/mini/io/path.h +++ b/mini/io/path.h @@ -6,9 +6,9 @@ namespace mini::io { class path { public: - static const char directory_separator = '\\'; - static const char alternative_directory_separator = '/'; - static const char extension_separator = '.'; + static constexpr auto directory_separator = string_ref("\\"); + static constexpr auto alternative_directory_separator = string_ref("/"); + static constexpr auto extension_separator = string_ref("."); static string combine( diff --git a/mini/net/detail/ssl_context.h b/mini/net/detail/ssl_context.h index 38fd6d5..9d6471d 100644 --- a/mini/net/detail/ssl_context.h +++ b/mini/net/detail/ssl_context.h @@ -103,7 +103,7 @@ class ssl_context SECURITY_STATUS initialize( io::stream& sock, - const string_ref target_name = string_ref::empty + const string_ref target_name = string_ref() ); void diff --git a/mini/net/ssl_socket.h b/mini/net/ssl_socket.h index f044c0f..e6044f7 100644 --- a/mini/net/ssl_socket.h +++ b/mini/net/ssl_socket.h @@ -11,7 +11,7 @@ class ssl_socket { public: ssl_socket( - const string_ref host = string_ref::empty + const string_ref host = string_ref() ); ssl_socket( diff --git a/mini/net/ssl_stream.h b/mini/net/ssl_stream.h index d041a91..53b7304 100644 --- a/mini/net/ssl_stream.h +++ b/mini/net/ssl_stream.h @@ -12,7 +12,7 @@ class ssl_stream public: ssl_stream( io::stream& underlying_stream, - const string_ref target_name = string_ref::empty + const string_ref target_name = string_ref() ); ~ssl_stream( diff --git a/mini/string.h b/mini/string.h index 1e63d73..68bd862 100644 --- a/mini/string.h +++ b/mini/string.h @@ -31,8 +31,8 @@ class string using iterator = pointer; using const_iterator = const_pointer; - static const size_type not_found = (size_type)-1; - static const size_type zero_terminated = (size_type)-1; + static constexpr size_type not_found = (size_type)-1; + static constexpr size_type zero_terminated = (size_type)-1; // // constructors. diff --git a/mini/string_ref.h b/mini/string_ref.h index f14d597..223b2c2 100644 --- a/mini/string_ref.h +++ b/mini/string_ref.h @@ -2,16 +2,18 @@ #include #include +#include // std::char_traits + namespace mini { class string_ref : public buffer_ref { public: - static const size_type not_found = string::not_found; - static const size_type zero_terminated = string::zero_terminated; + using traits_type = std::char_traits; - static const string_ref empty; + static constexpr auto not_found = string::not_found; + static constexpr auto zero_terminated = string::zero_terminated; using buffer_ref::buffer_ref; @@ -19,53 +21,36 @@ class string_ref // constructors. // - string_ref( + constexpr string_ref( void ); - string_ref( - char value - ); - - template < - size_type N - > constexpr string_ref( - const char (&value)[N] - ); - - string_ref( - const char* value - ); - - string_ref( const string_ref& other ) = default; - string_ref( - string_ref&& other - ) = default; + constexpr string_ref( + const char* value + ); + constexpr string_ref( + const char* value, size_type count + ); // // assign operators. // - string_ref& + constexpr string_ref& operator=( const string_ref& other - ); - - string_ref& - operator=( - string_ref&& other - ); + ) = default; // // swap. // - void + constexpr void swap( string_ref& other ); @@ -74,7 +59,7 @@ class string_ref // capacity. // - bool + constexpr bool is_empty( void ) const; @@ -83,29 +68,29 @@ class string_ref // lookup. // - size_type + inline size_type index_of( const string_ref item, size_type from_offset = 0 ) const; - size_type + inline size_type last_index_of( const string_ref item, size_type from_offset = 0 ) const; - bool + inline bool contains( const string_ref item ) const; - bool + inline bool starts_with( const string_ref item ) const; - bool + inline bool ends_with( const string_ref item ) const; @@ -114,28 +99,28 @@ class string_ref // operations. // - bool + inline bool equals( const string_ref other ) const; - int + inline int compare( const string_ref other ) const; - string_ref + inline string_ref substring( size_type offset ) const; - string_ref + inline string_ref substring( size_type offset, size_type length ) const; - string_collection + inline string_collection split( const string_ref delimiter, size_type count = size_type_max @@ -143,14 +128,14 @@ class string_ref #if !defined(MINI_MODE_KERNEL) - int + inline int to_int( void ) const; #endif - bool + inline bool is_zero_terminated( void ) const; @@ -159,11 +144,11 @@ class string_ref // conversion operators. // - operator string( + inline operator string( void ) const; - operator byte_buffer_ref( + inline operator byte_buffer_ref( void ) const; @@ -171,38 +156,35 @@ class string_ref // non-member operations. // - friend bool + inline friend bool operator==( const string_ref& lhs, const string& rhs ); - friend bool + inline friend bool operator!=( const string_ref& lhs, const string& rhs ); - friend bool + inline friend bool operator==( const string_ref& lhs, const string_ref& rhs ); - friend bool + inline friend bool operator!=( const string_ref& lhs, const string_ref& rhs ); - friend string + inline friend string operator+( const string_ref& lhs, const string_ref& rhs ); - - private: - char _internal_char_buffer[2]; }; class mutable_string_ref @@ -236,3 +218,5 @@ struct hash }; } + +#include "string_ref.inl" diff --git a/mini/string_ref.cpp b/mini/string_ref.inl similarity index 81% rename from mini/string_ref.cpp rename to mini/string_ref.inl index 5979b67..5aa4799 100644 --- a/mini/string_ref.cpp +++ b/mini/string_ref.inl @@ -3,91 +3,50 @@ namespace mini { -const string_ref string_ref::empty; - // // constructors. // -string_ref::string_ref( +constexpr string_ref::string_ref( void ) - : buffer_ref(_internal_char_buffer, _internal_char_buffer) + : buffer_ref() { - _internal_char_buffer[0] = '\0'; -} -string_ref::string_ref( - char value - ) - : buffer_ref(_internal_char_buffer, _internal_char_buffer + 1) -{ - _internal_char_buffer[0] = value; - _internal_char_buffer[1] = '\0'; } -template < - size_type N -> constexpr string_ref::string_ref( - const char (&value)[N] - ) - : buffer_ref(value, value + N - 1) -{ - -} - -string_ref::string_ref( const char* value ) - : buffer_ref(value, value + strlen(value)) + : buffer_ref(value, value + traits_type::length(value)) { } -// -// assign operators. -// - -string_ref& -string_ref::operator=( - const string_ref& other - ) -{ - _begin = other._begin; - _end = other._end; - - return *this; -} - -string_ref& -string_ref::operator=( - string_ref&& other - ) +constexpr string_ref::string_ref( + const char* value, size_type count + ) : buffer_ref(value, value + count) { - swap(other); - return *this; } // // swap. // -void +constexpr void string_ref::swap( string_ref& other ) { buffer_ref::swap(other); - mini::swap(_internal_char_buffer, other._internal_char_buffer); } // // capacity. // -bool +constexpr bool string_ref::is_empty( void ) const @@ -269,7 +228,7 @@ string_ref::is_zero_terminated( ) const { // - // warning: this may actually trigger an memory access violation! + // warning: this may actually trigger memory access violation! // return get_buffer()[get_size() + 1] == '\0'; } diff --git a/mini/win32/api_set.h b/mini/win32/api_set.h index 883e20b..bfefb80 100644 --- a/mini/win32/api_set.h +++ b/mini/win32/api_set.h @@ -124,9 +124,9 @@ class api_set_t if (!is_api_set_name(dll_name) || !dll_name.ends_with(".dll") || - (last_index_of_hyphen = dll_name.last_index_of('-')) == string_ref::not_found) + (last_index_of_hyphen = dll_name.last_index_of("-")) == string_ref::not_found) { - return string_ref::empty; + return string_ref(); } //