All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.basex.query.util.regex.Literal Maven / Gradle / Ivy

There is a newer version: 11.3
Show newest version
package org.basex.query.util.regex;

/**
 * A character literal.
 *
 * @author BaseX Team 2005-22, BSD License
 * @author Leo Woerteler
 */
public final class Literal extends RegExp {
  /** Code point. */
  private final int codepoint;

  /**
   * Constructor.
   * @param codepoint Unicode code point
   */
  public Literal(final int codepoint) {
    this.codepoint = codepoint;
  }

  @Override
  void toRegEx(final StringBuilder sb) {
    sb.append(escape(codepoint));
  }

  /**
   * Escapes the given code point for a regular expression.
   * @param cp code point
   * @return string representation
   */
  public static String escape(final int cp) {
    switch(cp) {
      case '\t': return "\\t";
      case '\r': return "\\r";
      case '\n': return "\\n";
      case '\\':
      case '|':
      case '.':
      case '?':
      case '*':
      case '+':
      case '(':
      case ')':
      case '{':
      case '}':
      case '$':
      case '-':
      case '[':
      case ']':
      case '^':
        return "\\" + (char) cp;
      default:
        if(cp < 128 && !Character.isISOControl(cp)) return String.valueOf((char) cp);
        if(cp < 0x10000) return String.format("\\u%04x", cp);
        return String.valueOf(Character.toChars(cp));
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy