Skip to content

Commit db41f75

Browse files
committed
refactor: change toString to toBytes
The Lua API has the concept of lstrings (length strings). These are used when the contents may contain zeroes, or when it is useful to know the length. Because this data is not necessarily a string, ziglua has renamed these lstring functions to bytes functions. The toString function returns a zero-terminated many item pointer which is useful when you know the data is not zero-terminated.
1 parent 21de06c commit db41f75

File tree

2 files changed

+30
-24
lines changed

2 files changed

+30
-24
lines changed

src/tests.zig

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ test "type of and getting values" {
250250
_ = lua.pushString("all your codebase are belong to us");
251251
try expectEqual(@as(?[*]const u8, null), lua.pushString(null));
252252
lua.pushCFunction(ziglua.wrap(add));
253-
_ = lua.pushLString("hello world");
253+
_ = lua.pushBytes("hello world");
254254
_ = lua.pushFString("%s %s %d", .{ "hello", "world", @as(i32, 10) });
255255
lua.pushValue(1);
256256

@@ -285,7 +285,7 @@ test "type of and getting values" {
285285
try expect(lua.isString(12));
286286
try expect(lua.isBoolean(13));
287287

288-
try expectEqualStrings("hello world 10", lua.toString(12).?);
288+
try expectEqualStrings("hello world 10", try lua.toBytes(12));
289289

290290
// the created thread should equal the main thread (but created thread has no allocator ref)
291291
try expectEqual(lua.state, (try lua.toThread(7)).state);
@@ -466,7 +466,7 @@ test "string buffers" {
466466
try expectEqualStrings("ziglua api some text here", buffer.addr());
467467

468468
buffer.pushResult();
469-
try expectEqualStrings("ziglua api some text here", lua.toString(-1).?);
469+
try expectEqualStrings("ziglua api some text here", try lua.toBytes(-1));
470470

471471
// now test a small buffer
472472
buffer = undefined;
@@ -478,7 +478,7 @@ test "string buffers" {
478478
b = buffer.prep();
479479
std.mem.copy(u8, b, "defghijklmnopqrstuvwxyz");
480480
buffer.pushResultSize(23);
481-
try expectEqualStrings("abcdefghijklmnopqrstuvwxyz", lua.toString(-1).?);
481+
try expectEqualStrings("abcdefghijklmnopqrstuvwxyz", try lua.toBytes(-1));
482482

483483
lua.len(-1);
484484
try expectEqual(@as(Integer, 26), lua.toInteger(-1));
@@ -587,7 +587,7 @@ test "concat" {
587587
_ = lua.pushString(" wow!");
588588
lua.concat(3);
589589

590-
try expectEqualStrings("hello 10.0 wow!", lua.toString(-1).?);
590+
try expectEqualStrings("hello 10.0 wow!", try lua.toBytes(-1));
591591
}
592592

593593
test "garbage collector" {
@@ -627,14 +627,14 @@ test "table access" {
627627
_ = lua.getGlobal("a");
628628

629629
try expectEqual(LuaType.string, lua.getIndex(1, 1));
630-
try expectEqualStrings("first", lua.toString(-1).?);
630+
try expectEqualStrings("first", try lua.toBytes(-1));
631631

632632
try expectEqual(LuaType.string, lua.rawGetIndex(1, 1));
633-
try expectEqualStrings("first", lua.toString(-1).?);
633+
try expectEqualStrings("first", try lua.toBytes(-1));
634634

635635
_ = lua.pushString("key");
636636
try expectEqual(LuaType.string, lua.getTable(1));
637-
try expectEqualStrings("value", lua.toString(-1).?);
637+
try expectEqualStrings("value", try lua.toBytes(-1));
638638

639639
_ = lua.pushString("other one");
640640
try expectEqual(LuaType.number, lua.rawGetTable(1));
@@ -803,7 +803,7 @@ test "userdata and uservalues" {
803803
try expectEqual(LuaType.number, try lua.getIndexUserValue(1, 1));
804804
try expectEqual(@as(Number, 1234.56), lua.toNumber(-1));
805805
try expectEqual(LuaType.string, try lua.getIndexUserValue(1, 2));
806-
try expectEqualStrings("test string", lua.toString(-1).?);
806+
try expectEqualStrings("test string", try lua.toBytes(-1));
807807

808808
try expectError(Error.Fail, lua.setIndexUserValue(1, 3));
809809
try expectError(Error.Fail, lua.getIndexUserValue(1, 3));
@@ -854,15 +854,15 @@ test "table traversal" {
854854
while (lua.next(1)) {
855855
switch (lua.typeOf(-1)) {
856856
.string => {
857-
try expectEqualStrings("key", lua.toString(-2).?);
858-
try expectEqualStrings("value", lua.toString(-1).?);
857+
try expectEqualStrings("key", try lua.toBytes(-2));
858+
try expectEqualStrings("value", try lua.toBytes(-1));
859859
},
860860
.boolean => {
861-
try expectEqualStrings("second", lua.toString(-2).?);
861+
try expectEqualStrings("second", try lua.toBytes(-2));
862862
try expectEqual(true, lua.toBoolean(-1));
863863
},
864864
.number => {
865-
try expectEqualStrings("third", lua.toString(-2).?);
865+
try expectEqualStrings("third", try lua.toBytes(-2));
866866
try expectEqual(@as(Integer, 1), lua.toInteger(-1));
867867
},
868868
else => unreachable,
@@ -883,7 +883,7 @@ test "registry" {
883883

884884
// get key from the registry
885885
_ = lua.rawGetPtr(ziglua.registry_index, key);
886-
try expectEqualStrings("hello there", lua.toString(-1).?);
886+
try expectEqualStrings("hello there", try lua.toBytes(-1));
887887
}
888888

889889
test "closing vars" {
@@ -931,7 +931,7 @@ test "raise error" {
931931

932932
lua.pushCFunction(ziglua.wrap(makeError));
933933
try expectError(Error.Runtime, lua.protectedCall(0, 0, 0));
934-
try expectEqualStrings("makeError made an error", lua.toString(-1).?);
934+
try expectEqualStrings("makeError made an error", try lua.toBytes(-1));
935935
}
936936

937937
fn continuation(l: *Lua, status: ziglua.Status, ctx: isize) i32 {
@@ -973,7 +973,7 @@ test "yielding" {
973973
thread.pop(results);
974974
}
975975
try expectEqual(ziglua.ResumeStatus.ok, try thread.resumeThread(lua, 0, &results));
976-
try expectEqualStrings("done", thread.toString(-1).?);
976+
try expectEqualStrings("done", try thread.toBytes(-1));
977977
}
978978

979979
test "refs" {

src/ziglua.zig

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -716,7 +716,7 @@ pub const Lua = struct {
716716
// }
717717

718718
/// Pushes the bytes onto the stack. Returns a slice pointing to Lua's internal copy of the string
719-
pub fn pushLString(lua: *Lua, str: []const u8) []const u8 {
719+
pub fn pushBytes(lua: *Lua, str: []const u8) []const u8 {
720720
return c.lua_pushlstring(lua.state, str.ptr, str.len)[0..str.len];
721721
}
722722

@@ -951,8 +951,14 @@ pub const Lua = struct {
951951
return result;
952952
}
953953

954-
// NOTE: No need to have both toLString and toString for a Zig API
955-
// pub fn toLString(lua: *Lua, index: i32) []const u8 { ... }
954+
/// Returns a slice of bytes at the given index
955+
/// If the value is not a string or number, returns an error
956+
/// If the value was a number the actual value in the stack will be changed to a string
957+
pub fn toBytes(lua: *Lua, index: i32) ![:0]const u8 {
958+
var length: usize = undefined;
959+
if (c.lua_tolstring(lua.state, index, &length)) |ptr| return ptr[0..length :0];
960+
return Error.Fail;
961+
}
956962

957963
/// Equivalent to toNumberX but does not return errors
958964
pub fn toNumber(lua: *Lua, index: i32) Number {
@@ -975,14 +981,14 @@ pub const Lua = struct {
975981
return Error.Fail;
976982
}
977983

978-
/// Converts the Lua value at the given `index` to a zero-terminated slice (string)
979-
/// Returns null if the value was not a string or number
984+
/// Converts the Lua value at the given `index` to a zero-terminated many-itemed-pointer (string)
985+
/// Returns an error if the conversion failed
980986
/// If the value was a number the actual value in the stack will be changed to a string
981-
pub fn toString(lua: *Lua, index: i32) ?[:0]const u8 {
987+
pub fn toString(lua: *Lua, index: i32) ![*:0]const u8 {
982988
var length: usize = undefined;
983989
if (c.lua_tolstring(lua.state, index, &length)) |str| {
984-
return str[0..length :0];
985-
} else return null;
990+
return std.mem.span(str);
991+
} else return Error.Fail;
986992
}
987993

988994
/// Converts the value at the given `index` to a Lua thread (wrapped with a `Lua` struct)

0 commit comments

Comments
 (0)