From 06700137025a410be14892b91feaddb4aa9e4a07 Mon Sep 17 00:00:00 2001 From: bolero-MURAKAMI Date: Sat, 22 Dec 2012 01:02:49 +0900 Subject: [PATCH] fix cstring, cwchar --- sprout/cstring/memchr.hpp | 45 +++++++++++++++++++++++++---------- sprout/cstring/memcmp.hpp | 21 ++++------------ sprout/cstring/strcspn.hpp | 15 +++--------- sprout/cstring/strlen.hpp | 11 +++++++++ sprout/cstring/strncmp.hpp | 16 ++++++------- sprout/cstring/strspn.hpp | 19 ++++----------- sprout/cwchar/wcscmp.hpp | 3 +++ sprout/cwchar/wcscoll.hpp | 5 +++- sprout/cwchar/wcscspn.hpp | 20 ++++++---------- sprout/cwchar/wcslen.hpp | 14 +++++++++++ sprout/cwchar/wcsncmp.hpp | 19 ++++++++------- sprout/cwchar/wcspbrk.hpp | 5 +++- sprout/cwchar/wcsrchr.hpp | 3 +++ sprout/cwchar/wcsspn.hpp | 24 +++++++------------ sprout/cwchar/wmemchr.hpp | 42 +++++++++++++++++++++++++------- sprout/cwchar/wmemcmp.hpp | 22 +++++++---------- sprout/string/char_traits.hpp | 15 ++++++++---- 17 files changed, 172 insertions(+), 127 deletions(-) diff --git a/sprout/cstring/memchr.hpp b/sprout/cstring/memchr.hpp index b93abc89..de47093c 100644 --- a/sprout/cstring/memchr.hpp +++ b/sprout/cstring/memchr.hpp @@ -3,30 +3,49 @@ #include #include +#include +#include HDR_ALGORITHM_SSCRISK_CEL_OR_SPROUT namespace sprout { - // Copyright (C) 2011 RiSK (sscrisk) - namespace detail { - inline SPROUT_CONSTEXPR void const* - memchr_impl(unsigned char const* s, unsigned char c, std::size_t n) { - return !n ? 0 - : *s == c ? s - : sprout::detail::memchr_impl(s + 1, c, n - 1) - ; + inline SPROUT_CONSTEXPR unsigned char const* + memchr_impl(unsigned char const* found, unsigned char const* last) { + return found == last ? nullptr + : found + ; + } + inline SPROUT_CONSTEXPR unsigned char* + memchr_impl(unsigned char* found, unsigned char* last) { + return found == last ? nullptr + : found + ; } } // namespace detail // 7.21.5.1 memchr 関数 inline SPROUT_CONSTEXPR void const* - memchr(void const* s, int c, size_t n) { - return sprout::detail::memchr_impl(static_cast(s), static_cast(c), n); + memchr(void const* s, int c, std::size_t n) { + return sprout::detail::memchr_impl( + sprout::as_iterator_base( + NS_SSCRISK_CEL_OR_SPROUT::find( + sprout::as_iterator(static_cast(s)), sprout::as_iterator(static_cast(s), n), + static_cast(c) + ) + ), + static_cast(s) + n + ); } inline SPROUT_CONSTEXPR void* - memchr(void* s, int c, size_t n) { - return const_cast( - sprout::detail::memchr_impl(static_cast(s), static_cast(c), n) + memchr(void* s, int c, std::size_t n) { + return sprout::detail::memchr_impl( + sprout::as_iterator_base( + NS_SSCRISK_CEL_OR_SPROUT::find( + sprout::as_iterator(static_cast(s)), sprout::as_iterator(static_cast(s), n), + static_cast(c) + ) + ), + static_cast(s) + n ); } } // namespace sprout diff --git a/sprout/cstring/memcmp.hpp b/sprout/cstring/memcmp.hpp index 5370b38d..2ad44770 100644 --- a/sprout/cstring/memcmp.hpp +++ b/sprout/cstring/memcmp.hpp @@ -3,27 +3,16 @@ #include #include +#include +#include namespace sprout { - // Copyright (C) 2011 RiSK (sscrisk) - - namespace detail { - inline SPROUT_CONSTEXPR int - memcmp_impl(unsigned char const* s1, unsigned char const* s2, std::size_t n) { - return !n ? 0 - : *s1 == *s2 ? sprout::detail::memcmp_impl(s1 + 1, s2 + 1, n - 1) - : *s1 - *s2 - ; - } - } // namespace detail - // 7.21.4.1 memcmp 関数 inline SPROUT_CONSTEXPR int memcmp(void const* s1, void const* s2, std::size_t n) { - return sprout::detail::memcmp_impl( - static_cast(s1), - static_cast(s2), - n + return sprout::tristate_lexicographical_compare( + sprout::as_iterator(static_cast(s1)), sprout::as_iterator(static_cast(s1), n), + sprout::as_iterator(static_cast(s2)), sprout::as_iterator(static_cast(s2), n) ); } } // namespace sprout diff --git a/sprout/cstring/strcspn.hpp b/sprout/cstring/strcspn.hpp index 49d5e2e8..cbb620b2 100644 --- a/sprout/cstring/strcspn.hpp +++ b/sprout/cstring/strcspn.hpp @@ -6,21 +6,12 @@ #include namespace sprout { - // Copyright (C) 2011 RiSK (sscrisk) - - namespace detail { - inline SPROUT_CONSTEXPR std::size_t - strcspn_impl(char const* s1, char const* s2, std::size_t n) { - return !*s1 || sprout::strchr(s2, *s1) ? n - : sprout::detail::strcspn_impl(s1 + 1, s2, n + 1) - ; - } - } // amespace detail - // 7.21.5.3 strcspn 関数 inline SPROUT_CONSTEXPR std::size_t strcspn(char const* s1, char const* s2) { - return sprout::detail::strcspn_impl(s1, s2, 0); + return !*s1 || sprout::strchr(s2, *s1) ? 0 + : 1 + sprout::strcspn(s1 + 1, s2) + ; } } // namespace sprout diff --git a/sprout/cstring/strlen.hpp b/sprout/cstring/strlen.hpp index 44809ff9..029ca42f 100644 --- a/sprout/cstring/strlen.hpp +++ b/sprout/cstring/strlen.hpp @@ -3,6 +3,9 @@ #include #include +#include +#include HDR_ALGORITHM_SSCRISK_CEL_OR_SPROUT +#include HDR_ITERATOR_SSCRISK_CEL_OR_SPROUT namespace sprout { // 7.21.6.3 strlen 関数 @@ -12,6 +15,14 @@ namespace sprout { : 1 + sprout::strlen(s + 1) ; } + + inline SPROUT_CONSTEXPR std::size_t + strlen(char const* s, std::size_t n) { + return NS_SSCRISK_CEL_OR_SPROUT::distance( + sprout::as_iterator(s), + NS_SSCRISK_CEL_OR_SPROUT::find(sprout::as_iterator(s), sprout::as_iterator(s, n), '\0') + ); + } } // namespace sprout #endif // #ifndef SPROUT_CSTRING_STRLEN_HPP diff --git a/sprout/cstring/strncmp.hpp b/sprout/cstring/strncmp.hpp index d0db0c9d..56bac73a 100644 --- a/sprout/cstring/strncmp.hpp +++ b/sprout/cstring/strncmp.hpp @@ -3,19 +3,19 @@ #include #include +#include +#include +#include +#include HDR_ALGORITHM_SSCRISK_CEL_OR_SPROUT namespace sprout { - // Copyright (C) 2011 RiSK (sscrisk) - // 7.21.4.4 strncmp 関数 inline SPROUT_CONSTEXPR int strncmp(char const* s1, char const* s2, std::size_t n) { - return !n || (!*s1 && !*s2) ? 0 - : !*s1 ? -1 - : !*s2 ? 1 - : *s1 == *s2 ? sprout::strncmp(s1 + 1, s2 + 1, n - 1) - : static_cast(*s1) - static_cast(*s2) - ; + return sprout::tristate_lexicographical_compare( + sprout::as_iterator(s1), sprout::as_iterator(s1, sprout::strlen(s1, n)), + sprout::as_iterator(s2), sprout::as_iterator(s2, sprout::strlen(s2, n)) + ); } } // namespace sprout diff --git a/sprout/cstring/strspn.hpp b/sprout/cstring/strspn.hpp index 0ffb2cdb..ab2a061b 100644 --- a/sprout/cstring/strspn.hpp +++ b/sprout/cstring/strspn.hpp @@ -1,27 +1,18 @@ #ifndef SPROUT_CSTRING_STRSPN_HPP -#define SPROUT_CSTRING_XXX_HPP +#define SPROUT_CSTRING_STRSPN_HPP #include #include #include namespace sprout { - // Copyright (C) 2011 RiSK (sscrisk) - - namespace detail { - inline SPROUT_CONSTEXPR std::size_t - strspn_impl(char const* s1, char const* s2, std::size_t n) { - return !*s1 || !sprout::strchr(s2, *s1) ? n - : sprout::detail::strspn_impl(s1 + 1, s2, n + 1) - ; - } - } // namespace detail - // 7.21.5.6 strspn 関数 inline SPROUT_CONSTEXPR std::size_t strspn(char const* s1, char const* s2) { - return sprout::detail::strspn_impl(s1, s2, 0); + return !*s1 || !sprout::strchr(s2, *s1) ? 0 + : 1 + sprout::strspn(s1 + 1, s2) + ; } } // namespace sprout -#endif // #ifndef SPROUT_CSTRING_XXX_HPP +#endif // #ifndef SPROUT_CSTRING_STRSPN_HPP diff --git a/sprout/cwchar/wcscmp.hpp b/sprout/cwchar/wcscmp.hpp index 0f6b5521..96baded0 100644 --- a/sprout/cwchar/wcscmp.hpp +++ b/sprout/cwchar/wcscmp.hpp @@ -6,6 +6,9 @@ namespace sprout { // Copyright (C) 2011 RiSK (sscrisk) + // + // wcscmp + // inline SPROUT_CONSTEXPR int wcscmp(wchar_t const* s1, wchar_t const* s2) { return !*s1 && !*s2 ? 0 diff --git a/sprout/cwchar/wcscoll.hpp b/sprout/cwchar/wcscoll.hpp index 03ce6821..45b6f338 100644 --- a/sprout/cwchar/wcscoll.hpp +++ b/sprout/cwchar/wcscoll.hpp @@ -2,11 +2,14 @@ #define SPROUT_CWCHAR_WCSCOLL_HPP #include -#include +#include namespace sprout { // Copyright (C) 2011 RiSK (sscrisk) + // + // wcscoll + // inline SPROUT_CONSTEXPR int wcscoll(wchar_t const* s1, wchar_t const* s2) { return sprout::wcscmp(s1, s2); diff --git a/sprout/cwchar/wcscspn.hpp b/sprout/cwchar/wcscspn.hpp index f2a4a0ba..fe68b86e 100644 --- a/sprout/cwchar/wcscspn.hpp +++ b/sprout/cwchar/wcscspn.hpp @@ -3,23 +3,17 @@ #include #include -#include +#include namespace sprout { - // Copyright (C) 2011 RiSK (sscrisk) - - namespace detail { - inline SPROUT_CONSTEXPR std::size_t - wcscspn_impl(wchar_t const* s1, wchar_t const* s2, std::size_t n) { - return !*s1 || sprout::wcschr(s2, *s1) ? n - : sprout::detail::wcscspn_impl(s1 + 1, s2, n + 1) - ; - } - } // amespace detail - + // + // wcscspn + // inline SPROUT_CONSTEXPR std::size_t wcscspn(wchar_t const* s1, wchar_t const* s2) { - return sprout::detail::wcscspn_impl(s1, s2, 0); + return !*s1 || sprout::wcschr(s2, *s1) ? 0 + : 1 + sprout::wcscspn(s1 + 1, s2) + ; } } // namespace sprout diff --git a/sprout/cwchar/wcslen.hpp b/sprout/cwchar/wcslen.hpp index b901a4c7..0271e579 100644 --- a/sprout/cwchar/wcslen.hpp +++ b/sprout/cwchar/wcslen.hpp @@ -3,14 +3,28 @@ #include #include +#include +#include HDR_ALGORITHM_SSCRISK_CEL_OR_SPROUT +#include HDR_ITERATOR_SSCRISK_CEL_OR_SPROUT namespace sprout { + // + // wcslen + // inline SPROUT_CONSTEXPR std::size_t wcslen(wchar_t const* s) { return !*s ? 0 : 1 + sprout::wcslen(s + 1) ; } + + inline SPROUT_CONSTEXPR std::size_t + wcslen(wchar_t const* s, std::size_t n) { + return NS_SSCRISK_CEL_OR_SPROUT::distance( + sprout::as_iterator(s), + NS_SSCRISK_CEL_OR_SPROUT::find(sprout::as_iterator(s), sprout::as_iterator(s, n), L'\0') + ); + } } // namespace sprout #endif // #ifndef SPROUT_CWCHAR_WCSLEN_HPP diff --git a/sprout/cwchar/wcsncmp.hpp b/sprout/cwchar/wcsncmp.hpp index 77b0c44e..ecf7c083 100644 --- a/sprout/cwchar/wcsncmp.hpp +++ b/sprout/cwchar/wcsncmp.hpp @@ -3,18 +3,21 @@ #include #include +#include +#include +#include +#include HDR_ALGORITHM_SSCRISK_CEL_OR_SPROUT namespace sprout { - // Copyright (C) 2011 RiSK (sscrisk) - + // + // wcsncmp + // inline SPROUT_CONSTEXPR int wcsncmp(wchar_t const* s1, wchar_t const* s2, std::size_t n) { - return !n || (!*s1 && !*s2) ? 0 - : !*s1 ? -1 - : !*s2 ? 1 - : *s1 == *s2 ? sprout::wcsncmp(s1 + 1, s2 + 1, n - 1) - : *s1 - *s2 - ; + return sprout::tristate_lexicographical_compare( + sprout::as_iterator(s1), sprout::as_iterator(s1, sprout::wcslen(s1, n)), + sprout::as_iterator(s2), sprout::as_iterator(s2, sprout::wcslen(s2, n)) + ); } } // namespace sprout diff --git a/sprout/cwchar/wcspbrk.hpp b/sprout/cwchar/wcspbrk.hpp index 1921fa77..6ac95b1a 100644 --- a/sprout/cwchar/wcspbrk.hpp +++ b/sprout/cwchar/wcspbrk.hpp @@ -3,11 +3,14 @@ #include #include -#include +#include namespace sprout { // Copyright (C) 2011 RiSK (sscrisk) + // + // wcspbrk + // inline SPROUT_CONSTEXPR wchar_t const* wcspbrk(wchar_t const* s1, wchar_t const* s2) { return !*s1 ? nullptr diff --git a/sprout/cwchar/wcsrchr.hpp b/sprout/cwchar/wcsrchr.hpp index d750af79..e5561e5f 100644 --- a/sprout/cwchar/wcsrchr.hpp +++ b/sprout/cwchar/wcsrchr.hpp @@ -7,6 +7,9 @@ namespace sprout { // Copyright (C) 2011 RiSK (sscrisk) + // + // wcsrchr + // inline SPROUT_CONSTEXPR wchar_t const* wcsrchr(wchar_t const* s, int c) { return *s == static_cast(c) && (!*s || !sprout::wcsrchr(s + 1, c))? s diff --git a/sprout/cwchar/wcsspn.hpp b/sprout/cwchar/wcsspn.hpp index f43bf342..a636e3d0 100644 --- a/sprout/cwchar/wcsspn.hpp +++ b/sprout/cwchar/wcsspn.hpp @@ -1,26 +1,20 @@ #ifndef SPROUT_CWCHAR_WCSSPN_HPP -#define SPROUT_CWCHAR_XXX_HPP +#define SPROUT_CWCHAR_WCSSPN_HPP #include #include -#include +#include namespace sprout { - // Copyright (C) 2011 RiSK (sscrisk) - - namespace detail { - inline SPROUT_CONSTEXPR std::size_t - wcsspn_impl(wchar_t const* s1, wchar_t const* s2, std::size_t n) { - return !*s1 || !sprout::wcschr(s2, *s1) ? n - : sprout::detail::wcsspn_impl(s1 + 1, s2, n + 1) - ; - } - } // namespace detail - + // + // wcsspn + // inline SPROUT_CONSTEXPR std::size_t wcsspn(wchar_t const* s1, wchar_t const* s2) { - return sprout::detail::wcsspn_impl(s1, s2, 0); + return !*s1 || !sprout::wcschr(s2, *s1) ? 0 + : 1 + sprout::wcsspn(s1 + 1, s2) + ; } } // namespace sprout -#endif // #ifndef SPROUT_CWCHAR_XXX_HPP +#endif // #ifndef SPROUT_CWCHAR_WCSSPN_HPP diff --git a/sprout/cwchar/wmemchr.hpp b/sprout/cwchar/wmemchr.hpp index e4b92b00..e77638bd 100644 --- a/sprout/cwchar/wmemchr.hpp +++ b/sprout/cwchar/wmemchr.hpp @@ -3,28 +3,52 @@ #include #include +#include +#include HDR_ALGORITHM_SSCRISK_CEL_OR_SPROUT namespace sprout { - // Copyright (C) 2011 RiSK (sscrisk) - namespace detail { inline SPROUT_CONSTEXPR wchar_t const* - wmemchr_impl(wchar_t const* s, wchar_t c, std::size_t n) { - return !n ? 0 - : *s == c ? s - : sprout::detail::wmemchr_impl(s + 1, c, n - 1) - ; + wmemchr_impl(wchar_t const* found, wchar_t const* last) { + return found == last ? nullptr + : found + ; + } + inline SPROUT_CONSTEXPR wchar_t* + wmemchr_impl(wchar_t* found, wchar_t* last) { + return found == last ? nullptr + : found + ; } } // namespace detail + // + // wmemchr + // inline SPROUT_CONSTEXPR wchar_t const* wmemchr(wchar_t const* s, wchar_t c, size_t n) { - return sprout::detail::wmemchr_impl(s, c, n); + return sprout::detail::wmemchr_impl( + sprout::as_iterator_base( + NS_SSCRISK_CEL_OR_SPROUT::find( + sprout::as_iterator(s), sprout::as_iterator(s, n), + c + ) + ), + s + n + ); } inline SPROUT_CONSTEXPR wchar_t* wmemchr(wchar_t* s, wchar_t c, size_t n) { - return const_cast(sprout::detail::wmemchr_impl(s, c, n)); + return sprout::detail::wmemchr_impl( + sprout::as_iterator_base( + NS_SSCRISK_CEL_OR_SPROUT::find( + sprout::as_iterator(s), sprout::as_iterator(s, n), + c + ) + ), + s + n + ); } } // namespace sprout diff --git a/sprout/cwchar/wmemcmp.hpp b/sprout/cwchar/wmemcmp.hpp index 003061f8..650555e2 100644 --- a/sprout/cwchar/wmemcmp.hpp +++ b/sprout/cwchar/wmemcmp.hpp @@ -3,23 +3,19 @@ #include #include +#include +#include namespace sprout { - // Copyright (C) 2011 RiSK (sscrisk) - - namespace detail { - inline SPROUT_CONSTEXPR int - wmemcmp_impl(wchar_t const* s1, wchar_t const* s2, std::size_t n) { - return !n ? 0 - : *s1 == *s2 ? sprout::detail::wmemcmp_impl(s1 + 1, s2 + 1, n - 1) - : *s1 - *s2 - ; - } - } // namespace detail - + // + // wmemcmp + // inline SPROUT_CONSTEXPR int wmemcmp(wchar_t const* s1, wchar_t const* s2, std::size_t n) { - return sprout::detail::wmemcmp_impl(s1, s2, n); + return sprout::tristate_lexicographical_compare( + sprout::as_iterator(s1), sprout::as_iterator(s1, n), + sprout::as_iterator(s2), sprout::as_iterator(s2, n) + ); } } // namespace sprout diff --git a/sprout/string/char_traits.hpp b/sprout/string/char_traits.hpp index 7de7a07c..46798a23 100644 --- a/sprout/string/char_traits.hpp +++ b/sprout/string/char_traits.hpp @@ -44,6 +44,13 @@ namespace sprout { : found ; } + template + static SPROUT_CONSTEXPR std::size_t len(Iterator s, std::size_t n) { + return NS_SSCRISK_CEL_OR_SPROUT::distance( + s, + NS_SSCRISK_CEL_OR_SPROUT::find(s, s + n, char_type()) + ); + } public: static void assign(char_type& c1, char_type const& c2) SPROUT_NOEXCEPT { impl_type::assign(c1, c2); @@ -56,8 +63,8 @@ namespace sprout { } static SPROUT_CONSTEXPR int compare(char_type const* s1, char_type const* s2, std::size_t n) { return sprout::tristate_lexicographical_compare( - sprout::as_iterator(s1), sprout::as_iterator(s1, n), - sprout::as_iterator(s2), sprout::as_iterator(s2, n), + sprout::as_iterator(s1), sprout::as_iterator(s1, len(sprout::as_iterator(s1), n)), + sprout::as_iterator(s2), sprout::as_iterator(s2, len(sprout::as_iterator(s2), n)), lt_() ); } @@ -105,7 +112,7 @@ namespace sprout { template static SPROUT_CONSTEXPR int compare(char_type const* s1, ConstIterator s2, std::size_t n) { return sprout::tristate_lexicographical_compare( - sprout::as_iterator(s1), sprout::as_iterator(s1, n), + sprout::as_iterator(s1), sprout::as_iterator(s1, len(sprout::as_iterator(s1), n)), s2, s2 + n, lt_() ); @@ -114,7 +121,7 @@ namespace sprout { static SPROUT_CONSTEXPR int compare(ConstIterator s1, char_type const* s2, std::size_t n) { return sprout::tristate_lexicographical_compare( s1, s1 + n, - sprout::as_iterator(s2), sprout::as_iterator(s2, n), + sprout::as_iterator(s2), sprout::as_iterator(s2, len(sprout::as_iterator(s2), n)), lt_() ); }