100.00% Lines (25/25) 100.00% Functions (6/6)
TLA Baseline Branch
Line Hits Code Line Hits Code
1   // 1   //
2   // Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com) 2   // Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com)
3   // 3   //
4   // Distributed under the Boost Software License, Version 1.0. (See accompanying 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) 5   // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6   // 6   //
7   // Official repository: https://github.com/CPPAlliance/http 7   // Official repository: https://github.com/CPPAlliance/http
8   // 8   //
9   9  
10   #ifndef BOOST_HTTP_DETAIL_NUMBER_STRING_HPP 10   #ifndef BOOST_HTTP_DETAIL_NUMBER_STRING_HPP
11   #define BOOST_HTTP_DETAIL_NUMBER_STRING_HPP 11   #define BOOST_HTTP_DETAIL_NUMBER_STRING_HPP
12   12  
13   #include <boost/http/detail/config.hpp> 13   #include <boost/http/detail/config.hpp>
14   #include <boost/core/detail/string_view.hpp> 14   #include <boost/core/detail/string_view.hpp>
15   #include <cstdint> 15   #include <cstdint>
16   16  
17   namespace boost { 17   namespace boost {
18   namespace http { 18   namespace http {
19   namespace detail { 19   namespace detail {
20   20  
21   // Convert integer to decimal 21   // Convert integer to decimal
22   // string using in-place storage 22   // string using in-place storage
23   class number_string 23   class number_string
24   { 24   {
25   static constexpr unsigned buf_size = 18; 25   static constexpr unsigned buf_size = 18;
26   char buf_[buf_size + 1]; 26   char buf_[buf_size + 1];
27   std::size_t size_ = 0; 27   std::size_t size_ = 0;
28   28  
29   void 29   void
HITCBC 30   37 construct_unsigned( 30   37 construct_unsigned(
31   std::uint64_t n) noexcept 31   std::uint64_t n) noexcept
32   { 32   {
HITCBC 33   37 buf_[buf_size] = '\0'; 33   37 buf_[buf_size] = '\0';
HITCBC 34   37 auto const end = 34   37 auto const end =
35   &buf_[buf_size]; 35   &buf_[buf_size];
HITCBC 36   37 auto p = end; 36   37 auto p = end;
HITCBC 37   37 if(n == 0) 37   37 if(n == 0)
38   { 38   {
HITCBC 39   6 *--p = '0'; 39   6 *--p = '0';
40   } 40   }
41   else 41   else
42   { 42   {
HITCBC 43   74 while(n > 0) 43   74 while(n > 0)
44   { 44   {
HITCBC 45   43 *--p = '0' + (n%10); 45   43 *--p = '0' + (n%10);
HITCBC 46   43 n /= 10; 46   43 n /= 10;
47   } 47   }
48   } 48   }
HITCBC 49   37 size_ = end - p; 49   37 size_ = end - p;
HITCBC 50   37 } 50   37 }
51   51  
52   public: 52   public:
53   number_string() = default; 53   number_string() = default;
54   number_string( 54   number_string(
55   number_string const&) = default; 55   number_string const&) = default;
56   number_string& operator= 56   number_string& operator=
57   (number_string const&) = default; 57   (number_string const&) = default;
58   58  
59   explicit 59   explicit
HITCBC 60   37 number_string( 60   37 number_string(
61   std::uint64_t n) noexcept 61   std::uint64_t n) noexcept
HITCBC 62   37 { 62   37 {
HITCBC 63   37 construct_unsigned(n); 63   37 construct_unsigned(n);
HITCBC 64   37 } 64   37 }
65   65  
66   char const* 66   char const*
HITCBC 67   37 data() const noexcept 67   37 data() const noexcept
68   { 68   {
HITCBC 69   37 return &buf_[ 69   37 return &buf_[
HITCBC 70   37 buf_size] - size_; 70   37 buf_size] - size_;
71   } 71   }
72   72  
73   std::size_t 73   std::size_t
HITCBC 74   37 size() const noexcept 74   37 size() const noexcept
75   { 75   {
HITCBC 76   37 return size_; 76   37 return size_;
77   } 77   }
78   78  
79   core::string_view 79   core::string_view
HITCBC 80   37 str() const noexcept 80   37 str() const noexcept
81   { 81   {
HITCBC 82   37 return core::string_view( 82   37 return core::string_view(
HITCBC 83   37 data(), size()); 83   37 data(), size());
84   } 84   }
85   85  
HITCBC 86   37 operator 86   37 operator
87   core::string_view() const noexcept 87   core::string_view() const noexcept
88   { 88   {
HITCBC 89   37 return str(); 89   37 return str();
90   } 90   }
91   }; 91   };
92   92  
93   } // detail 93   } // detail
94   } // http 94   } // http
95   } // boost 95   } // boost
96   96  
97   #endif 97   #endif