Lua: Set string as metatable of string values, allowing for strVal:upper() style invocations

This commit is contained in:
Zef Hemel
2025-02-09 11:06:15 +01:00
parent 49b8a0f7dc
commit 654151437c
4 changed files with 124 additions and 52 deletions
+4 -1
View File
@@ -14,6 +14,10 @@ assert(string.sub("Hello", 2, 4) == "ell")
assert(string.upper("Hello") == "HELLO")
assert(string.lower("Hello") == "hello")
-- Invoke string metatable methods
assertEqual(("hello"):len(), 5)
assertEqual(("hello"):upper(), "HELLO")
-- Test string.gsub with various replacement types
-- Simple string replacement
local result, count = string.gsub("hello world", "hello", "hi")
@@ -79,7 +83,6 @@ assertEqual(m2, "ello")
-- Test with pattern with character class
assertEqual(string.match("c", "[abc]"), "c")
-- Test match with init position - need to capture the group
local initMatch = string.match("hello world", "(world)", 7)
assertEqual(initMatch, "world")