Binary variable identifiers

By jaytea on Mar 26, 2008

/*


*

  • binvar identifiers by jaytea
  • re-creates mirc's own text identifiers but lets you pass the name of a binvar to use
  • in the main string parameter. all identifiers that normally return strings will instead
  • return the name of the binary variable containing the result ($1). this means if you want
  • to preserve the original binvar you'll need to /bcopy first.
  • this behavior allows you to nest binvar identifiers very easily, for example:
  • //bset -t &x 1 haaahah lolol | echo -a $bvar($bremove($bmid(&x,2,-3),a),1-).text
  • each alias works as a standalone piece of code, ie. they're all independent of each
  • other. so you can pick and choose which you want if you don't wish to copyt them all
  • syntax is equivalent to the assosciated mirc identifier, except you prepend a 'b' to the
  • name of the identifier and pass the name of a binvar as the first parameter
  • token identifiers not yet implemented, i'll do those if there's a desire for them, though
  • there's a single prop:
  • $bident().text
  • for all appropriate identifiers. why would you need it? well, as it stands, every parameter
  • (except $1) that's normally a string is checked for a sequence of bytes, for example:
  • $bcount(&b,abc,32) counts the number of "abc"s as well as the spaces $chr(32)
  • $bremove(&b,97 98 99) removes all occurences of "abc"
  • this mimics $bfind()'s behavior. you can stop this from happening with the .text prop
  • like i said i've tried to replicate mirc's own behavior in almost every way with regard to
  • subtleties such as preserving spaces, and reproducing less known but equally useful behavior

  • */

alias bcount {
  if (!$isid) || ($0 < 2) || (!$regex($1,/^&[^ ]+$/)) {
    echo -ces i * Invalid parameters: $!bcount
    return
  }
  var %x = 1, %c = 0
  if ($bvar($1,0)) {
    while (%x < $0) {
      inc %x
      var %i = 1
      while ($bfind($1,%i,$eval($ $+ %x,2)) [ $+ [ $iif($prop == text,.text) ] ]) {
        %i = $v1 + 1
        inc %c
      }
    }
  }
  return %c
}

alias bhash {
  if (!$isid) || ($0 != 2) || (!$regex($1,/^&[^ ]+$/)) || ($int($2) !isnum 1-32) {
    echo -ces i * Invalid parameters: $!bhash
    return
  }
  if (!$bvar($1,0)) return
  var %i = 1, %x = 0
  while ($bvar($1,%i) >= 0) {
    %x = $calc(($v1 + (%x - %x % 2^24) / 2^24 + %x) * 2^8 % 2^32)
    inc %i
  }
  return $iif(%x,$iif($ceil($calc(2^($int($2) - 32) * %x)),$v1,1),0)
}

alias bislower {
  if (!$isid) || ($0 != 1) || (!$regex($1,/^&[^ ]+$/)) {
    echo -ces i * Invalid parameters: $!bislower
    return
  }
  if (!$bvar($1,0)) return
  var %i = 65
  while (%i < 91) {
    if ($bfind($1,1,%i)) return $false
    inc %i
  }
  return $true
}

alias bisupper {
  if (!$isid) || ($0 != 1) || (!$regex($1,/^&[^ ]+$/)) {
    echo -ces i * Invalid parameters: $!bisupper
    return
  }
  if (!$bvar($1,0)) return
  var %i = 97
  while (%i < 123) {
    if ($bfind($1,1,%i)) return $false
    inc %i
  }
  return $true
}

alias bleft {
  if (!$isid) || ($0 != 2) || (!$regex($1,/^&[^ ]+$/)) || ($int($2) !isnum) {
    echo -ces i * Invalid parameters: $!bleft
    return
  }
  if ($bvar($1,0)) bcopy -c $1 1 $1 1 $calc($v1 * $iif($int($2) < 0,1,0) + $v1)
  return $1
}

alias blower {
  if (!$isid) || ($0 != 1) || (!$regex($1,/^&[^ ]+$/)) {
    echo -ces i * Invalid parameters: $!blower
    return
  }
  if ($bvar($1,0)) {
    var %i = 65, %o
    while (%i < 91) {
      %o = %o %i $biton(%i,6)
      inc %i
    }
    breplace $1 %o
  }
  return $1
}

alias blen {
  if (!$isid) || ($0 != 1) || (!$regex($1,/^&[^ ]+$/)) {
    echo -ces i * Invalid parameters: $!blen
    return
  }
  return $bvar($1,0)
}

