SQLite dialog: escaping of wild card characters

@param string $string     Unsafe input
@param string $escapeChar

@return string
197883
public static function escapeLike($string, $escapeChar = '=')
    {
        return str_replace(
            [
                $escapeChar,
                '_',
                '%',
            ],
            [
                $escapeChar . $escapeChar,
                $escapeChar . '_',
                $escapeChar . '%',
            ],
            $string
        );
    }
515206
public static function escape($string, $force_quotes=false)
    {
        if ($string === null) {
            return 'NIL';
        }

        if ($string === '') {
            return '""';
        }

        // atom-string (only safe characters)
        if (!$force_quotes && !preg_match('/[\x00-\x20\x22\x25\x28-\x2A\x5B-\x5D\x7B\x7D\x80-\xFF]/', $string)) {
            return $string;
        }

        // quoted-string
        if (!preg_match('/[\r\n\x00\x80-\xFF]/', $string)) {
            return '"' . addcslashes($string, '\\"') . '"';
        }

        // literal-string
        return sprintf("{%d}\r\n%s", strlen($string), $string);
    }
1370314
public static function escape($string)
    {
        // when we have a numeric string only, nothing needs to be done
        if (is_numeric($string)) {
            return $string;
        }

        // when no whitespaces are in the query we can also just escape the special characters
        if (preg_match('/\W/', $string) != 1) {
            return static::escapeSpecialCharacters($string);
        }

        // when there are no quotes inside the query string we can also just escape the whole string
        $hasQuotes = strrpos($string, '"') !== false;
        if (!$hasQuotes) {
            return static::escapeSpecialCharacters($string);
        }

        $result = static::tokenizeByQuotesAndEscapeDependingOnContext($string);

        return $result;
    }
48971
public static function escape($string, $context = 'html')
    {
        $method = 'escape'.ucfirst($context);

        if(!method_exists(__CLASS__, $method)) {
            throw new \InvalidArgumentException(sprintf('Cannot escape to: %s. Unknow context.', $context));
        }

        return static::$method($string);
    }
527293
private String escape(final String val) {
		// TODO: this function is ugly, pass this work off to SQLite, then we
		//       don't have to worry about Unicode 4, other characters needing
		//       escaping, etc.
		int len = val.length();
		StringBuilder buf = new StringBuilder(len);
		for (int i = 0; i < len; i++) {
			if (val.charAt(i) == '\'') {
				buf.append('\'');
			}
			buf.append(val.charAt(i));
		}
		return buf.toString();
	}
970219
public function EscapeString($String) {
      $Array = FALSE;
      if (substr($String, -2) == '[]') {
         $String = substr($String, 0, -2);
         $Array = TRUE;
      }
      $Return = urlencode(str_replace(' ', '_', $String));
      if ($Array === TRUE) $Return .= '[]';

      return str_replace('.', '-dot-', $Return);
   }
645843
public static String escapeSqlLikePattern(String pattern, char escapeChar) {

        char[] special = new char[] {escapeChar, '%', '_'};
        String result = pattern;
        for (char charToEscape : special) {
            result = result.replaceAll("" + charToEscape, "" + escapeChar + charToEscape);
        }
        return result;
    }
412959
public function escape( $string )
	{	
		if ( \DB::is_expression( $string ) ) 
		{
			return $string->value;
		}

		// the string might contain an 'as' statement that we wil have to split.
		if ( strpos( $string, ' as ' ) !== false )
		{
			$string = explode( ' as ', $string );
			return $this->escape( trim( $string[0] ) ).' as '. $this->escape( trim( $string[1] ) );
		}

		// it also might contain dott seperations we have to split
		if ( strpos( $string, '.' ) !== false )
		{
			$string = explode( '.', $string );

			foreach( $string as $key => $item )
			{
				$string[$key] = $this->escape_string( $item );
			}

			return implode( '.' , $string );
		}

		return $this->escape_string( $string );
	}
912008
protected function escape($string)
    {
        $string = HTMLPurifier_Encoder::cleanUTF8($string);
        $string = htmlspecialchars($string, ENT_COMPAT, 'UTF-8');
        return $string;
    }
1250144
private String escapeSpecialCharacters(final String string) {
		final StringBuilder escaped = new StringBuilder();
		for (final char character : string.toCharArray()) {
			if (character == '%' || character == '_' || character == '\\') {
				escaped.append('\\');
			}
			escaped.append(character);
		}
		return escaped.toString();
	}
59894
protected function escape(string $string): string
	{
		if(($formatter = $this->output->getFormatter()) === null)
		{
			return $string;
		}

		return $formatter->escape($string);
	}
592826
protected Pattern generateEscapePattern(final char escapeChar, final char[] wildcards) {
		StringBuilder builder = new StringBuilder();
		builder.append("[");
		for (char wildcard : wildcards) {
			if (escapeChar == '[' || escapeChar == ']') {
				builder.append("\\");
			}
			builder.append(Matcher.quoteReplacement(String.valueOf(escapeChar)));
			if (wildcard == '[' || wildcard == ']') {
				builder.append("\\");
			}
			builder.append(wildcard);
		}
		builder.append("]");
		return Pattern.compile(builder.toString());
	}
1160534
@Override
    protected String escapeCharacter(char ch, char previousChar) {
      if(ch < ' ' && ch != '\t' && ch != '\n' && ch != '\r') {
          return "";
      }
      switch(ch) {
          case '&': return ESCAPED_AMP;
          case '<': return ESCAPED_LT;
          case '>': return ESCAPED_GT;
          case '"': return ESCAPED_QUOTE;
          case '\'': return  ESCAPED_APOS;
          case '\\': return  ESCAPED_BACKSLASH;
          case '@': return ESCAPED_AT;
          case '`': return ESCAPED_BACKTICK;
          case NBSP: return ESCAPED_NON_BREAKING_SPACE;
          case LINE_SEPARATOR: return ESCAPED_LINE_SEPARATOR;
          case PARAGRAPH_SEPARATOR: return ESCAPED_PARAGRAPH_SEPARATOR;
      }
      return null;
    }
709789
private static final String escape(String str) {
        if (str == null) {
            return null;
        }

        StringBuilder sb = new StringBuilder(str.length() * 2);
        int len = str.length();
        for (int i = 0; i < len; i++) {
            char c = str.charAt(i);
            switch (c) {
                case TOKEN_DELIM:
                case USER_DATA_DELIM:
                case USER_ATTRIB_DELIM:
                    sb.append('\\');
                    break;
                default:
                    break;
            }
            sb.append(c);
        }
        return sb.toString();
    }
487419
public function escape($string)
    {
        if(is_string($string)) {
            $string = htmlspecialchars($string, ENT_COMPAT | ENT_SUBSTITUTE, 'UTF-8', false);
        }

        return $string;
    }
1021986
public static String escapeDelimitedColumn(String str, char delimiter) {
        if (str == null || (containsNone(str, SEARCH_CHARS) && str.indexOf(delimiter) < 0)) {
            return str;
        } else {
            StringBuilder writer = new StringBuilder();
            writer.append(QUOTE);
            for (int j = 0, n = str.length(); j < n; j++) {
                char c = str.charAt(j);
                if (c == QUOTE) {
                    writer.append(QUOTE);
                }
                writer.append(c);
            }
            writer.append(QUOTE);
            return writer.toString();
        }
    }
767735
protected function escape(string $string): string
	{
		if($this->formatter !== null)
		{
			return $this->formatter->escape($string);
		}

		return $string;
	}