Mercurial > libunicode
changeset 7:6ecc84c922b2
Switch to C++17
author | David Demelier <markand@malikania.fr> |
---|---|
date | Mon, 09 Jul 2018 23:34:00 +0200 |
parents | d9c9a35cb4b2 |
children | e7a1a3c4f850 |
files | CMakeLists.txt generator/make-unicode/unicode-after.cpp generator/make-unicode/unicode.hpp unicode.cpp unicode.hpp |
diffstat | 5 files changed, 153 insertions(+), 127 deletions(-) [+] |
line wrap: on
line diff
--- a/CMakeLists.txt Wed May 09 08:56:34 2018 +0200 +++ b/CMakeLists.txt Mon Jul 09 23:34:00 2018 +0200 @@ -19,7 +19,7 @@ cmake_minimum_required(VERSION 3.5) project(unicode CXX) -set(CMAKE_CXX_STANDARD 14) +set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD_REQUIRED On) set(UNICODE_VERSION_MAJOR "1")
--- a/generator/make-unicode/unicode-after.cpp Wed May 09 08:56:34 2018 +0200 +++ b/generator/make-unicode/unicode-after.cpp Mon Jul 09 23:34:00 2018 +0200 @@ -55,7 +55,7 @@ } } -int nbytes_utf8(char c) noexcept +auto nbytes_utf8(char c) noexcept -> int { if (static_cast<unsigned char>(c) <= 127) return 1; @@ -69,7 +69,7 @@ return -1; } -int nbytes_point(char32_t c) noexcept +auto nbytes_point(char32_t c) noexcept -> int { if (c <= 0x7F) return 1; @@ -83,18 +83,18 @@ return -1; } -unsigned length(const std::string& str) +auto length(std::string_view str) -> unsigned { unsigned total = 0; - for_each(str, [&] (char32_t) { + for_each(str, [&] (auto) { ++ total; }); return total; } -std::string to_utf8(const std::u32string& array) +auto to_utf8(std::u32string_view array) -> std::string { std::string res; @@ -112,7 +112,7 @@ return res; } -std::u32string to_utf32(const std::string& str) +auto to_utf32(std::string_view str) -> std::u32string { std::u32string res; @@ -123,4 +123,50 @@ return res; } +auto toupper(std::u32string_view str) -> std::u32string +{ + std::u32string res(str); + + for (size_t i = 0; i < str.size(); ++i) + res[i] = toupper(str[i]); + + return res; +} + +auto toupper(std::string_view str) -> std::string +{ + std::string res; + char buffer[5]; + + for_each(str, [&] (auto code) { + encode(toupper(code), buffer); + res += buffer; + }); + + return res; +} + +auto tolower(std::u32string_view str) -> std::u32string +{ + std::u32string ret(str); + + for (size_t i = 0; i < str.size(); ++i) + ret[i] = tolower(str[i]); + + return ret; +} + +auto tolower(std::string_view str) -> std::string +{ + std::string res; + char buffer[5]; + + for_each(str, [&] (auto code) { + encode(tolower(code), buffer); + res += buffer; + }); + + return res; +} + } // !unicode
--- a/generator/make-unicode/unicode.hpp Wed May 09 08:56:34 2018 +0200 +++ b/generator/make-unicode/unicode.hpp Mon Jul 09 23:34:00 2018 +0200 @@ -28,6 +28,7 @@ #include <stdexcept> #include <string> +#include <string_view> /** * \brief Unicode namespace. @@ -60,7 +61,7 @@ * \param c the first multi byte character * \return the number of bytes [1-4] or -1 if invalid */ -int nbytes_utf8(char c) noexcept; +auto nbytes_utf8(char c) noexcept -> int; /** * Get the number of bytes for the unicode point. @@ -68,7 +69,7 @@ * \param point the unicode point * \return the number of bytes [1-4] or -1 if invalid */ -int nbytes_point(char32_t point) noexcept; +auto nbytes_point(char32_t point) noexcept -> int; /** * Get real number of character in a string. @@ -77,7 +78,7 @@ * \return the length * \throw std::invalid_argument on invalid sequence */ -unsigned length(const std::string& str); +auto length(std::string_view str) -> unsigned; /** * Iterate over all real characters in the UTF-8 string. @@ -90,7 +91,7 @@ * \throw std::invalid_argument on invalid sequence */ template <typename Func> -void for_each(const std::string& str, Func function) +void for_each(std::string_view str, Func function) { for (size_t i = 0; i < str.size(); ) { char32_t point = 0; @@ -113,7 +114,7 @@ * \return the UTF-8 string * \throw std::invalid_argument on invalid sequence */ -std::string to_utf8(const std::u32string& array); +auto to_utf8(std::u32string_view array) -> std::string; /** * Convert a UTF-8 string to UTF-32 string. @@ -122,7 +123,7 @@ * \return the UTF-32 string * \throw std::invalid_argument on invalid sequence */ -std::u32string to_utf32(const std::string& str); +auto to_utf32(std::string_view str) -> std::u32string; /** * Check if the unicode character is space. @@ -130,7 +131,7 @@ * \param c the character * \return true if space */ -bool isspace(char32_t c) noexcept; +auto isspace(char32_t c) noexcept -> bool; /** * Check if the unicode character is digit. @@ -138,7 +139,7 @@ * \param c the character * \return true if digit */ -bool isdigit(char32_t c) noexcept; +auto isdigit(char32_t c) noexcept -> bool; /** * Check if the unicode character is alpha category. @@ -146,7 +147,7 @@ * \param c the character * \return true if alpha */ -bool isalpha(char32_t c) noexcept; +auto isalpha(char32_t c) noexcept -> bool; /** * Check if the unicode character is upper case. @@ -154,7 +155,7 @@ * \param c the character * \return true if upper case */ -bool isupper(char32_t c) noexcept; +auto isupper(char32_t c) noexcept -> bool; /** * Check if the unicode character is lower case. @@ -162,7 +163,7 @@ * \param c the character * \return true if lower case */ -bool islower(char32_t c) noexcept; +auto islower(char32_t c) noexcept -> bool; /** * Check if the unicode character is title case. @@ -170,7 +171,7 @@ * \param c the character * \return true if title case */ -bool istitle(char32_t c) noexcept; +auto istitle(char32_t c) noexcept -> bool; /** * Convert to upper case. @@ -178,7 +179,7 @@ * \param c the character * \return the upper case character */ -char32_t toupper(char32_t c) noexcept; +auto toupper(char32_t c) noexcept -> char32_t; /** * Convert to lower case. @@ -186,7 +187,7 @@ * \param c the character * \return the lower case character */ -char32_t tolower(char32_t c) noexcept; +auto tolower(char32_t c) noexcept -> char32_t; /** * Convert to title case. @@ -194,75 +195,41 @@ * \param c the character * \return the title case character */ -char32_t totitle(char32_t c) noexcept; +auto totitle(char32_t c) noexcept -> char32_t; /** * Convert the UTF-32 string to upper case. * - * \param str the str + * \param str the string * \return the upper case string */ -inline std::u32string toupper(std::u32string str) -{ - for (size_t i = 0; i < str.size(); ++i) - str[i] = toupper(str[i]); - - return str; -} +auto toupper(std::u32string_view str) -> std::u32string; /** * Convert the UTF-8 string to upper case. * - * \param str the str + * \param str the string * \return the upper case string * \warning very slow at the moment */ -inline std::string toupper(const std::string& str) -{ - std::string result; - char buffer[5]; - - for_each(str, [&] (char32_t code) { - encode(toupper(code), buffer); - result += buffer; - }); - - return result; -} +auto toupper(std::string_view str) -> std::string; /** * Convert the UTF-32 string to lower case. * - * \param str the str + * \param str the string * \return the lower case string */ -inline std::u32string tolower(std::u32string str) -{ - for (size_t i = 0; i < str.size(); ++i) - str[i] = tolower(str[i]); - - return str; -} +auto tolower(std::u32string_view str) -> std::u32string; /** * Convert the UTF-8 string to lower case. * - * \param str the str + * \param str the string * \return the lower case string * \warning very slow at the moment */ -inline std::string tolower(const std::string& str) -{ - std::string result; - char buffer[5]; - - for_each(str, [&] (char32_t code) { - encode(tolower(code), buffer); - result += buffer; - }); - - return result; -} +auto tolower(std::string_view str) -> std::string; } // !unicode
--- a/unicode.cpp Wed May 09 08:56:34 2018 +0200 +++ b/unicode.cpp Mon Jul 09 23:34:00 2018 +0200 @@ -4721,7 +4721,7 @@ } } -int nbytes_utf8(char c) noexcept +auto nbytes_utf8(char c) noexcept -> int { if (static_cast<unsigned char>(c) <= 127) return 1; @@ -4735,7 +4735,7 @@ return -1; } -int nbytes_point(char32_t c) noexcept +auto nbytes_point(char32_t c) noexcept -> int { if (c <= 0x7F) return 1; @@ -4749,18 +4749,18 @@ return -1; } -unsigned length(const std::string& str) +auto length(std::string_view str) -> unsigned { unsigned total = 0; - for_each(str, [&] (char32_t) { + for_each(str, [&] (auto) { ++ total; }); return total; } -std::string to_utf8(const std::u32string& array) +auto to_utf8(std::u32string_view array) -> std::string { std::string res; @@ -4778,7 +4778,7 @@ return res; } -std::u32string to_utf32(const std::string& str) +auto to_utf32(std::string_view str) -> std::u32string { std::u32string res; @@ -4789,4 +4789,50 @@ return res; } +auto toupper(std::u32string_view str) -> std::u32string +{ + std::u32string res(str); + + for (size_t i = 0; i < str.size(); ++i) + res[i] = toupper(str[i]); + + return res; +} + +auto toupper(std::string_view str) -> std::string +{ + std::string res; + char buffer[5]; + + for_each(str, [&] (auto code) { + encode(toupper(code), buffer); + res += buffer; + }); + + return res; +} + +auto tolower(std::u32string_view str) -> std::u32string +{ + std::u32string ret(str); + + for (size_t i = 0; i < str.size(); ++i) + ret[i] = tolower(str[i]); + + return ret; +} + +auto tolower(std::string_view str) -> std::string +{ + std::string res; + char buffer[5]; + + for_each(str, [&] (auto code) { + encode(tolower(code), buffer); + res += buffer; + }); + + return res; +} + } // !unicode
--- a/unicode.hpp Wed May 09 08:56:34 2018 +0200 +++ b/unicode.hpp Mon Jul 09 23:34:00 2018 +0200 @@ -28,6 +28,7 @@ #include <stdexcept> #include <string> +#include <string_view> /** * \brief Unicode namespace. @@ -60,7 +61,7 @@ * \param c the first multi byte character * \return the number of bytes [1-4] or -1 if invalid */ -int nbytes_utf8(char c) noexcept; +auto nbytes_utf8(char c) noexcept -> int; /** * Get the number of bytes for the unicode point. @@ -68,7 +69,7 @@ * \param point the unicode point * \return the number of bytes [1-4] or -1 if invalid */ -int nbytes_point(char32_t point) noexcept; +auto nbytes_point(char32_t point) noexcept -> int; /** * Get real number of character in a string. @@ -77,7 +78,7 @@ * \return the length * \throw std::invalid_argument on invalid sequence */ -unsigned length(const std::string& str); +auto length(std::string_view str) -> unsigned; /** * Iterate over all real characters in the UTF-8 string. @@ -90,7 +91,7 @@ * \throw std::invalid_argument on invalid sequence */ template <typename Func> -void for_each(const std::string& str, Func function) +void for_each(std::string_view str, Func function) { for (size_t i = 0; i < str.size(); ) { char32_t point = 0; @@ -113,7 +114,7 @@ * \return the UTF-8 string * \throw std::invalid_argument on invalid sequence */ -std::string to_utf8(const std::u32string& array); +auto to_utf8(std::u32string_view array) -> std::string; /** * Convert a UTF-8 string to UTF-32 string. @@ -122,7 +123,7 @@ * \return the UTF-32 string * \throw std::invalid_argument on invalid sequence */ -std::u32string to_utf32(const std::string& str); +auto to_utf32(std::string_view str) -> std::u32string; /** * Check if the unicode character is space. @@ -130,7 +131,7 @@ * \param c the character * \return true if space */ -bool isspace(char32_t c) noexcept; +auto isspace(char32_t c) noexcept -> bool; /** * Check if the unicode character is digit. @@ -138,7 +139,7 @@ * \param c the character * \return true if digit */ -bool isdigit(char32_t c) noexcept; +auto isdigit(char32_t c) noexcept -> bool; /** * Check if the unicode character is alpha category. @@ -146,7 +147,7 @@ * \param c the character * \return true if alpha */ -bool isalpha(char32_t c) noexcept; +auto isalpha(char32_t c) noexcept -> bool; /** * Check if the unicode character is upper case. @@ -154,7 +155,7 @@ * \param c the character * \return true if upper case */ -bool isupper(char32_t c) noexcept; +auto isupper(char32_t c) noexcept -> bool; /** * Check if the unicode character is lower case. @@ -162,7 +163,7 @@ * \param c the character * \return true if lower case */ -bool islower(char32_t c) noexcept; +auto islower(char32_t c) noexcept -> bool; /** * Check if the unicode character is title case. @@ -170,7 +171,7 @@ * \param c the character * \return true if title case */ -bool istitle(char32_t c) noexcept; +auto istitle(char32_t c) noexcept -> bool; /** * Convert to upper case. @@ -178,7 +179,7 @@ * \param c the character * \return the upper case character */ -char32_t toupper(char32_t c) noexcept; +auto toupper(char32_t c) noexcept -> char32_t; /** * Convert to lower case. @@ -186,7 +187,7 @@ * \param c the character * \return the lower case character */ -char32_t tolower(char32_t c) noexcept; +auto tolower(char32_t c) noexcept -> char32_t; /** * Convert to title case. @@ -194,75 +195,41 @@ * \param c the character * \return the title case character */ -char32_t totitle(char32_t c) noexcept; +auto totitle(char32_t c) noexcept -> char32_t; /** * Convert the UTF-32 string to upper case. * - * \param str the str + * \param str the string * \return the upper case string */ -inline std::u32string toupper(std::u32string str) -{ - for (size_t i = 0; i < str.size(); ++i) - str[i] = toupper(str[i]); - - return str; -} +auto toupper(std::u32string_view str) -> std::u32string; /** * Convert the UTF-8 string to upper case. * - * \param str the str + * \param str the string * \return the upper case string * \warning very slow at the moment */ -inline std::string toupper(const std::string& str) -{ - std::string result; - char buffer[5]; - - for_each(str, [&] (char32_t code) { - encode(toupper(code), buffer); - result += buffer; - }); - - return result; -} +auto toupper(std::string_view str) -> std::string; /** * Convert the UTF-32 string to lower case. * - * \param str the str + * \param str the string * \return the lower case string */ -inline std::u32string tolower(std::u32string str) -{ - for (size_t i = 0; i < str.size(); ++i) - str[i] = tolower(str[i]); - - return str; -} +auto tolower(std::u32string_view str) -> std::u32string; /** * Convert the UTF-8 string to lower case. * - * \param str the str + * \param str the string * \return the lower case string * \warning very slow at the moment */ -inline std::string tolower(const std::string& str) -{ - std::string result; - char buffer[5]; - - for_each(str, [&] (char32_t code) { - encode(tolower(code), buffer); - result += buffer; - }); - - return result; -} +auto tolower(std::string_view str) -> std::string; } // !unicode