Unicode aware replacement for strrepalce()

@author Harry Fuecks <hfuecks@gmail.com>
@see    strreplace();
776995
private static function str_replace($s,$r,$str)
    {
        if(!is_array($s)){
            $s = '!'.preg_quote($s,'!').'!u';
        }else{
            foreach ($s as $k => $v) {
                $s[$k] = '!'.preg_quote($v).'!u';
            }
        }
        return preg_replace($s,$r,$str);
    }
741815
function clean_string($str)
	{
		if ($this->_is_ascii($str) === FALSE)
		{
			$str = @iconv('UTF-8', 'UTF-8//IGNORE', $str);
		}

		return $str;
	}
1037452
private static function unicodeChar($dec)
    {
        if ($dec < 0x80) {
            return chr($dec);
        }

        if ($dec < 0x0800) {
            return chr(0xC0 + ($dec >> 6))
                .chr(0x80 + ($dec & 0x3f));
        }

        if ($dec < 0x010000) {
            return chr(0xE0 + ($dec >> 12))
                    .chr(0x80 + (($dec >> 6) & 0x3f))
                    .chr(0x80 + ($dec & 0x3f));
        }

        if ($dec < 0x200000) {
            return chr(0xF0 + ($dec >> 18))
                    .chr(0x80 + (($dec >> 12) & 0x3f))
                    .chr(0x80 + (($dec >> 6) & 0x3f))
                    .chr(0x80 + ($dec & 0x3f));
        }
    }
1003052
def handle_charref(self, charRef):
        '''
            
        '''
        inTag = self._inTag
        if len(inTag) > 0:
            inTag[-1].appendText('&#%s;' %(charRef,))
        else:
            raise MultipleRootNodeException()
387050
function strRepeat(str, n) {
    var res = '';
    do {
        if (n & 1) {
            res += str;
        }

        str += str;
    } while (n = n >> 1);

    return res;
}
21528
function toUnicode(theString) {
    let unicodeString = ''
    for (var i=0; i < theString.length; i++) {
        let theUnicode = theString.charCodeAt(i).toString(16).toUpperCase()
        while (theUnicode.length < 4) {
            theUnicode = '0' + theUnicode
        }
        theUnicode = '\\u' + theUnicode
        unicodeString += theUnicode
    }
    return unicodeString
}
425246
public function str($len, $_charset = null) {
    if($_charset == null) {
      $_charset = $this->_charset;
    }

    $str = '';
    for ($i = 0; $i < $len; $i++) {
      $pos = $this->int(0,strlen($_charset)-1);
      $str .= $_charset[$pos];
    }
    return $str;
  }
1267039
function identifierEndPosition( $tpl, &$text, $start_pos, $len )
    {
        $pos = $start_pos;
        while ( $pos < $len )
        {
            if ( !preg_match( "/^[a-zA-Z0-9_-]$/", $text[$pos] ) )
            {
                return $pos;
            }
            ++$pos;
        }
        return $pos;
    }
1170269
public static function escapeAssertion($s)
    {
        if (0 === strpos($s, 'r.')) {
            $s = substr_replace($s, 'r_', 0, 2);
        }
        if (0 === strpos($s, 'p.')) {
            $s = substr_replace($s, 'p_', 0, 2);
        }

        $s = preg_replace_callback("~(\|| |=|\)|\(|&|<|>|,|\+|-|!|\*|\/)(r|p)(\.)~", function ($m) {
            return $m[1].$m[2].'_';
        }, $s);

        return $s;
    }
520187
@Override
    public void addReplacementSetTo(UnicodeSet toUnionTo) {
        int ch;
        for (int i=0; i<output.length(); i+=UTF16.getCharCount(ch)) {
            ch = UTF16.charAt(output, i);
            UnicodeReplacer r = data.lookupReplacer(ch);
            if (r == null) {
                toUnionTo.add(ch);
            } else {
                r.addReplacementSetTo(toUnionTo);
            }
        }
    }
