Skip to content

Commit

Permalink
feat: rename addLString to addBytes
Browse files Browse the repository at this point in the history
This is for consistency. Also removes several unnecessary pointer casts
  • Loading branch information
natecraddock committed Feb 18, 2023
1 parent 582bf5f commit 5505a3e
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 24 deletions.
9 changes: 4 additions & 5 deletions src/ziglua-5.1/lib.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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];
}
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/ziglua-5.1/tests.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
9 changes: 4 additions & 5 deletions src/ziglua-5.2/lib.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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];
}
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/ziglua-5.2/tests.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
9 changes: 4 additions & 5 deletions src/ziglua-5.3/lib.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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];
}
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/ziglua-5.3/tests.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
9 changes: 4 additions & 5 deletions src/ziglua-5.4/lib.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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];
}
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/ziglua-5.4/tests.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 5505a3e

Please sign in to comment.