Ejemplo n.º 1
0
  /**
   * Convert a potentially escaped string to text.
   *
   * @param t The source string.
   * @param start Starting offset.
   * @param end Ending offset.
   * @return Return the un-escaped string which may be zero-length but not null.
   */
  private static String unescape(Text t, int start, int end) {
    final Text result = new Text();

    /*
     * We know that there's a starting and ending quote that's been removed
     * from the string but otherwise we can't trust the contents.
     */
    for (int i = start; i < end; i++) {
      final char c = t.charAt(i);
      final char escaped;
      if (c == '\\') {
        if (i < end - 1) {
          i++;
          final char next = t.charAt(i);
          if (next == 't') {
            escaped = '\t';
          } else if (next == 'n') {
            escaped = '\n';
          } else if (next == '"') {
            escaped = '"';
          } else if (next == '\\') {
            escaped = '\\';
          } else {
            error("Invalid escape: '\\" + next + '\'');
            escaped = next;
          }
        } else {
          error("Unterminated string");
          escaped = c;
        }
      } else {
        escaped = c;
      }
      result.append(escaped);
    }

    return result.toString();
  }