Line | Branch | Exec | Source |
---|---|---|---|
1 | // | ||
2 | // Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com) | ||
3 | // | ||
4 | // Distributed under the Boost Software License, Version 1.0. (See accompanying | ||
5 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | ||
6 | // | ||
7 | // Official repository: https://github.com/boostorg/url | ||
8 | // | ||
9 | |||
10 | #ifndef BOOST_URL_DETAIL_MOVE_CHARS_HPP | ||
11 | #define BOOST_URL_DETAIL_MOVE_CHARS_HPP | ||
12 | |||
13 | #include <boost/core/detail/string_view.hpp> | ||
14 | #include <boost/assert.hpp> | ||
15 | #include <cstring> | ||
16 | #include <functional> | ||
17 | |||
18 | namespace boost { | ||
19 | namespace urls { | ||
20 | namespace detail { | ||
21 | |||
22 | // Moves characters, and adjusts any passed | ||
23 | // views if they point to any moved characters. | ||
24 | |||
25 | // true if s completely overlapped by buf | ||
26 | inline | ||
27 | bool | ||
28 | 1288 | is_overlapping( | |
29 | core::string_view buf, | ||
30 | core::string_view s) noexcept | ||
31 | { | ||
32 | 1288 | auto const b0 = buf.data(); | |
33 | 1288 | auto const e0 = b0 + buf.size(); | |
34 | 1288 | auto const b1 = s.data(); | |
35 | 1288 | auto const e1 = b1 + s.size(); | |
36 | auto const less_equal = | ||
37 | std::less_equal<char const*>(); | ||
38 |
2/2✓ Branch 1 taken 88 times.
✓ Branch 2 taken 1200 times.
|
1288 | if(less_equal(e0, b1)) |
39 | 88 | return false; | |
40 |
2/2✓ Branch 1 taken 1198 times.
✓ Branch 2 taken 2 times.
|
1200 | if(less_equal(e1, b0)) |
41 | 1198 | return false; | |
42 | // partial overlap is undefined | ||
43 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
|
2 | BOOST_ASSERT(less_equal(e1, e0)); |
44 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
|
2 | BOOST_ASSERT(less_equal(b0, b1)); |
45 | 2 | return true; | |
46 | } | ||
47 | |||
48 | inline | ||
49 | void | ||
50 | 1908 | move_chars_impl( | |
51 | std::ptrdiff_t, | ||
52 | core::string_view const&) noexcept | ||
53 | { | ||
54 | 1908 | } | |
55 | |||
56 | template<class... Sn> | ||
57 | void | ||
58 | 2576 | move_chars_impl( | |
59 | std::ptrdiff_t d, | ||
60 | core::string_view const& buf, | ||
61 | core::string_view& s, | ||
62 | Sn&... sn) noexcept | ||
63 | { | ||
64 |
2/2✓ Branch 1 taken 2 times.
✓ Branch 2 taken 1286 times.
|
2576 | if(is_overlapping(buf, s)) |
65 | 4 | s = {s.data() + d, s.size()}; | |
66 | 2576 | move_chars_impl(d, buf, sn...); | |
67 | 2576 | } | |
68 | |||
69 | template<class... Args> | ||
70 | void | ||
71 | 3816 | move_chars( | |
72 | char* dest, | ||
73 | char const* src, | ||
74 | std::size_t n, | ||
75 | Args&... args) noexcept | ||
76 | { | ||
77 | 3816 | core::string_view buf(src, n); | |
78 | 3816 | move_chars_impl( | |
79 | dest - src, | ||
80 | core::string_view(src, n), | ||
81 | args...); | ||
82 | 3816 | std::memmove( | |
83 | dest, src, n); | ||
84 | 3816 | } | |
85 | |||
86 | } // detail | ||
87 | } // urls | ||
88 | } // boost | ||
89 | |||
90 | #endif | ||
91 |