Skip to content

Commit a17b582

Browse files
committed
Remove checkBytes() and optBytes()
For the same reasons as described in previous commits, these functions can be removed in favor of the String versions
1 parent 5be0cdf commit a17b582

File tree

2 files changed

+29
-57
lines changed

2 files changed

+29
-57
lines changed

src/lib.zig

Lines changed: 13 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2413,15 +2413,6 @@ pub const Lua = struct {
24132413
return c.luaL_checkinteger(lua.state, arg);
24142414
}
24152415

2416-
/// Checks whether the function argument `arg` is a slice of bytes and returns the slice
2417-
/// See https://www.lua.org/manual/5.4/manual.html#luaL_checklstring
2418-
pub fn checkBytes(lua: *Lua, arg: i32) [:0]const u8 {
2419-
var length: usize = 0;
2420-
const str = c.luaL_checklstring(lua.state, arg, &length);
2421-
// luaL_checklstring never returns null (throws lua error)
2422-
return str[0..length :0];
2423-
}
2424-
24252416
/// Checks whether the function argument `arg` is a number and returns the number
24262417
/// See https://www.lua.org/manual/5.4/manual.html#luaL_checknumber
24272418
pub fn checkNumber(lua: *Lua, arg: i32) Number {
@@ -2436,9 +2427,9 @@ pub const Lua = struct {
24362427
pub fn checkOption(lua: *Lua, comptime T: type, arg: i32, default: ?T) T {
24372428
const name = blk: {
24382429
if (default) |defaultName| {
2439-
break :blk lua.optBytes(arg, @tagName(defaultName));
2430+
break :blk lua.optString(arg, @tagName(defaultName));
24402431
} else {
2441-
break :blk lua.checkBytes(arg);
2432+
break :blk lua.checkString(arg);
24422433
}
24432434
};
24442435

@@ -2460,8 +2451,11 @@ pub const Lua = struct {
24602451

24612452
/// Checks whether the function argument `arg` is a string and returns the string
24622453
/// See https://www.lua.org/manual/5.4/manual.html#luaL_checkstring
2463-
pub fn checkString(lua: *Lua, arg: i32) [*:0]const u8 {
2464-
return c.luaL_checklstring(lua.state, arg, null);
2454+
pub fn checkString(lua: *Lua, arg: i32) [:0]const u8 {
2455+
var length: usize = 0;
2456+
const str = c.luaL_checklstring(lua.state, arg, &length);
2457+
// luaL_checklstring never returns null (throws lua error)
2458+
return str[0..length :0];
24652459
}
24662460

24672461
/// Checks whether the function argument `arg` has type `t`
@@ -2756,17 +2750,6 @@ pub const Lua = struct {
27562750
return c.luaL_optinteger(lua.state, arg, default);
27572751
}
27582752

2759-
/// If the function argument `arg` is a slice of bytes, returns the slice
2760-
/// If the argument is absent or nil returns `default`
2761-
/// See https://www.lua.org/manual/5.4/manual.html#luaL_optlstring
2762-
pub fn optBytes(lua: *Lua, arg: i32, default: [:0]const u8) [:0]const u8 {
2763-
var length: usize = 0;
2764-
// will never return null because default cannot be null
2765-
const ret: [*]const u8 = c.luaL_optlstring(lua.state, arg, default.ptr, &length);
2766-
if (ret == default.ptr) return default;
2767-
return ret[0..length :0];
2768-
}
2769-
27702753
/// If the function argument `arg` is a number, returns the number
27712754
/// If the argument is absent or nil returns `default`
27722755
/// See https://www.lua.org/manual/5.4/manual.html#luaL_optnumber
@@ -2777,9 +2760,12 @@ pub const Lua = struct {
27772760
/// If the function argument `arg` is a string, returns the string
27782761
/// If the argment is absent or nil returns `default`
27792762
/// See https://www.lua.org/manual/5.4/manual.html#luaL_optstring
2780-
pub fn optString(lua: *Lua, arg: i32, default: [:0]const u8) [*:0]const u8 {
2781-
// translate-c error
2782-
return c.luaL_optlstring(lua.state, arg, default.ptr, null);
2763+
pub fn optString(lua: *Lua, arg: i32, default: [:0]const u8) [:0]const u8 {
2764+
var length: usize = 0;
2765+
// will never return null because default cannot be null
2766+
const ret: [*]const u8 = c.luaL_optlstring(lua.state, arg, default.ptr, &length);
2767+
if (ret == default.ptr) return default;
2768+
return ret[0..length :0];
27832769
}
27842770

27852771
/// If the function argument is a number, returns this number as an unsigned

src/tests.zig

Lines changed: 16 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1332,11 +1332,10 @@ test "aux check functions" {
13321332
fn inner(l: *Lua) i32 {
13331333
l.checkAny(1);
13341334
_ = l.checkInteger(2);
1335-
_ = l.checkBytes(3);
1336-
_ = l.checkNumber(4);
1337-
_ = l.checkString(5);
1338-
l.checkType(6, .boolean);
1339-
_ = if (ziglua.lang == .lua52) l.checkUnsigned(7);
1335+
_ = l.checkNumber(3);
1336+
_ = l.checkString(4);
1337+
l.checkType(5, .boolean);
1338+
_ = if (ziglua.lang == .lua52) l.checkUnsigned(6);
13401339
return 0;
13411340
}
13421341
}.inner);
@@ -1365,29 +1364,18 @@ test "aux check functions" {
13651364
lua.pushFunction(function);
13661365
lua.pushNil();
13671366
lua.pushInteger(3);
1368-
_ = lua.pushString("hello world");
1369-
lua.protectedCall(3, 0, 0) catch {
1370-
try expectStringContains("number expected", try lua.toString(-1));
1371-
lua.pop(-1);
1372-
};
1373-
1374-
lua.pushFunction(function);
1375-
lua.pushNil();
1376-
lua.pushInteger(3);
1377-
_ = lua.pushString("hello world");
13781367
lua.pushNumber(4);
1379-
lua.protectedCall(4, 0, 0) catch {
1368+
lua.protectedCall(3, 0, 0) catch {
13801369
try expectStringContains("string expected", try lua.toString(-1));
13811370
lua.pop(-1);
13821371
};
13831372

13841373
lua.pushFunction(function);
13851374
lua.pushNil();
13861375
lua.pushInteger(3);
1387-
_ = lua.pushString("hello world");
13881376
lua.pushNumber(4);
1389-
_ = lua.pushStringZ("hello world");
1390-
lua.protectedCall(5, 0, 0) catch {
1377+
_ = lua.pushString("hello world");
1378+
lua.protectedCall(4, 0, 0) catch {
13911379
try expectStringContains("boolean expected", try lua.toString(-1));
13921380
lua.pop(-1);
13931381
};
@@ -1396,12 +1384,11 @@ test "aux check functions" {
13961384
lua.pushFunction(function);
13971385
lua.pushNil();
13981386
lua.pushInteger(3);
1399-
_ = lua.pushString("hello world");
14001387
lua.pushNumber(4);
1401-
_ = lua.pushStringZ("hello world");
1388+
_ = lua.pushString("hello world");
14021389
lua.pushBoolean(true);
1403-
lua.protectedCall(6, 0, 0) catch {
1404-
try expectEqualStrings("bad argument #7 to '?' (number expected, got no value)", try lua.toString(-1));
1390+
lua.protectedCall(5, 0, 0) catch {
1391+
try expectEqualStrings("bad argument #6 to '?' (number expected, got no value)", try lua.toString(-1));
14051392
lua.pop(-1);
14061393
};
14071394
}
@@ -1410,14 +1397,13 @@ test "aux check functions" {
14101397
// test pushFail here (currently acts the same as pushNil)
14111398
if (ziglua.lang == .lua54) lua.pushFail() else lua.pushNil();
14121399
lua.pushInteger(3);
1413-
_ = lua.pushString("hello world");
14141400
lua.pushNumber(4);
1415-
_ = lua.pushStringZ("hello world");
1401+
_ = lua.pushString("hello world");
14161402
lua.pushBoolean(true);
14171403
if (ziglua.lang == .lua52) {
14181404
lua.pushUnsigned(1);
1419-
try lua.protectedCall(7, 0, 0);
1420-
} else try lua.protectedCall(6, 0, 0);
1405+
try lua.protectedCall(6, 0, 0);
1406+
} else try lua.protectedCall(5, 0, 0);
14211407
}
14221408

14231409
test "aux opt functions" {
@@ -1427,9 +1413,9 @@ test "aux opt functions" {
14271413
const function = ziglua.wrap(struct {
14281414
fn inner(l: *Lua) i32 {
14291415
expectEqual(10, l.optInteger(1, 10)) catch unreachable;
1430-
expectEqualStrings("zig", l.optBytes(2, "zig")) catch unreachable;
1416+
expectEqualStrings("zig", l.optString(2, "zig")) catch unreachable;
14311417
expectEqual(1.23, l.optNumber(3, 1.23)) catch unreachable;
1432-
expectEqualStrings("lang", std.mem.span(l.optString(4, "lang"))) catch unreachable;
1418+
expectEqualStrings("lang", l.optString(4, "lang")) catch unreachable;
14331419
return 0;
14341420
}
14351421
}.inner);
@@ -2333,7 +2319,7 @@ test "namecall" {
23332319

23342320
pub fn vectorNamecall(l: *Lua) i32 {
23352321
const atom_idx, _ = l.namecallAtom() catch {
2336-
l.raiseErrorStr("%s is not a valid vector method", .{l.checkString(1)});
2322+
l.raiseErrorStr("%s is not a valid vector method", .{l.checkString(1).ptr});
23372323
};
23382324
switch (atom_idx) {
23392325
dot_idx => {

0 commit comments

Comments
 (0)