Pattern (and JavaScript) escaping betterness #785

Merged
merged 3 commits into from Aug 24, 2014

Projects

None yet

4 participants

@willox
Collaborator
willox commented Aug 14, 2014

Makes string.JavascriptSafe work more nicely
Adds string.PatternSafe, changing current code that escapes patterns to use it
Makes default search providers for Sandbox use string.PatternSafe, stopping the spawn menu from creating errors when special characters are passed to its search box.

willox added some commits Aug 14, 2014
@willox willox string.JavascriptSafe improvements
Now less intensive on the GC, uses a nicer method of char replacement
a0d3ce2
@willox willox added string.PatternSafe
Escapes a string for safe use in a Lua pattern.
170c2ce
@willox willox make things use string.PatternSafe
Also fixes the spawn menu search box taking patterns and causing errors.
6ebe6d4
@Lexicality Lexicality commented on the diff Aug 14, 2014
garrysmod/lua/includes/extensions/string.lua
@@ -15,32 +15,54 @@ end
Name: string.JavascriptSafe( string )
Desc: Takes a string and escapes it for insertion in to a JavaScript string
-----------------------------------------------------------]]
-function string.JavascriptSafe( str )
+local javascript_escape_replacements = {
+ ["\\"] = "\\\\",
+ ["\0"] = "\\0" ,
+ ["\b"] = "\\b" ,
+ ["\t"] = "\\t" ,
+ ["\n"] = "\\n" ,
+ ["\v"] = "\\v" ,
+ ["\f"] = "\\f" ,
+ ["\r"] = "\\r" ,
+ ["\""] = "\\\"",
+ ["\'"] = "\\\'"
@Lexicality
Lexicality Aug 14, 2014 Contributor

um, ["'"] = "\\'"? You don't need to escape 's in "s

@willox
willox Aug 14, 2014 Collaborator

That was just to keep line lengths pretty. It's still valid.

@Python1320
Contributor

looks good to me

@robotboy655 robotboy655 merged commit 28e182c into garrynewman:master Aug 24, 2014
@willox willox deleted the unknown repository branch Aug 24, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment