Skip to content

Commit 18f8a14

Browse files
committed
Better documentation
1 parent aaf4011 commit 18f8a14

File tree

1 file changed

+23
-12
lines changed

1 file changed

+23
-12
lines changed

lib/std/ascii.zig

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
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
910

1011
const std = @import("std");
1112

1213
/// 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`
1416
pub const control_code = struct {
1517
pub const NUL = 0x00;
1618
pub const SOH = 0x01;
@@ -47,7 +49,9 @@ pub const control_code = struct {
4749

4850
pub const DEL = 0x7F;
4951

52+
/// An alias to `DC1`.
5053
pub const XON = 0x11;
54+
/// An alias to `DC3`.
5155
pub const XOFF = 0x13;
5256
};
5357

@@ -188,15 +192,20 @@ fn inTable(c: u8, t: tIndex) bool {
188192
return (combinedTable[c] & (@as(u8, 1) << @enumToInt(t))) != 0;
189193
}
190194

195+
/// Returns whether the character is alphanumeric. This is case-insensitive.
191196
pub fn isAlNum(c: u8) bool {
192197
return (combinedTable[c] & ((@as(u8, 1) << @enumToInt(tIndex.Alpha)) |
193198
@as(u8, 1) << @enumToInt(tIndex.Digit))) != 0;
194199
}
195200

201+
/// Returns whether the character is alphabetic. This is case-insensitive.
196202
pub fn isAlpha(c: u8) bool {
197203
return inTable(c, tIndex.Alpha);
198204
}
199205

206+
/// Returns whether the character is a control character.
207+
///
208+
/// See also: `control`
200209
pub fn isCntrl(c: u8) bool {
201210
return c < 0x20 or c == 127; //DEL
202211
}
@@ -213,6 +222,7 @@ pub fn isLower(c: u8) bool {
213222
return inTable(c, tIndex.Lower);
214223
}
215224

225+
/// Returns whether the character has some graphical representation and can be printed.
216226
pub fn isPrint(c: u8) bool {
217227
return inTable(c, tIndex.Graph) or c == ' ';
218228
}
@@ -225,8 +235,8 @@ pub fn isSpace(c: u8) bool {
225235
return inTable(c, tIndex.Space);
226236
}
227237

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.
230240
pub const spaces = [_]u8{ ' ', '\t', '\n', '\r', control_code.VT, control_code.FF };
231241

232242
test "spaces" {
@@ -243,6 +253,7 @@ pub fn isUpper(c: u8) bool {
243253
return inTable(c, tIndex.Upper);
244254
}
245255

256+
/// Returns whether the character is a hexadecimal digit. This is case-insensitive.
246257
pub fn isXDigit(c: u8) bool {
247258
return inTable(c, tIndex.Hex);
248259
}
@@ -415,7 +426,7 @@ pub fn orderIgnoreCase(lhs: []const u8, rhs: []const u8) std.math.Order {
415426
return std.math.order(lhs.len, rhs.len);
416427
}
417428

418-
/// Returns true if lhs < rhs, false otherwise
429+
/// Returns whether lhs < rhs.
419430
/// TODO rename "IgnoreCase" to "Insensitive" in this entire file.
420431
pub fn lessThanIgnoreCase(lhs: []const u8, rhs: []const u8) bool {
421432
return orderIgnoreCase(lhs, rhs) == .lt;

0 commit comments

Comments
 (0)