1
- // Does NOT look at the locale the way C89's toupper(3), isspace() et cetera does.
2
- // I could have taken only a u7 to make this clear, but it would be slower
3
- // It is my opinion that encodings other than UTF-8 should not be supported.
4
- //
5
- // (and 128 bytes is not much to pay).
6
- // Also does not handle Unicode character classes.
7
- //
8
- // https://upload.wikimedia.org/wikipedia/commons/thumb/c/cf/USASCII_code_chart.png/1200px-USASCII_code_chart.png
1
+ //! The 7-bit [ASCII](https://en.wikipedia.org/wiki/ASCII) character encoding standard.
2
+ //!
3
+ //! This is not to be confused with the 8-bit [Extended ASCII](https://en.wikipedia.org/wiki/Extended_ASCII).
4
+ //!
5
+ //! Even though this module concerns itself with 7-bit ASCII,
6
+ //! functions use `u8` as the type instead of `u7` for convenience and compatibility.
7
+ //! Characters outside of the 7-bit range are gracefully handled (e.g. by returning `false`).
8
+ //!
9
+ //! See also: https://en.wikipedia.org/wiki/ASCII#Character_set
9
10
10
11
const std = @import ("std" );
11
12
12
13
/// Contains constants for the C0 control codes of the ASCII encoding.
13
- /// https://en.wikipedia.org/wiki/C0_and_C1_control_codes
14
+ ///
15
+ /// See also: https://en.wikipedia.org/wiki/C0_and_C1_control_codes and `is_control`
14
16
pub const control_code = struct {
15
17
pub const NUL = 0x00 ;
16
18
pub const SOH = 0x01 ;
@@ -47,7 +49,9 @@ pub const control_code = struct {
47
49
48
50
pub const DEL = 0x7F ;
49
51
52
+ /// An alias to `DC1`.
50
53
pub const XON = 0x11 ;
54
+ /// An alias to `DC3`.
51
55
pub const XOFF = 0x13 ;
52
56
};
53
57
@@ -188,15 +192,20 @@ fn inTable(c: u8, t: tIndex) bool {
188
192
return (combinedTable [c ] & (@as (u8 , 1 ) << @enumToInt (t ))) != 0 ;
189
193
}
190
194
195
+ /// Returns whether the character is alphanumeric. This is case-insensitive.
191
196
pub fn isAlNum (c : u8 ) bool {
192
197
return (combinedTable [c ] & ((@as (u8 , 1 ) << @enumToInt (tIndex .Alpha )) |
193
198
@as (u8 , 1 ) << @enumToInt (tIndex .Digit ))) != 0 ;
194
199
}
195
200
201
+ /// Returns whether the character is alphabetic. This is case-insensitive.
196
202
pub fn isAlpha (c : u8 ) bool {
197
203
return inTable (c , tIndex .Alpha );
198
204
}
199
205
206
+ /// Returns whether the character is a control character.
207
+ ///
208
+ /// See also: `control`
200
209
pub fn isCntrl (c : u8 ) bool {
201
210
return c < 0x20 or c == 127 ; //DEL
202
211
}
@@ -213,6 +222,7 @@ pub fn isLower(c: u8) bool {
213
222
return inTable (c , tIndex .Lower );
214
223
}
215
224
225
+ /// Returns whether the character has some graphical representation and can be printed.
216
226
pub fn isPrint (c : u8 ) bool {
217
227
return inTable (c , tIndex .Graph ) or c == ' ' ;
218
228
}
@@ -225,8 +235,8 @@ pub fn isSpace(c: u8) bool {
225
235
return inTable (c , tIndex .Space );
226
236
}
227
237
228
- /// All the values for which isSpace() returns true. This may be used with
229
- /// e.g. std.mem.trim() to trim whiteSpace .
238
+ /// All the values for which ` isSpace()` returns ` true`.
239
+ /// This may be used with e.g. ` std.mem.trim()` to trim spaces .
230
240
pub const spaces = [_ ]u8 { ' ' , '\t ' , '\n ' , '\r ' , control_code .VT , control_code .FF };
231
241
232
242
test "spaces" {
@@ -243,6 +253,7 @@ pub fn isUpper(c: u8) bool {
243
253
return inTable (c , tIndex .Upper );
244
254
}
245
255
256
+ /// Returns whether the character is a hexadecimal digit. This is case-insensitive.
246
257
pub fn isXDigit (c : u8 ) bool {
247
258
return inTable (c , tIndex .Hex );
248
259
}
@@ -415,7 +426,7 @@ pub fn orderIgnoreCase(lhs: []const u8, rhs: []const u8) std.math.Order {
415
426
return std .math .order (lhs .len , rhs .len );
416
427
}
417
428
418
- /// Returns true if lhs < rhs, false otherwise
429
+ /// Returns whether lhs < rhs.
419
430
/// TODO rename "IgnoreCase" to "Insensitive" in this entire file.
420
431
pub fn lessThanIgnoreCase (lhs : []const u8 , rhs : []const u8 ) bool {
421
432
return orderIgnoreCase (lhs , rhs ) == .lt ;
0 commit comments