1 /* 2 * Copyright (C) 2008 The Android Open Source Project 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 dasm; 18 19 /** 20 * Different string manipulations utility methods 21 */ 22 public class Utils { 23 24 /** 25 * Replace '.' characters with '/' in a string 26 */ convertDotsToSlashes(String name)27 public static String convertDotsToSlashes(String name) { 28 if (name == null) return null; 29 30 return name.replace('.', '/'); 31 } 32 33 /** 34 * Splits string like "v1, v2, v3" or "v1..v3" into list of registers 35 */ splitRegList(String list)36 public static String[] splitRegList(String list) { 37 String[] result = null; 38 if (list.length() > 0) 39 result = list.split("[\\s]*,[\\s]*|[\\s]*\\.\\.[\\s]*"); 40 return result; 41 } 42 43 /** 44 * Converts string to a smallest possible number (int, long, float or 45 * double) 46 */ stringToNumber(String str)47 public static Number stringToNumber(String str) 48 throws NumberFormatException { 49 if (str.startsWith("+")) { 50 return new Integer(str.substring(1)); 51 } 52 if (str.startsWith("0x")) { 53 return (Utils.stringToSmallestInteger(str.substring(2), 16)); 54 } else if (str.indexOf('.') != -1) { 55 56 double x = Double.parseDouble(str); 57 if (x <= (double) Float.MAX_VALUE && x >= (float) Float.MIN_VALUE) { 58 return new Float((float) x); 59 } 60 61 return new Double(x); 62 } else { 63 return (Utils.stringToSmallestInteger(str, 10)); 64 } 65 } 66 67 /** 68 * Converts string to a smallest possible integer (int or long) 69 */ stringToSmallestInteger(String str, int radix)70 private static Number stringToSmallestInteger(String str, int radix) 71 throws NumberFormatException { 72 long x = Long.parseLong(str, radix); 73 if (x <= (long) Integer.MAX_VALUE && x >= (long) Integer.MIN_VALUE) { 74 return new Integer((int) x); 75 } 76 return new Long(x); 77 } 78 79 /** 80 * Splits string "package/class/method(param)return_type" into 81 * "package/class", "method", "(param)return_type" 82 */ getClassMethodSignatureFromString(String name)83 public static String[] getClassMethodSignatureFromString(String name) { 84 85 String signature = convertDotsToSlashes(name); 86 87 String result[] = new String[3]; 88 int mpos = 0, sigpos = 0; 89 int i; 90 91 // find first '(' 92 sigpos = signature.indexOf('('); 93 if (sigpos == -1) { 94 sigpos = 0; 95 i = signature.length() - 1; 96 } else { 97 i = sigpos - 1; 98 } 99 100 // find last '/' before '(' 101 for (; i >= 0; i--) { 102 if (signature.charAt(i) == '/') { 103 mpos = i; 104 break; 105 } 106 } 107 try { 108 result[0] = signature.substring(0, mpos); 109 result[1] = signature.substring(mpos + 1, sigpos); 110 result[2] = signature.substring(sigpos); 111 } catch (StringIndexOutOfBoundsException e) { 112 throw new IllegalArgumentException("malformed method signature : " 113 + name); 114 } 115 return result; 116 } 117 118 /** 119 * Splits string "package/class/field" into "package/class" and "field" 120 */ getClassFieldFromString(String name)121 public static String[] getClassFieldFromString(String name) { 122 name = convertDotsToSlashes(name); 123 124 String result[] = new String[2]; 125 int pos = name.lastIndexOf('/'); 126 127 if (pos == -1) { 128 result[0] = null; 129 result[1] = name; 130 } else { 131 result[0] = name.substring(0, pos); 132 result[1] = name.substring(pos + 1); 133 } 134 135 return result; 136 } 137 138 /** 139 * Splits string "method(param)return_type" into "method" and 140 * "(param)return_type" 141 */ getMethodSignatureFromString(String name)142 public static String[] getMethodSignatureFromString(String name) { 143 int sigpos = name.indexOf('('); 144 if (sigpos == -1) sigpos = 0; 145 146 String result[] = new String[2]; 147 result[0] = name.substring(0, sigpos); 148 result[1] = convertDotsToSlashes(name.substring(sigpos)); 149 150 return result; 151 } 152 153 } 154