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
18
21
23
24namespace restinio
25{
26
27namespace utils
28{
29
32
33inline void
34read_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
39inline void
40read_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
45inline void
46read_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
51inline void
52read_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
57inline void
58read_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
63inline void
64read_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
69inline void
70read_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
75inline void
76read_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
85inline void
86read_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
93inline void
94read_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
103template < typename Value_Type >
104Value_Type
106{
107 Value_Type result;
108
109 read_value( result, s.data(), s.length() );
110
111 return result;
112}
113
115template <>
116inline std::string
118{
119 return std::string{ s.data(), s.size() };
120}
121
123template <>
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 */
A special wrapper around fmtlib include files.
string_view_t from_string< string_view_t >(string_view_t s)
Get a value from string_view.
void read_value(std::int64_t &v, const char *data, std::size_t size)
Read int values.
Definition: from_string.hpp:34
std::string from_string< std::string >(string_view_t s)
Get a value from string.
Value_Type from_string(string_view_t s)
Get a value from string.
nonstd::string_view string_view_t
Definition: string_view.hpp:19