mirror of
https://github.com/bolero-MURAKAMI/Sprout.git
synced 2025-07-14 15:04:09 +00:00
add <cstring> and <cwchar> compatible constexpr functions
This commit is contained in:
parent
fe255a5e74
commit
b04dea6002
26 changed files with 526 additions and 103 deletions
49
sprout/cstring/strncpy.hpp
Normal file
49
sprout/cstring/strncpy.hpp
Normal file
|
@ -0,0 +1,49 @@
|
|||
/*=============================================================================
|
||||
Copyright (c) 2011-2014 Bolero MURAKAMI
|
||||
https://github.com/bolero-MURAKAMI/Sprout
|
||||
|
||||
Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||
file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
=============================================================================*/
|
||||
#ifndef SPROUT_CSTRING_STRNCPY_HPP
|
||||
#define SPROUT_CSTRING_STRNCPY_HPP
|
||||
|
||||
#include <iterator>
|
||||
#include <sprout/config.hpp>
|
||||
|
||||
namespace sprout {
|
||||
namespace detail {
|
||||
template<typename OutputCStrIterator, typename CStrIterator>
|
||||
inline SPROUT_CXX14_CONSTEXPR OutputCStrIterator
|
||||
strncpy(OutputCStrIterator s1, CStrIterator s2, std::size_t n) {
|
||||
typedef typename std::iterator_traits<OutputCStrIterator>::value_type value_type;
|
||||
OutputCStrIterator result = s1;
|
||||
while (n) {
|
||||
--n;
|
||||
if (!(*s1++ = *s2++)) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
while (n) {
|
||||
--n;
|
||||
*s1++ = value_type();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
} // namespace detail
|
||||
|
||||
// 7.21.2.4 The strncpy function
|
||||
//
|
||||
inline SPROUT_CXX14_CONSTEXPR char*
|
||||
strncpy(char* s1, char const* s2, std::size_t n) {
|
||||
return sprout::detail::strncpy(s1, s2, n);
|
||||
}
|
||||
|
||||
template<typename Elem>
|
||||
inline SPROUT_CXX14_CONSTEXPR Elem*
|
||||
strncpy(Elem* s1, Elem const* s2, std::size_t n) {
|
||||
return sprout::detail::strncpy(s1, s2, n);
|
||||
}
|
||||
} // namespace sprout
|
||||
|
||||
#endif // #ifndef SPROUT_CSTRING_STRNCPY_HPP
|
Loading…
Add table
Add a link
Reference in a new issue