Multi-byte-safe string length calculation

@param string $str
@return int
513746
protected static function ourStrlen($str)
    {
        // Premature optimization: cache the function_exists() result
        static $exists = null;
        if ($exists === null) {
            $exists = \function_exists('\\mb_strlen');
        }

        // If it exists, we need to make sure we're using 8bit mode
        if ($exists) {
            return \mb_strlen($str, '8bit');
        }
        return \strlen($str);
    }
532941
public function fromBin(string $str):string {
		$this->checkLength($str);
		$this->checkBin($str);

		// Base32 works on the first 5 bits of a byte, so we insert blanks to pad it out
		$str = preg_replace('/(.{5})/', '000$1', $str);

		// We need a string divisible by 5
		$length = strlen($str);
		$rbits = $length&7;

		if($rbits > 0){
			// Excessive bits need to be padded
			$ebits = substr($str, $length-$rbits);
			$str = substr($str, 0, $length-$rbits).'000'.$ebits.str_repeat('0', 5-strlen($ebits));
		}

		preg_match_all('/.{8}/', $str, $chrs);

		$chrs = array_map(function($str){
			return $this->charset[bindec($str)];
		}, $chrs[0]);

		return implode('', $chrs);
	}
1088102
public static function getBytes($size_str)
  {
    switch (substr ($size_str, -1))
    {
      case 'M': case 'm': return (int)$size_str * 1048576;
      case 'K': case 'k': return (int)$size_str * 1024;
      case 'G': case 'g': return (int)$size_str * 1073741824;
      default: return $size_str;
    }
  }
1225163
public static function length($str, $encoding = null) {
        $encoding or $encoding = \Fuel::$encoding;
        return function_exists('mb_strlen') ? mb_strlen($str, $encoding) : strlen($str);
    }
500192
private static function _decimalToNR3(string $str): string
    {
        // if number is in exponent form
        /** @var $match string[] */
        if (preg_match(self::PHP_EXPONENT_DNUM, $str, $match)) {
            $parts = explode(".", $match[1]);
            $m = ltrim($parts[0], "0");
            $e = intval($match[2]);
            // if mantissa had decimals
            if (count($parts) == 2) {
                $d = rtrim($parts[1], "0");
                $e -= strlen($d);
                $m .= $d;
            }
        } else {
            // explode from decimal
            $parts = explode(".", $str);
            $m = ltrim($parts[0], "0");
            // if number had decimals
            if (count($parts) == 2) {
                // exponent is negative number of the decimals
                $e = -strlen($parts[1]);
                // append decimals to the mantissa
                $m .= $parts[1];
            } else {
                $e = 0;
            }
            // shift trailing zeroes from the mantissa to the exponent
            while (substr($m, -1) === "0") {
                $e++;
                $m = substr($m, 0, -1);
            }
        }
        /* if exponent is zero, it must be prefixed with a "+" sign
         (X.690 07-2002, section 11.3.2.6) */
        if (0 == $e) {
            $es = "+";
        } else {
            $es = $e < 0 ? "-" : "";
        }
        return sprintf("%s.E%s%d", $m, $es, abs($e));
    }
1072046
public function computeStringSize($value)
    {
        $length = mb_strlen($value, '8bit');
        $size   = $length + $this->computeVarintSize($length);

        return $size;
    }
1320531
public static int calcUtfLength(String str) {
        final int strlen = str.length();
        int utflen = 0;
        for (int i = 0; i < strlen; i++) {
            int c = str.charAt(i);
            if ((c >= 0x0001) && (c <= 0x007F)) {
                utflen++;
            } else if (c > 0x07FF) {
                utflen += 3;
            } else {
                utflen += 2;
            }
        }
        return utflen;
    }
473081
private static function cutString(string $str): string
    {
        static $mb;
        if ($mb === null) {
            $mb = function_exists('mb_strlen');
        }
        if ($mb) {
            $len = mb_strlen($str, 'UTF-8');
        } else {
            $len = strlen($str);
        }
        if ($len > self::MAX_LEN) {
            if ($mb) {
                return mb_substr($str, 0, self::MAX_LEN, 'UTF-8').'...';
            } else {
                return substr($str, 0, self::MAX_LEN).'...';
            }
        }
        return $str;
    }
1000755
public static function bytes2int($str, $l = false)
    {
        if ($l) {
            $str = strrev($str);
        }

        $dec = 0;
        $len = strlen($str);

        for ($i = 0; $i < $len; ++$i)
        {
            $dec += ord(static::binarySubstr($str, $i, 1)) * pow(0x100, $len - $i - 1);
        }

        return $dec;
    }
1051228
public static function toInt(string $str) : int
    {
        $result = 0;
        $str    = mb_convert_kana(self::replaceRoman($str), 'a');
        foreach (self::$romans as $key => $value) {
            while (mb_strpos($str, $key) === 0) {
                $result += $value;
                $str = mb_substr($str, strlen($key));
            }
        }
        return $result;
    }
1065489
public static function len($string): int
    {
        if (self::isMBString()) {
            return mb_strlen($string, self::$encoding);
        }

        return strlen($string); // @codeCoverageIgnore
    }
303032
protected function hash($str) {
        $seed  = 131;
        $seed2 = 137;
        $hash  = 0;
        // Make hash more sensitive for short string like 'a', 'b', 'c'
        $str .= 'x';
        $max = intval(9007199254740991 / $seed2);
        for ($i = 0, $ilen = mb_strlen($str, 'UTF-8'); $i < $ilen; $i++) {
            if ($hash > $max) {
                $hash = intval($hash / $seed2);
            }
            $hash = $hash * $seed + $this->getCharCode(mb_substr($str, $i, 1, 'UTF-8'));
        }
        return $hash;
    }
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;
    }
965994
private static function utf8ToUnicode($str)
    {
        $unicode = array();
        $values = array();
        $lookingFor = 1;

        for ($i = 0; $i < strlen($str); $i++ ) {
            $thisValue = ord($str[$i]);

            if ($thisValue < 128) {
                $unicode[] = $thisValue;
            }
            else {
                if (count($values) == 0) {
                    $lookingFor = $thisValue < 224 ? 2 : 3;
                }

                $values[] = $thisValue;

                if (count($values) == $lookingFor) {
                    $number = $lookingFor == 3
                              ? (($values[0] % 16) * 4096) + (($values[1] % 64) * 64) + ($values[2] % 64)
                              : (($values[0] % 32) * 64) + ($values[1] % 64);

                    $unicode[] = $number;
                    $values = array();
                    $lookingFor = 1;
                }
            }
        }

        return $unicode;
    }
870754
static public function PackStringWithLength($str)
    {
        $len = strlen($str);
        # UTF-8
        if (!self::ValidateUTF8($str)) {
            throw new Exception('Bad UTF-8 String');
        }
        return pack('n', $len) . $str;
    }
776084
private function readRawDecodeAndAppend($length, &$str)
    {
        $block = $this->stream->read($length);
        if ($block === false || $block === '') {
            return -1;
        }
        $decoded = $this->decodeBlock($block);
        $count = strlen($decoded);
        $str .= $decoded;
        return $count;
    }
867448
public function substr($str, $offset = 0, $length = null)
    {
        return mb_substr($str, $offset, $length, $this->getEncoding());
    }