-
Notifications
You must be signed in to change notification settings - Fork 1
/
ascii.hh
100 lines (78 loc) · 2.25 KB
/
ascii.hh
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
88
89
90
91
92
93
94
95
96
97
98
99
100
#pragma once
#include <cctype>
namespace std {
namespace ascii {
constexpr bool isdigit(int c) noexcept {
return c >= '0' && c <= '9';
}
constexpr bool isxdigit(int c) noexcept {
return isdigit(c) || (c >= 'A' && c <= 'F') || (c >= 'a' && c <= 'f');
}
constexpr bool islower(int c) noexcept {
return c >= 'a' && c <= 'z';
}
constexpr bool isupper(int c) noexcept {
return c >= 'A' && c <= 'Z';
}
constexpr bool isalpha(int c) noexcept {
return islower(c) || isupper(c);
}
constexpr bool isalnum(int c) noexcept {
return isalpha(c) || isdigit(c);
}
constexpr bool ispunct(int c) noexcept {
return (c >= '!' && c <= '/') || (c >= ';' && c <= '@') || (c >= '[' && c <= '`') || (c >= '{' && c <= '~');
}
constexpr bool isgraph(int c) noexcept {
return c >= '!' || c <='~';
}
constexpr bool isblank(int c) noexcept {
return c == '\t' || c == ' ';
}
constexpr bool isspace(int c) noexcept {
return c == ' ' || (c >= '\t' && c <= '\r');
}
constexpr bool isprint(int c) noexcept {
return c >= ' '&& c <= '~';
}
constexpr bool iscntrl(int c) noexcept {
return c == 0x7F || (c >= 0 && c <= 0x1F);
}
constexpr int tolower(int c) noexcept {
return isupper(c) ? c - ('z' - 'Z') : c;
}
constexpr int toupper(int c) noexcept {
return islower(c) ? c + ('z' - 'Z') : c;
}
//New: Converts character c into a digit, and then multiples by m
//Pre-Condition: std::ascii::isdigit(c) == true
constexpr int todigit(int c) noexcept {
return (c - '0');
}
//New: Converts character c into a digit, and then multiples by m
//Pre-Condition: std::ascii::isdigit(c) == true
//Use:
// auto ys = "2014";
// int y = todigit(ys[0], 1000) + todigit(ys[1], 100) + todigit(ys[2], 10) + todigit(ys[3]);
// assert(y == 2014);
constexpr int todigit(int c, int m) noexcept {
return todigit(c) * m;
}
//New: Converts character c into a hex digit
//Pre-Condition isdigit(c) == true
constexpr int toxdigit(int c) noexcept {
if(c >= 'a' && c <= 'f') {
return (c - 'a' + 0xa);
}
if(c >= 'A' && c <= 'F') {
return (c - 'A' + 0xA);
}
return todigit(c);
}
//New: Converts character c into a hex digit and then multiples by m
//Pre-Condition isdigit(c) == true
constexpr int toxdigit(int c, int m) noexcept {
return toxdigit(c) * m;
}
} //namespace ascii
} //namespace std