From 8b931fa4a72bc0ec48ff08a506d327611ba465e2 Mon Sep 17 00:00:00 2001 From: Inversion <40722286+NeoInversion@users.noreply.github.com> Date: Wed, 10 Feb 2021 22:02:35 -0800 Subject: [PATCH] Add string.len to base library --- src/cbaselib.c | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/cbaselib.c b/src/cbaselib.c index 5a5e7d8..c834ccb 100644 --- a/src/cbaselib.c +++ b/src/cbaselib.c @@ -510,13 +510,25 @@ int cosmoB_sChar(CState *state, int nargs, CValue *args) { return 1; } +int cosmoB_sLen(CState *state, int nargs, CValue *args) { + if (nargs < 1) { + cosmoV_error(state, "string.len() expected 1 argument, got %d", nargs); + return 0; + } + + cosmoV_pushNumber(state, strlen(cosmoO_readCString(cosmoV_readString(args[0])))); + + return 1; +} + void cosmoB_loadStrLib(CState *state) { const char *identifiers[] = { "sub", "find", "split", "byte", - "char" + "char", + "len" }; CosmoCFunction strLib[] = { @@ -524,7 +536,8 @@ void cosmoB_loadStrLib(CState *state) { cosmoB_sFind, cosmoB_sSplit, cosmoB_sByte, - cosmoB_sChar + cosmoB_sChar, + cosmoB_sLen }; // make string library object