From 5505a3ea81df8bca7824d7aceb5c55394fe8c34e Mon Sep 17 00:00:00 2001 From: Nathan Craddock Date: Fri, 17 Feb 2023 20:46:58 -0700 Subject: [PATCH] feat: rename addLString to addBytes This is for consistency. Also removes several unnecessary pointer casts --- src/ziglua-5.1/lib.zig | 9 ++++----- src/ziglua-5.1/tests.zig | 2 +- src/ziglua-5.2/lib.zig | 9 ++++----- src/ziglua-5.2/tests.zig | 2 +- src/ziglua-5.3/lib.zig | 9 ++++----- src/ziglua-5.3/tests.zig | 2 +- src/ziglua-5.4/lib.zig | 9 ++++----- src/ziglua-5.4/tests.zig | 2 +- 8 files changed, 20 insertions(+), 24 deletions(-) diff --git a/src/ziglua-5.1/lib.zig b/src/ziglua-5.1/lib.zig index 25359f9..13af5eb 100644 --- a/src/ziglua-5.1/lib.zig +++ b/src/ziglua-5.1/lib.zig @@ -705,8 +705,7 @@ pub const Lua = struct { /// Push a formatted string onto the stack and return a pointer to the string /// See https://www.lua.org/manual/5.1/manual.html#lua_pushfstring pub fn pushFStringEx(lua: *Lua, fmt: [:0]const u8, args: anytype) [*:0]const u8 { - const ptr = @call(.auto, c.lua_pushfstring, .{ lua.state, fmt.ptr } ++ args); - return @ptrCast([*:0]const u8, ptr); + return @call(.auto, c.lua_pushfstring, .{ lua.state, fmt.ptr } ++ args); } /// Pushes an integer with value `n` onto the stack @@ -1193,7 +1192,7 @@ pub const Lua = struct { /// See https://www.lua.org/manual/5.1/manual.html#luaL_checklstring pub fn checkBytes(lua: *Lua, arg: i32) [:0]const u8 { var length: usize = 0; - const str = c.luaL_checklstring(lua.state, arg, @ptrCast([*c]usize, &length)); + const str = c.luaL_checklstring(lua.state, arg, &length); // luaL_checklstring never returns null (throws lua error) return str[0..length :0]; } @@ -1558,8 +1557,8 @@ pub const Buffer = struct { /// Adds the string to the buffer /// See https://www.lua.org/manual/5.1/manual.html#luaL_addlstring /// TODO: rename to addBytes - pub fn addLString(buf: *Buffer, str: []const u8) void { - c.luaL_addlstring(&buf.b, @ptrCast([*c]const u8, str), str.len); + pub fn addBytes(buf: *Buffer, str: []const u8) void { + c.luaL_addlstring(&buf.b, str.ptr, str.len); } /// Adds to the buffer a string of `length` previously copied to the buffer area diff --git a/src/ziglua-5.1/tests.zig b/src/ziglua-5.1/tests.zig index d8f9b33..e3dac5c 100644 --- a/src/ziglua-5.1/tests.zig +++ b/src/ziglua-5.1/tests.zig @@ -373,7 +373,7 @@ test "string buffers" { str[1] = 'a'; buffer.addSize(2); - buffer.addLString(" api "); + buffer.addBytes(" api "); lua.pushNumber(5.1); buffer.addValue(); buffer.pushResult(); diff --git a/src/ziglua-5.2/lib.zig b/src/ziglua-5.2/lib.zig index 5fa109d..9e55020 100644 --- a/src/ziglua-5.2/lib.zig +++ b/src/ziglua-5.2/lib.zig @@ -774,8 +774,7 @@ pub const Lua = struct { /// Push a formatted string onto the stack and return a pointer to the string pub fn pushFStringEx(lua: *Lua, fmt: [:0]const u8, args: anytype) [*:0]const u8 { - const ptr = @call(.auto, c.lua_pushfstring, .{ lua.state, fmt.ptr } ++ args); - return @ptrCast([*:0]const u8, ptr); + return @call(.auto, c.lua_pushfstring, .{ lua.state, fmt.ptr } ++ args); } /// Pushes the global environment onto the stack @@ -1288,7 +1287,7 @@ pub const Lua = struct { /// Checks whether the function argument `arg` is a slice of bytes and returns the slice pub fn checkBytes(lua: *Lua, arg: i32) [:0]const u8 { var length: usize = 0; - const str = c.luaL_checklstring(lua.state, arg, @ptrCast([*c]usize, &length)); + const str = c.luaL_checklstring(lua.state, arg, &length); // luaL_checklstring never returns null (throws lua error) return str[0..length :0]; } @@ -1712,8 +1711,8 @@ pub const Buffer = struct { } /// Adds the string to the buffer - pub fn addLString(buf: *Buffer, str: []const u8) void { - c.luaL_addlstring(&buf.b, @ptrCast([*c]const u8, str), str.len); + pub fn addBytes(buf: *Buffer, str: []const u8) void { + c.luaL_addlstring(&buf.b, str.ptr, str.len); } /// Adds to the buffer a string of `length` previously copied to the buffer area diff --git a/src/ziglua-5.2/tests.zig b/src/ziglua-5.2/tests.zig index 64515fe..f2354f7 100644 --- a/src/ziglua-5.2/tests.zig +++ b/src/ziglua-5.2/tests.zig @@ -426,7 +426,7 @@ test "string buffers" { str[2] = 'a'; buffer.addSize(3); - buffer.addLString(" api "); + buffer.addBytes(" api "); lua.pushNumber(5.4); buffer.addValue(); buffer.sub(4); diff --git a/src/ziglua-5.3/lib.zig b/src/ziglua-5.3/lib.zig index 775d974..3bb3493 100644 --- a/src/ziglua-5.3/lib.zig +++ b/src/ziglua-5.3/lib.zig @@ -826,8 +826,7 @@ pub const Lua = struct { /// Push a formatted string onto the stack and return a pointer to the string pub fn pushFStringEx(lua: *Lua, fmt: [:0]const u8, args: anytype) [*:0]const u8 { - const ptr = @call(.auto, c.lua_pushfstring, .{ lua.state, fmt.ptr } ++ args); - return @ptrCast([*:0]const u8, ptr); + return @call(.auto, c.lua_pushfstring, .{ lua.state, fmt.ptr } ++ args); } /// Pushes the global environment onto the stack @@ -1349,7 +1348,7 @@ pub const Lua = struct { /// Checks whether the function argument `arg` is a slice of bytes and returns the slice pub fn checkBytes(lua: *Lua, arg: i32) [:0]const u8 { var length: usize = 0; - const str = c.luaL_checklstring(lua.state, arg, @ptrCast([*c]usize, &length)); + const str = c.luaL_checklstring(lua.state, arg, &length); // luaL_checklstring never returns null (throws lua error) return str[0..length :0]; } @@ -1762,8 +1761,8 @@ pub const Buffer = struct { } /// Adds the string to the buffer - pub fn addLString(buf: *Buffer, str: []const u8) void { - c.luaL_addlstring(&buf.b, @ptrCast([*c]const u8, str), str.len); + pub fn addBytes(buf: *Buffer, str: []const u8) void { + c.luaL_addlstring(&buf.b, str.ptr, str.len); } /// Adds to the buffer a string of `length` previously copied to the buffer area diff --git a/src/ziglua-5.3/tests.zig b/src/ziglua-5.3/tests.zig index af3ec37..1919d9e 100644 --- a/src/ziglua-5.3/tests.zig +++ b/src/ziglua-5.3/tests.zig @@ -453,7 +453,7 @@ test "string buffers" { str[2] = 'a'; buffer.addSize(3); - buffer.addLString(" api "); + buffer.addBytes(" api "); lua.pushNumber(5.4); buffer.addValue(); buffer.sub(4); diff --git a/src/ziglua-5.4/lib.zig b/src/ziglua-5.4/lib.zig index cbd87aa..4f59e4c 100644 --- a/src/ziglua-5.4/lib.zig +++ b/src/ziglua-5.4/lib.zig @@ -838,8 +838,7 @@ pub const Lua = struct { /// Push a formatted string onto the stack and return a pointer to the string pub fn pushFStringEx(lua: *Lua, fmt: [:0]const u8, args: anytype) [*:0]const u8 { - const ptr = @call(.auto, c.lua_pushfstring, .{ lua.state, fmt.ptr } ++ args); - return @ptrCast([*:0]const u8, ptr); + return @call(.auto, c.lua_pushfstring, .{ lua.state, fmt.ptr } ++ args); } /// Pushes the global environment onto the stack @@ -1393,7 +1392,7 @@ pub const Lua = struct { /// Checks whether the function argument `arg` is a slice of bytes and returns the slice pub fn checkBytes(lua: *Lua, arg: i32) [:0]const u8 { var length: usize = 0; - const str = c.luaL_checklstring(lua.state, arg, @ptrCast([*c]usize, &length)); + const str = c.luaL_checklstring(lua.state, arg, &length); // luaL_checklstring never returns null (throws lua error) return str[0..length :0]; } @@ -1822,8 +1821,8 @@ pub const Buffer = struct { } /// Adds the string to the buffer - pub fn addLString(buf: *Buffer, str: []const u8) void { - c.luaL_addlstring(&buf.b, @ptrCast([*c]const u8, str), str.len); + pub fn addBytes(buf: *Buffer, str: []const u8) void { + c.luaL_addlstring(&buf.b, str.ptr, str.len); } /// Adds to the buffer a string of `length` previously copied to the buffer area diff --git a/src/ziglua-5.4/tests.zig b/src/ziglua-5.4/tests.zig index f412f01..aad81a0 100644 --- a/src/ziglua-5.4/tests.zig +++ b/src/ziglua-5.4/tests.zig @@ -460,7 +460,7 @@ test "string buffers" { try expectEqual(@as(usize, 6), buffer.len()); try expectEqualStrings("ziglua", buffer.addr()); - buffer.addLString(" api "); + buffer.addBytes(" api "); try expectEqualStrings("ziglua api ", buffer.addr()); lua.pushNumber(5.4);