Strtoupper multibyte wrapper function with HTML entities handling.

@param  string $str Text to convert
@return string Converted text
16520
protected function strtoupper($str)
    {
        $str = html_entity_decode($str, $this->htmlFuncFlags, self::ENCODING);
        $str = mb_strtoupper($str);
        $str = htmlspecialchars($str, $this->htmlFuncFlags, self::ENCODING);

        return $str;
    }
72984
public static function toString($str){
		$str = strtoupper($str);

		// csSave actually has to be able to consider extra characters
		if(self::$charset === Base32Characters::CROCKFORD){
			$str = str_replace('O', '0', $str);
			$str = str_replace(['I', 'L'], '1', $str);
		}

		return self::bin2str(self::toBin($str));
	}
652793
private function _out_string_encoding( &$str )
     {
        $rsc = $this->_source_result_charset();
        if( $rsc==1 ) {
            $rsstr = iconv(UCS2, 'utf-8', $str);
        }
        else if( $rsc==2 ) {
            $rsstr = iconv('utf-8', 'gb18030', iconv(UCS2, 'utf-8', $str) );
        }
        else{
            $rsstr = iconv('utf-8', 'big5', iconv(UCS2, 'utf-8', $str) );
        }
        return $rsstr;
     }
110946
public function parse($str)
	{
		if ($this->isQuoteHtml() === true)
		{
			$str = htmlspecialchars($str, ENT_QUOTES);
		}

		foreach ($this->getBbCodes() as $key => $val)
		{
			$str = preg_replace($key, $val, $str);
		}

		return $str;
	}
1062683
private function charPad($str)
    {
        $l = strlen($str)*8;     // # of bits from input string
        $str .= "\x80";          // append the "1" bit followed by 7 0's
        $k = (512 - (($l + 8 + 64) % 512)) / 8;   // # of 0 bytes to append
        $k += 4;    // PHP String's will never exceed (2^31)-1, so 1st 32bits of
                    // the 64-bit value representing $l can be all 0's
        for ($x = 0; $x < $k; $x++) {
            $str .= "\0";
        }
        // append the last 32-bits representing the # of bits from input string ($l)
        $str .= chr((($l >> 24) & 0xFF));
        $str .= chr((($l >> 16) & 0xFF));
        $str .= chr((($l >> 8) & 0xFF));
        $str .= chr(($l & 0xFF));
        return $str;
    }
358362
public static function convertStringToXMLValue(string $str) : string
    {
        $str = str_replace("&amp;", "§", $str);
        $str = str_replace("&", "§", $str);
        $str = str_replace("§", "&amp;", $str);
        $str = str_replace("<", "&lt;", $str);
        $str = str_replace(">", "&gt;", $str);
        $str = str_replace("'", "&apos;", $str);
        $str = str_replace("\"", "&quot;", $str);

        return $str;
    }
1113148
private function _entity_decode(string $str): string
    {
        static $HTML_ENTITIES_CACHE;

        $flags = ENT_QUOTES | ENT_HTML5 | ENT_DISALLOWED | ENT_SUBSTITUTE;

        // decode
        $str = UTF8::html_entity_decode($str, $flags);

        // decode-again, for e.g. HHVM or miss configured applications ...
        if (\preg_match_all('/(?<html_entity>&[A-Za-z]{2,}[;]{0})/', $str, $matches)) {
            if ($HTML_ENTITIES_CACHE === null) {

                // links:
                // - http://dev.w3.org/html5/html-author/charref
                // - http://www.w3schools.com/charsets/ref_html_entities_n.asp
                $entitiesSecurity = [
                    '&#x00000;'          => '',
                    '&#0;'               => '',
                    '&#x00001;'          => '',
                    '&#1;'               => '',
                    '&nvgt;'             => '',
                    '&#61253;'           => '',
                    '&#x0EF45;'          => '',
                    '&shy;'              => '',
                    '&#x000AD;'          => '',
                    '&#173;'             => '',
                    '&colon;'            => ':',
                    '&#x0003A;'          => ':',
                    '&#58;'              => ':',
                    '&lpar;'             => '(',
                    '&#x00028;'          => '(',
                    '&#40;'              => '(',
                    '&rpar;'             => ')',
                    '&#x00029;'          => ')',
                    '&#41;'              => ')',
                    '&quest;'            => '?',
                    '&#x0003F;'          => '?',
                    '&#63;'              => '?',
                    '&sol;'              => '/',
                    '&#x0002F;'          => '/',
                    '&#47;'              => '/',
                    '&apos;'             => '\'',
                    '&#x00027;'          => '\'',
                    '&#039;'             => '\'',
                    '&#39;'              => '\'',
                    '&#x27;'             => '\'',
                    '&bsol;'             => '\'',
                    '&#x0005C;'          => '\\',
                    '&#92;'              => '\\',
                    '&comma;'            => ',',
                    '&#x0002C;'          => ',',
                    '&#44;'              => ',',
                    '&period;'           => '.',
                    '&#x0002E;'          => '.',
                    '&quot;'             => '"',
                    '&QUOT;'             => '"',
                    '&#x00022;'          => '"',
                    '&#34;'              => '"',
                    '&grave;'            => '`',
                    '&DiacriticalGrave;' => '`',
                    '&#x00060;'          => '`',
                    '&#96;'              => '`',
                    '&#46;'              => '.',
                    '&equals;'           => '=',
                    '&#x0003D;'          => '=',
                    '&#61;'              => '=',
                    '&newline;'          => "\n",
                    '&#x0000A;'          => "\n",
                    '&#10;'              => "\n",
                    '&tab;'              => "\t",
                    '&#x00009;'          => "\t",
                    '&#9;'               => "\t",
                ];

                $HTML_ENTITIES_CACHE = \array_merge(
                    $entitiesSecurity,
                    \array_flip(\get_html_translation_table(HTML_ENTITIES, $flags)),
                    \array_flip(self::_get_data('entities_fallback'))
                );
            }

            $search = [];
            $replace = [];
            foreach ($matches['html_entity'] as $match) {
                $match .= ';';
                if (isset($HTML_ENTITIES_CACHE[$match])) {
                    $search[$match] = $match;
                    $replace[$match] = $HTML_ENTITIES_CACHE[$match];
                }
            }

            if (\count($replace) > 0) {
                $str = \str_replace($search, $replace, $str);
            }
        }

        return $str;
    }
