-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutilities.hpp
More file actions
85 lines (76 loc) · 2.12 KB
/
utilities.hpp
File metadata and controls
85 lines (76 loc) · 2.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#ifndef _FINDNAME_HPP_
#define _FINDNAME_HPP_
#include "utfstring.hpp"
#include "utfstringview.hpp"
namespace hls
{
bool isspace(char32_t codepoint)
{
// TODO: extend to tab character and other spacing characters
return codepoint == 0x20;
}
bool isdigit(char32_t codepoint)
{
return codepoint >= '0' && codepoint <= '9';
}
template <typename CharType>
Result<uint64_t> atou(const UTFStringView<CharType> &str_view)
{
if (!str_view.is_valid_view())
return error<uint64_t>(Error::INVALID_ARGUMENT);
uint64_t nmb = 0;
bool digit_started = false;
for (auto c : str_view)
{
if (!digit_started)
{
if ((digit_started = isdigit(c)))
nmb = c - '0';
else if (isspace(c))
continue;
else
return error<uint64_t>(Error::INVALID_ARGUMENT);
}
else
{
if (isdigit(c))
nmb = nmb * 10 + (c - '0');
else
break;
}
}
return value(nmb);
}
template <typename CharType>
Result<int64_t> atoi(const UTFStringView<CharType> &str_view)
{
if (!str_view.is_valid_view())
return error<int64_t>(Error::INVALID_ARGUMENT);
int64_t nmb = 0;
int64_t sign = 1;
bool digit_started = false;
for (auto c : str_view)
{
if (!digit_started)
{
if (c == '-')
sign = sign * -1;
else if ((digit_started = isdigit(c)))
nmb = sign * (c - '0');
else if (isspace(c))
continue;
else
return error<int64_t>(Error::INVALID_ARGUMENT);
}
else
{
if (isdigit(c))
nmb = nmb * 10 + sign * (c - '0');
else
break;
}
}
return value(nmb);
}
} // namespace hls
#endif