1 64 65 package com.jcorporate.expresso.core.misc; 66 67 72 73 import java.io.PrintStream ; 74 import java.io.PrintWriter ; 75 76 77 80 public class Format { 81 private int width; 82 private int precision; 83 private String pre; 84 private String post; 85 private boolean leadingZeroes; 86 private boolean showPlus; 87 private boolean alternate; 88 private boolean showSpace; 89 private boolean leftAlign; 90 private char fmt; 92 131 public Format(String s) { 132 width = 0; 133 precision = -1; 134 pre = ""; 135 post = ""; 136 leadingZeroes = false; 137 showPlus = false; 138 alternate = false; 139 showSpace = false; 140 leftAlign = false; 141 fmt = ' '; 142 143 int length = s.length(); 144 int parseState = 0; 145 146 int i = 0; 149 150 while (parseState == 0) { 151 if (i >= length) { 152 parseState = 5; 153 } else if (s.charAt(i) == '%') { 154 if (i < length - 1) { 155 if (s.charAt(i + 1) == '%') { 156 pre = pre + '%'; 157 i++; 158 } else { 159 parseState = 1; 160 } 161 } else { 162 throw new java.lang.IllegalArgumentException (); 163 } 164 } else { 165 pre = pre + s.charAt(i); 166 } 167 168 i++; 169 } 170 171 while (parseState == 1) { 172 if (i >= length) { 173 parseState = 5; 174 } else if (s.charAt(i) == ' ') { 175 showSpace = true; 176 } else if (s.charAt(i) == '-') { 177 leftAlign = true; 178 } else if (s.charAt(i) == '+') { 179 showPlus = true; 180 } else if (s.charAt(i) == '0') { 181 leadingZeroes = true; 182 } else if (s.charAt(i) == '#') { 183 alternate = true; 184 } else { 185 parseState = 2; 186 i--; 187 } 188 189 i++; 190 } 191 192 while (parseState == 2) { 193 if (i >= length) { 194 parseState = 5; 195 } else if ('0' <= s.charAt(i) && s.charAt(i) <= '9') { 196 width = width * 10 + s.charAt(i) - '0'; 197 i++; 198 } else if (s.charAt(i) == '.') { 199 parseState = 3; 200 precision = 0; 201 i++; 202 } else { 203 parseState = 4; 204 } 205 } 206 207 while (parseState == 3) { 208 if (i >= length) { 209 parseState = 5; 210 } else if ('0' <= s.charAt(i) && s.charAt(i) <= '9') { 211 precision = precision * 10 + s.charAt(i) - '0'; 212 i++; 213 } else { 214 parseState = 4; 215 } 216 } 217 218 if (parseState == 4) { 219 if (i >= length) { 220 parseState = 5; 221 } else { 222 fmt = s.charAt(i); 223 } 224 225 i++; 226 } 227 if (i < length) { 228 post = s.substring(i, length); 229 } 230 } 231 232 238 public static double atof(String s) { 239 int i = 0; 240 int sign = 1; 241 double r = 0; double f = 0; double p = 1; int state = 0; 246 while (i < s.length() && Character.isWhitespace(s.charAt(i))) { 247 i++; 248 } 249 if (i < s.length() && s.charAt(i) == '-') { 250 sign = -1; 251 i++; 252 } else if (i < s.length() && s.charAt(i) == '+') { 253 i++; 254 } 255 while (i < s.length()) { 256 char ch = s.charAt(i); 257 258 if ('0' <= ch && ch <= '9') { 259 if (state == 0) { 260 r = r * 10 + ch - '0'; 261 } else if (state == 1) { 262 p = p / 10; 263 r = r + p * (ch - '0'); 264 } 265 } else if (ch == '.') { 266 if (state == 0) { 267 state = 1; 268 } else { 269 return sign * r; 270 } 271 } else if (ch == 'e' || ch == 'E') { 272 long e = (int) parseLong(s.substring(i + 1), 10); 273 274 return sign * r * Math.pow(10, e); 275 } else { 276 return sign * r; 277 } 278 279 i++; 280 } 281 282 return sign * r; 283 } 284 285 292 public static int atoi(String s) { 293 return (int) atol(s); 294 } 295 296 303 public static long atol(String s) { 304 int i = 0; 305 306 while (i < s.length() && Character.isWhitespace(s.charAt(i))) { 307 i++; 308 } 309 if (i < s.length() && s.charAt(i) == '0') { 310 if (i + 1 < s.length() && 311 (s.charAt(i + 1) == 'x' || s.charAt(i + 1) == 'X')) { 312 return parseLong(s.substring(i + 2), 16); 313 } else { 314 return parseLong(s, 8); 315 } 316 } else { 317 return parseLong(s, 10); 318 } 319 } 320 321 328 private static String convert(long x, int n, int m, String d) { 329 if (x == 0) { 330 return ("0"); 331 } 332 333 String r = ""; 334 335 while (x != 0) { 336 r = d.charAt((int) (x & m)) + r; 337 x = x >>> n; 338 } 339 340 return r; 341 } 342 343 347 private String expFormat(double d) { 348 String f = ""; 349 int e = 0; 350 double dd = d; 351 double factor = 1; 352 353 if (d != 0) { 354 while (dd > 10) { 355 e++; 356 factor /= 10; 357 dd = dd / 10; 358 } 359 while (dd < 1) { 360 e--; 361 factor *= 10; 362 dd = dd * 10; 363 } 364 } 365 if ((fmt == 'g' || fmt == 'G') && e >= -4 && e < precision) { 366 return fixedFormat(d); 367 } 368 369 d = d * factor; 370 f = f + fixedFormat(d); 371 372 if (fmt == 'e' || fmt == 'g') { 373 f = f + "e"; 374 } else { 375 f = f + "E"; 376 } 377 378 String p = "000"; 379 380 if (e >= 0) { 381 f = f + "+"; 382 p = p + e; 383 } else { 384 f = f + "-"; 385 p = p + (-e); 386 } 387 388 return f + p.substring(p.length() - 3, p.length()); 389 } 390 391 395 private String fixedFormat(double d) { 396 boolean removeTrailing = (fmt == 'G' || fmt == 'g') && !alternate; 397 398 if (d > 0x7FFFFFFFFFFFFFFFL) { 400 return expFormat(d); 401 } 402 if (precision == 0) { 403 return (long) (d + 0.5) + (removeTrailing ? "" : "."); 404 } 405 406 long whole = (long) d; 407 double fr = d - whole; 409 if (fr >= 1 || fr < 0) { 410 return expFormat(d); 411 } 412 413 double factor = 1; 414 String leadingZeroes = ""; 415 416 for (int i = 1; i <= precision && factor <= 0x7FFFFFFFFFFFFFFFL; i++) { 417 factor *= 10; 418 leadingZeroes = leadingZeroes + "0"; 419 } 420 421 long l = (long) (factor * fr + 0.5); 422 423 if (l >= factor) { l = 0; 425 whole++; 426 } 427 428 String z = leadingZeroes + l; 429 z = "." + z.substring(z.length() - precision, z.length()); 430 431 if (removeTrailing) { 432 int t = z.length() - 1; 433 434 while (t >= 0 && z.charAt(t) == '0') { 435 t--; 436 } 437 if (t >= 0 && z.charAt(t) == '.') { 438 t--; 439 } 440 441 z = z.substring(0, t + 1); 442 } 443 444 return whole + z; 445 } 446 447 453 public String form(char c) { 454 if (fmt != 'c') { 455 throw new java.lang.IllegalArgumentException (); 456 } 457 458 String r = "" + c; 459 460 return pad(r); 461 } 462 463 469 public String form(double x) { 470 String r; 471 472 if (precision < 0) { 473 precision = 6; 474 } 475 476 int s = 1; 477 478 if (x < 0) { 479 x = -x; 480 s = -1; 481 } 482 if (fmt == 'f') { 483 r = fixedFormat(x); 484 } else if (fmt == 'e' || fmt == 'E' || fmt == 'g' || fmt == 'G') { 485 r = expFormat(x); 486 } else { 487 throw new java.lang.IllegalArgumentException (); 488 } 489 490 return pad(sign(s, r)); 491 } 492 493 499 public String form(long x) { 500 String r; 501 int s = 0; 502 503 if (fmt == 'd' || fmt == 'i') { 504 if (x < 0) { 505 r = ("" + x).substring(1); 506 s = -1; 507 } else { 508 r = "" + x; 509 s = 1; 510 } 511 } else if (fmt == 'o') { 512 r = convert(x, 3, 7, "01234567"); 513 } else if (fmt == 'x') { 514 r = convert(x, 4, 15, "0123456789abcdef"); 515 } else if (fmt == 'X') { 516 r = convert(x, 4, 15, "0123456789ABCDEF"); 517 } else { 518 throw new java.lang.IllegalArgumentException (); 519 } 520 521 return pad(sign(s, r)); 522 } 523 524 530 public String form(String s) { 531 if (fmt != 's') { 532 throw new java.lang.IllegalArgumentException (); 533 } 534 if (precision >= 0 && precision < s.length()) { 535 s = s.substring(0, precision); 536 } 537 538 return pad(s); 539 } 540 541 545 private String pad(String r) { 546 String p = repeat(' ', width - r.length()); 547 548 if (leftAlign) { 549 return pre + r + p + post; 550 } else { 551 return pre + p + r + post; 552 } 553 } 554 555 560 private static long parseLong(String s, int base) { 561 int i = 0; 562 int sign = 1; 563 long r = 0; 564 565 while (i < s.length() && Character.isWhitespace(s.charAt(i))) { 566 i++; 567 } 568 if (i < s.length() && s.charAt(i) == '-') { 569 sign = -1; 570 i++; 571 } else if (i < s.length() && s.charAt(i) == '+') { 572 i++; 573 } 574 while (i < s.length()) { 575 char ch = s.charAt(i); 576 577 if ('0' <= ch && ch < '0' + base) { 578 r = r * base + ch - '0'; 579 } else if ('A' <= ch && ch < 'A' + base - 10) { 580 r = r * base + ch - 'A' + 10; 581 } else if ('a' <= ch && ch < 'a' + base - 10) { 582 r = r * base + ch - 'a' + 10; 583 } else { 584 return r * sign; 585 } 586 587 i++; 588 } 589 590 return r * sign; 591 } 592 593 600 public static void print(PrintStream s, String fmt, char x) { 601 s.print(new Format(fmt).form(x)); 602 } 603 604 611 public static void print(PrintStream s, String fmt, double x) { 612 s.print(new Format(fmt).form(x)); 613 } 614 615 622 public static void print(PrintStream s, String fmt, long x) { 623 s.print(new Format(fmt).form(x)); 624 } 625 626 633 public static void print(PrintStream s, String fmt, String x) { 634 s.print(new Format(fmt).form(x)); 635 } 636 637 644 public static void print(PrintWriter s, String fmt, double x) { 645 s.print(new Format(fmt).form(x)); 646 } 647 648 655 public static void print(PrintWriter s, String fmt, String x) { 656 s.print(new Format(fmt).form(x)); 657 } 658 659 664 private static String repeat(char c, int n) { 665 if (n <= 0) { 666 return (""); 667 } 668 669 StringBuffer s = new StringBuffer (n); 670 671 for (int i = 0; i < n; i++) { 672 s.append(c); 673 } 674 675 return s.toString(); 676 } 677 678 683 private String sign(int s, String r) { 684 String p = (""); 685 686 if (s < 0) { 687 p = "-"; 688 } else if (s > 0) { 689 if (showPlus) { 690 p = "+"; 691 } else if (showSpace) { 692 p = " "; 693 } 694 } else { 695 if (fmt == 'o' && alternate && r.length() > 0 && 696 r.charAt(0) != '0') { 697 p = "0"; 698 } else if (fmt == 'x' && alternate) { 699 p = "0x"; 700 } else if (fmt == 'X' && alternate) { 701 p = "0X"; 702 } 703 } 704 705 int w = 0; 706 707 if (leadingZeroes) { 708 w = width; 709 } else if ((fmt == 'd' || fmt == 'i' || fmt == 'x' || fmt == 'X' || 710 fmt == 'o') && precision > 0) { 711 w = precision; 712 } 713 714 return p + repeat('0', w - p.length() - r.length()) + r; 715 } 716 717 } 718 | Popular Tags |