Repository navigation
Lua - #301
Lua#301callumok2004 wants to merge 12 commits into
Conversation
no longer breaks without an extension, and now null terminates
| public ReadOnlySpan<char> CheckString(int stackPos, Span<char> buffer) { | ||
| int type = lua_type(state, stackPos); | ||
| if (type != TSTRING && type != TNUMBER) | ||
| TypeError("string", stackPos); | ||
| ReadOnlySpan<byte> bytes = GetStringBytes(stackPos); | ||
| if (Encoding.UTF8.GetMaxCharCount(bytes.Length) > buffer.Length) | ||
| return Encoding.UTF8.GetString(bytes); | ||
| return buffer[..Encoding.UTF8.GetChars(bytes, buffer)]; | ||
| } |
There was a problem hiding this comment.
This is honestly an odd choice to go with, we shouldn't ever assume that what comes from lua is actually valid UTF-8, also having to provide a buffer is strange, this function should just hand out a span of bytes and let the function using it decide what to do with it, for as long the function doesn't go out of scope Lua has no chance to garbage collect the string, quite often the string is not used to permanently store it and only then should it copy that to a new string object or buffer.
There was a problem hiding this comment.
Yes I'm not a fan of the other CheckString variant either, the data is already on heap provided by Lua, majority of the time we pass it along into some function which also quite often already expects Span or Span, the best would be to stick to byte as in Lua a string has no encoding, thats just a binary blob and should be treated as such.

No description provided.