forked from ares-emulator/ares
-
Notifications
You must be signed in to change notification settings - Fork 0
/
atoi.hpp
87 lines (71 loc) · 2.13 KB
/
atoi.hpp
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
86
87
#pragma once
#include <nall/stdint.hpp>
namespace nall {
constexpr inline auto toBinary_(const char* s, u64 sum = 0) -> u64 {
return (
*s == '0' || *s == '1' ? toBinary_(s + 1, (sum << 1) | *s - '0') :
*s == '\'' ? toBinary_(s + 1, sum) :
sum
);
}
constexpr inline auto toOctal_(const char* s, u64 sum = 0) -> u64 {
return (
*s >= '0' && *s <= '7' ? toOctal_(s + 1, (sum << 3) | *s - '0') :
*s == '\'' ? toOctal_(s + 1, sum) :
sum
);
}
constexpr inline auto toDecimal_(const char* s, u64 sum = 0) -> u64 {
return (
*s >= '0' && *s <= '9' ? toDecimal_(s + 1, (sum * 10) + *s - '0') :
*s == '\'' ? toDecimal_(s + 1, sum) :
sum
);
}
constexpr inline auto toHex_(const char* s, u64 sum = 0) -> u64 {
return (
*s >= 'A' && *s <= 'F' ? toHex_(s + 1, (sum << 4) | *s - 'A' + 10) :
*s >= 'a' && *s <= 'f' ? toHex_(s + 1, (sum << 4) | *s - 'a' + 10) :
*s >= '0' && *s <= '9' ? toHex_(s + 1, (sum << 4) | *s - '0') :
*s == '\'' ? toHex_(s + 1, sum) :
sum
);
}
//
constexpr inline auto toBinary(const char* s) -> u64 {
return (
*s == '0' && (*(s + 1) == 'B' || *(s + 1) == 'b') ? toBinary_(s + 2) :
*s == '%' ? toBinary_(s + 1) : toBinary_(s)
);
}
constexpr inline auto toOctal(const char* s) -> u64 {
return (
*s == '0' && (*(s + 1) == 'O' || *(s + 1) == 'o') ? toOctal_(s + 2) :
toOctal_(s)
);
}
constexpr inline auto toHex(const char* s) -> u64 {
return (
*s == '0' && (*(s + 1) == 'X' || *(s + 1) == 'x') ? toHex_(s + 2) :
*s == '$' ? toHex_(s + 1) : toHex_(s)
);
}
//
constexpr inline auto toNatural(const char* s) -> u64 {
return (
*s == '0' && (*(s + 1) == 'B' || *(s + 1) == 'b') ? toBinary_(s + 2) :
*s == '0' && (*(s + 1) == 'O' || *(s + 1) == 'o') ? toOctal_(s + 2) :
*s == '0' && (*(s + 1) == 'X' || *(s + 1) == 'x') ? toHex_(s + 2) :
*s == '%' ? toBinary_(s + 1) : *s == '$' ? toHex_(s + 1) : toDecimal_(s)
);
}
constexpr inline auto toInteger(const char* s) -> s64 {
return (
*s == '+' ? +toNatural(s + 1) : *s == '-' ? -toNatural(s + 1) : toNatural(s)
);
}
//
inline auto toReal(const char* s) -> f64 {
return atof(s);
}
}