mirror of
https://github.com/koreader/koreader.git
synced 2025-08-10 00:52:38 +00:00
@@ -14,7 +14,7 @@ local bor = bit.bor
|
||||
|
||||
local util = {}
|
||||
|
||||
--- Strips all punctuation marks and spaces from a string.
|
||||
---- Strips all punctuation marks and spaces from a string.
|
||||
---- @string text the string to be stripped
|
||||
---- @treturn string stripped text
|
||||
function util.stripPunctuation(text)
|
||||
@@ -24,6 +24,33 @@ function util.stripPunctuation(text)
|
||||
return text:gsub("\226[\128-\131][\128-\191]", ''):gsub("^%p+", ''):gsub("%p+$", '')
|
||||
end
|
||||
|
||||
-- Various whitespace trimming helpers, from http://lua-users.org/wiki/CommonFunctions & http://lua-users.org/wiki/StringTrim
|
||||
---- Remove leading whitespace from string.
|
||||
---- @string s the string to be trimmed
|
||||
---- @treturn string trimmed text
|
||||
function util.ltrim(s)
|
||||
return (s:gsub("^%s*", ""))
|
||||
end
|
||||
|
||||
---- Remove trailing whitespace from string.
|
||||
---- @string s the string to be trimmed
|
||||
---- @treturn string trimmed text
|
||||
function util.rtrim(s)
|
||||
local n = #s
|
||||
while n > 0 and s:find("^%s", n) do
|
||||
n = n - 1
|
||||
end
|
||||
return s:sub(1, n)
|
||||
end
|
||||
|
||||
---- Remove leading & trailing whitespace from string.
|
||||
---- @string s the string to be trimmed
|
||||
---- @treturn string trimmed text
|
||||
function util.trim(s)
|
||||
local from = s:match"^%s*()"
|
||||
return from > #s and "" or s:match(".*%S", from)
|
||||
end
|
||||
|
||||
--[[--
|
||||
Splits a string by a pattern
|
||||
|
||||
|
||||
Reference in New Issue
Block a user