All pastes #409430 Raw Edit

Mortal

public text v1 · immutable
#409430 ·published 2007-03-25 18:13 UTC
rendered paste body
// Mortal, 2007-03-25
// breaks down a whitespace-separated list into an array
// needs a little tweaking to allow tabs to separate and maybe some other things, but it does C-style as well as C++-style comments just fine.
/*
input =
// Userlist.
//  Nick      Reg date     Access
    Mortal    1174816255   100
    foobarbaz 1174842314   20

output =
array(
  array('Mortal', '1174816255', '100'),
  array('foobarbaz', '1174842314', '20'),
)
*/

function quotedchar($c) {
  if ($c == 't') return "\t";
  if ($c == 'n') return "\n";
  if ($c == '0') return "\0";
  return $c;
}
function xplode($s) {
  $i = 0;
  $n = strlen($s);
  $ret = array();
  while ($i < $n and $s{$i} == ' ') $i++;
  if ($i >= $n) return $ret;
  $j = $n;
  while ($n and $s{$n-1} == ' ') $n--;
  $cur = '';
  for ($i = $i; $i < $n; $i++) {
    if ($s{$i} === ' ') {
      if (!empty($cur)) $ret[] = $cur; $cur = '';
      continue;
    }
    if ($s{$i} === '\\') {
      $cur .= quotedchar($s{++$i});
      continue;
    }
    if ($s{$i} === '#' or ($i+1 < $n and ($s{$i}.$s{$i+1} === '//'))) break;
    if ($s{$i} === '"') {
      if (!empty($cur)) $ret[] = $cur; $cur = '';
      while (++$i < $n and $s{$i} != '"') {
        if ($s{$i} == '\\') {
          $cur .= quotedchar($s{++$i});
          continue;
        } else $cur .= $s{$i};
      }
      $i++;
      $ret[] = $cur; $cur = '';
      continue;
    }
    $cur .= $s{$i};
  }
  if (!empty($cur)) $ret[] = $cur; $cur = '';
  return $ret; 
}