lua gen_inputbox message length check order

Posted by Fiendish on Sat 30 Apr 2011 11:51 PM — 5 posts, 26,634 views.

USA Global Moderator #0
I was looking through scripting/lua_utils.cpp and saw this in gen_inputbox


  // if we leave in & it will make the next letter underlined
  string sInputMsg = FindAndReplace (inputmsg, "&", "&&");

  if (sInputMsg.length () > 1000)
     luaL_error (L, "inputbox message too long (max 1000 characters)");


Unless I'm misinterpreting the "&" comment, that length check seems like it should come before the FindAndReplace.
Australia Forum Administrator #1
I suppose it depends *which* string we are applying the restraint to. In any case from memory the 1000 is just the Sanity Clause.
USA #2
It doesn't exactly represent the intent of the code very well, though. It either wants to limit the memory used by the string, or the amount of information seen on-screen. The former doesn't make too much sense (it's a fairly transient string, and the memory's already there when you check the length), so the latter seems to be right.

However, the &-escaping causes the string to expand, without changing the amount of data actually visible on-screen. If you have no &'s in your message you can fit 1000 characters, but with two &'s you're down to 998 for no immediately obvious reason. At the most ridiculous end, a string composed only of &'s can only be 500 characters long.

If the check came first, the visible data is always limited to 1000 characters. No surprises, clearer code.
USA Global Moderator #3
The documentation for utils.msgbox says:

Quote:
message to display (max 1000 characters)


The simplest change is obviously to make it say

Quote:
message to display (max 1000 minus the number of ampersands characters)


:D
Australia Forum Administrator #4
http://github.com/nickgammon/mushclient/commit/97888a35f3