fix cstring, cwchar

This commit is contained in:
bolero-MURAKAMI 2012-12-22 01:02:49 +09:00
parent 8227569c43
commit 0670013702
17 changed files with 172 additions and 127 deletions

View file

@ -3,30 +3,49 @@
#include <cstddef>
#include <sprout/config.hpp>
#include <sprout/iterator/ptr_index_iterator.hpp>
#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 ŠÖ<C5A0>
inline SPROUT_CONSTEXPR void const*
memchr(void const* s, int c, size_t n) {
return sprout::detail::memchr_impl(static_cast<unsigned char const*>(s), static_cast<unsigned char>(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<unsigned char const*>(s)), sprout::as_iterator(static_cast<unsigned char const*>(s), n),
static_cast<unsigned char>(c)
)
),
static_cast<unsigned char const*>(s) + n
);
}
inline SPROUT_CONSTEXPR void*
memchr(void* s, int c, size_t n) {
return const_cast<void*>(
sprout::detail::memchr_impl(static_cast<unsigned char*>(s), static_cast<unsigned char>(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<unsigned char*>(s)), sprout::as_iterator(static_cast<unsigned char*>(s), n),
static_cast<unsigned char>(c)
)
),
static_cast<unsigned char*>(s) + n
);
}
} // namespace sprout

View file

@ -3,27 +3,16 @@
#include <cstddef>
#include <sprout/config.hpp>
#include <sprout/iterator/ptr_index_iterator.hpp>
#include <sprout/algorithm/tristate_lexicographical_compare.hpp>
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 ŠÖ<C5A0>
inline SPROUT_CONSTEXPR int
memcmp(void const* s1, void const* s2, std::size_t n) {
return sprout::detail::memcmp_impl(
static_cast<unsigned char const*>(s1),
static_cast<unsigned char const*>(s2),
n
return sprout::tristate_lexicographical_compare(
sprout::as_iterator(static_cast<unsigned char const*>(s1)), sprout::as_iterator(static_cast<unsigned char const*>(s1), n),
sprout::as_iterator(static_cast<unsigned char const*>(s2)), sprout::as_iterator(static_cast<unsigned char const*>(s2), n)
);
}
} // namespace sprout

View file

@ -6,21 +6,12 @@
#include <sprout/cstring/strchr.hpp>
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 ŠÖ<C5A0>
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

View file

@ -3,6 +3,9 @@
#include <cstddef>
#include <sprout/config.hpp>
#include <sprout/iterator/ptr_index_iterator.hpp>
#include HDR_ALGORITHM_SSCRISK_CEL_OR_SPROUT
#include HDR_ITERATOR_SSCRISK_CEL_OR_SPROUT
namespace sprout {
// 7.21.6.3 strlen ŠÖ<C5A0>
@ -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

View file

@ -3,19 +3,19 @@
#include <cstddef>
#include <sprout/config.hpp>
#include <sprout/iterator/ptr_index_iterator.hpp>
#include <sprout/algorithm/tristate_lexicographical_compare.hpp>
#include <sprout/cstring/strlen.hpp>
#include HDR_ALGORITHM_SSCRISK_CEL_OR_SPROUT
namespace sprout {
// Copyright (C) 2011 RiSK (sscrisk)
// 7.21.4.4 strncmp ŠÖ<C5A0>
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<unsigned char>(*s1) - static_cast<unsigned char>(*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

View file

@ -1,27 +1,18 @@
#ifndef SPROUT_CSTRING_STRSPN_HPP
#define SPROUT_CSTRING_XXX_HPP
#define SPROUT_CSTRING_STRSPN_HPP
#include <cstddef>
#include <sprout/config.hpp>
#include <sprout/cstring/strchr.hpp>
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 ŠÖ<C5A0>
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

View file

@ -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

View file

@ -2,11 +2,14 @@
#define SPROUT_CWCHAR_WCSCOLL_HPP
#include <sprout/config.hpp>
#include <sprout/cstring/strcmp.hpp>
#include <sprout/cwchar/wcscmp.hpp>
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);

View file

@ -3,23 +3,17 @@
#include <cstddef>
#include <sprout/config.hpp>
#include <sprout/cstring/strchr.hpp>
#include <sprout/cwchar/wcschr.hpp>
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

View file

@ -3,14 +3,28 @@
#include <cstddef>
#include <sprout/config.hpp>
#include <sprout/iterator/ptr_index_iterator.hpp>
#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

View file

@ -3,18 +3,21 @@
#include <cstddef>
#include <sprout/config.hpp>
#include <sprout/iterator/ptr_index_iterator.hpp>
#include <sprout/algorithm/tristate_lexicographical_compare.hpp>
#include <sprout/cwchar/wcslen.hpp>
#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

View file

@ -3,11 +3,14 @@
#include <cstddef>
#include <sprout/config.hpp>
#include <sprout/cstring/strchr.hpp>
#include <sprout/cwchar/wcschr.hpp>
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

View file

@ -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<wchar_t>(c) && (!*s || !sprout::wcsrchr(s + 1, c))? s

View file

@ -1,26 +1,20 @@
#ifndef SPROUT_CWCHAR_WCSSPN_HPP
#define SPROUT_CWCHAR_XXX_HPP
#define SPROUT_CWCHAR_WCSSPN_HPP
#include <cstddef>
#include <sprout/config.hpp>
#include <sprout/cstring/strchr.hpp>
#include <sprout/cwchar/wcschr.hpp>
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

View file

@ -3,28 +3,52 @@
#include <cstddef>
#include <sprout/config.hpp>
#include <sprout/iterator/ptr_index_iterator.hpp>
#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<wchar_t*>(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

View file

@ -3,23 +3,19 @@
#include <cstddef>
#include <sprout/config.hpp>
#include <sprout/iterator/ptr_index_iterator.hpp>
#include <sprout/algorithm/tristate_lexicographical_compare.hpp>
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

View file

@ -44,6 +44,13 @@ namespace sprout {
: found
;
}
template<typename Iterator>
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<typename ConstIterator>
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_()
);
}