mirror of
https://codeberg.org/ziglang/zig.git
synced 2026-03-08 05:04:52 +01:00
52 lines
1.8 KiB
Zig
52 lines
1.8 KiB
Zig
const std = @import("std");
|
|
const expectEqual = std.testing.expectEqual;
|
|
const expectEqualStrings = std.testing.expectEqualStrings;
|
|
const fmt = std.fmt;
|
|
|
|
test "using slices for strings" {
|
|
// Zig has no concept of strings. String literals are const pointers
|
|
// to null-terminated arrays of u8, and by convention parameters
|
|
// that are "strings" are expected to be UTF-8 encoded slices of u8.
|
|
// Here we coerce *const [5:0]u8 and *const [6:0]u8 to []const u8
|
|
const hello: []const u8 = "hello";
|
|
const world: []const u8 = "世界";
|
|
|
|
var all_together: [100]u8 = undefined;
|
|
// You can use slice syntax with at least one runtime-known index on an
|
|
// array to convert an array into a slice.
|
|
var start: usize = 0;
|
|
_ = &start;
|
|
const all_together_slice = all_together[start..];
|
|
// String concatenation example.
|
|
const hello_world = try fmt.bufPrint(all_together_slice, "{s} {s}", .{ hello, world });
|
|
|
|
// Generally, you can use UTF-8 and not worry about whether something is a
|
|
// string. If you don't need to deal with individual characters, no need
|
|
// to decode.
|
|
try expectEqualStrings("hello 世界", hello_world);
|
|
}
|
|
|
|
test "slice pointer" {
|
|
var array: [10]u8 = undefined;
|
|
const ptr = &array;
|
|
try expectEqual(*[10]u8, @TypeOf(ptr));
|
|
|
|
// A pointer to an array can be sliced just like an array:
|
|
var start: usize = 0;
|
|
var end: usize = 5;
|
|
_ = .{ &start, &end };
|
|
const slice = ptr[start..end];
|
|
// The slice is mutable because we sliced a mutable pointer.
|
|
try expectEqual([]u8, @TypeOf(slice));
|
|
slice[2] = 3;
|
|
try expectEqual(3, array[2]);
|
|
|
|
// Again, slicing with comptime-known indexes will produce another pointer
|
|
// to an array:
|
|
const ptr2 = slice[2..3];
|
|
try expectEqual(1, ptr2.len);
|
|
try expectEqual(3, ptr2[0]);
|
|
try expectEqual(*[1]u8, @TypeOf(ptr2));
|
|
}
|
|
|
|
// test
|