From 33bcdc554b3e407354a2580a8bbf2b34501acf11 Mon Sep 17 00:00:00 2001 From: Astralcircle <142503363+Astralcircle@users.noreply.github.com> Date: Fri, 12 Jun 2026 11:49:14 +0300 Subject: [PATCH 1/3] Cheaper string.Trim* functions for E2 I took some code from Starfall to make the regex check less strict --- .../gmod_wire_expression2/core/string.lua | 21 +++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/lua/entities/gmod_wire_expression2/core/string.lua b/lua/entities/gmod_wire_expression2/core/string.lua index eb62f04a33..64f66ea51d 100644 --- a/lua/entities/gmod_wire_expression2/core/string.lua +++ b/lua/entities/gmod_wire_expression2/core/string.lua @@ -232,7 +232,12 @@ end __e2setcost(2) e2function string string:trim() - local ok, ret = pcall(function() WireLib.CheckRegex(this, "^%s*(.-)%s*$") return string.Trim(this) end) + local ok, ret = pcall(function() + WireLib.CheckRegex(this, "%s*") + local _, start = string.find(this, "^%s*") + local stop = string.find(this, "%s*$") + return string.sub(this, start + 1, stop - 1) + end) if not ok then return self:throw(ret) @@ -242,7 +247,11 @@ e2function string string:trim() end e2function string string:trimLeft() - local ok, ret = pcall(function() WireLib.CheckRegex(this, "^%s*(.+)$") return string.TrimLeft(this) end) + local ok, ret = pcall(function() + WireLib.CheckRegex(this, "%s*") + local _, start = string.find(this, "^%s*") + return string.sub(this, start + 1) + end) if not ok then return self:throw(ret) @@ -252,7 +261,11 @@ e2function string string:trimLeft() end e2function string string:trimRight() - local ok, ret = pcall(function() WireLib.CheckRegex(this, "^(.-)%s*$") return string.TrimRight(this) end) + local ok, ret = pcall(function() + WireLib.CheckRegex(this, "%s*") + local stop = string.find(this, "%s*$") + return string.sub(this, 1, stop - 1) + end) if not ok then return self:throw(ret) @@ -571,4 +584,4 @@ end [nodiscard] e2function string hashSHA256(string text) return hash_generic(self, text, util.SHA256) -end \ No newline at end of file +end From 249117774c174e1100e1169f70c1e5b18c021968 Mon Sep 17 00:00:00 2001 From: Astralcircle <142503363+Astralcircle@users.noreply.github.com> Date: Fri, 12 Jun 2026 18:32:30 +0300 Subject: [PATCH 2/3] Use string.byte trimming + fix small regression from previous commit --- .../base/preprocessor.lua | 13 +++++- .../gmod_wire_expression2/core/string.lua | 43 +++---------------- 2 files changed, 19 insertions(+), 37 deletions(-) diff --git a/lua/entities/gmod_wire_expression2/base/preprocessor.lua b/lua/entities/gmod_wire_expression2/base/preprocessor.lua index 2868e508f6..3a44320631 100644 --- a/lua/entities/gmod_wire_expression2/base/preprocessor.lua +++ b/lua/entities/gmod_wire_expression2/base/preprocessor.lua @@ -114,6 +114,18 @@ function PreProcessor:Trim(line) return string.sub(line, first, last) end +function PreProcessor:TrimLeft(line) + for i = 1, #line do + local b = string.byte(line, i) + + if b ~= 32 and (b < 9 or b > 13) then + return string.sub(line, i) + end + end + + return "" +end + function PreProcessor:TrimRight(line) for i = #line, 1, -1 do local b = string.byte(line, i) @@ -123,7 +135,6 @@ function PreProcessor:TrimRight(line) end end - -- The line consists only of spaces return "" end diff --git a/lua/entities/gmod_wire_expression2/core/string.lua b/lua/entities/gmod_wire_expression2/core/string.lua index 64f66ea51d..98dde4946b 100644 --- a/lua/entities/gmod_wire_expression2/core/string.lua +++ b/lua/entities/gmod_wire_expression2/core/string.lua @@ -231,47 +231,18 @@ end __e2setcost(2) -e2function string string:trim() - local ok, ret = pcall(function() - WireLib.CheckRegex(this, "%s*") - local _, start = string.find(this, "^%s*") - local stop = string.find(this, "%s*$") - return string.sub(this, start + 1, stop - 1) - end) +-- E2Lib.PreProcessor trimming functions are much more efficient than regular ones, so it's better to use them - if not ok then - return self:throw(ret) - else - return ret - end +e2function string string:trim() + return E2Lib.PreProcessor.Trim(this) end e2function string string:trimLeft() - local ok, ret = pcall(function() - WireLib.CheckRegex(this, "%s*") - local _, start = string.find(this, "^%s*") - return string.sub(this, start + 1) - end) - - if not ok then - return self:throw(ret) - else - return ret - end + return E2Lib.PreProcessor.TrimLeft(this) end e2function string string:trimRight() - local ok, ret = pcall(function() - WireLib.CheckRegex(this, "%s*") - local stop = string.find(this, "%s*$") - return string.sub(this, 1, stop - 1) - end) - - if not ok then - return self:throw(ret) - else - return ret - end + return E2Lib.PreProcessor.TrimRight(this) end --[[******************************************************************************]]-- @@ -283,7 +254,7 @@ e2function number string:findRE(string pattern) local ok, ret = pcall(function() WireLib.CheckRegex(this, pattern) return string_find(this, pattern) end) if not ok then - return self:throw(ret) + return self:throw(ret, 0) else return ret or 0 end @@ -294,7 +265,7 @@ e2function number string:findRE(string pattern, start) local ok, ret = pcall(function() WireLib.CheckRegex(this, pattern) return string_find(this, pattern, start) end) if not ok then - return self:throw(ret) + return self:throw(ret, 0) else return ret or 0 end From 0385077a089ad41c230512e47240f791288eeff8 Mon Sep 17 00:00:00 2001 From: Astralcircle <142503363+Astralcircle@users.noreply.github.com> Date: Sun, 14 Jun 2026 07:16:17 +0300 Subject: [PATCH 3/3] Fixes --- lua/entities/gmod_wire_expression2/core/string.lua | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/lua/entities/gmod_wire_expression2/core/string.lua b/lua/entities/gmod_wire_expression2/core/string.lua index 98dde4946b..c7c6774b25 100644 --- a/lua/entities/gmod_wire_expression2/core/string.lua +++ b/lua/entities/gmod_wire_expression2/core/string.lua @@ -232,17 +232,16 @@ end __e2setcost(2) -- E2Lib.PreProcessor trimming functions are much more efficient than regular ones, so it's better to use them - e2function string string:trim() - return E2Lib.PreProcessor.Trim(this) + return E2Lib.PreProcessor.Trim(nil, this) end e2function string string:trimLeft() - return E2Lib.PreProcessor.TrimLeft(this) + return E2Lib.PreProcessor.TrimLeft(nil, this) end e2function string string:trimRight() - return E2Lib.PreProcessor.TrimRight(this) + return E2Lib.PreProcessor.TrimRight(nil, this) end --[[******************************************************************************]]--