157 posts
Posted 11 June 2014 - 05:33 PM
From my understanding apperantely carriage return doesn't \r doesn't affect the string. Maybe it does, I'm not sure. I want to create a function that checks length of a string, I want to do this because the original one if you have something like \n it will count the \n as 1 long, it's not 2 but still it's there and there are none of either \ or n drawn on the screen. I want to make a length check that doesn't take escape sequences that do affect the string in account or something like that. Plz help
Regards
Augustas
3057 posts
Location
United States of America
Posted 11 June 2014 - 06:09 PM
So, you want to measure the length of the string without escape characters? I recommend using patterns, but there may be an easier way.
local string = "some\r random\n string"
local unform = ""
for line in string:gmatch( "%^\\*" ) do --#thank you, Lyqyd, for pointing that out
unform = unform..line
end
Edited on 11 June 2014 - 04:34 PM
8543 posts
Posted 11 June 2014 - 06:28 PM
I think you meant gmatch.
157 posts
Posted 11 June 2014 - 06:42 PM
So, you want to measure the length of the string without escape characters? I recommend using patterns, but there may be an easier way.
local string = "some\r random\n string"
local unform = ""
for line in string:gmatch( "%^\\*" ) do --#thank you, Lyqyd, for pointing that out
unform = unform..line
end
This code doesn't work, after the loop I type print(unform) it prints nothing, empty string.
Edited on 11 June 2014 - 04:42 PM
157 posts
Posted 11 June 2014 - 06:47 PM
To clarify my idea of the lengths
\n would count as 0
\x would count as 1 (I can take care of checking wether if the value after \ is a proper escape sequence call)
\\ would count as 1 (it's gonna write \, so it's 1)
So like, check if the char after the slash is a proper escape sequence char. If it's not then count the sequence as 1, else count it as 0. But I still have a problem.
\\\ would count as 1, because \\ is 1, but \ isn't.
\\\\ would count as 2, because \\ is 1, and \\ is 2
\\\\\ would count as 2, because \\ is 1, and \\ is 2 and \ isn't.
I could perhaps split the string into characters and just loop through every character, but I'm thinking is there a more lag-free method?
Regards
Augustas
Edited on 11 June 2014 - 04:47 PM
1140 posts
Location
Kaunas, Lithuania
Posted 11 June 2014 - 07:00 PM
You could remove all unwanted characters using string.gsub:
(untested code)
local text = "\ntext\r"
local newText = text:gsub("[\n\r]+", "")
print(#newText) -->> 4
157 posts
Posted 11 June 2014 - 07:05 PM
Yes, that would probably be the best way! I can't think of any scenarios where that wouldn't work, afterall, the length of \n with regular .len() or # is 1, meaning the \ doesn't count, so if I have some non-escape sequences but still with the \, for example \x. It would count as 1, and would only print "x" and "\\\\" is 2 and "\\\\\ " is 3. Thanks!
Edit: I have actually tested that it's the \ that doesn't count when it comes to \n
I had overthought it, I already had a function that gsubs all exclusions from texts that you want.
Regards
Augustas
Edited on 11 June 2014 - 09:15 PM