1 17 18 package org.apache.tomcat.util.buf; 19 20 26 public final class Ascii { 27 30 31 private static final byte[] toUpper = new byte[256]; 32 private static final byte[] toLower = new byte[256]; 33 34 37 38 private static final boolean[] isAlpha = new boolean[256]; 39 private static final boolean[] isUpper = new boolean[256]; 40 private static final boolean[] isLower = new boolean[256]; 41 private static final boolean[] isWhite = new boolean[256]; 42 private static final boolean[] isDigit = new boolean[256]; 43 44 47 48 static { 49 for (int i = 0; i < 256; i++) { 50 toUpper[i] = (byte)i; 51 toLower[i] = (byte)i; 52 } 53 54 for (int lc = 'a'; lc <= 'z'; lc++) { 55 int uc = lc + 'A' - 'a'; 56 57 toUpper[lc] = (byte)uc; 58 toLower[uc] = (byte)lc; 59 isAlpha[lc] = true; 60 isAlpha[uc] = true; 61 isLower[lc] = true; 62 isUpper[uc] = true; 63 } 64 65 isWhite[ ' '] = true; 66 isWhite['\t'] = true; 67 isWhite['\r'] = true; 68 isWhite['\n'] = true; 69 isWhite['\f'] = true; 70 isWhite['\b'] = true; 71 72 for (int d = '0'; d <= '9'; d++) { 73 isDigit[d] = true; 74 } 75 } 76 77 80 81 public static int toUpper(int c) { 82 return toUpper[c & 0xff] & 0xff; 83 } 84 85 88 89 public static int toLower(int c) { 90 return toLower[c & 0xff] & 0xff; 91 } 92 93 96 97 public static boolean isAlpha(int c) { 98 return isAlpha[c & 0xff]; 99 } 100 101 104 105 public static boolean isUpper(int c) { 106 return isUpper[c & 0xff]; 107 } 108 109 112 113 public static boolean isLower(int c) { 114 return isLower[c & 0xff]; 115 } 116 117 120 121 public static boolean isWhite(int c) { 122 return isWhite[c & 0xff]; 123 } 124 125 128 129 public static boolean isDigit(int c) { 130 return isDigit[c & 0xff]; 131 } 132 133 140 public static int parseInt(byte[] b, int off, int len) 141 throws NumberFormatException 142 { 143 int c; 144 145 if (b == null || len <= 0 || !isDigit(c = b[off++])) { 146 throw new NumberFormatException (); 147 } 148 149 int n = c - '0'; 150 151 while (--len > 0) { 152 if (!isDigit(c = b[off++])) { 153 throw new NumberFormatException (); 154 } 155 n = n * 10 + c - '0'; 156 } 157 158 return n; 159 } 160 161 public static int parseInt(char[] b, int off, int len) 162 throws NumberFormatException 163 { 164 int c; 165 166 if (b == null || len <= 0 || !isDigit(c = b[off++])) { 167 throw new NumberFormatException (); 168 } 169 170 int n = c - '0'; 171 172 while (--len > 0) { 173 if (!isDigit(c = b[off++])) { 174 throw new NumberFormatException (); 175 } 176 n = n * 10 + c - '0'; 177 } 178 179 return n; 180 } 181 182 189 public static long parseLong(byte[] b, int off, int len) 190 throws NumberFormatException 191 { 192 int c; 193 194 if (b == null || len <= 0 || !isDigit(c = b[off++])) { 195 throw new NumberFormatException (); 196 } 197 198 long n = c - '0'; 199 long m; 200 201 while (--len > 0) { 202 if (!isDigit(c = b[off++])) { 203 throw new NumberFormatException (); 204 } 205 m = n * 10 + c - '0'; 206 207 if (m < n) { 208 throw new NumberFormatException (); 210 } else { 211 n = m; 212 } 213 } 214 215 return n; 216 } 217 218 public static long parseLong(char[] b, int off, int len) 219 throws NumberFormatException 220 { 221 int c; 222 223 if (b == null || len <= 0 || !isDigit(c = b[off++])) { 224 throw new NumberFormatException (); 225 } 226 227 long n = c - '0'; 228 long m; 229 230 while (--len > 0) { 231 if (!isDigit(c = b[off++])) { 232 throw new NumberFormatException (); 233 } 234 m = n * 10 + c - '0'; 235 236 if (m < n) { 237 throw new NumberFormatException (); 239 } else { 240 n = m; 241 } 242 } 243 244 return n; 245 } 246 247 } 248 | Popular Tags |