| 1 |
<?php |
| 2 |
|
| 3 |
namespace BitCode\BitForm\Jcof; |
| 4 |
|
| 5 |
class StringWriter |
| 6 |
{ |
| 7 |
public $str; |
| 8 |
|
| 9 |
public $maybeNextSep; |
| 10 |
|
| 11 |
public $prevCh; |
| 12 |
|
| 13 |
public function __construct() |
| 14 |
{ |
| 15 |
$this->str = ''; |
| 16 |
$this->maybeNextSep = null; |
| 17 |
$this->prevCh = null; |
| 18 |
} |
| 19 |
|
| 20 |
public function write($s) |
| 21 |
{ |
| 22 |
if ($this->maybeNextSep) { |
| 23 |
if (! $this->isSep($this->prevCh) && ! $this->isSep(substr($s, 0, 1))) { |
| 24 |
$this->str .= $this->maybeNextSep; |
| 25 |
} |
| 26 |
$this->maybeNextSep = null; |
| 27 |
} |
| 28 |
|
| 29 |
$this->str .= $s; |
| 30 |
$this->prevCh = substr($s, strlen($s) - 1, 1); |
| 31 |
} |
| 32 |
|
| 33 |
public function maybeSep($sep) |
| 34 |
{ |
| 35 |
if ($this->maybeNextSep) { |
| 36 |
$this->write($this->maybeNextSep); |
| 37 |
} |
| 38 |
|
| 39 |
$this->maybeNextSep = $sep; |
| 40 |
} |
| 41 |
|
| 42 |
public function isSep($ch) |
| 43 |
{ |
| 44 |
return $ch == '[' || $ch == ']' || $ch == '{' || $ch == '}' || |
| 45 |
$ch == '(' || $ch == ')' || $ch == ',' || $ch == ':' || $ch == '"'; |
| 46 |
} |
| 47 |
} |
| 48 |
|