Enable response chunking

@link https://github.com/klein/klein.php/wiki/Response-Chunking
@link http://bit.ly/hg3gHb
@param string $str   An optional string to send as a response "chunk"
@return Response
323280
public function chunk($str = null)
	{
		if (false === $this->chunked)
		{
			$this->chunked = true;
			$this->header('Transfer-encoding', 'chunked');
			flush();
		}

		if (($body_length = strlen($this->body)) > 0)
		{
			printf("%x\r\n", $body_length);
			$this->sendBody();
			$this->setBody('');
			echo "\r\n";
			flush();
		}

		if (null !== $str)
		{
			printf("%x\r\n", strlen($str));
			echo "$str\r\n";
			flush();
		}

		return $this;
	}
163535
private function split(string $str) {
        $len = strlen($str);
        for ($i = 0; $i < $len; $i++) {
            $c = $str[$i];
            $n = ord($c);
            if (($n >> 7) == 0) {
                //0xxx xxxx, asci, single
                yield $c;
            } elseif (($n >> 4) == 15) { //1111 xxxx, first in four char
                if ($i < $len - 3) {
                    yield $c . $str[$i + 1] . $str[$i + 2] . $str[$i + 3];
                    $i += 3;
                }
            } elseif (($n >> 5) == 7) {
                //111x xxxx, first in three char
                if ($i < $len - 2) {
                    yield $c . $str[$i + 1] . $str[$i + 2];
                    $i += 2;
                }
            } elseif (($n >> 6) == 3) {
                //11xx xxxx, first in two char
                if ($i < $len - 1) {
                    yield $c . $str[$i + 1];
                    $i++;
                }
            }
        }
    }
1001915
private static function hash($str)
    {
        $hash = 0;
        $str = str_split($str, 1);
        foreach ($str as $char) {
            $hash = ($hash << 4) + ord($char);
            $g = $hash & (0xf << (self::INT_SIZE - 4));
            if ($g !== 0) {
                $hash = $hash ^ ($g >> self::INT_SIZE - 8);
                $hash = $hash ^ $g;
            }
        }
        return $hash;
    }
216852
public function writeOutput($str)
    {
        if (empty($str)) {
            return $this;
        }

        $this->output[] = $str;

        if ($this->consoleOutput) {
            $this->consoleOutput->writeln($str);
        }

        return $this;
    }
485553
private static function _parseHttpSocketHeader($str) {
    $slice = explode("\r\n", $str);
    $headers = array();

    foreach ($slice as $v) {
      if (false !== strpos($v, "HTTP")) {
        list(, $headers["http_code"]) = explode(" ", $v);
        $headers["status"] = $v;
      } else {
        $item = explode(":", $v);
        $headers[$item[0]] = isset($item[1]) ? $item[1] : '';
      }
    }

    return $headers;
  }
535404
function load($str) {
		$this->data=imagecreatefromstring($str);
		imagesavealpha($this->data,TRUE);
		$this->save();
		return $this;
	}
1279817
public static function getString(&$str)
    {
        $p = strpos($str, "\x00");
        if ($p === false) {
            return '';
        }
        $r   = static::binarySubstr($str, 0, $p);
        $str = static::binarySubstr($str, $p + 1);

        return $r;
    }
1174551
public function loadTemplateFromString($str)
    {
        $this->source = 'string: '.$str;
        $this->template = $this->tags = [];
        if (!$str) {
            return;
        }
        $this->tag_cnt = [];

        /* First expand self-closing tags {$tag} -> {tag}{/tag} */
        $str = preg_replace('/{\$([-_:\w]+)}/', '{\1}{/\1}', $str);

        $this->parseTemplate($str);

        return $this;
    }
835641
static public function fromCamel($str) {
      $str[0] = strtolower($str[0]);
      return preg_replace_callback('/([A-Z])/', function($char) {return "_" . strtolower($char[1]);}, $str);
  }
692261
private function doUnserialize($str)
    {
        $m = $this->allowedClasses ? @unserialize($str, [
            'allowed_classes' => $this->allowedClasses,
        ]) : @unserialize($str);

        if (false === $m) {
            $err = error_get_last();
            throw new SerializationError(sprintf(
                'Error unserializing message: %s', 
                $err && isset($err['message']) ? $err['message'] : 'unknown error'
            ));
        }

        return $m;
    }
773332
public static function decode(String $str) : String
    {
        return str_replace(array_values(self::$phpTagChars), array_keys(self::$phpTagChars), $str);
    }
276343
protected function stringToBytes(string $str): int
    {
        $str = strtoupper(trim($str));

        $value = substr($str, 0, -1);
        $unit = substr($str, -1);
        switch ($unit) {
            case self::GIGABYTES:
                $value *= 1024;
            case self::MEGABYTES:
                $value *= 1024;
            case self::KILOBYTES:
                $value *= 1024;
        }

        return (int) $value;
    }
747954
public static function camelize($str)
    {
        if (strpos($str, '_') !== false) {
            if (PHP_VERSION_ID >= 50516) {
                //http://php.net/manual/en/function.ucwords.php
                return str_replace('_', '', ucwords($str, '_'));
            } else {
                $parts = explode('_', $str);
                foreach ($parts as $k => $v) {
                    $parts[$k] = ucfirst($v);
                }

                return implode('', $parts);
            }
        } else {
            return ucfirst($str);
        }
    }
721852
private function _strtoupper($str)
    {
        $str = html_entity_decode($str, ENT_COMPAT, $this->charset);
        $str = mb_strtoupper($str);
        $str = htmlspecialchars($str, ENT_COMPAT, $this->charset);

        return $str;
    }
1329623
public static function wordWrapEnable($str, $width)
    {
        if (!empty($str) && !empty($width) && $width >= 1) {
            return wordwrap($params['str'], $params['width'], '<br />\n');
        } else {
            return false;
        }
    }
554562
function encode_string ($str, $encoding = "base64") {
      switch(strtolower($encoding)) {
         case "base64":
            // chunk_split is found in PHP >= 3.0.6
            $encoded = chunk_split(base64_encode($str));
            break;

         case "7bit":
         case "8bit":
            $encoded = $this->fix_eol($str);
            if (substr($encoded, -2) != "\r\n")
               $encoded .= "\r\n";
            break;

         case "binary":
            $encoded = $str;
            break;

         case "quoted-printable":
            $encoded = $this->encode_qp($str);
            break;

         default:
            $this->error_handler(sprintf("Unknown encoding: %s", $encoding));
            return false;
      }
      return($encoded);
   }
425922
public function parse($str)
    {
        /* Set the tree root back to a fresh DocumentElement. */
        $this->reset();

        $parent = $this->treeRoot;
        $tokenizer = new Tokenizer($str);

        while ($tokenizer->hasNext()) {
            $parent = $this->parseStartState($parent, $tokenizer);
            if ($parent->getCodeDefinition() && false ===
                $parent->getCodeDefinition()->parseContent()) {
                /* We're inside an element that does not allow its contents to be parseable. */
                $this->parseAsTextUntilClose($parent, $tokenizer);
                $parent = $parent->getParent();
            }
        }

        /* We parsed ignoring nest limits. Do an O(n) traversal to remove any elements that
         * are nested beyond their CodeDefinition's nest limit. */
        $this->removeOverNestedElements();

        return $this;
    }