From 62957a9270bba37cb37e22de721b8a5a646726c4 Mon Sep 17 00:00:00 2001 From: Max Cahill Date: Wed, 14 Apr 2021 17:06:31 +1000 Subject: [PATCH] [modified] PR #17 a little; simplified various unneeded sections and added comments --- stringx.lua | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/stringx.lua b/stringx.lua index b0568ab..10e189f 100644 --- a/stringx.lua +++ b/stringx.lua @@ -215,11 +215,8 @@ function stringx.trim(s) return s:sub(head, tail) end +--trim the start of a string function stringx.ltrim(s) - if s == "" or s == string.rep(" ", s:len()) then - return "" - end - local head = 1 for i = 1, #s do if not _whitespace_bytes[s:byte(i)] then @@ -227,13 +224,14 @@ function stringx.ltrim(s) break end end + if head == 1 then + return s + end return s:sub(head) end +--trim the end of a string function stringx.rtrim(s) - if s == "" or s == string.rep(" ", s:len()) then - return "" - end local tail = #s for i = #s, 1, -1 do @@ -243,6 +241,10 @@ function stringx.rtrim(s) end end + if tail == #s then + return s + end + return s:sub(1, tail) end @@ -322,13 +324,13 @@ function stringx.starts_with(s, prefix) return true end -function stringx.ends_with(s, posfix) - if posfix == "" then return true end - - if #posfix > #s then return false end - - for i = 0, #posfix-1 do - if s:byte(#s-i) ~= posfix:byte(#posfix-i) then +--check if a given string ends with another +--(without garbage) +function stringx.ends_with(s, suffix) + local len = #s + local suffix_len = #suffix + for i = 0, suffix_len - 1 do + if s:byte(len - i) ~= suffix:byte(suffix_len - i) then return false end end