authorgravatar for bratishkaerik@getgoogleoff.meEric Joldasov <bratishkaerik@getgoogleoff.me> 2023-06-25 21:33:55+06:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-06-25 14:51:03-07:00
log0a868dacdd31b7d5c529a332da718683477a2505
treea5d91a35d7f66b78c26110923eda548e52eb6258
parent852eb272bf895ee6fc242e298e31d666caa9d0df

std.cstr: deprecate namespace

Signed-off-by: Eric Joldasov <bratishkaerik@getgoogleoff.me>

12 files changed, 38 insertions(+), 116 deletions(-)

lib/c.zig+5-1
......@@ -153,7 +153,11 @@ test "strncat" {
153153}
154154
155155fn strcmp(s1: [*:0]const u8, s2: [*:0]const u8) callconv(.C) c_int {
156 return std.cstr.cmp(s1, s2);
156 return switch (std.mem.orderZ(u8, s1, s2)) {
157 .lt => -1,
158 .eq => 0,
159 .gt => 1,
160 };
157161}
158162
159163fn strlen(s: [*:0]const u8) callconv(.C) usize {
lib/std/child_process.zig-1
......@@ -1,6 +1,5 @@
11const std = @import("std.zig");
22const builtin = @import("builtin");
3const cstr = std.cstr;
43const unicode = std.unicode;
54const io = std.io;
65const fs = std.fs;
lib/std/cstr.zig+3-100
......@@ -1,100 +1,3 @@
1const std = @import("std.zig");
2const builtin = @import("builtin");
3const debug = std.debug;
4const mem = std.mem;
5const testing = std.testing;
6
7pub const line_sep = switch (builtin.os.tag) {
8 .windows => "\r\n",
9 else => "\n",
10};
11
12pub fn cmp(a: [*:0]const u8, b: [*:0]const u8) i8 {
13 var index: usize = 0;
14 while (a[index] == b[index] and a[index] != 0) : (index += 1) {}
15 if (a[index] > b[index]) {
16 return 1;
17 } else if (a[index] < b[index]) {
18 return -1;
19 } else {
20 return 0;
21 }
22}
23
24test "cstr fns" {
25 try comptime testCStrFnsImpl();
26 try testCStrFnsImpl();
27}
28
29fn testCStrFnsImpl() !void {
30 try testing.expect(cmp("aoeu", "aoez") == -1);
31}
32
33/// Returns a mutable, null-terminated slice with the same length as `slice`.
34/// Caller owns the returned memory.
35pub fn addNullByte(allocator: mem.Allocator, slice: []const u8) ![:0]u8 {
36 const result = try allocator.alloc(u8, slice.len + 1);
37 @memcpy(result[0..slice.len], slice);
38 result[slice.len] = 0;
39 return result[0..slice.len :0];
40}
41
42test "addNullByte" {
43 const slice = try addNullByte(std.testing.allocator, "hello"[0..4]);
44 defer std.testing.allocator.free(slice);
45 try testing.expect(slice.len == 4);
46 try testing.expect(slice[4] == 0);
47}
48
49pub const NullTerminated2DArray = struct {
50 allocator: mem.Allocator,
51 byte_count: usize,
52 ptr: ?[*:null]?[*:0]u8,
53
54 /// Takes N lists of strings, concatenates the lists together, and adds a null terminator
55 /// Caller must deinit result
56 pub fn fromSlices(allocator: mem.Allocator, slices: []const []const []const u8) !NullTerminated2DArray {
57 var new_len: usize = 1; // 1 for the list null
58 var byte_count: usize = 0;
59 for (slices) |slice| {
60 new_len += slice.len;
61 for (slice) |inner| {
62 byte_count += inner.len;
63 }
64 byte_count += slice.len; // for the null terminators of inner
65 }
66
67 const index_size = @sizeOf(usize) * new_len; // size of the ptrs
68 byte_count += index_size;
69
70 const buf = try allocator.alignedAlloc(u8, @alignOf(?*u8), byte_count);
71 errdefer allocator.free(buf);
72
73 var write_index = index_size;
74 const index_buf = mem.bytesAsSlice(?[*]u8, buf);
75
76 var i: usize = 0;
77 for (slices) |slice| {
78 for (slice) |inner| {
79 index_buf[i] = buf.ptr + write_index;
80 i += 1;
81 @memcpy(buf[write_index..][0..inner.len], inner);
82 write_index += inner.len;
83 buf[write_index] = 0;
84 write_index += 1;
85 }
86 }
87 index_buf[i] = null;
88
89 return NullTerminated2DArray{
90 .allocator = allocator,
91 .byte_count = byte_count,
92 .ptr = @as(?[*:null]?[*:0]u8, @ptrCast(buf.ptr)),
93 };
94 }
95
96 pub fn deinit(self: *NullTerminated2DArray) void {
97 const buf = @as([*]u8, @ptrCast(self.ptr));
98 self.allocator.free(buf[0..self.byte_count]);
99 }
100};
1pub const line_sep = @compileError("deprecated; choose correct end-of-line sequence of characters by yourself instead");
2pub const cmp = @compileError("deprecated; use `std.mem.orderZ` instead");
3pub const addNullByte = @compileError("deprecated; use `allocator.dupeZ(u8, stuff)` instead");
lib/std/mem.zig+13-1
......@@ -607,12 +607,24 @@ pub fn order(comptime T: type, lhs: []const T, rhs: []const T) math.Order {
607607 return math.order(lhs.len, rhs.len);
608608}
609609
610test "order" {
610/// Compares two many-item pointers with NUL-termination lexicographically.
611pub fn orderZ(comptime T: type, lhs: [*:0]const T, rhs: [*:0]const T) math.Order {
612 var i: usize = 0;
613 while (lhs[i] == rhs[i] and lhs[i] != 0) : (i += 1) {}
614 return math.order(lhs[i], rhs[i]);
615}
616
617test "order and orderZ" {
611618 try testing.expect(order(u8, "abcd", "bee") == .lt);
619 try testing.expect(orderZ(u8, "abcd", "bee") == .lt);
612620 try testing.expect(order(u8, "abc", "abc") == .eq);
621 try testing.expect(orderZ(u8, "abc", "abc") == .eq);
613622 try testing.expect(order(u8, "abc", "abc0") == .lt);
623 try testing.expect(orderZ(u8, "abc", "abc0") == .lt);
614624 try testing.expect(order(u8, "", "") == .eq);
625 try testing.expect(orderZ(u8, "", "") == .eq);
615626 try testing.expect(order(u8, "", "a") == .lt);
627 try testing.expect(orderZ(u8, "", "a") == .lt);
616628}
617629
618630/// Returns true if lhs < rhs, false otherwise
lib/std/net.zig+2-2
......@@ -783,7 +783,7 @@ pub fn getAddressList(allocator: mem.Allocator, name: []const u8, port: u16) Get
783783 errdefer result.deinit();
784784
785785 if (builtin.target.os.tag == .windows) {
786 const name_c = try std.cstr.addNullByte(allocator, name);
786 const name_c = try allocator.dupeZ(u8, name);
787787 defer allocator.free(name_c);
788788
789789 const port_c = try std.fmt.allocPrintZ(allocator, "{}", .{port});
......@@ -855,7 +855,7 @@ pub fn getAddressList(allocator: mem.Allocator, name: []const u8, port: u16) Get
855855 }
856856
857857 if (builtin.link_libc) {
858 const name_c = try std.cstr.addNullByte(allocator, name);
858 const name_c = try allocator.dupeZ(u8, name);
859859 defer allocator.free(name_c);
860860
861861 const port_c = try std.fmt.allocPrintZ(allocator, "{}", .{port});
lib/std/zig/c_builtins.zig+5-1
......@@ -126,7 +126,11 @@ pub inline fn __builtin_strlen(s: [*c]const u8) usize {
126126 return std.mem.sliceTo(s, 0).len;
127127}
128128pub inline fn __builtin_strcmp(s1: [*c]const u8, s2: [*c]const u8) c_int {
129 return @as(c_int, std.cstr.cmp(s1, s2));
129 return switch (std.mem.orderZ(u8, s1, s2)) {
130 .lt => -1,
131 .eq => 0,
132 .gt => 1,
133 };
130134}
131135
132136pub inline fn __builtin_object_size(ptr: ?*const anyopaque, ty: c_int) usize {
src/Compilation.zig+1-1
......@@ -5066,7 +5066,7 @@ fn parseLldStderr(comp: *Compilation, comptime prefix: []const u8, stderr: []con
50665066 defer context_lines.deinit();
50675067
50685068 var current_err: ?*LldError = null;
5069 var lines = mem.splitSequence(u8, stderr, std.cstr.line_sep);
5069 var lines = mem.splitSequence(u8, stderr, if (builtin.os.tag == .windows) "\r\n" else "\n");
50705070 while (lines.next()) |line| {
50715071 if (mem.startsWith(u8, line, prefix ++ ":")) {
50725072 if (current_err) |err| {
src/codegen/c/type.zig+1-2
......@@ -1,5 +1,4 @@
11const std = @import("std");
2const cstr = std.cstr;
32const mem = std.mem;
43const Allocator = mem.Allocator;
54const assert = std.debug.assert;
......@@ -1025,7 +1024,7 @@ pub const CType = extern union {
10251024 for (lhs_data, rhs_data) |lhs_field, rhs_field| {
10261025 if (!ctx.eqlIndex(lhs_field.type, rhs_field.type)) return false;
10271026 if (lhs_field.alignas.@"align" != rhs_field.alignas.@"align") return false;
1028 if (cstr.cmp(lhs_field.name, rhs_field.name) != 0) return false;
1027 if (std.mem.orderZ(u8, lhs_field.name, rhs_field.name) != .eq) return false;
10291028 }
10301029 return true;
10311030 },
test/behavior/basic.zig+1-1
......@@ -647,7 +647,7 @@ test "multiline string literal is null terminated" {
647647 \\three
648648 ;
649649 const s2 = "one\ntwo)\nthree";
650 try expect(std.cstr.cmp(s1, s2) == 0);
650 try expect(std.mem.orderZ(u8, s1, s2) == .eq);
651651}
652652
653653test "string escapes" {
test/cbe.zig+5-4
......@@ -1,5 +1,6 @@
11const std = @import("std");
22const Cases = @import("src/Cases.zig");
3const nl = if (@import("builtin").os.tag == .windows) "\r\n" else "\n";
34
45// These tests should work with all platforms, but we're using linux_x64 for
56// now for consistency. Will be expanded eventually.
......@@ -19,7 +20,7 @@ pub fn addCases(ctx: *Cases) !void {
1920 \\ _ = puts("hello world!");
2021 \\ return 0;
2122 \\}
22 , "hello world!" ++ std.cstr.line_sep);
23 , "hello world!" ++ nl);
2324
2425 // Now change the message only
2526 case.addCompareOutput(
......@@ -28,7 +29,7 @@ pub fn addCases(ctx: *Cases) !void {
2829 \\ _ = puts("yo");
2930 \\ return 0;
3031 \\}
31 , "yo" ++ std.cstr.line_sep);
32 , "yo" ++ nl);
3233
3334 // Add an unused Decl
3435 case.addCompareOutput(
......@@ -38,7 +39,7 @@ pub fn addCases(ctx: *Cases) !void {
3839 \\ return 0;
3940 \\}
4041 \\fn unused() void {}
41 , "yo!" ++ std.cstr.line_sep);
42 , "yo!" ++ nl);
4243
4344 // Comptime return type and calling convention expected.
4445 case.addError(
......@@ -67,7 +68,7 @@ pub fn addCases(ctx: *Cases) !void {
6768 \\ _ = printf("Hello, %s!\n", "world");
6869 \\ return 0;
6970 \\}
70 , "Hello, world!" ++ std.cstr.line_sep);
71 , "Hello, world!" ++ nl);
7172 }
7273
7374 {
test/compare_output.zig+1-1
......@@ -15,7 +15,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
1515 \\ _ = c.puts("Hello, world!");
1616 \\ return 0;
1717 \\}
18 , "Hello, world!" ++ std.cstr.line_sep);
18 , "Hello, world!" ++ if (@import("builtin").os.tag == .windows) "\r\n" else "\n");
1919
2020 cases.add("hello world without libc",
2121 \\const io = @import("std").io;
test/run_translated_c.zig+1-1
......@@ -1,6 +1,6 @@
11const std = @import("std");
22const tests = @import("tests.zig");
3const nl = std.cstr.line_sep;
3const nl = if (@import("builtin").os.tag == .windows) "\r\n" else "\n";
44
55pub fn addCases(cases: *tests.RunTranslatedCContext) void {
66 cases.add("dereference address of",