From f99b70233e49db2229350bb82d9ddc6e2f4131c0 Mon Sep 17 00:00:00 2001 From: William Boman Date: Tue, 13 Jun 2023 15:45:34 +0200 Subject: fix(functional): strip_prefix and strip_suffix should not use patterns (#1352) --- lua/mason-core/functional/string.lua | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) (limited to 'lua/mason-core/functional') diff --git a/lua/mason-core/functional/string.lua b/lua/mason-core/functional/string.lua index e9e060f7..c641dcbc 100644 --- a/lua/mason-core/functional/string.lua +++ b/lua/mason-core/functional/string.lua @@ -108,21 +108,27 @@ _.trim_end_matches = fun.curryN(function(pattern, str) end, 2) _.strip_prefix = fun.curryN(function(prefix_pattern, str) - local _, start = string.find(str, "^" .. prefix_pattern) - if start then - return str:sub(start + 1) - else + if #prefix_pattern > #str then return str end + for i = 1, #prefix_pattern do + if str:sub(i, i) ~= prefix_pattern:sub(i, i) then + return str + end + end + return str:sub(#prefix_pattern + 1) end, 2) _.strip_suffix = fun.curryN(function(suffix_pattern, str) - local stop = string.find(str, suffix_pattern .. "$") - if stop then - return str:sub(1, stop - 1) - else + if #suffix_pattern > #str then return str end + for i = 1, #suffix_pattern do + if str:sub(-i, -i) ~= suffix_pattern:sub(-i, -i) then + return str + end + end + return str:sub(1, -#suffix_pattern - 1) end, 2) return _ -- cgit v1.2.3-70-g09d2