Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:53:26

0001 //
0002 // Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com)
0003 //
0004 // Distributed under the Boost Software License, Version 1.0. (See accompanying
0005 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0006 //
0007 // Official repository: https://github.com/boostorg/url
0008 //
0009 
0010 #ifndef BOOST_URL_DETAIL_MOVE_CHARS_HPP
0011 #define BOOST_URL_DETAIL_MOVE_CHARS_HPP
0012 
0013 #include <boost/core/detail/string_view.hpp>
0014 #include <boost/assert.hpp>
0015 #include <cstring>
0016 #include <functional>
0017 
0018 namespace boost {
0019 namespace urls {
0020 namespace detail {
0021 
0022 // Moves characters, and adjusts any passed
0023 // views if they point to any moved characters.
0024 
0025 // true if s completely overlapped by buf
0026 inline
0027 bool
0028 is_overlapping(
0029     core::string_view buf,
0030     core::string_view s) noexcept
0031 {
0032     auto const b0 = buf.data();
0033     auto const e0 = b0 + buf.size();
0034     auto const b1 = s.data();
0035     auto const e1 = b1 + s.size();
0036     auto const less_equal =
0037         std::less_equal<char const*>();
0038     if(less_equal(e0, b1))
0039         return false;
0040     if(less_equal(e1, b0))
0041         return false;
0042     // partial overlap is undefined
0043     BOOST_ASSERT(less_equal(e1, e0));
0044     BOOST_ASSERT(less_equal(b0, b1));
0045     return true;
0046 }
0047 
0048 inline
0049 void
0050 move_chars_impl(
0051     std::ptrdiff_t,
0052     core::string_view const&) noexcept
0053 {
0054 }
0055 
0056 template<class... Sn>
0057 void
0058 move_chars_impl(
0059     std::ptrdiff_t d,
0060     core::string_view const& buf,
0061     core::string_view& s,
0062     Sn&... sn) noexcept
0063 {
0064     if(is_overlapping(buf, s))
0065         s = {s.data() + d, s.size()};
0066     move_chars_impl(d, buf, sn...);
0067 }
0068 
0069 template<class... Args>
0070 void
0071 move_chars(
0072     char* dest,
0073     char const* src,
0074     std::size_t n,
0075     Args&... args) noexcept
0076 {
0077     core::string_view buf(src, n);
0078     move_chars_impl(
0079         dest - src,
0080         core::string_view(src, n),
0081         args...);
0082     std::memmove(
0083         dest, src, n);
0084 }
0085 
0086 } // detail
0087 } // urls
0088 } // boost
0089 
0090 #endif