| ... | @@ -288,36 +288,62 @@ test "ascii character classes" { | ... | @@ -288,36 +288,62 @@ test "ascii character classes" { |
| 288 | try testing.expect(isSpace(' ')); | 288 | try testing.expect(isSpace(' ')); |
| 289 | } | 289 | } |
| 290 | | 290 | |
| | 291 | /// Writes a lower case copy of `ascii_string` to `output`. |
| | 292 | /// Asserts `output.len >= ascii_string.len`. |
| | 293 | pub fn lowerString(output: []u8, ascii_string: []const u8) []u8 { |
| | 294 | std.debug.assert(output.len >= ascii_string.len); |
| | 295 | for (ascii_string) |c, i| { |
| | 296 | output[i] = toLower(c); |
| | 297 | } |
| | 298 | return output[0..ascii_string.len]; |
| | 299 | } |
| | 300 | |
| | 301 | test "lowerString" { |
| | 302 | var buf: [1024]u8 = undefined; |
| | 303 | const result = lowerString(&buf, "aBcDeFgHiJkLmNOPqrst0234+💩!"); |
| | 304 | try std.testing.expectEqualStrings("abcdefghijklmnopqrst0234+💩!", result); |
| | 305 | } |
| | 306 | |
| 291 | /// Allocates a lower case copy of `ascii_string`. | 307 | /// Allocates a lower case copy of `ascii_string`. |
| 292 | /// Caller owns returned string and must free with `allocator`. | 308 | /// Caller owns returned string and must free with `allocator`. |
| 293 | pub fn allocLowerString(allocator: *std.mem.Allocator, ascii_string: []const u8) ![]u8 { | 309 | pub fn allocLowerString(allocator: *std.mem.Allocator, ascii_string: []const u8) ![]u8 { |
| 294 | const result = try allocator.alloc(u8, ascii_string.len); | 310 | const result = try allocator.alloc(u8, ascii_string.len); |
| 295 | for (result) |*c, i| { | 311 | return lowerString(result, ascii_string); |
| 296 | c.* = toLower(ascii_string[i]); | | |
| 297 | } | | |
| 298 | return result; | | |
| 299 | } | 312 | } |
| 300 | | 313 | |
| 301 | test "allocLowerString" { | 314 | test "allocLowerString" { |
| 302 | const result = try allocLowerString(std.testing.allocator, "aBcDeFgHiJkLmNOPqrst0234+💩!"); | 315 | const result = try allocLowerString(std.testing.allocator, "aBcDeFgHiJkLmNOPqrst0234+💩!"); |
| 303 | defer std.testing.allocator.free(result); | 316 | defer std.testing.allocator.free(result); |
| 304 | try std.testing.expect(std.mem.eql(u8, "abcdefghijklmnopqrst0234+💩!", result)); | 317 | try std.testing.expectEqualStrings("abcdefghijklmnopqrst0234+💩!", result); |
| | 318 | } |
| | 319 | |
| | 320 | /// Writes an upper case copy of `ascii_string` to `output`. |
| | 321 | /// Asserts `output.len >= ascii_string.len`. |
| | 322 | pub fn upperString(output: []u8, ascii_string: []const u8) []u8 { |
| | 323 | std.debug.assert(output.len >= ascii_string.len); |
| | 324 | for (ascii_string) |c, i| { |
| | 325 | output[i] = toUpper(c); |
| | 326 | } |
| | 327 | return output[0..ascii_string.len]; |
| | 328 | } |
| | 329 | |
| | 330 | test "upperString" { |
| | 331 | var buf: [1024]u8 = undefined; |
| | 332 | const result = upperString(&buf, "aBcDeFgHiJkLmNOPqrst0234+💩!"); |
| | 333 | try std.testing.expectEqualStrings("ABCDEFGHIJKLMNOPQRST0234+💩!", result); |
| 305 | } | 334 | } |
| 306 | | 335 | |
| 307 | /// Allocates an upper case copy of `ascii_string`. | 336 | /// Allocates an upper case copy of `ascii_string`. |
| 308 | /// Caller owns returned string and must free with `allocator`. | 337 | /// Caller owns returned string and must free with `allocator`. |
| 309 | pub fn allocUpperString(allocator: *std.mem.Allocator, ascii_string: []const u8) ![]u8 { | 338 | pub fn allocUpperString(allocator: *std.mem.Allocator, ascii_string: []const u8) ![]u8 { |
| 310 | const result = try allocator.alloc(u8, ascii_string.len); | 339 | const result = try allocator.alloc(u8, ascii_string.len); |
| 311 | for (result) |*c, i| { | 340 | return upperString(result, ascii_string); |
| 312 | c.* = toUpper(ascii_string[i]); | | |
| 313 | } | | |
| 314 | return result; | | |
| 315 | } | 341 | } |
| 316 | | 342 | |
| 317 | test "allocUpperString" { | 343 | test "allocUpperString" { |
| 318 | const result = try allocUpperString(std.testing.allocator, "aBcDeFgHiJkLmNOPqrst0234+💩!"); | 344 | const result = try allocUpperString(std.testing.allocator, "aBcDeFgHiJkLmNOPqrst0234+💩!"); |
| 319 | defer std.testing.allocator.free(result); | 345 | defer std.testing.allocator.free(result); |
| 320 | try std.testing.expect(std.mem.eql(u8, "ABCDEFGHIJKLMNOPQRST0234+💩!", result)); | 346 | try std.testing.expectEqualStrings("ABCDEFGHIJKLMNOPQRST0234+💩!", result); |
| 321 | } | 347 | } |
| 322 | | 348 | |
| 323 | /// Compares strings `a` and `b` case insensitively and returns whether they are equal. | 349 | /// Compares strings `a` and `b` case insensitively and returns whether they are equal. |