Coverage Report - org.apache.commons.lang3.text.translate.OctalUnescaper
 
Classes in this File Line Coverage Branch Coverage Complexity
OctalUnescaper
100%
17/17
83%
20/24
4,667
 
 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.lang3.text.translate;
 18  
 
 19  
 import java.io.IOException;
 20  
 import java.io.Writer;
 21  
 
 22  
 /**
 23  
  * Translate escaped octal Strings back to their octal values.
 24  
  *
 25  
  * For example, "\45" should go back to being the specific value (a %).
 26  
  *
 27  
  * Note that this currently only supports the viable range of octal for Java; namely 
 28  
  * 1 to 377. This is because parsing Java is the main use case.
 29  
  * 
 30  
  * @since 3.0
 31  
  * @version $Id: OctalUnescaper.java 967237 2010-07-23 20:08:57Z mbenson $
 32  
  */
 33  2
 public class OctalUnescaper extends CharSequenceTranslator {
 34  
 
 35  
     /**
 36  
      * {@inheritDoc}
 37  
      */
 38  
     @Override
 39  
     public int translate(final CharSequence input, final int index, final Writer out) throws IOException {
 40  109
         int remaining = input.length() - index - 1; // how many characters left, ignoring the first \
 41  109
         StringBuilder builder = new StringBuilder();
 42  109
         if(input.charAt(index) == '\\' && remaining > 0 && isOctalDigit(input.charAt(index + 1)) ) {
 43  11
             int next = index + 1;
 44  11
             int next2 = index + 2;
 45  11
             int next3 = index + 3;
 46  
 
 47  
             // we know this is good as we checked it in the if block above
 48  11
             builder.append(input.charAt(next));
 49  
 
 50  11
             if(remaining > 1 && isOctalDigit(input.charAt(next2))) {
 51  10
                 builder.append(input.charAt(next2));
 52  10
                 if(remaining > 2 && isZeroToThree(input.charAt(next)) && isOctalDigit(input.charAt(next3))) {
 53  6
                     builder.append(input.charAt(next3));
 54  
                 }
 55  
             }
 56  
 
 57  11
             out.write( Integer.parseInt(builder.toString(), 8) );
 58  11
             return 1 + builder.length();
 59  
         }
 60  98
         return 0;
 61  
     }
 62  
 
 63  
     /**
 64  
      * Checks if the given char is an octal digit. Octal digits are the character representations of the digits 0 to 7.
 65  
      * @param ch the char to check
 66  
      * @return true if the given char is the character representation of one of the digits from 0 to 7
 67  
      */
 68  
     private boolean isOctalDigit(char ch) {
 69  60
         return ch >= '0' && ch <= '7';
 70  
     }
 71  
 
 72  
     /**
 73  
      * Checks if the given char is the character representation of one of the digit from 0 to 3.
 74  
      * @param ch the char to check
 75  
      * @return true if the given char is the character representation of one of the digits from 0 to 3
 76  
      */
 77  
     private boolean isZeroToThree(char ch) {
 78  9
         return ch >= '0' && ch <= '3';
 79  
     }
 80  
 }