Coverage Report - eu.simuline.octave.util.StringUtil
 
Classes in this File Line Coverage Branch Coverage Complexity
StringUtil
38%
20/52
34%
9/26
3.5
 
 1  
 /*
 2  
  * Copyright 2007, 2008 Ange Optimization ApS
 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  
 package eu.simuline.octave.util;
 17  
 
 18  
 import java.util.Map;
 19  
 import java.util.HashMap;
 20  
 
 21  
 /**
 22  
  * Class for holding static utility functions for string handling: quoting 
 23  
  * and for parsing numbers. 
 24  
  * 
 25  
  * @author Kim Hansen
 26  
  */
 27  
 public final class StringUtil {
 28  
 
 29  
     private static final Map<Character, String> CHAR2STR;
 30  
 
 31  
     static {
 32  2
         CHAR2STR = new HashMap<Character, String>();
 33  2
         CHAR2STR.put('"',  "\\\"");
 34  2
         CHAR2STR.put('\\', "\\\\");
 35  2
         CHAR2STR.put('\n',  "\\n");
 36  2
         CHAR2STR.put('\r',  "\\r");
 37  2
         CHAR2STR.put('\f',  "\\f");
 38  2
         CHAR2STR.put('\b',  "\\b");
 39  2
         CHAR2STR.put('\t',  "\\t");
 40  2
     } // static 
 41  
 
 42  0
     private StringUtil() {
 43  0
     }
 44  
 
 45  
     @SuppressWarnings("checkstyle:magicnumber")
 46  
     private static void appendChar(StringBuffer buf, char c) {
 47  0
         String str = CHAR2STR.get(c);
 48  0
         if (str != null) {
 49  0
             buf.append(str);
 50  0
             return;
 51  
         }
 52  0
         if (c < 0x20) { // NOPMD 
 53  0
             buf.append("\\u00");
 54  0
             int x = c / 0x10;
 55  0
             buf.append((char) (x < 0xA ? x + '0' : x - 0xA + 'A'));
 56  0
             x = c & 0xF;
 57  0
             buf.append((char) (x < 0xA ? x + '0' : x - 0xA + 'A'));
 58  0
         } else {
 59  0
             buf.append(c);
 60  
         }
 61  0
     }
 62  
 
 63  
     /**
 64  
      * Quotes string as Java Language string literal. 
 65  
      * Returns the string "<code>null</code>" (with length 4) 
 66  
      * if <code>str</code> is <code>null</code>.
 67  
      * 
 68  
      * Code taken from http://freemarker.sourceforge.net/
 69  
      * 
 70  
      * @param str
 71  
      * @return the string encoded and quoted
 72  
      */
 73  
     public static String jQuote(final String str) {
 74  0
         if (str == null) {
 75  0
             return "null";
 76  
         }
 77  0
         final int len = str.length();
 78  0
         final StringBuffer buf = new StringBuffer(len + 4);
 79  0
         buf.append('"');
 80  0
         for (int i = 0; i < len; i++) {
 81  0
             appendChar(buf, str.charAt(i));
 82  
         } // for each characters
 83  0
         buf.append('"');
 84  0
         return buf.toString();
 85  
     }
 86  
 
 87  
     /**
 88  
      * Quotes the first <code>len</code> characters of <code>cbuf</code> 
 89  
      * as Java Language string literal. 
 90  
      * Returns string <code>null</code> if <code>s</code> is <code>null</code>. 
 91  
      * 
 92  
      * @param cbuf
 93  
      *    the buffer
 94  
      * @param len
 95  
      *    How much of the buffer to quote
 96  
      * @return the string encoded and quoted: 
 97  
      *    Starts and ends with <code>"</code>
 98  
      */
 99  
     public static String jQuote(final char[] cbuf, final int len) {
 100  0
         final StringBuffer buf = new StringBuffer(len + 4);
 101  0
         buf.append('"');
 102  0
         for (int i = 0; i < len; i++) {
 103  0
             appendChar(buf, cbuf[i]);
 104  
         } // for each characters
 105  0
         buf.append('"');
 106  0
         return buf.toString();
 107  
     }
 108  
 
 109  
     // does not fit: whereas parseBoolean throws OctaveParseExeption, 
 110  
     // parseDouble does not. 
 111  
     public static boolean parseBoolean(String line) {
 112  228
         line = line.trim();
 113  228
         if ("0".equals(line)) {
 114  144
             return false;
 115  
         }
 116  84
         if ("1".equals(line)) {
 117  84
             return true;
 118  
         }
 119  0
         throw new NumberFormatException("Invalid input, '" + line + "'");
 120  
         //throw new OctaveParseException("Invalid input, '" + line + "'");
 121  
     }
 122  
 
 123  
     /**
 124  
      * Returns the string representation of booleans in octave: 
 125  
      * 1 for true and 0 for false. 
 126  
      */
 127  
     public static String toString(boolean bool) {
 128  386
         return bool ? "1" : "0";
 129  
     }
 130  
 
 131  
     /**
 132  
      * Returns the string representation of the object <code>obj</code> 
 133  
      * which is defined also for null returning just "null". 
 134  
      */
 135  
     public static <T> String toString(T obj) {
 136  0
         return obj == null ? "null" : obj.toString();
 137  
     }
 138  
 
 139  
     /**
 140  
      * This is almost the same as Double.parseDouble(), 
 141  
      * but it handles a few more versions of infinity. 
 142  
      *
 143  
      * @param string
 144  
      * @return The parsed Double
 145  
      */
 146  
     public static double parseDouble(final String string) {
 147  6716
         if ("Inf".equals(string)) {
 148  2
             return Double.POSITIVE_INFINITY;
 149  
         }
 150  6714
         if ("-Inf".equals(string)) {
 151  2
             return Double.NEGATIVE_INFINITY;
 152  
         }
 153  6712
         return Double.parseDouble(string);
 154  
     }
 155  
 }