March 03, 2004

Space woes

Recently, while scripting a text replacer, I found myself faced with a very curious situation: an IF statement that didn't trigger even if, to all apparences, the text passing through it should have matched and triggered it.

ON *:TEXT:*:*: {
  var %checktext = $strip($1-)
  if (some text isin %checktext) {
    ; do something
  }
}

In some (not all) cases, even if %checktext contained "some text", the IF statement wouldn't catch it. Not even turning the match into a wildcard match (*some*text* iswm %checktext) could fix the problem. What was happening?

After a few experiments, I narrowed the problem down to control codes and space collapsing. mIRC, as commonly known, will collapse multiple spaces into a single one in scripts. However, for some reason, it didn't seem to be collapsing properly space strings with a control code in the middle of them, even if I was stripping the control codes from the strip first thing.

A line  with some  text in it.
A line  with some  text in it.

The first line would correctly trigger the IF statement, the second one would not, even though both returned the same thing when stripped:

A line with some text in it.
A line with some text in it.

Or did they?

After a lot of investigating, and with the help of a friend, I further narrowed down the problem. mIRC did not, indeed, properly collapse spaces when they were separated by a control code - the stripped text looked like it only contained one space, but instead contained two (or more, depending on how the control codes and the spaces were positioned). This became obvious by looking at the ascii values of each character in the stripped line with a loop (32 being the value for a space).

The first string went:

65 32 108 105 110 101 32 119 105 116 104 32 115 111 109 101 32 116 101 120 116 32 105 110 32 105 116 46

While the second one:

65 32 108 105 110 101 32 32 119 105 116 104 32 115 111 109 101 32 32 116 101 120 116 32 105 110 32 105 116 46

See the double spaces?

[edited - March 5 2004: a simpler workaround was suggested to me, so I replaced mine with the suggestion I received]

Now to fix it...

  var %checktext = $gettok($strip($1-),1-,32)

By using $gettok to fetch all the tokens from the string, using a space as the separating character, the extra spaces are stripped from the text.

And the final, working script:

ON *:TEXT:*:*: {
  var %checktext = $gettok($strip($1-),1-,32)
  if (some text isin %checktext) {
    ; do something
  }
}
Posted by sailoreagle at March 3, 2004 10:58 PM | Tutorials
Comments