mirror of
https://codeberg.org/1414codeforge/gear.git
synced 2025-06-05 22:09:24 +02:00
[spec/*,*] Split library in submodules.
This commit is contained in:
28
strings.lua
Normal file
28
strings.lua
Normal file
@ -0,0 +1,28 @@
|
||||
local strings = {}
|
||||
|
||||
|
||||
--- Test whether a string starts with a prefix.
|
||||
function strings.startswith(s, prefix)
|
||||
-- optimized version of: return s:sub(1, #prefix) == prefix
|
||||
for i = 1,#prefix do
|
||||
if s:byte(i) ~= prefix:byte(i) then
|
||||
return false
|
||||
end
|
||||
end
|
||||
return true
|
||||
end
|
||||
|
||||
--- Test whether a string ends with a trailing suffix.
|
||||
function strings.endswith(s, trailing)
|
||||
-- optimized version of: return trailing == "" or s:sub(-#trailing) == trailing
|
||||
local n1,n2 = #s,#trailing
|
||||
|
||||
for i = 0,n2-1 do
|
||||
if s:byte(n1-i) ~= trailing:byte(n2-i) then
|
||||
return false
|
||||
end
|
||||
end
|
||||
return true
|
||||
end
|
||||
|
||||
return strings
|
Reference in New Issue
Block a user