RESTinio
from_string.hpp
Go to the documentation of this file.
1 /*
2  restinio
3 */
4 
5 /*!
6  Convert strings to numeric types.
7 */
8 
9 #pragma once
10 
11 #include <cctype>
12 #include <string>
13 #include <limits>
14 #include <stdexcept>
15 #include <algorithm>
16 
17 #include <fmt/format.h>
18 
19 #include <restinio/string_view.hpp>
20 #include <restinio/exception.hpp>
21 
23 
24 namespace restinio
25 {
26 
27 namespace utils
28 {
29 
30 //! Read int values.
31 //! \{
32 
33 inline void
34 read_value( std::int64_t & v, const char * data, std::size_t size )
35 {
36  v = details::parse_integer< details::int64_parse_traits_t >( data, data + size );
37 }
38 
39 inline void
40 read_value( std::uint64_t & v, const char * data, std::size_t size )
41 {
42  v = details::parse_integer< details::uint64_parse_traits_t >( data, data + size );
43 }
44 
45 inline void
46 read_value( std::int32_t & v, const char * data, std::size_t size )
47 {
48  v = details::parse_integer< details::int32_parse_traits_t >( data, data + size );
49 }
50 
51 inline void
52 read_value( std::uint32_t & v, const char * data, std::size_t size )
53 {
54  v = details::parse_integer< details::uint32_parse_traits_t >( data, data + size );
55 }
56 
57 inline void
58 read_value( std::int16_t & v, const char * data, std::size_t size )
59 {
60  v = details::parse_integer< details::int16_parse_traits_t >( data, data + size );
61 }
62 
63 inline void
64 read_value( std::uint16_t & v, const char * data, std::size_t size )
65 {
66  v = details::parse_integer< details::uint16_parse_traits_t >( data, data + size );
67 }
68 
69 inline void
70 read_value( std::int8_t & v, const char * data, std::size_t size )
71 {
72  v = details::parse_integer< details::int8_parse_traits_t >( data, data + size );
73 }
74 
75 inline void
76 read_value( std::uint8_t & v, const char * data, std::size_t size )
77 {
78  v = details::parse_integer< details::uint8_parse_traits_t >( data, data + size );
79 }
80 //! \}
81 
82 
83 //! Read float values.
84 //! \{
85 inline void
86 read_value( float & v, const char * data, std::size_t size )
87 {
88  std::string buf{ data, size };
89 
90  v = std::stof( buf );
91 }
92 
93 inline void
94 read_value( double & v, const char * data, std::size_t size )
95 {
96  std::string buf{ data, size };
97 
98  v = std::stod( buf );
99 }
100 //! \}
101 
102 //! Get a value from string.
103 template < typename Value_Type >
104 Value_Type
105 from_string( string_view_t s )
106 {
108 
109  read_value( result, s.data(), s.length() );
110 
111  return result;
112 }
113 
114 //! Get a value from string.
115 template <>
116 inline std::string
118 {
119  return std::string{ s.data(), s.size() };
120 }
121 
122 //! Get a value from string_view.
123 template <>
124 inline string_view_t
126 {
127  return s;
128 }
129 
130 // //! Get a value from string.
131 // template < typename Value_Type >
132 // Value_Type
133 // from_string( const std::string & s )
134 // {
135 // return from_string< Value_Type >( string_view_t{ s.data(), s.size() } );
136 // }
137 
138 } /* namespace utils */
139 
140 } /* namespace restinio */
string_view_t from_string< string_view_t >(string_view_t s)
Get a value from string_view.
void read_value(std::uint8_t &v, const char *data, std::size_t size)
Definition: from_string.hpp:76
void read_value(float &v, const char *data, std::size_t size)
Read float values.
Definition: from_string.hpp:86
void read_value(double &v, const char *data, std::size_t size)
Definition: from_string.hpp:94
Value_Type from_string(string_view_t s)
Get a value from string.
std::enable_if< std::is_same< Parameter_Container, query_string_params_t >::value||std::is_same< Parameter_Container, router::route_params_t >::value, optional_t< Value_Type > >::type opt_value(const Parameter_Container &params, string_view_t key)
Gets the value of a parameter specified by key wrapped in optional_t<Value_Type> if parameter exists ...
Definition: value_or.hpp:64