Jump to content




Escape Sequences that affect the string, string length function


  • You cannot reply to this topic
6 replies to this topic

#1 augustas656

  • Members
  • 158 posts

Posted 11 June 2014 - 04: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

#2 KingofGamesYami

  • Members
  • 3,002 posts
  • LocationUnited States of America

Posted 11 June 2014 - 05: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 by KingofGamesYami, 11 June 2014 - 05:34 PM.


#3 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 11 June 2014 - 05:28 PM

I think you meant gmatch.

#4 augustas656

  • Members
  • 158 posts

Posted 11 June 2014 - 05:42 PM

View PostKingofGamesYami, on 11 June 2014 - 05:09 PM, said:

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 by augustas656, 11 June 2014 - 05:42 PM.


#5 augustas656

  • Members
  • 158 posts

Posted 11 June 2014 - 05: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 by augustas656, 11 June 2014 - 05:47 PM.


#6 MKlegoman357

  • Members
  • 1,170 posts
  • LocationKaunas, Lithuania

Posted 11 June 2014 - 06: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


#7 augustas656

  • Members
  • 158 posts

Posted 11 June 2014 - 06: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 by augustas656, 11 June 2014 - 10:15 PM.






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users