Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement constructor from and conversion to std::string_view. #68

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ target_include_directories(
target_compile_features(
${PROJECT_NAME}
INTERFACE
cxx_std_11
cxx_std_17
)

##############################################
Expand Down
21 changes: 21 additions & 0 deletions include/tinyutf8/tinyutf8.h
Original file line number Diff line number Diff line change
Expand Up @@ -1209,6 +1209,18 @@ namespace tiny_utf8
noexcept(TINY_UTF8_NOEXCEPT)
: basic_string( str.data() , str.size() , alloc , tiny_utf8_detail::read_bytes_tag() )
{}
/**
* Constructor taking an std::string_view
*
* @note Creates an Instance of type basic_string copying the string data from the supplied std::basic_string_view
* @param str The string object from which the data will be copied (interpreted as UTF-8)
* @param alloc (Optional) The allocator instance to use
*/
template<typename C>
inline basic_string( std::basic_string_view<data_type, C> str , const allocator_type& alloc = allocator_type() )
noexcept(TINY_UTF8_NOEXCEPT)
: basic_string( str.data() , str.size() , alloc , tiny_utf8_detail::read_bytes_tag() )
{}
/**
* Constructor taking an std::string
*
Expand Down Expand Up @@ -2692,6 +2704,15 @@ namespace tiny_utf8
* @return UTF-8 formatted data, wrapped inside an std::string
*/
inline std::basic_string<data_type> cpp_str( bool prepend_bom = false ) const noexcept(TINY_UTF8_NOEXCEPT) { return prepend_bom ? cpp_str_bom() : std::basic_string<DataType>( c_str() , size() ); }


/**
* Get the raw data contained in this basic_string wrapped by an std::string_view
*
* @note Returns the UTF-8 formatted content of this basic_string
* @return UTF-8 formatted data, wrapped inside an std::string_view
*/
inline std::basic_string_view<data_type> cpp_str_view() const noexcept(TINY_UTF8_NOEXCEPT) { return std::basic_string_view<DataType>( c_str() , size() ); }
};
} // Namespace 'tiny_utf8'

Expand Down
16 changes: 16 additions & 0 deletions test/src/test_construction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,22 @@ TEST(TinyUTF8, CTor_TakeAnAnsiString)
EXPECT_TRUE(str.lut_active());
}

TEST(TinyUTF8, CTor_TakeAStringView)
{
const std::string str_orig("Löwen, Bären, Vögel und Käfer sind Tiere.");
const std::string_view str_view(str_orig);
tiny_utf8::string str(str_view);

EXPECT_EQ(str_view.length(), 45);
EXPECT_EQ(str_view.size(), 45);
EXPECT_EQ(str.length(), 41);
EXPECT_EQ(str.size(), 45);

EXPECT_TRUE(str.requires_unicode());
EXPECT_FALSE(str.sso_active());
EXPECT_TRUE(str.lut_active());
}

TEST(TinyUTF8, CopyCTor)
{
tiny_utf8::string str_orig(U"Hello ツ World");
Expand Down
8 changes: 8 additions & 0 deletions test/src/test_conversion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,11 @@ TEST(TinyUTF8, ToWideLiteral)
++it_fwd;
}
}

TEST(TinyUTF8, ToCppStr)
{
const std::string str_orig("Löwen, Bären, Vögel und Käfer sind Tiere.");
tiny_utf8::string str(str_orig);
EXPECT_EQ(str.cpp_str(), str_orig);
EXPECT_EQ(str.cpp_str_view(), str_orig);
}