alias bmid {
  if (!$isid) || ($0 !isnum 2-3) || (!$regex($1,/^&[^ ]+$/)) || ($int($2) !isnum) || (($0 == 3) && ($int($3) !isnum)) {
    echo -ces i * Invalid parameters: $!bmid
    return
  }
  var %a = $bvar($1,0), %x
  if (%a) {
    %x = $iif($iif($int($2) < 0,$calc(1 + %a + $v1),$v1) < 1,1,$v1)
    bcopy -c $1 1 $1 %x $iif($int($3),$iif($v1 < 0,$calc(1 + %a + $v1 - %x),$v1)) -1
  }
  return $iif($int($3) == 0,$bvar($1,0),$1)
}

alias bnoqt {
  if (!$isid) || ($0 != 1) || (!$regex($1,/^&[^ ]+$/)) {
    echo -ces i * Invalid parameters: $!bnoqt
    return
  }
  var %x = $bvar($1,0)
  if ($bvar($1,%x) == 34) && ($calc(%x - 1)) bset -c $1 $v1 $bvar($1,$v1)
  if ($bvar($1,1) == 34) {
    if ($bvar($1,0) > 1) bcopy -c $1 1 $1 2 -1
    else bunset $1
  }
}

alias bpos {
  if (!$isid) || ($0 !isnum 2-3) || (!$regex($1,/^&[^ ]+$/)) || (($0 == 3) && ($int($3) !isnum)) {
    echo -ces i * Invalid parameters: $!bpos
    return
  }
  var %x = 0, %i = 1 
  while ($bfind($1,%i,$2) [ $+ [ $iif($prop == text,.text) ] ]) {
    %i = $v1
    inc %x
    if (%x == $int($3 1)) return %i
    inc %i
  }
  if (!$int($3)) return %x
}

alias bqt {
  if (!$isid) || ($0 != 1) || (!$regex($1,/^&[^ ]+$/)) {
    echo -ces i * Invalid parameters: $!bqt
    return
  }
  if ($bvar($1,1) != 34) {
    if ($len($v1)) bcopy $1 2 $1 1 -1
    bset $1 1 34
  }
  if ($bvar($1,0) < 2) || ($bvar($1,$v1) != 34) bset $1 $calc(1 + $bvar($1,0)) 34
  return $1
}

alias bremove {
  if (!$isid) || ($0 < 2) || (!$regex($1,/^&[^ ]+$/)) {
    echo -ces i * Invalid parameters: $!bremove
    return
  }
  if ($bvar($1,0)) {
    var %x = 1
    while (%x < $0) {
      inc %x
      var %i = 1, %t = $eval($ $+ %x,2) $chr(32)
      while ($bfind($1,%i,$left(%t,-2)) [ $+ [ $iif($prop == text,.text) ] ]) {
        var %i = $v1, %p = $iif($prop != text && $regex(bremove,%t,/^ *\d+(?: +\d+)* *$/i),$numtok(%t,32),$len($mid(%t,3))) + %i
        if (%p <= $bvar($1,0)) bcopy -c $1 %i $1 %p -1
        elseif ($calc(%i - 1)) bset -c $1 $v1 $bvar($1,$v1)
        else bunset $1
      }
      inc %x 2
    }
  }
  return $1
}

alias breplace {
  if (!$isid) {
    !breplace $1-
    return
  }
  if ($0 < 3) || (2 // $0) || (!$regex($1,/^&[^ ]+$/)) {
    echo -ces i * Invalid parameters: $!breplace
    return
  }
  if ($bvar($1,0)) {
    var %x = 1, %j
    while (%x < $0) {
      inc %x
      var %j = $eval($ $+ %x,2) $chr(32), %z
      bunset &breplaceOut
      if ($prop != text) && ($regex(breplace,%j,/^ *\d+(?: +\d+)* *$/i)) bset &breplaceOut 1 %j
      else {
        %z = 1
        while ($asc($mid(%j,%z,-2))) {
          bset &breplaceOut %z $v1
          inc %z
        } 
      }
      var %i = $bvar(&breplaceIn,0), %o = $bvar(&breplaceOut,0)
      if (%x & 1) && (%i) {
        %j = 1
        while ($bfind($1,%j,$eval($ $+ $calc(%x - 1),2)) [ $+ [ $iif(%z,.text) ] ]) {
          %j = $v1
          if ($calc(%j + %i) <= $bvar($1,0)) bcopy -c $1 $calc(%j + %o) $1 $v1 -1
          elseif ($calc(%j - 1)) bset -c $1 $v1 $bvar($1,$v1)
          else bunset $1
          %z = 1
          while ($bvar(&breplaceOut,%z) >= 0) {
            bset $1 %j $v1
            inc %z
            inc %j
          }
        }
      }
      bunset &breplaceIn
      if (%o) bcopy -c &breplaceIn 1 &breplaceOut 1 -1
    }
  }
  return $1
}

