Scripting: Add making strings from non-UTF8-clean text

This commit is contained in:
Vicki Pfau 2022-05-15 19:29:25 -07:00
parent 98f5298e04
commit 1895ed34fc
2 changed files with 13 additions and 0 deletions

View File

@ -686,6 +686,7 @@ struct mScriptValue* mScriptValueUnwrap(struct mScriptValue* val);
const struct mScriptValue* mScriptValueUnwrapConst(const struct mScriptValue* val);
struct mScriptValue* mScriptStringCreateFromUTF8(const char* string);
struct mScriptValue* mScriptStringCreateFromASCII(const char* string);
struct mScriptValue* mScriptValueCreateFromUInt(uint32_t value);
struct mScriptValue* mScriptValueCreateFromSInt(int32_t value);

View File

@ -7,6 +7,7 @@
#include <mgba/script/context.h>
#include <mgba-util/hash.h>
#include <mgba-util/string.h>
#include <mgba-util/table.h>
static void _allocList(struct mScriptValue*);
@ -738,9 +739,20 @@ struct mScriptValue* mScriptStringCreateFromUTF8(const char* string) {
struct mScriptValue* val = mScriptValueAlloc(mSCRIPT_TYPE_MS_STR);
struct mScriptString* internal = val->value.opaque;
internal->size = strlen(string);
internal->length = utf8strlen(string);
internal->buffer = strdup(string);
return val;
}
struct mScriptValue* mScriptStringCreateFromASCII(const char* string) {
struct mScriptValue* val = mScriptValueAlloc(mSCRIPT_TYPE_MS_STR);
struct mScriptString* internal = val->value.opaque;
internal->size = strlen(string);
internal->length = strlen(string);
internal->buffer = latin1ToUtf8(string, internal->size + 1);
return val;
}
struct mScriptValue* mScriptValueCreateFromSInt(int32_t value) {
struct mScriptValue* val = mScriptValueAlloc(mSCRIPT_TYPE_MS_S32);
val->value.s32 = value;