Skip to content

Commit

Permalink
string.gsub: accept functions with no returns
Browse files Browse the repository at this point in the history
Note that we're doing a little hack here, but notating the
return type as vararg, as a stand-in for something like a `?`
in the return type. I guess we need `min_arity` calculations
in the return types as well.

Also fix the declaration of `gsub` because Lua does not accept
functions that return booleans, but it does accept numbers.
  • Loading branch information
hishamhm committed Sep 18, 2024
1 parent f0d714e commit cfc986c
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 2 deletions.
71 changes: 71 additions & 0 deletions spec/stdlib/string_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,75 @@ describe("string", function()
]]))
end)

describe("gsub", function()
it("accepts a string, returns a string", util.check([[
local s = "hello"
local hi: string = s:gsub("ello", "i")
]]))

it("accepts a string, returns a string and integer", util.check([[
local s = "hello world"
local wordword, count: string, integer = s:gsub("%w+", "word")
]]))

it("accepts a string and integer, returns a string and integer", util.check([[
local s = "hello world"
local helloword, count: string, integer = s:gsub("%w+", "word", 6)
]]))

it("accepts a map, returns a string and integer", util.check([[
local s = "hello world"
local map = {
["hello"] = "hola",
["world"] = "mundo",
}
local holamundo, count: string, integer = s:gsub("%w+", map)
]]))

it("accepts a map and integer, returns a string and integer", util.check([[
local s = "hello world"
local map = {
["hello"] = "hola",
["world"] = "mundo",
}
local hellomundo, count: string, integer = s:gsub("%w+", map, 6)
]]))

it("accepts a function to strings, returns a string", util.check([[
local s = "hello world"
local function f(x: string): string
return x:upper()
end
local ret: string = s:gsub("%w+", f)
]]))

it("accepts a function to integers, returns a string", util.check([[
local s = "hello world"
local function f(x: string): integer
return #x
end
local ret: string = s:gsub("%w+", f)
]]))

it("accepts a function to numbers, returns a string", util.check([[
local s = "hello world"
local function f(x: string): number
return #x * 1.5
end
local ret: string = s:gsub("%w+", f)
]]))

it("accepts a function that returns nothing", util.check([[
local function parse_integers(s: string, i0: integer) : {integer}
local t, p = {}, i0 or 1
local function f(x: string)
t[p] = math.tointeger(x)
p = p + 1
end
s:gsub("[-%d]+", f)
return t
end
]]))
end)

end)
2 changes: 1 addition & 1 deletion tl.lua
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ do
gsub: function(string, string, string, ? integer): string, integer
gsub: function(string, string, {string:string}, ? integer): string, integer
gsub: function(string, string, function(string...): (string | integer | boolean), ? integer): string, integer
gsub: function(string, string, function(string...): ((string | integer | number)...), ? integer): string, integer
len: function(string): integer
lower: function(string): string
Expand Down
2 changes: 1 addition & 1 deletion tl.tl
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ do
gsub: function(string, string, string, ? integer): string, integer
gsub: function(string, string, {string:string}, ? integer): string, integer
gsub: function(string, string, function(string...): (string | integer | boolean), ? integer): string, integer
gsub: function(string, string, function(string...): ((string | integer | number)...), ? integer): string, integer
len: function(string): integer
lower: function(string): string
Expand Down

0 comments on commit cfc986c

Please sign in to comment.