-
Notifications
You must be signed in to change notification settings - Fork 332
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add WireLib.ParseEscapes #2752
Add WireLib.ParseEscapes #2752
Conversation
A lot cleaner this way also.
Noticed E2 string literals could advantage from this. Not going to touch it until #2555 is done though. |
Expose new function WireLib.HexToNum (this should be a Garry's feature imo)
Feels a bit uncomfortable adding any more passes. Also I added |
HexToNum can just be |
Of course. I knew I was missing something. |
The implementation of this keeps bugging me, obvious global functions or not. I'm going to draft it until it feels right. |
Try adding unicode to that, I dare you. Added Lua unicode support
That's not even the multi-pass gsub. I'm going to bed. |
I'd like to see an implementation more like local singleCharEscapes =
{
n = "\n", r = "\r", t = "\t", ["\\"] = "\\",
["'"] = "'", ["\""] = "\"", a = "\a",
b = "\b", f = "\f", v = "\v"
}
local multiCharEscapes =
{
x = function(arg)
end
}
str = string.gsub(str, "(\\(.?)([^\\]?[^\\]?))", function(str, i, arg)
return singleCharEscapes[i] or (multiCharEscapes[i] and multiCharEscapes[i](arg)) or str
end) |
Not as simple against unicode but I'll see what I can do. |
I can only feasibly get that idea to make sense by still having unicode parsed separately, and in that case it only parses two (or 10) multi conditions which is |
I think I'll settle for this which derives from your idea but inlines each case, which looks almost like what I started with... huh. |
Update comments
You can't do two passes otherwise stuff like |
Ah, you're right. Sorry, I'm not being thorough enough. |
I tried your suggestion again and realized it was simpler than I had thought. The only thing I'd like to know is if you wouldn't mind having it inlined instead. |
Looks pretty nice. I don't mind if its inlined. |
Fixed case where escape character would omit trailing text Fixed case where \x would omit trailing text
Missed a .
One last thing that we might want to be done is to benchmark it and limit the string length depending on how much it can do. Like maybe feed it a string.rep("\ ", 100) |
Hex should be specifically two characters long to match the Lua standard. |
All right, sure |
On my 2015 CPU in a server filled with sents, benchmark is between 0.001 to 0.004 seconds with 27075 semi-randomized characters and sequences with roughly 6290 matches. Even |
And I was just about to include it in the tokenizer. Oh well, another time. |
New WireLib function
ParseEscapes
. Takes a string input generated by the user (such as from a TextEntry panel) and parses escape characters out. The output is the escaped string. This is a complete implementation of Luajit's escape sequences.Purpose:
Some user inputs have their own implementation of escaping characters. This is meant to be a unified and thorough alternative and supports the entire Lua escape sequence library.
Notes:
This differs from Lua's escaping only in that invalid escape sequences are skipped instead of erroring.
I've only implemented this in constant value more as a demonstration than anything. Any user input should be changed to use this if desired. E2's tokenizer, for example, is a noteworthy target.