Package io.jawk.jrt

Class AwkPrintf

java.lang.Object
io.jawk.jrt.AwkPrintf

public final class AwkPrintf extends Object
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:

  • %s converts numeric values to strings with AWK's number-to-string rules: integral values are printed without a fractional part, and other values are formatted with CONVFMT;
  • %c prints the character for a numeric code point, or the first character of a string value;
  • %i is an alias for %d, and %u prints 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 %g notation (%u/%o/%x/%X), like gawk;
  • NaN and infinities print as nan, inf, and -inf;
  • %e, %f, and %g round halfway cases to even, like the C library used by gawk;
  • unknown conversion specifiers are printed verbatim without consuming an argument, and a fatal AwkRuntimeException is 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 Details

    • DEFAULT_CONVFMT

      public static final String DEFAULT_CONVFMT
      Default AWK number-to-string conversion format (CONVFMT).
      See Also:
  • Method Details

    • sprintf

      public static String sprintf(String format, Object... args)
      Formats the given arguments with AWK's sprintf() semantics, using Locale.US and the default CONVFMT ("%.6g").
      Parameters:
      format - AWK format string
      args - arguments supplied after the format string
      Returns:
      the formatted text
      Throws:
      AwkRuntimeException - when there are not enough arguments to satisfy the format string
    • sprintf

      public static String sprintf(Locale locale, String convfmt, String format, Object... args)
      Formats the given arguments with AWK's sprintf() semantics.
      Parameters:
      locale - locale used for numeric formatting (decimal separator, grouping separator for the ' flag)
      convfmt - number-to-string conversion format (CONVFMT) used by %s for non-integral numeric values
      format - AWK format string
      args - arguments supplied after the format string
      Returns:
      the formatted text
      Throws:
      AwkRuntimeException - when there are not enough arguments to satisfy the format string
    • toAwkString

      public static String toAwkString(Object value, String conversionFormat, Locale locale)
      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 as nan, inf, and -inf, and all other numeric values are formatted with the supplied conversion format (CONVFMT or OFMT).

      Parameters:
      value - value to convert
      conversionFormat - number-to-string conversion format
      locale - locale used for numeric formatting
      Returns:
      the AWK string value of value