1 /**
2     ASCII Helpers
3     
4     Copyright:
5         Copyright © 2023-2025, Kitsunebi Games
6         Copyright © 2023-2025, Inochi2D Project
7     
8     License:   $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost License 1.0)
9     Authors:   Luna Nielsen
10 */
11 module nulib.text.ascii;
12 
13 @nogc nothrow:
14 
15 /**
16     Gets whether the character is ascii
17 */
18 bool isASCII(char c) {
19     return c < 128;
20 }
21 
22 /**
23     Gets whether the character is a hexidedcimal digit
24 */
25 bool isHex(char c) {
26     return 
27         (c >= 'a' && c <= 'f') || 
28         (c >= 'A' && c <= 'F') || 
29         (c >= '0' && c <= '9');
30 }
31 
32 /**
33     Gets whether the given character is uppercase.
34 */
35 bool isUpper(char c) {
36     return (c >= 'A' && c <= 'Z');
37 }
38 
39 /**
40     Gets whether the given character is lowercase.
41 */
42 bool isLower(char c) {
43     return (c >= 'a' && c <= 'z');
44 }
45 
46 /**
47     Gets whether the character is numeric.
48 */
49 bool isDigit(char c) {
50     return (c >= '0' && c <= '9');
51 }
52 
53 /**
54     Gets whether the character is alphabetic.
55 */
56 bool isAlpha(char c) {
57     return 
58         (c >= 'a' && c <= 'z') || 
59         (c >= 'A' && c <= 'Z');
60 }
61 
62 /**
63     Gets whether the character is alpha-numeric.
64 */
65 bool isAlphaNumeric(char c) {
66     return 
67         (c >= 'a' && c <= 'z') || 
68         (c >= 'A' && c <= 'Z') || 
69         (c >= '0' && c <= '9');
70 }
71 
72 /**
73     Gets whether the character is printable
74 */
75 bool isPrintable(char c) {
76     return (c >= 32 && c < 126);
77 }
78 
79 /**
80     Gets whether the character is an ASCII non-printable escape character
81 */
82 bool isEscapeCharacter(char c) {
83     return
84         (c >= 0 && c <= 31) || 
85         (c == 127);
86 }
87 
88 /**
89     Converts the given ascii character to lowercase.
90 */
91 char toLower(char c) {
92     if (isUpper(c))
93         return cast(char)(c + 32);
94     
95     return c;
96 }
97 
98 /**
99     Converts the given ascii character to uppercase.
100 */
101 char toUpper(char c) {
102     if (isLower(c))
103         return cast(char)(c - 32);
104     
105     return c;
106 }