907554
public function pregReplace()
    {
        if ($this->getParameter('pattern') == null)
            return $this->getParameter('subject');
        return preg_replace($this->getParameter('pattern'), $this->getParameter('replace'), $this->getParameter('subject'));
    }
1016872
private String escapeUnrecognizedCharacters(String s) {
    // TODO(yitingwang) Move this method to a suitable place
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < s.length(); i++) {
      char c = s.charAt(i);
      switch (c) {
          // From the SingleEscapeCharacter grammar production.
        case '\b':
        case '\f':
        case '\n':
        case '\r':
        case '\t':
        case '\\':
        case '\"':
        case '\'':
        case '$':
        case '`':
        case '\u2028':
        case '\u2029':
          sb.append(c);
          break;
        default:
          if ((outputCharsetEncoder != null && outputCharsetEncoder.canEncode(c))
              || (c > 0x1f && c < 0x7f)) {
            // If we're given an outputCharsetEncoder, then check if the character can be
            // represented in this character set. If no charsetEncoder provided - pass straight
            // Latin characters through, and escape the rest. Doing the explicit character check is
            // measurably faster than using the CharsetEncoder.
            sb.append(c);
          } else {
            // Other characters can be misinterpreted by some JS parsers,
            // or perhaps mangled by proxies along the way,
            // so we play it safe and Unicode escape them.
            Util.appendHexJavaScriptRepresentation(sb, c);
          }
      }
    }
    return sb.toString();
  }
1283920
function replaceCharacter(search, replace, subject, count) {
  var i = 0, j = 0, temp = '', repl = '', sl = 0,
    fl = 0, f = [].concat(search), r = [].concat(replace),
    s = subject, ra = Object.prototype.toString.call(r) === '[object Array]',
    sa = Object.prototype.toString.call(s) === '[object Array]', s = [].concat(s);
  
  if(typeof(search) === 'object' && typeof(replace) === 'string' ) {
    temp = replace; 
    replace = new Array();
    for (i=0; i < search.length; i+=1) { 
      replace[i] = temp; 
    }
    temp = ''; 
    r = [].concat(replace); 
    ra = Object.prototype.toString.call(r) === '[object Array]';
  }
  
  if (count) {
    this.window[count] = 0;
  }

  for (i = 0, sl = s.length; i < sl; i++) {
    if (s[i] === '') {
      continue;
    }
    for (j = 0, fl = f.length; j < fl; j++) {
      temp = s[i] + '';
      repl = ra ? (r[j] !== undefined ? r[j] : '') : r[0];
      s[i] = (temp)
        .split(f[j])
        .join(repl);
      if (count) {
        this.window[count] += ((temp.split(f[j])).length - 1);
      } 
    }
  }
  return sa ? s : s[0];
}
1054312
function PascalCase()
    {
        $key = (string) $this;
        
        ## prefix and postfix __ remains; like: __this__
        
        $pos = 'prefix';
        $prefix = '';
        $posfix = '';
        for ($i = 0; $i <= strlen($key)-1; $i++) {
            if ($key[$i] == '_')
                $$pos.='_';
            else {
                $posfix = ''; // reset posix, _ may found within string
                $pos  = 'posfix';
            }
        }

        $r = str_replace(' ', '', ucwords(str_replace('_', ' ', $key)));
        $r = $prefix.$r.$posfix;
        return new self($r);
    }
1026728
function tmpl(str, data) {
			return str.replace(/\{ *([\w_]+) *\}/g, function (str, key) {
				return data[key] || '';
			});
		}
577846
function (str) {
                if (!str) {
                    return str;
                }
                return String(str).replace(/[\uff-\uffff]/g, function (c) {
                    return escape(c).replace('%', '\\');
                });
            }
515683
function indento(input, width, char) {
    char = typeof char !== "string" ? " " : char;
    return String(input).replace(/^/gm, char.repeat(width));
}