1071024
private static function mb_ucfirst( $str ) {
		if ( ! is_string( $str ) || '' === $str) { return ''; }
		$first = mb_strtoupper( mb_substr( $str, 0, 1 ) );
		if ( 1 === mb_strlen( $str ) ) { return $first; }
		return $first . mb_substr( $str, 1 );
	}
29094
private static function utf8ToUnicode($str) {
    $new = array();
    $len = iconv_strlen($str);
    for ($n = 0; $n < $len; $n++) {
      $char = iconv_substr($str, $n, 1);
      $new[] = self::uniord($char);
    }
    return $new;
  }
115232
final public static function safeEncodeStr(string $str): string
    {
        return preg_replace_callback('/&#([a-z\d]+);/i', function ($m) {
            $value = (string)$m[0];
            $value = mb_convert_encoding($value, 'UTF-8', 'HTML-ENTITIES');
            return $value;
        }, $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;
    }
707876
public static function tohtml ($str)
    {
        $ret = '';
        $max = strlen($str);
        $last = 0;  // keeps the index of the last regular character
        for ($i=0; $i<$max; $i++) {
            $c = $str{$i};
            $c1 = ord($c);
            if ($c1>>5 == 6) {  // 110x xxxx, 110 prefix for 2 bytes unicode
                $ret .= substr($str, $last, $i-$last); // append all the regular characters we've passed
                $c1 &= 31; // remove the 3 bit two bytes prefix
                $c2 = ord($str{++$i}); // the next byte
                $c2 &= 63;  // remove the 2 bit trailing byte prefix
                $c2 |= (($c1 & 3) << 6); // last 2 bits of c1 become first 2 of c2
                $c1 >>= 2; // c1 shifts 2 to the right
                $ret .= '&#' . ($c1 * 100 + $c2) . ';'; // this is the fastest string concatenation
                $last = $i+1;
            }
        }
        return $ret . substr($str, $last, $i); // append the last batch of regular characters
    }
1344665
public static function stringFromDB2HTML($str)
    {

        if (!$str || $str === null || $str === '' || is_array($str)) {
            return $str;
        }

        $str = trim($str);
        $str = htmlspecialchars($str, ENT_COMPAT, 'utf-8');

        return $str;
    }
1180679
public static function humanize(string $str): string
    {
        $result = preg_replace(
            [
                '/([A-Z])/',
                '/[-\s]+/',
                '/[_\s]+/',
            ],
            [
                '_$1',
                ' ',
                ' ',
            ],
            static::removeMultipleSpaces($str)
        );

        return mb_ucfirst(mb_strtolower($result));
    }
1064022
public static function core($str)
    {
        $str = _String::normalizePunctuationSpaces($str);
        $str = mb_strtolower(trim($str));
        $str = _String::normalizeSpaces($str);
        return $str;
    }
1185327
protected static function exportString($str)
	{
		// foo becomes 'foo'
		if (strpos($str, "'") === false)
		{
			return "'" . $str . "'";
		}

		// d'oh becomes "d'oh"
		if (strpos($str, '"') === false)
		{
			return '"' . $str . '"';
		}

		// This string contains both ' and ". XPath 1.0 doesn't have a mechanism to escape quotes,
		// so we have to get creative and use concat() to join chunks in single quotes and chunks
		// in double quotes
		$toks = [];
		$c    = '"';
		$pos  = 0;
		while ($pos < strlen($str))
		{
			$spn = strcspn($str, $c, $pos);
			if ($spn)
			{
				$toks[] = $c . substr($str, $pos, $spn) . $c;
				$pos   += $spn;
			}
			$c = ($c === '"') ? "'" : '"';
		}

		return 'concat(' . implode(',', $toks) . ')';
	}
1271576
protected function convertString($str, $use_codepage = false)
    {
        if ($convert && $this->codepage
            && ($charset = rcube_charset::$windows_codepages[$this->codepage])
        ) {
            $str = rcube_charset::convert($str, $charset);
        }
        else if (strpos($str, "\0") !== false) {
            $str = rcube_charset::convert($str, 'UTF-16LE');
        }

        $str = rtrim($str, "\0");

        return $str;
    }