| 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/CPPAlliance/http_proto | ||
| 8 | // | ||
| 9 | |||
| 10 | #ifndef BOOST_HTTP_PROTO_DETAIL_MOVE_CHARS_HPP | ||
| 11 | #define BOOST_HTTP_PROTO_DETAIL_MOVE_CHARS_HPP | ||
| 12 | |||
| 13 | #include <boost/http_proto/string_view.hpp> | ||
| 14 | #include <cstring> | ||
| 15 | #include <functional> | ||
| 16 | |||
| 17 | namespace boost { | ||
| 18 | namespace http_proto { | ||
| 19 | namespace detail { | ||
| 20 | |||
| 21 | // Moves characters, and adjusts any passed | ||
| 22 | // views if they point to any moved characters. | ||
| 23 | |||
| 24 | // true if s completely overlapped by buf | ||
| 25 | inline | ||
| 26 | bool | ||
| 27 | 60 | is_overlapping( | |
| 28 | string_view buf, | ||
| 29 | string_view s) noexcept | ||
| 30 | { | ||
| 31 | 60 | auto const b0 = buf.data(); | |
| 32 | 60 | auto const e0 = b0 + buf.size(); | |
| 33 | 60 | auto const b1 = s.data(); | |
| 34 | 60 | auto const e1 = b1 + s.size(); | |
| 35 | auto const less_equal = | ||
| 36 | std::less_equal<char const*>(); | ||
| 37 | 
        2/2✓ Branch 1 taken 15 times. 
          ✓ Branch 2 taken 45 times. 
         | 
      60 | if(less_equal(e0, b1)) | 
| 38 | 15 | return false; | |
| 39 | 
        2/2✓ Branch 1 taken 39 times. 
          ✓ Branch 2 taken 6 times. 
         | 
      45 | if(less_equal(e1, b0)) | 
| 40 | 39 | return false; | |
| 41 | // partial overlap is undefined | ||
| 42 | 
        1/2✗ Branch 1 not taken. 
          ✓ Branch 2 taken 6 times. 
         | 
      6 | BOOST_ASSERT(less_equal(e1, e0)); | 
| 43 | 
        1/2✗ Branch 1 not taken. 
          ✓ Branch 2 taken 6 times. 
         | 
      6 | BOOST_ASSERT(less_equal(b0, b1)); | 
| 44 | 6 | return true; | |
| 45 | } | ||
| 46 | |||
| 47 | inline | ||
| 48 | void | ||
| 49 | 37 | move_chars_impl( | |
| 50 | std::ptrdiff_t, | ||
| 51 | string_view const&) noexcept | ||
| 52 | { | ||
| 53 | 37 | } | |
| 54 | |||
| 55 | template<class... Sn> | ||
| 56 | void | ||
| 57 | 148 | move_chars_impl( | |
| 58 | std::ptrdiff_t d, | ||
| 59 | string_view const& buf, | ||
| 60 | string_view* s, | ||
| 61 | Sn&&... sn) noexcept | ||
| 62 | { | ||
| 63 | 
        6/6✓ Branch 0 taken 60 times. 
          ✓ Branch 1 taken 14 times. 
          ✓ Branch 2 taken 6 times. 
          ✓ Branch 3 taken 54 times. 
          ✓ Branch 4 taken 6 times. 
          ✓ Branch 5 taken 68 times. 
         | 
      268 | if( s != nullptr && | 
| 64 | 120 | is_overlapping(buf, *s)) | |
| 65 | { | ||
| 66 | 12 | *s = { s->data() + d, s->size() }; | |
| 67 | } | ||
| 68 | 148 | move_chars_impl(d, buf, sn...); | |
| 69 | 148 | } | |
| 70 | |||
| 71 | template<class... Args> | ||
| 72 | void | ||
| 73 | 37 | move_chars( | |
| 74 | char* dest, | ||
| 75 | char const* src, | ||
| 76 | std::size_t n, | ||
| 77 | Args&&... args) noexcept | ||
| 78 | { | ||
| 79 | 37 | move_chars_impl( | |
| 80 | dest - src, | ||
| 81 | string_view(src, n), | ||
| 82 | args...); | ||
| 83 | 37 | std::memmove( | |
| 84 | dest, src, n); | ||
| 85 | 37 | } | |
| 86 | |||
| 87 | } // detail | ||
| 88 | } // http_proto | ||
| 89 | } // boost | ||
| 90 | |||
| 91 | #endif | ||
| 92 |