Coverage Report - org.ini4j.Convert
 
Classes in this File Line Coverage Branch Coverage Complexity
Convert
97%
34/35
94%
15/16
5.5
 
 1  
 /*
 2  
  * Copyright 2005 [ini4j] Development Team
 3  
  *
 4  
  * Licensed under the Apache License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  *     http://www.apache.org/licenses/LICENSE-2.0
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  */
 16  
 
 17  
 package org.ini4j;
 18  
 
 19  0
 class Convert
 20  
 {
 21  12
     private static final char HEX[] = "0123456789abcdef".toCharArray();
 22  
 
 23  
     protected static String escape(String line)
 24  
     {
 25  211
         int len = line.length();
 26  211
         StringBuilder buffer = new StringBuilder(len*2);
 27  
 
 28  1834
         for(int i = 0 ; i < len; i++)
 29  
         {
 30  1623
             char c = line.charAt(i);
 31  1623
             int idx = "\\\t\n\f".indexOf(c);
 32  
 
 33  1623
             if ( idx >= 0 )
 34  
             {
 35  3
                 buffer.append('\\');
 36  3
                 buffer.append( "\\tnf".charAt(idx));
 37  
             }
 38  
             else
 39  
             {
 40  1620
                 if ((c < 0x0020) || (c > 0x007e))
 41  
                 {
 42  25
                     buffer.append("\\u");
 43  25
                     buffer.append( HEX[(c >>> 12) & 0x0f] );
 44  25
                     buffer.append( HEX[(c >>> 8) & 0x0f] );
 45  25
                     buffer.append( HEX[(c >>> 4) & 0x0f] );
 46  25
                     buffer.append( HEX[c & 0x0f] );
 47  
                 }
 48  
                 else
 49  
                 {
 50  1595
                     buffer.append(c);
 51  
                 }
 52  
             }
 53  
         }
 54  211
         return buffer.toString();
 55  
     }
 56  
 
 57  
     protected static String unescape(String line)
 58  
     {
 59  1953
         int n = line.length();
 60  1953
         StringBuilder buffer = new StringBuilder(n);
 61  
         
 62  1953
         for(int i = 0; i < n; )
 63  
         {
 64  16732
             char c = line.charAt(i++);
 65  
             
 66  16732
             if ( c == '\\' )
 67  
             {
 68  32
                 c = line.charAt(i++);
 69  
                 
 70  32
                 if ( c == 'u' )
 71  
                 {
 72  
                     try
 73  
                     {
 74  28
                         c = (char) Integer.parseInt(line.substring(i,i+=4), 16);
 75  
                     }
 76  1
                     catch (RuntimeException x)
 77  
                     {
 78  1
                         throw new IllegalArgumentException("Malformed \\uxxxx encoding.");
 79  27
                     }
 80  
                 }
 81  
                 else
 82  
                 {
 83  4
                     int idx = "\\tnf".indexOf(c);
 84  
                     
 85  4
                     if ( idx >= 0 )
 86  
                     {
 87  3
                         c = "\\\t\n\f".charAt(idx);
 88  
                     }
 89  
                 }
 90  
             }
 91  
             
 92  16731
             buffer.append(c);
 93  16731
         }
 94  
         
 95  1952
         return buffer.toString();
 96  
     }
 97  
 }