alias breplacecs {
  if (!$isid) || ($0 < 3) || (2 // $0) || (!$regex($1,/^&[^ ]+$/)) {
    echo -ces i * Invalid parameters: $!breplacecs
    return
  }
  if ($bvar($1,0)) {
    var %x = 1, %j
    while (%x < $0) {
      inc %x
      %j = $eval($ $+ %x,2) $chr(32)
      bunset &breplaceOut
      if ($prop != text) && ($regex(bremove,%j,/^ *\d+(?: +\d+)* *$/i)) bset &breplaceOut 1 %j
      else {
        var %z = 1
        while ($asc($mid(%j,%z,-2))) {
          bset &breplaceOut %z $v1
          inc %z
        } 
      }
      var %i = $bvar(&breplaceIn,0), %o = $bvar(&breplaceOut,0)
      if (%x & 1) && (%i) {
        %j = 1
        while ($bfind($1,%j,$bvar(&breplaceIn,1-))) {
          %j = $v1
          if ($calc(%j + %i) <= $bvar($1,0)) bcopy -c $1 $calc(%j + %o) $1 $v1 -1
          elseif ($calc(%j - 1)) bset -c $1 $v1 $bvar($1,$v1)
          else bunset $1
          var %z = 1
          while ($bvar(&breplaceOut,%z) >= 0) {
            bset $1 %j $v1
            inc %z
            inc %j
          }
        }
      }
      bunset &breplaceIn
      if (%o) bcopy -c &breplaceIn 1 &breplaceOut 1 -1
    }
  }
  return $1
}

alias breplacex {
  if (!$isid) || ($0 < 3) || (2 // $0) || (!$regex($1,/^&[^ ]+$/)) {
    echo -ces i * Invalid parameters: $!breplacex
    return
  }
  if ($bvar($1,0)) {
    bcopy $1 1 $1 1 -1
    if ($window(@breplacex)) close -@ $v1
    window -h @breplacex
    var %i = 1
    while (%i < $0) {
      inc %i
      var %j = $eval($ $+ %i,2) $chr(32), %z = 1
      if (2 // %i) {
        %z = 0
        while ($bfind($1,%z,$left(%j,-2)) [ $+ [ $iif($prop == text,.text) ] ]) {
          echo @breplacex $v1 %i
          %z = $v1 + 1
        }
      }
      if (%z) {
        if ($prop != text) && ($regex(breplacex,%j,/^ *\d+(?: +\d+)* *$/i)) bset -c &breplacexparm $+ %i 1 %j
        else {
          %z = 1
          while ($asc($mid(%j,%z,-2))) {
            bset -c &breplacexparm $+ %i %z $v1
            inc %z
          }
        }
      }
    }
    filter -cwwtu 1 32 @breplacex @breplacex
    var %j = 1, %w = 0
    while ($line(@breplacex,1)) {
      tokenize 32 $1 $v1
      if ($2 >= %j) {
        var %i = $bvar(&breplacexparm $+ $3,0), %o = $bvar(&breplacexparm $+ $calc(1 + $3),0), %x = $2 + %w
        if ($calc(%x + %i) <= $bvar($1,0)) bcopy -c $1 $calc(%x + %o) $1 $v1 -1
        elseif ($calc(%x - 1)) bset -c $1 $v1 $bvar($1,$v1)
        else bunset $1
        var %z = 1, %w = $calc(%w + %o - %i), %j = $2 + %i
        while ($bvar(&breplacexparm $+ $calc(1 + $3),%z) >= 0) {
          bset $1 %x $v1
          inc %z
          inc %x
        }
      }
      dline @breplacex 1
    }
    close -@ @breplacex
  }
  return $1
}

