Skip to content

Commit 75d5ac2

Browse files
authored
Cheaper string.Trim* functions for E2 (#3626)
* Cheaper string.Trim* functions for E2 I took some code from Starfall to make the regex check less strict * Use string.byte trimming + fix small regression from previous commit * Fixes
1 parent f3723da commit 75d5ac2

2 files changed

Lines changed: 19 additions & 25 deletions

File tree

lua/entities/gmod_wire_expression2/base/preprocessor.lua

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,18 @@ function PreProcessor:Trim(line)
114114
return string.sub(line, first, last)
115115
end
116116

117+
function PreProcessor:TrimLeft(line)
118+
for i = 1, #line do
119+
local b = string.byte(line, i)
120+
121+
if b ~= 32 and (b < 9 or b > 13) then
122+
return string.sub(line, i)
123+
end
124+
end
125+
126+
return ""
127+
end
128+
117129
function PreProcessor:TrimRight(line)
118130
for i = #line, 1, -1 do
119131
local b = string.byte(line, i)
@@ -123,7 +135,6 @@ function PreProcessor:TrimRight(line)
123135
end
124136
end
125137

126-
-- The line consists only of spaces
127138
return ""
128139
end
129140

lua/entities/gmod_wire_expression2/core/string.lua

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -231,34 +231,17 @@ end
231231

232232
__e2setcost(2)
233233

234+
-- E2Lib.PreProcessor trimming functions are much more efficient than regular ones, so it's better to use them
234235
e2function string string:trim()
235-
local ok, ret = pcall(function() WireLib.CheckRegex(this, "^%s*(.-)%s*$") return string.Trim(this) end)
236-
237-
if not ok then
238-
return self:throw(ret)
239-
else
240-
return ret
241-
end
236+
return E2Lib.PreProcessor.Trim(nil, this)
242237
end
243238

244239
e2function string string:trimLeft()
245-
local ok, ret = pcall(function() WireLib.CheckRegex(this, "^%s*(.+)$") return string.TrimLeft(this) end)
246-
247-
if not ok then
248-
return self:throw(ret)
249-
else
250-
return ret
251-
end
240+
return E2Lib.PreProcessor.TrimLeft(nil, this)
252241
end
253242

254243
e2function string string:trimRight()
255-
local ok, ret = pcall(function() WireLib.CheckRegex(this, "^(.-)%s*$") return string.TrimRight(this) end)
256-
257-
if not ok then
258-
return self:throw(ret)
259-
else
260-
return ret
261-
end
244+
return E2Lib.PreProcessor.TrimRight(nil, this)
262245
end
263246

264247
--[[******************************************************************************]]--
@@ -270,7 +253,7 @@ e2function number string:findRE(string pattern)
270253
local ok, ret = pcall(function() WireLib.CheckRegex(this, pattern) return string_find(this, pattern) end)
271254

272255
if not ok then
273-
return self:throw(ret)
256+
return self:throw(ret, 0)
274257
else
275258
return ret or 0
276259
end
@@ -281,7 +264,7 @@ e2function number string:findRE(string pattern, start)
281264
local ok, ret = pcall(function() WireLib.CheckRegex(this, pattern) return string_find(this, pattern, start) end)
282265

283266
if not ok then
284-
return self:throw(ret)
267+
return self:throw(ret, 0)
285268
else
286269
return ret or 0
287270
end
@@ -571,4 +554,4 @@ end
571554
[nodiscard]
572555
e2function string hashSHA256(string text)
573556
return hash_generic(self, text, util.SHA256)
574-
end
557+
end

0 commit comments

Comments
 (0)