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_NUMBER_STRING_HPP |
11 |
|
|
#define BOOST_HTTP_PROTO_DETAIL_NUMBER_STRING_HPP |
12 |
|
|
|
13 |
|
|
#include <boost/http_proto/detail/config.hpp> |
14 |
|
|
#include <cstdint> |
15 |
|
|
|
16 |
|
|
namespace boost { |
17 |
|
|
namespace http_proto { |
18 |
|
|
namespace detail { |
19 |
|
|
|
20 |
|
|
// Convert integer to decimal |
21 |
|
|
// string using in-place storage |
22 |
|
|
class number_string |
23 |
|
|
{ |
24 |
|
|
static constexpr unsigned buf_size = 18; |
25 |
|
|
char buf_[buf_size + 1]; |
26 |
|
|
std::size_t size_ = 0; |
27 |
|
|
|
28 |
|
|
void |
29 |
|
✗ |
construct_unsigned( |
30 |
|
|
std::uint64_t n) noexcept |
31 |
|
|
{ |
32 |
|
✗ |
buf_[buf_size] = '\0'; |
33 |
|
✗ |
auto const end = |
34 |
|
|
&buf_[buf_size]; |
35 |
|
✗ |
auto p = end; |
36 |
|
✗ |
if(n == 0) |
37 |
|
|
{ |
38 |
|
✗ |
*--p = '0'; |
39 |
|
|
} |
40 |
|
|
else |
41 |
|
|
{ |
42 |
|
✗ |
while(n > 0) |
43 |
|
|
{ |
44 |
|
✗ |
*--p = '0' + (n%10); |
45 |
|
✗ |
n /= 10; |
46 |
|
|
} |
47 |
|
|
} |
48 |
|
✗ |
size_ = end - p; |
49 |
|
|
} |
50 |
|
|
|
51 |
|
|
public: |
52 |
|
|
number_string() = default; |
53 |
|
|
number_string( |
54 |
|
|
number_string const&) = default; |
55 |
|
|
number_string& operator= |
56 |
|
|
(number_string const&) = default; |
57 |
|
|
|
58 |
|
|
explicit |
59 |
|
✗ |
number_string( |
60 |
|
|
std::uint64_t n) noexcept |
61 |
|
|
{ |
62 |
|
✗ |
construct_unsigned(n); |
63 |
|
|
} |
64 |
|
|
|
65 |
|
|
char const* |
66 |
|
✗ |
data() const noexcept |
67 |
|
|
{ |
68 |
|
✗ |
return &buf_[ |
69 |
|
✗ |
buf_size] - size_; |
70 |
|
|
} |
71 |
|
|
|
72 |
|
|
std::size_t |
73 |
|
✗ |
size() const noexcept |
74 |
|
|
{ |
75 |
|
✗ |
return size_; |
76 |
|
|
} |
77 |
|
|
|
78 |
|
|
string_view |
79 |
|
✗ |
str() const noexcept |
80 |
|
|
{ |
81 |
|
✗ |
return string_view( |
82 |
|
✗ |
data(), size()); |
83 |
|
|
} |
84 |
|
|
|
85 |
|
✗ |
operator |
86 |
|
|
string_view() const noexcept |
87 |
|
|
{ |
88 |
|
✗ |
return str(); |
89 |
|
|
} |
90 |
|
|
}; |
91 |
|
|
|
92 |
|
|
} // detail |
93 |
|
|
} // http_proto |
94 |
|
|
} // boost |
95 |
|
|
|
96 |
|
|
#endif |
97 |
|
|
|