alias breplacexcs {
  if (!$isid) || ($0 < 3) || (2 // $0) || (!$regex($1,/^&[^ ]+$/)) {
    echo -ces i * Invalid parameters: $!breplacexcs
    return
  }
  if ($bvar($1,0)) {
    if ($window(@breplacexcs)) close -@ $v1
    window -h @breplacexcs
    var %i = 1
    while (%i < $0) {
      inc %i
      var %j = $eval($ $+ %i,2) $chr(32), %z = 1
      if ($prop != text) && ($regex(breplacexcs,%j,/^ *\d+(?: +\d+)* *$/i)) bset -c &breplacexcsparm $+ %i 1 %j
      else {
        %z = 1
        while ($asc($mid(%j,%z,-2))) {
          bset -c &breplacexcsparm $+ %i %z $v1
          inc %z
        }
      }
      if (2 // %i) {
        %z = 0
        while ($bfind($1,%z,$bvar(&breplacexcsparm $+ %i,1-))) {
          echo @breplacexcs $v1 %i
          %z = $v1 + 1
        }
      }
    }
    filter -cwwtu 1 32 @breplacexcs @breplacexcs
    var %j = 1, %w = 0
    while ($line(@breplacexcs,1)) {
      tokenize 32 $1 $v1
      if ($2 >= %j) {
        var %i = $bvar(&breplacexcsparm $+ $3,0), %o = $bvar(&breplacexcsparm $+ $calc(1 + $3),0), %x = $2 + %w
        if ($calc(%x + %i) <= $bvar($1,0)) bcopy -c $1 $calc(%x + %o) $1 $v1 -1
        elseif ($calc(%x - 1)) bset -c $1 $v1 $bvar($1,$v1)
        else bunset $1
        var %z = 1, %w = $calc(%w + %o - %i), %j = $2 + %i
        while ($bvar(&breplacexcsparm $+ $calc(1 + $3),%z) >= 0) {
          bset $1 %x $v1
          inc %z
          inc %x
        }
      }
      dline @breplacexcs 1
    }
    close -@ @breplacexcs
  }
  return $1
}

alias bright {
  if (!$isid) || ($0 != 2) || (!$regex($1,/^&[^ ]+$/)) || ($int($2) !isnum) {
    echo -ces i * Invalid parameters: $!bright
    return
  }
  if ($bvar($1,0)) bcopy -c $1 1 $1 $iif($calc($v1 * $iif($int($2) < 0,0,1) + 1 - $v1) < 1,1,$v1) -1
  return $1
}

alias bstr {
  if ($0 != 2) || (!$regex($1,/^&[^ ]+$/)) || ($int($2) !isnum 0-) {
    echo -ces i * Invalid parameters: $!bstr
    return
  }
  var %x = $bvar($1,0), %n = $int($2)
  if (%x) && (%n) {
    while (%n > 1) {
      bcopy $1 $calc(1 + $bvar($1,0)) $1 1 %x
      dec %n
    }
  }
  else bunset $1
  return $1
}

alias bstrip {
  if (!$isid) || ($0 !isnum 1-2) || (!$regex($1,/^&[^ ]+$/)) {
    echo -ces i * Invalid parameters: $!bstrip
    return
  }
  if (? iswm #$2) || (!$bvar($1,0)) bunset $1
  else {
    bcopy -c &bstripbak 1 $1 1 -1
    var %i = 0, %j = 5, %z = $+($chr(2),$chr(15),$chr(22),$chr(31),$chr(3))
    while (%j) {
      if ($mid(%z,%j,1) !isin $strip(%z,$2)) || ($0 < 2) {    
        var %k = 1
        while ($bfind($1,%k,$asc($mid(%z,%j)))) {
          inc %i
          var %k = $v1, %l = $v1 + 1
          if (%j == 5) && ($bvar($1,%l) isnum 48-57) {
            inc %l
            if ($bvar($1,%l) isnum $v2) inc %l
            if ($bvar($1,%l) == 44) && ($bvar($1,$calc(1 + %l)) isnum 48-57) {
              inc %l
              if ($bvar($1,%l) isnum $v2) inc %l
            }
          }
          if (%l <= $bvar($1,0)) bcopy -c $1 %k $1 %l -1
          elseif ($calc(%k - 1)) bset -c $1 $v1 $bvar($1,$v1)
          else bunset $1
        }
      }
      dec %j
    }
    if (m isincs $2) && ($gettok($readini($mircini,options,n0),29-,44) > 1) && (%i <= $mid($v1,3)) bcopy -c $1 1 &bstripbak 1 -1
    bunset &bstripbak
  }
  return $1
}

alias bupper {
  if (!$isid) || ($0 != 1) || (!$regex($1,/^&[^ ]+$/)) {
    echo -ces i * Invalid parameters: $!bupper
    return
  }
  if ($bvar($1,0)) {
    var %i = 65, %o
    while (%i < 91) {
      %o = %o $biton(%i,6) %i
      inc %i
    }
    breplace $1 %o
  }
  return $1
}

Comments

Sign in to comment.
FordLawnmower   -  Jun 14, 2009

Awesome stuff!! Thanks :)

 Respond  
Jonesy44   -  Jul 23, 2008

Don\'t understand it whatsover, im ashamed to say xD

 Respond  
H_M   -  Jul 23, 2008

It\'s very nice, but I don\'t think i\'ll ever have much use for all of these. Incredibly job though.

 Respond  
Lindrian   -  Mar 26, 2008

Looks useful! great job :)

 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.