OctalUnescaper.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.commons.text.translate;

  18. import java.io.IOException;
  19. import java.io.Writer;

  20. /**
  21.  * Translate escaped octal Strings back to their octal values.
  22.  *
  23.  * For example, "\45" should go back to being the specific value (a %).
  24.  *
  25.  * Note that this currently only supports the viable range of octal for Java; namely
  26.  * 1 to 377. This is because parsing Java is the main use case.
  27.  *
  28.  * @since 1.0
  29.  */
  30. public class OctalUnescaper extends CharSequenceTranslator {

  31.     /**
  32.      * Creates a new instance.
  33.      */
  34.     public OctalUnescaper() {
  35.         // empty
  36.     }

  37.     /**
  38.      * Tests if the given char is an octal digit. Octal digits are the character representations of the digits 0 to 7.
  39.      *
  40.      * @param ch the char to check
  41.      * @return true if the given char is the character representation of one of the digits from 0 to 7
  42.      */
  43.     private boolean isOctalDigit(final char ch) {
  44.         return ch >= '0' && ch <= '7';
  45.     }

  46.     /**
  47.      * Tests if the given char is the character representation of one of the digit from 0 to 3.
  48.      *
  49.      * @param ch the char to check
  50.      * @return true if the given char is the character representation of one of the digits from 0 to 3
  51.      */
  52.     private boolean isZeroToThree(final char ch) {
  53.         return ch >= '0' && ch <= '3';
  54.     }

  55.     /**
  56.      * {@inheritDoc}
  57.      */
  58.     @Override
  59.     public int translate(final CharSequence input, final int index, final Writer writer) throws IOException {
  60.         final int remaining = input.length() - index - 1; // how many characters left, ignoring the first \
  61.         final StringBuilder builder = new StringBuilder();
  62.         if (input.charAt(index) == '\\' && remaining > 0 && isOctalDigit(input.charAt(index + 1))) {
  63.             final int next = index + 1;
  64.             final int next2 = index + 2;
  65.             final int next3 = index + 3;

  66.             // we know this is good as we checked it in the if block above
  67.             builder.append(input.charAt(next));

  68.             if (remaining > 1 && isOctalDigit(input.charAt(next2))) {
  69.                 builder.append(input.charAt(next2));
  70.                 if (remaining > 2 && isZeroToThree(input.charAt(next)) && isOctalDigit(input.charAt(next3))) {
  71.                     builder.append(input.charAt(next3));
  72.                 }
  73.             }

  74.             writer.write(Integer.parseInt(builder.toString(), 8));
  75.             return 1 + builder.length();
  76.         }
  77.         return 0;
  78.     }
  79. }