RESTinio

from_string.hpp
Go to the documentation of this file.
1/*
2 restinio
3*/
4
9#pragma once
10
11#include <cctype>
12#include <string>
13#include <limits>
14#include <stdexcept>
15#include <algorithm>
16
17#include <restinio/impl/include_fmtlib.hpp>
18
19#include <restinio/string_view.hpp>
20#include <restinio/exception.hpp>
21
22#include "from_string_details.ipp"
23
24 namespace restinio
25{
26
27 namespace utils
28{
29
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}
81
82
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}
101
103 template < typename Value_Type >
104Value_Type
105 from_string( string_view_t s )
106{
107 Value_Type result;
108
109 read_value( result, s.data(), s.length() );
110
111 return result;
112}
113
115 template <>
116 inline std::string
117 from_string< std::string >( string_view_t s )
118{
119 return std::string{ s.data(), s.size() };
120}
121
123 template <>
124 inline string_view_t
125 from_string< string_view_t >( string_view_t s )
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 */
exception.hpp
from_string_details.ipp
include_fmtlib.hpp
A special wrapper around fmtlib include files.
restinio::utils::from_string< string_view_t >
string_view_t from_string< string_view_t >(string_view_t s)
Get a value from string_view.
Definition: from_string.hpp:125
restinio::utils::read_value
void read_value(std::int64_t &v, const char *data, std::size_t size)
Read int values.
Definition: from_string.hpp:34
restinio::utils::from_string< std::string >
std::string from_string< std::string >(string_view_t s)
Get a value from string.
Definition: from_string.hpp:117
restinio::utils::from_string
Value_Type from_string(string_view_t s)
Get a value from string.
Definition: from_string.hpp:105
restinio
Definition: asio_include.hpp:21
restinio::string_view_t
nonstd::string_view string_view_t
Definition: string_view.hpp:19
string_view.hpp