Token identifiers with null tokens

By jaytea on Jul 03, 2011

Currently, all of mIRC's $*tok() identifiers will ignore and effectively strip null tokens / excess delimiters in their input. This can be desirable, but it can also be quite inconvenient and limiting, and a workaround is non-trivial in the general case.

For example:

//echo -a $gettok(a..b, 2, 46)

This returns 'b' since the null token between the two '.'s is ignored. With this snippet, $tok(get, a..b, 2, 46) returns $null instead.

Another more practical example where this is likely to pose a problem is in the handling of comma delimited settings:

//var %settings = 1,,5,,,100,0,,10,,,3 | echo -a $gettok(%settings, 5, 44)

If we use null tokens to represent uninitialized settings then it can be frustrating to discover that $gettok() is not capable of handling those values in the way that we require.

/*
{ -------------------------------------------------------------------------------

  $tok(<name>, <text>, [, parm, parm, ...])[.cs]

  Returns the value of a call to $<name>tok[cs](<text>, [, parm1, parm2, ...])
  but acknowledges null tokens both in <text> and appropriate parms.

  Use the .cs property to call the case sensitive version of the token identifier.

  Examples:

  $tok(num, a..b..c, 46) = 5
  $tok(rem, a..b..c, , 2, 46) = a..b.c
  $tok(put, a..c, b, 2, 46) = a.b.c
  $tok(rep, a..c, c, , 46) = a..
  $tok(find, .a.A, A, 46).cs = 3

  Note that if you plan to use this for sorting, the randomly selected placeholder,
  chosen from the 256-55295 code point range will result in null tokens being placed 
  after characters in the typically used 1-255 range. 

} -------------------------------------------------------------------------------
*/

alias tok {
  while ($chr($rand(256, 55295)) isin $2) /
  var %marker = $v1 | ; random placeholder

  var %C = $iif($1 == sort, $3, $eval($ $+ $0, 2))
  if (%C !isnum) return

  var %i = 3, %parms, %delim = $+(\Q, $chr(%C), \E), %cs
  if ($prop == cs) && ($istok(add find is match rem rep sort wild, $1, 32)) %cs = cs 

  if ($0 > 2) && ($3 == $null) && ($istok(add find ins is put rem rep wild, $1, 32)) {
    %i = 4
    %parms = ,%marker
  }

  while (%i <= $0) {
    if (%i == 4) && ($1 == rep) && ($4 == $null) {
      %parms = %parms ,%marker
    }
    else {
      %parms = %parms ,$ $+ %i
    }
    inc %i
  }

  returnex $remove($eval($+($, $1, tok, %cs, ( $eval(                 $&
    $regsubex(__tokn,$2, /(?<=^| %delim )(?=\z| %delim )/gx, %marker) $&
    , 0) %parms )), 2), %marker)
}

/*
{ -------------------------------------------------------------------------------

  $tokcs(<name>, <text>, [, parm, parm, ...])

  Syntactic sugar for $tok().cs.

  For example:

  $tokcs(rep, A..a., a, , 46) = $tok(rep, A..a., a, , 46).cs = A...

} -------------------------------------------------------------------------------
*/

alias tokcs {
  var %i = 1, %parms
  while (%i <= $0) {
    %parms = %parms ,$ $+ %i
    inc %i
  }
  returnex $tok( [ $mid(%parms, 2) ] ).cs
}

Comments

Sign in to comment.
PuNkTuReD   -  Jul 10, 2011

reinventing the mirc scripting language...
i like...

its obvious you have alot of knowledge regarding mircs scripting language jaytea,
have you done, or plan on doing some in depth tutorials or write ups on it?

 Respond  
KholdStare88   -  Jul 09, 2011

Very nice. Sometimes ignoring null tokens can help in certain situations, but like most things having options for all situations is the best thing.

 Respond  
jaytea   -  Jul 05, 2011

thanks again!

If I may ask, what's returnex and it's syntax?

it's the same as /return, except it will preserve leading, trailing and multiple consecutive spaces.

for example, $tok(get, a. ., 2, 46) should return $chr(32), but '/return $chr(32)' will result in $null being returned.

it's a good idea to use /returnex when returning arbitrary text.

 Respond  
Veritas   -  Jul 05, 2011

Very nice script Jaytea, 9/10 +like, I think this can be very usefull :)

 Respond  
Conscious   -  Jul 05, 2011

10/10

If I may ask, what's returnex and it's syntax?

 Respond  
jaytea   -  Jul 05, 2011

thanks guys! napalm, as someone who's been participating in scripting competitions on various sites for ~7 years, i'm a little embarrassed to admit i've never seen one related to this :P if you can dig it up i'd be quite interested to have a look

 Respond  
napa182   -  Jul 03, 2011

Nice job jaytea 8/10 +Like

 Respond  
napalm`   -  Jul 03, 2011

I believe they had a scripting competition some time ago that dealt with this issue. I can't recall what site, but I'm certain this would have been a quality contestant. Nice work. 8/10 +Like

 Respond  
blackvenomm666   -  Jul 03, 2011

umm truly i dont understand tokens enough to understand this one but i do understand that your a damn good scriptor so i'll say it anyways. good job jaytea:D

 Respond  
Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.