Skip to content

Instantly share code, notes, and snippets.

@doccaico
Created April 10, 2024 08:54
Show Gist options
  • Save doccaico/ff283f0c066f5fb9a7431c7be6ec9ba6 to your computer and use it in GitHub Desktop.
Save doccaico/ff283f0c066f5fb9a7431c7be6ec9ba6 to your computer and use it in GitHub Desktop.
shift zig implementation (naive ver.)
const std = @import("std");
const fmt = std.fmt;
const mem = std.mem;
const print = std.debug.print;
const assert = std.debug.assert;
const expect = std.testing.expect;
pub var allocator: std.mem.Allocator = undefined;
pub fn main() !void {
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
defer arena.deinit();
allocator = arena.allocator();
var word = [_]u8{ 'a', 'b', 'c' };
// var word = [_]u8{ 'a', 'b', 'c', 'x', 'y', 'z' };
print("word '{s}', len {d}\n", .{ word, word.len }); // word 'abc', len 3
const r = shift(&word);
print("word '{s}', len {d}\n", .{ word, word.len }); // word 'bc', len 3
print("{c}\n", .{r}); // a
}
// slice len is not chagend...
fn shift(array: []u8) u8 {
const r = array[0];
var i: usize = 0;
while (i == array.len - 1) : (i += 1) {
array[i] = array[i + 1];
}
array[i] = '\x00';
return r;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment