Format.java revision 7c478bd95313f5f23a4c958a745db2134aa03244
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* ident "%Z%%M% %I% %E% SMI"
*
* Copyright (c) 2001 by Sun Microsystems, Inc.
* All rights reserved.
*/
/*
* Cay S. Horstmann & Gary Cornell, Core Java
* Published By Sun Microsystems Press/Prentice-Hall
* Copyright (C) 1997 Sun Microsystems Inc.
* All Rights Reserved.
*
* Permission to use, copy, modify, and distribute this
* software and its documentation for NON-COMMERCIAL purposes
* and without fee is hereby granted provided that this
* copyright notice appears in all copies.
*
* THE AUTHORS AND PUBLISHER MAKE NO REPRESENTATIONS OR
* WARRANTIES ABOUT THE SUITABILITY OF THE SOFTWARE, EITHER
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
* IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE, OR NON-INFRINGEMENT. THE AUTHORS
* AND PUBLISHER SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED
* BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
* THIS SOFTWARE OR ITS DERIVATIVES.
*/
/**
* A class for formatting numbers that follows printf conventions.
* Also implements C-like atoi and atof functions
* @version 1.01 15 Feb 1996
* @author Cay Horstmann
*/
public class Format
{
/**
* Formats the number following printf conventions.
* Main limitation: Can only handle one format parameter at a time
* Use multiple Format objects to format more than one number
* @param s the format string following printf conventions
* The string has a prefix, a format code and a suffix. The prefix and
* suffix become part of the formatted output. The format code directs the
* formatting of the (single) parameter to be formatted. The code has the
* following structure
* <ul>
* <li> a % (required)
* <li> a modifier (optional)
* <dl>
* <dt> + <dd> forces display of + for positive numbers
* <dt> 0 <dd> show leading zeroes
* <dt> - <dd> align left in the field
* <dt> space <dd> prepend a space in front of positive numbers
* <dt> # <dd> use "alternate" format. Add 0 or 0x for octal or hexadecimal
* numbers. Don't suppress trailing zeroes in general floating point format.
* </dl>
* <li> an integer denoting field width (optional)
* <li> a period followed by an integer denoting precision (optional)
* <li> a format descriptor (required)
* <dl>
* <dt>f <dd> floating point number in fixed format
* <dt>e, E <dd> floating point number in exponential notation (scientific
* format). The E format results in an uppercase E for the exponent
* (1.14130E+003), the e format in a lowercase e.
* <dt>g, G <dd> floating point number in general format (fixed format for
* small numbers, exponential format for large numbers). Trailing zeroes
* are suppressed. The G format results in an uppercase E for the exponent
* (if any), the g format in a lowercase e.
* <dt>d, i <dd> integer in decimal
* <dt>x <dd> integer in hexadecimal
* <dt>o <dd> integer in octal
* <dt>s <dd> string
* <dt>c <dd> character
* </dl>
* </ul>
* @exception IllegalArgumentException if bad format
*/
width = 0;
precision = -1;
pre = "";
post = "";
leading_zeroes = false;
show_plus = false;
alternate = false;
show_space = false;
left_align = false;
fmt = ' ';
int state = 0;
int parse_state = 0;
// 0 = prefix, 1 = flags, 2 = width, 3 = precision,
// 4 = format, 5 = end
int i = 0;
while (parse_state == 0) {
if (i >= length) {
parse_state = 5;
} else if (s.charAt(i) == '%') {
if (i < length - 1) {
i++;
} else {
parse_state = 1;
}
} else {
}
} else {
}
i++;
}
while (parse_state == 1) {
if (i >= length) {
parse_state = 5;
} else if (s.charAt(i) == ' ') {
show_space = true;
} else if (s.charAt(i) == '-') {
left_align = true;
} else if (s.charAt(i) == '+') {
show_plus = true;
} else if (s.charAt(i) == '0') {
leading_zeroes = true;
} else if (s.charAt(i) == '#') {
alternate = true;
} else {
parse_state = 2; i--;
}
i++;
}
while (parse_state == 2) {
if (i >= length) {
parse_state = 5;
i++;
} else if (s.charAt(i) == '.') {
parse_state = 3;
precision = 0;
i++;
} else {
parse_state = 4;
}
}
while (parse_state == 3) {
if (i >= length) {
parse_state = 5;
i++;
} else {
parse_state = 4;
}
}
if (parse_state == 4) {
if (i >= length) {
parse_state = 5;
} else {
}
i++;
}
if (i < length) {
}
}
/**
* prints a formatted number following printf conventions
* @param s a PrintStream
* @param fmt the format string
* @param x the double to print
*/
}
/**
* prints a formatted number following printf conventions
* @param s a PrintStream
* @param fmt the format string
* @param x the long to print
*/
}
/**
* prints a formatted number following printf conventions
* @param s a PrintStream
* @param fmt the format string
* @param x the character to
*/
}
/**
* prints a formatted number following printf conventions
* @param s a PrintStream, fmt the format string
* @param x a string that represents the digits to print
*/
}
/**
* Converts a string of digits(decimal, octal or hex) to an integer
* @param s a string
* @return the numeric value of the prefix of s representing a base
* 10 integer
*/
return (int)atol(s);
}
/**
* Converts a string of digits(decimal, octal or hex) to a long integer
* @param s a string
* @return the numeric value of the prefix of s representing a base
* 10 integer
*/
int i = 0;
i++;
}
} else {
return parseLong(s, 8);
}
} else {
return parseLong(s, 10);
}
}
int i = 0;
int sign = 1;
long r = 0;
i++;
}
sign = -1; i++;
i++;
}
while (i < s.length()) {
} else {
return r * sign;
}
i++;
}
return r * sign;
}
/**
* Converts a string of digits to an double
* @param s a string
*/
int i = 0;
int sign = 1;
double r = 0; // integer part
double f = 0; // fractional part
double p = 1; // exponent of fractional part
i++;
}
sign = -1; i++;
i++;
}
while (i < s.length()) {
if (state == 0) {
} else if (state == 1) {
p = p / 10;
r = r + p * (ch - '0');
}
} else if (ch == '.') {
if (state == 0) {
state = 1;
} else {
return sign * r;
}
} else {
return sign * r;
}
i++;
}
return sign * r;
}
/**
* Formats a double into a string (like sprintf in C)
* @param x the number to format
* @return the formatted string
* @exception IllegalArgumentException if bad argument
*/
String r;
if (precision < 0) {
precision = 6;
}
int s = 1;
if (x < 0) {
x = -x;
s = -1;
}
if (fmt == 'f') {
r = fixed_format(x);
r = exp_format(x);
} else {
}
}
/**
* Formats a long integer into a string (like sprintf in C)
* @param x the number to format
* @return the formatted string
*/
String r;
int s = 0;
s = 1;
if (x < 0) {
x = -x; s = -1;
}
r = "" + x;
} else if (fmt == 'o') {
} else if (fmt == 'x') {
} else if (fmt == 'X') {
} else {
}
}
/**
* Formats a character into a string (like sprintf in C)
* @param x the value to format
* @return the formatted string
*/
if (fmt != 'c') {
}
String r = "" + c;
return pad(r);
}
/**
* Formats a string into a larger string (like sprintf in C)
* @param x the value to format
* @return the formatted string
*/
if (fmt != 's') {
}
if (precision >= 0) {
}
return pad(s);
}
if (n <= 0) {
return "";
}
StringBuffer s = new StringBuffer(n);
for (int i = 0; i < n; i++) {
s.append(c);
}
return s.toString();
}
if (x == 0) {
return "0";
}
String r = "";
while (x != 0) {
r = d.charAt((int)(x & m)) + r;
x = x >>> n;
}
return r;
}
if (left_align) {
} else {
}
}
String p = "";
if (s < 0) {
p = "-";
} else if (s > 0) {
if (show_plus) {
p = "+";
} else if (show_space) {
p = " ";
}
} else {
p = "0";
p = "0x";
p = "0X";
}
}
int w = 0;
if (leading_zeroes) {
w = width;
w = precision;
}
}
private String fixed_format(double d) {
String f = "";
if (d > 0x7FFFFFFFFFFFFFFFL) {
return exp_format(d);
}
f = f + l;
double fr = d - l; // fractional part
return exp_format(d);
}
}
// precondition: 0 <= fr < 1
String z = "";
if (precision > 0) {
double factor = 1;
i++) {
factor *= 10;
}
z = leading_zeroes + l;
}
z = "." + z;
}
// remove trailing zeroes and decimal point
int t = z.length() - 1;
t--;
}
t--;
}
}
return z;
}
private String exp_format(double d) {
String f = "";
int e = 0;
double dd = d;
double factor = 1;
while (dd > 10) {
}
while (dd < 1) {
}
return fixed_format(d);
}
d = d * factor;
f = f + fixed_format(d);
f = f + "e";
} else {
f = f + "E";
}
String p = "000";
if (e >= 0) {
f = f + "+";
p = p + e;
} else {
f = f + "-";
p = p + (-e);
}
}
private int width;
private int precision;
private boolean leading_zeroes;
private boolean show_plus;
private boolean alternate;
private boolean show_space;
private boolean left_align;
private char fmt; // one of cdeEfgGiosxXos
}