9
9
//! See also: https://en.wikipedia.org/wiki/ASCII#Character_set
10
10
11
11
const std = @import ("std" );
12
+ const testing = std .testing ;
12
13
13
14
/// Contains constants for the C0 control codes of the ASCII encoding.
14
15
///
@@ -213,7 +214,6 @@ pub fn isSpace(c: u8) bool {
213
214
pub const spaces = [_ ]u8 { ' ' , '\t ' , '\n ' , '\r ' , control_code .VT , control_code .FF };
214
215
215
216
test "spaces" {
216
- const testing = std .testing ;
217
217
for (spaces ) | space | try testing .expect (isSpace (space ));
218
218
219
219
var i : u8 = 0 ;
@@ -259,15 +259,50 @@ pub fn toLower(c: u8) u8 {
259
259
}
260
260
261
261
test "ascii character classes" {
262
- const testing = std .testing ;
262
+ try testing .expect (! isCntrl ('a' ));
263
+ try testing .expect (! isCntrl ('z' ));
264
+ try testing .expect (isCntrl (control_code .NUL ));
265
+ try testing .expect (isCntrl (control_code .FF ));
266
+ try testing .expect (isCntrl (control_code .US ));
263
267
264
268
try testing .expect ('C' == toUpper ('c' ));
265
269
try testing .expect (':' == toUpper (':' ));
266
270
try testing .expect ('\xab ' == toUpper ('\xab ' ));
271
+ try testing .expect (! isUpper ('z' ));
272
+
267
273
try testing .expect ('c' == toLower ('C' ));
274
+ try testing .expect (':' == toLower (':' ));
275
+ try testing .expect ('\xab ' == toLower ('\xab ' ));
276
+ try testing .expect (! isLower ('Z' ));
277
+
278
+ try testing .expect (isAlNum ('Z' ));
279
+ try testing .expect (isAlNum ('z' ));
280
+ try testing .expect (isAlNum ('5' ));
281
+ try testing .expect (isAlNum ('5' ));
282
+ try testing .expect (! isAlNum ('!' ));
283
+
284
+ try testing .expect (! isAlpha ('5' ));
268
285
try testing .expect (isAlpha ('c' ));
269
286
try testing .expect (! isAlpha ('5' ));
287
+
270
288
try testing .expect (isSpace (' ' ));
289
+ try testing .expect (isSpace ('\t ' ));
290
+ try testing .expect (isSpace ('\r ' ));
291
+ try testing .expect (isSpace ('\n ' ));
292
+ try testing .expect (! isSpace ('.' ));
293
+
294
+ try testing .expect (! isXDigit ('g' ));
295
+ try testing .expect (isXDigit ('b' ));
296
+ try testing .expect (isXDigit ('9' ));
297
+
298
+ try testing .expect (! isDigit ('~' ));
299
+ try testing .expect (isDigit ('0' ));
300
+ try testing .expect (isDigit ('9' ));
301
+
302
+ try testing .expect (isPrint (' ' ));
303
+ try testing .expect (isPrint ('@' ));
304
+ try testing .expect (isPrint ('~' ));
305
+ try testing .expect (! isPrint (control_code .ESC ));
271
306
}
272
307
273
308
/// Writes a lower case copy of `ascii_string` to `output`.
@@ -283,7 +318,7 @@ pub fn lowerString(output: []u8, ascii_string: []const u8) []u8 {
283
318
test "lowerString" {
284
319
var buf : [1024 ]u8 = undefined ;
285
320
const result = lowerString (& buf , "aBcDeFgHiJkLmNOPqrst0234+💩!" );
286
- try std . testing .expectEqualStrings ("abcdefghijklmnopqrst0234+💩!" , result );
321
+ try testing .expectEqualStrings ("abcdefghijklmnopqrst0234+💩!" , result );
287
322
}
288
323
289
324
/// Allocates a lower case copy of `ascii_string`.
@@ -294,9 +329,9 @@ pub fn allocLowerString(allocator: std.mem.Allocator, ascii_string: []const u8)
294
329
}
295
330
296
331
test "allocLowerString" {
297
- const result = try allocLowerString (std . testing .allocator , "aBcDeFgHiJkLmNOPqrst0234+💩!" );
298
- defer std . testing .allocator .free (result );
299
- try std . testing .expectEqualStrings ("abcdefghijklmnopqrst0234+💩!" , result );
332
+ const result = try allocLowerString (testing .allocator , "aBcDeFgHiJkLmNOPqrst0234+💩!" );
333
+ defer testing .allocator .free (result );
334
+ try testing .expectEqualStrings ("abcdefghijklmnopqrst0234+💩!" , result );
300
335
}
301
336
302
337
/// Writes an upper case copy of `ascii_string` to `output`.
@@ -312,7 +347,7 @@ pub fn upperString(output: []u8, ascii_string: []const u8) []u8 {
312
347
test "upperString" {
313
348
var buf : [1024 ]u8 = undefined ;
314
349
const result = upperString (& buf , "aBcDeFgHiJkLmNOPqrst0234+💩!" );
315
- try std . testing .expectEqualStrings ("ABCDEFGHIJKLMNOPQRST0234+💩!" , result );
350
+ try testing .expectEqualStrings ("ABCDEFGHIJKLMNOPQRST0234+💩!" , result );
316
351
}
317
352
318
353
/// Allocates an upper case copy of `ascii_string`.
@@ -323,12 +358,12 @@ pub fn allocUpperString(allocator: std.mem.Allocator, ascii_string: []const u8)
323
358
}
324
359
325
360
test "allocUpperString" {
326
- const result = try allocUpperString (std . testing .allocator , "aBcDeFgHiJkLmNOPqrst0234+💩!" );
327
- defer std . testing .allocator .free (result );
328
- try std . testing .expectEqualStrings ("ABCDEFGHIJKLMNOPQRST0234+💩!" , result );
361
+ const result = try allocUpperString (testing .allocator , "aBcDeFgHiJkLmNOPqrst0234+💩!" );
362
+ defer testing .allocator .free (result );
363
+ try testing .expectEqualStrings ("ABCDEFGHIJKLMNOPQRST0234+💩!" , result );
329
364
}
330
365
331
- /// Compares strings `a` and `b` case insensitively and returns whether they are equal.
366
+ /// Compares strings `a` and `b` case- insensitively and returns whether they are equal.
332
367
pub fn eqlIgnoreCase (a : []const u8 , b : []const u8 ) bool {
333
368
if (a .len != b .len ) return false ;
334
369
for (a ) | a_c , i | {
@@ -338,27 +373,27 @@ pub fn eqlIgnoreCase(a: []const u8, b: []const u8) bool {
338
373
}
339
374
340
375
test "eqlIgnoreCase" {
341
- try std . testing .expect (eqlIgnoreCase ("HEl💩Lo!" , "hel💩lo!" ));
342
- try std . testing .expect (! eqlIgnoreCase ("hElLo!" , "hello! " ));
343
- try std . testing .expect (! eqlIgnoreCase ("hElLo!" , "helro!" ));
376
+ try testing .expect (eqlIgnoreCase ("HEl💩Lo!" , "hel💩lo!" ));
377
+ try testing .expect (! eqlIgnoreCase ("hElLo!" , "hello! " ));
378
+ try testing .expect (! eqlIgnoreCase ("hElLo!" , "helro!" ));
344
379
}
345
380
346
381
pub fn startsWithIgnoreCase (haystack : []const u8 , needle : []const u8 ) bool {
347
382
return if (needle .len > haystack .len ) false else eqlIgnoreCase (haystack [0.. needle .len ], needle );
348
383
}
349
384
350
385
test "ascii.startsWithIgnoreCase" {
351
- try std . testing .expect (startsWithIgnoreCase ("boB" , "Bo" ));
352
- try std . testing .expect (! startsWithIgnoreCase ("Needle in hAyStAcK" , "haystack" ));
386
+ try testing .expect (startsWithIgnoreCase ("boB" , "Bo" ));
387
+ try testing .expect (! startsWithIgnoreCase ("Needle in hAyStAcK" , "haystack" ));
353
388
}
354
389
355
390
pub fn endsWithIgnoreCase (haystack : []const u8 , needle : []const u8 ) bool {
356
391
return if (needle .len > haystack .len ) false else eqlIgnoreCase (haystack [haystack .len - needle .len .. ], needle );
357
392
}
358
393
359
394
test "ascii.endsWithIgnoreCase" {
360
- try std . testing .expect (endsWithIgnoreCase ("Needle in HaYsTaCk" , "haystack" ));
361
- try std . testing .expect (! endsWithIgnoreCase ("BoB" , "Bo" ));
395
+ try testing .expect (endsWithIgnoreCase ("Needle in HaYsTaCk" , "haystack" ));
396
+ try testing .expect (! endsWithIgnoreCase ("BoB" , "Bo" ));
362
397
}
363
398
364
399
/// Finds `substr` in `container`, ignoring case, starting at `start_index`.
@@ -380,12 +415,11 @@ pub fn indexOfIgnoreCase(container: []const u8, substr: []const u8) ?usize {
380
415
}
381
416
382
417
test "indexOfIgnoreCase" {
383
- try std .testing .expect (indexOfIgnoreCase ("one Two Three Four" , "foUr" ).? == 14 );
384
- try std .testing .expect (indexOfIgnoreCase ("one two three FouR" , "gOur" ) == null );
385
- try std .testing .expect (indexOfIgnoreCase ("foO" , "Foo" ).? == 0 );
386
- try std .testing .expect (indexOfIgnoreCase ("foo" , "fool" ) == null );
387
-
388
- try std .testing .expect (indexOfIgnoreCase ("FOO foo" , "fOo" ).? == 0 );
418
+ try testing .expect (indexOfIgnoreCase ("one Two Three Four" , "foUr" ).? == 14 );
419
+ try testing .expect (indexOfIgnoreCase ("one two three FouR" , "gOur" ) == null );
420
+ try testing .expect (indexOfIgnoreCase ("foO" , "Foo" ).? == 0 );
421
+ try testing .expect (indexOfIgnoreCase ("foo" , "fool" ) == null );
422
+ try testing .expect (indexOfIgnoreCase ("FOO foo" , "fOo" ).? == 0 );
389
423
}
390
424
391
425
/// Compares two slices of numbers lexicographically. O(n).
0 commit comments