Iros
 
Loading...
Searching...
No Matches
strtol_implementation.h
Go to the documentation of this file.
1#pragma once
2
4#include <errno.h>
5
6#include "di/math/prelude.h"
7#include "di/util/prelude.h"
8
9namespace ccpp {
10template<di::concepts::OneOf<long, long long, unsigned long, unsigned long long> T>
11T strtol(char const* __restrict string, char** __restrict end, int radix) {
12 auto set_end = [&](char const* value) {
13 if (end) {
14 *end = const_cast<char*>(value);
15 }
16 };
17
18 if (radix < 0 || radix == 1 || radix > 36) {
19 set_end(string);
20 errno = EINVAL;
21 return 0;
22 }
23
24 auto context = di::parser::ZStringParserContext(di::ZCString(string));
25 auto parser = ~di::parser::match_zero_or_more(' '_m || '\f'_m || '\n'_m || '\r'_m || '\t'_m || '\v'_m) >>
27 auto result = parser.parse(context);
28
29 if (!result) {
30 switch (result.error()) {
32 errno = EINVAL;
33 set_end(string);
34 return 0;
36 errno = ERANGE;
37 set_end(context.iterator_on_error().base());
40 errno = ERANGE;
41 set_end(context.iterator_on_error().base());
43 default:
45 }
46 }
47
48 set_end(context.begin().base().base());
49 return *result;
50}
51}
#define EINVAL
Definition errno.h:32
#define ERANGE
Definition errno.h:71
Definition zstring_parser.h:24
#define errno
Definition errno.h:11
Definition getopt.cpp:3
T strtol(char const *__restrict string, char **__restrict end, int radix)
Definition strtol_implementation.h:11
ZStringImpl< char const > ZCString
Definition zstring.h:38
@ Underflow
Definition zstring_parser.h:21
@ Overflow
Definition zstring_parser.h:20
@ Invalid
Definition zstring_parser.h:19
constexpr auto integer
Definition integer.h:212
Definition zstring_parser.h:9
void unreachable()
Definition unreachable.h:4
Definition numeric_limits.h:7