╭ string.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭ find file ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ ╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │ │ │http│ ssh │ │ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1 const builtin = @import("builtin"); 2 const std = @import("std"); 3 4 const c = std.c; 5 const testing = std.testing; 6 7 test "strncmp" { 8 try testing.expect(c.strncmp(@ptrCast("a"), @ptrCast("b"), 1) < 0); 9 try testing.expect(c.strncmp(@ptrCast("a"), @ptrCast("c"), 1) < 0); 10 try testing.expect(c.strncmp(@ptrCast("b"), @ptrCast("a"), 1) > 0); 11 try testing.expect(c.strncmp(@ptrCast("\xff"), @ptrCast("\x02"), 1) > 0); 12 } 13 14 test "strdup" { 15 const org: [*:0]const u8 = "a"; 16 const cpy_opt = c.strdup(@ptrCast(org)); 17 const cpy = cpy_opt orelse return error.OutOfMemory; 18 defer c.free(cpy); 19 20 const cpy_u8: [*:0]u8 = @ptrCast(cpy); 21 try testing.expectEqualStrings(std.mem.span(org), std.mem.span(@as([*:0]const u8, cpy_u8))); 22 try testing.expect(@intFromPtr(cpy_u8) != @intFromPtr(org)); 23 24 cpy_u8[0] = 'b'; 25 try testing.expectEqualStrings("a", std.mem.span(org)); 26 try testing.expectEqualStrings("b", std.mem.span(@as([*:0]const u8, cpy_u8))); 27 } 28 29 test "strndup" { 30 if (builtin.target.os.tag == .windows) return; // no strndup 31 const org1: [*:0]const u8 = "Hello"; 32 33 const copy1_opt = c.strndup(@ptrCast(org1), 100); 34 const copy1 = copy1_opt orelse return error.OutOfMemory; 35 defer c.free(copy1); 36 const copy1_u8: [*:0]u8 = @ptrCast(copy1); 37 try testing.expectEqualStrings("Hello", std.mem.span(@as([*:0]const u8, copy1_u8))); 38 39 const org2: [*:0]const u8 = "Hello World!"; 40 const copy2_opt = c.strndup(@ptrCast(org2), 5); 41 const copy2 = copy2_opt orelse return error.OutOfMemory; 42 defer c.free(copy2); 43 const copy2_u8: [*:0]u8 = @ptrCast(copy2); 44 try testing.expectEqualStrings("Hello", std.mem.span(@as([*:0]const u8, copy2_u8))); 45 try testing.expectEqual(@as(usize, 5), std.mem.len(copy2_u8)); 46 47 const copy3_opt = c.strndup(@ptrCast(org1), 5); 48 const copy3 = copy3_opt orelse return error.OutOfMemory; 49 defer c.free(copy3); 50 const copy3_u8: [*:0]u8 = @ptrCast(copy3); 51 try testing.expectEqualStrings("Hello", std.mem.span(@as([*:0]const u8, copy3_u8))); 52 } 53 54 test "strnlen" { 55 const str: [*:0]const u8 = "Hello"; 56 try testing.expectEqual(3, c.strnlen(@ptrCast(str), 3)); 57 try testing.expectEqual(4, c.strnlen(@ptrCast(str), 4)); 58 try testing.expectEqual(5, c.strnlen(@ptrCast(str), 5)); 59 try testing.expectEqual(5, c.strnlen(@ptrCast(str), 6)); 60 try testing.expectEqual(5, c.strnlen(@ptrCast(str), 7)); 61 } 62