devatrun / sutfcpplib Goto Github PK
View Code? Open in Web Editor NEWSimple UTF library for C++
License: MIT License
Simple UTF library for C++
License: MIT License
#if !(defined(_MSC_VER) && _MSC_VER >= 1910 && ((defined(_MSVC_LANG) && _MSVC_LANG > 201402)) || (__cplusplus > 201402))
#error "Library SUTFCPP requires a compiler that supports C++ 17!"
#endif
I found such a check in your code.
I think it's much better to check for library feature macros and compiler feature macros for what you exactly used.
I had a lot of problems in my past, checking for __cplusplus and then having issues in Linux, when compiler and STL version are not in sync (imagine using clang 10 with libstdc++6 for example).
I saw you already used 'if contexpr'. So for me logic
of
////////////////////////////////////////////////////////////////////////////////////////////////////
template<typename char_t, std::enable_if_t<std::is_same_v<char_t, char> || std::is_same_v<char_t, char8s_t>, int>>
constexpr uint_t code_unit_count(uint_t cp) noexcept
{
if (cp < 0x80)
return 1;
if (cp < 0x800)
return 2;
if (cp < 0x10000)
return 3;
return 4;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
template<typename char_t, std::enable_if_t<std::is_same_v<char_t, char16_t> || (std::is_same_v<char_t, wchar_t> && sizeof(wchar_t) == sizeof(char16_t)), int>>
constexpr uint_t code_unit_count(uint_t cp) noexcept
{
return cp < 0x10000 ? 1 : 2;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
template<typename char_t, std::enable_if_t<std::is_same_v<char_t, char32_t> || (std::is_same_v<char_t, wchar_t> && sizeof(wchar_t) == sizeof(char32_t)), int>>
constexpr uint_t code_unit_count(uint_t /*cp*/) noexcept
{
return 1;
}
Would be much clearer in form of
template<typename char_t> // i wish some char concept here, but no C++20, oh well.
constexpr uint_t code_unit_count(uint_t cp) noexcept
{
if constexpr (sizeof(char_t) == 1) {
if (cp < 0x80)
return 1;
if (cp < 0x800)
return 2;
if (cp < 0x10000)
return 3;
return 4;
} else if constexpr (sizeof(char_t) == 2) {
return cp < 0x10000 ? 1 : 2;
} else {
return 1;
}
}
or something.
If you don't like methods getting too long, then at least make you own traits or something.
Overall I wish you can provide concepts version instead of sfinae (so I can clearly understand API by signature). Docs can do that too, but I am a fan of self-documented code.
I wanna talk about code_unit_count/code_point_count functions.
(as I started to dive into your library, estimating the length was point of interest for me).
Then I found couple of
if constexpr (is_native_string_v<type_t>)
--end;
and felt bad about it, so I can have unexpected result when passing iterator pair by myself (STL taught me to use end behind the end, while you code expects different meaning).
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.