dconv 1.0
C++14 library for printing and parsing floating point numbers
Loading...
Searching...
No Matches
view.hpp
Go to the documentation of this file.
1
25#ifndef __DCONV_VIEW_HPP__
26#define __DCONV_VIEW_HPP__
27
28// C++.
29#include <string>
30
31// C.
32#include <cstring>
33#include <cstddef>
34
35#define likely(x) __builtin_expect((x), 1)
36#define unlikely(x) __builtin_expect((x), 0)
37
38namespace dconv
39{
43 class View
44 {
45 public:
51 explicit constexpr View (const char * s, size_t count)
52 : _pos (s)
53 , _end (s ? s + count : s)
54 {
55 }
56
62 constexpr View (const char * first, const char * last)
63 : _pos (first)
64 , _end (last)
65 {
66 }
67
72 explicit View (const char * s)
73 : _pos (s)
74 , _end (s ? s + std::char_traits <char>::length (s) : s)
75 {
76 }
77
82 View (const View& other) noexcept = default;
83
89 View& operator= (const View& other) noexcept = default;
90
95 View (View&& other) noexcept = default;
96
102 View& operator=(View&& other) noexcept = default;
103
107 ~View () = default;
108
113 inline int peek () const noexcept
114 {
115 if (likely (_pos < _end))
116 {
117 return static_cast <unsigned char> (*_pos);
118 }
119 return std::char_traits <char>::eof ();
120 }
121
126 inline int get () noexcept
127 {
128 if (likely (_pos < _end))
129 {
130 return static_cast <unsigned char> (*_pos++);
131 }
132 return std::char_traits <char>::eof ();
133 }
134
140 inline bool getIf (char expected) noexcept
141 {
142 if (likely (_pos < _end) && (*_pos == expected))
143 {
144 ++_pos;
145 return true;
146 }
147 return false;
148 }
149
155 inline bool getIfNoCase (char expected) noexcept
156 {
157 if (likely (_pos < _end))
158 {
159 const char c = *_pos;
160 if ((c | 32) == (expected | 32))
161 {
162 ++_pos;
163 return true;
164 }
165 }
166 return false;
167 }
168
173 inline const char * data () const noexcept
174 {
175 return _pos;
176 }
177
182 inline size_t size () const noexcept
183 {
184 return _end - _pos;
185 }
186
187 private:
189 const char * _pos = nullptr;
190
192 const char * _end = nullptr;
193 };
194}
195
196#endif
char array view.
Definition view.hpp:44
View & operator=(const View &other) noexcept=default
copy assignment.
View(const View &other) noexcept=default
copy constructor.
int peek() const noexcept
get character without extracting it.
Definition view.hpp:113
View(const char *s)
default constructor.
Definition view.hpp:72
bool getIf(char expected) noexcept
extracts expected character (case sensitive).
Definition view.hpp:140
const char * data() const noexcept
returns a pointer to the first character of a view.
Definition view.hpp:173
View(View &&other) noexcept=default
move constructor.
View & operator=(View &&other) noexcept=default
move assignment.
constexpr View(const char *s, size_t count)
default constructor.
Definition view.hpp:51
size_t size() const noexcept
returns the number of characters in the view.
Definition view.hpp:182
~View()=default
destroy instance.
constexpr View(const char *first, const char *last)
default constructor.
Definition view.hpp:62
bool getIfNoCase(char expected) noexcept
extracts expected character (case insensitive, ASCII-only).
Definition view.hpp:155
int get() noexcept
extracts character.
Definition view.hpp:126
Definition atod.hpp:43
#define likely(x)
Definition view.hpp:35