Package io.jawk.jrt
Class AwkPrintf
java.lang.Object
io.jawk.jrt.AwkPrintf
AWK's
printf/sprintf formatting engine.
This class implements the POSIX AWK formatting semantics (as implemented by
gawk), which differ from both C's printf() and
String.format(String, Object...) in several ways:
%sconverts numeric values to strings with AWK's number-to-string rules: integral values are printed without a fractional part, and other values are formatted withCONVFMT;%cprints the character for a numeric code point, or the first character of a string value;%iis an alias for%d, and%uprints the value as an unsigned 64-bit integer;- dynamic field width and precision (
*) consume arguments, and gawk-style positional specifiers (%n$) are honored; - integer conversions of values that exceed the 64-bit range fall back to
the full decimal expansion (
%d/%i) or%gnotation (%u/%o/%x/%X), like gawk; - NaN and infinities print as
nan,inf, and-inf; %e,%f, and%ground halfway cases to even, like the C library used by gawk;- unknown conversion specifiers are printed verbatim without consuming an
argument, and a fatal
AwkRuntimeExceptionis raised when there are not enough arguments to satisfy the format string.
This formatting logic was originally externalized in the
Printf4J project, which
emulated glibc's printf(). It has been reincorporated into Jawk and
adapted to AWK's semantics.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringDefault AWK number-to-string conversion format (CONVFMT). -
Method Summary
Modifier and TypeMethodDescriptionstatic StringFormats the given arguments with AWK'ssprintf()semantics, usingLocale.USand the defaultCONVFMT("%.6g").static StringFormats the given arguments with AWK'ssprintf()semantics.static StringtoAwkString(Object value, String conversionFormat, Locale locale) Converts a value to a string using AWK's number-to-string rules.
-
Field Details
-
DEFAULT_CONVFMT
Default AWK number-to-string conversion format (CONVFMT).- See Also:
-
-
Method Details
-
sprintf
Formats the given arguments with AWK'ssprintf()semantics, usingLocale.USand the defaultCONVFMT("%.6g").- Parameters:
format- AWK format stringargs- arguments supplied after the format string- Returns:
- the formatted text
- Throws:
AwkRuntimeException- when there are not enough arguments to satisfy the format string
-
sprintf
Formats the given arguments with AWK'ssprintf()semantics.- Parameters:
locale- locale used for numeric formatting (decimal separator, grouping separator for the'flag)convfmt- number-to-string conversion format (CONVFMT) used by%sfor non-integral numeric valuesformat- AWK format stringargs- arguments supplied after the format string- Returns:
- the formatted text
- Throws:
AwkRuntimeException- when there are not enough arguments to satisfy the format string
-
toAwkString
Converts a value to a string using AWK's number-to-string rules.Non-numeric values are converted with
toString(). Numeric values holding an integral value are printed without a fractional part (using the full decimal expansion when the value exceeds the 64-bit range), NaN and infinities print asnan,inf, and-inf, and all other numeric values are formatted with the supplied conversion format (CONVFMTorOFMT).- Parameters:
value- value to convertconversionFormat- number-to-string conversion formatlocale- locale used for numeric formatting- Returns:
- the AWK string value of
value
-