ALib C++ Library
Library Version: 2402 R1
Documentation generated by doxygen
No Matches
FormatterJavaStyle Class Reference


Implements a Formatter according to the formatting standards of the Java language .

Inherited, public fields of parent class FormatterStdImpl provide important possibilities for changing the formatting behavior of instances of this class. Therefore, do not forget to consult the parent classes documentation.

In general, the original specification is covered quite well. The differences and specialties are:

  • In deviation of the Java specification, after creation, a formatter in this implementation does not produce locale aware output. Instead, number formatting is set to "computational", hence the decimal point separator is '.' and the grouping character ','. As the syntax specification does not provide a feature to switch between standard and locale setting, the corresponding fields of AlternativeNumberFormat are not used with this formatter. Instead, to enable localized output, method NumberFormat::SetFromLocale has to be invoked on field FormatterStdImpl::DefaultNumberFormat . Alternatively, attributes of this object may be changed manually or by other means to reflect a desired locale.
  • Hexadecimal floating point output (conversion type 'a' and 'A') is not supported.
  • Flag '(', used to write negative numbers in round brackets, is not supported.
  • Addressing the previous argument index using '%<' is already allowed with the first placeholder. This Chooses the first argument. (In Java a MissingFormatArgumentException would be thrown.)
  • Flag '^' is an extension to the standard and denotes center-alignment - just like '-' in the standard denotes left-alignment. Right-alignment is the default.
  • Floating point values:
    • If standard field type 's' is given together with a precision, the field is cut, even if it cuts the number somewhere. (This is just a warning and same behavior as in original specification.)
    • For lower case floating point format types ('f', 'g' and 'e'), the values specified in attributes ExponentSeparator, NANLiteral and INFLiteral of object FormatterStdImpl::AlternativeNumberFormat are used. For upper case types ('G' and 'E'), the corresponding attributes in FormatterStdImpl::DefaultNumberFormat apply.
    • Fixed point format ('f' ) is not supported to use arbitrary length. See class NumberFormat for the limits. Due to this limitation, the default number of fractional digits is not set with type 'f', while in Java it is set to 6. This is to allow higher numbers up to 1.e13 to be printed in non-scientific format
    • When both, a width and a precision is given, then the precision determines the fractional part, even if the type is 'g' or 'G'. This is different than specified with Java formatter, which uses precision as the overall width in case of types 'g' or 'G'.
  • Hexadecimal and Octal Numbers:
    • Hexadecimal and octal output is cut in size (!) when a field width is given that is smaller than the resulting amount of digits of the number arguments provided.
      This implies that a value written might not be equal to the value given. This is not a bug but a design decision. The rationale behind this is that with this behavior, there is no need to mask lower digits when passing the arguments to the format invocation. In other words, the formatter "assumes" that the given field width indicates that only a corresponding number of lower digits are of interest.
    • If no width is given and the argument contains a boxed pointer, then the platform-dependent full output width of pointer types is used.
    • The number grouping option (',') can also be used with binary, hexadecimal and octal output. The types support different grouping separators for nibbles, bytes, 16-bit and 32-bit words. Changing the separator symbols, is not possible with the format fields of the format strings (if it was, this would become very incompatible to Java standards). Changes have to be made prior to the format operation by modifying field FormatterStdImpl::AlternativeNumberFormat which is provided through parent class FormatterStdImpl.
    • Alternative form ('#') adds prefixes as specified in members

      For upper case formats, those are taken from field FormatterStdImpl::DefaultNumberFormat , for lower case formats from FormatterStdImpl::AlternativeNumberFormat . All defaults may be changed by the user.

  • Time and Date:
    • In this C++ version of the class, boxed values of type DateTime are applicable to conversion type 't'.
    • The following time conversion suffix characters are supported: 'H', 'k', 'I', 'l', 'M', 'S', 'B', 'b', 'h', 'A', 'a', 'Y', 'y', 'm', 'd', 'e', 'R', 'T', 'D' and 'F'
    • The following time conversion suffix characters are not supported: 'L', 'N', 'p', 'z', 'Z', 's', 'Q', 'C', 'j', 'r' and 'c'.

Reference Documentation


Definition at line 125 of file formatterjavastyle.hpp.

#include <formatterjavastyle.hpp>

Inheritance diagram for FormatterJavaStyle:
Collaboration diagram for FormatterJavaStyle:

Inner Type Index:

struct  PlaceholderAttributesJS

Public Method Index:

ALIB_API FormatterJavaStyle ()
virtual ALIB_API FormatterStdImplClone () override
- Public Method Index: inherited from FormatterStdImpl
 FormatterStdImpl (const String &formatterClassName)
- Public Method Index: inherited from Formatter
virtual ~Formatter ()
ALIB_API BoxesAcquire (const NCString &dbgFile, int dbgLine, const NCString &dbgFunc)
virtual ALIB_API void CloneSettings (Formatter &reference)
int CountAcquirements () const
template<typename... TArgs>
FormatterFormat (AString &target, TArgs &&... args)
ALIB_API FormatterFormatArgs (AString &target)
FormatterFormatArgs (AString &target, const Boxes &args)
ALIB_API void Release ()
ALIB_API void ReplaceDefault (Formatter *newFormatter)
- Public Method Index: inherited from ThreadLock
ALIB_API ThreadLock (lang::Safeness safeness=lang::Safeness::Safe)
ALIB_API ~ThreadLock ()
ALIB_API void Acquire (const NCString &dbgFile, int dbgLine, const NCString &dbgFunc)
int CountAcquirements () const
ThreadGetOwner () const
lang::Safeness GetSafeness () const
bool IsOwnedByCurrentThread () const
ALIB_API void Release ()
ALIB_API void SetSafeness (lang::Safeness safeness)
bool WillRelease () const

Additional Inherited Members

- Public Static Method Index: inherited from Formatter
static SPFormatter AcquireDefault (const NCString &dbgFile, int dbgLine, const NCString &dbgFunc)
static SPFormatter GetDefault ()
- Public Field Index: inherited from Formatter
NumberFormat AlternativeNumberFormat
NumberFormat DefaultNumberFormat
std::shared_ptr< FormatterNext
- Public Field Index: inherited from ThreadLock
NCString DbgOwnerFile =nullptr
NCString DbgOwnerFunc =nullptr
int DbgOwnerLine
uint16_t DbgRecursionWarningThreshold =10
integer DbgWarningAfterWaitTimeInMillis =2000L

Field Details:

◆ placeholderJS

PlaceholderAttributesJS placeholderJS

The extended placeholder attributes.

Definition at line 156 of file formatterjavastyle.hpp.

Constructor(s) / Destructor Details::

◆ FormatterJavaStyle()

Constructs this formatter. Inherited field DefaultNumberFormat is initialized to meet the formatting defaults of Java.

Definition at line 27 of file formatterjavastyle.cpp.

Method Details:

◆ checkStdFieldAgainstArgument()

bool checkStdFieldAgainstArgument ( )

Makes some attribute adjustments and invokes standard implementation

true if OK, false if replacement should be aborted.

Reimplemented from FormatterStdImpl.

Definition at line 415 of file formatterjavastyle.cpp.

Here is the call graph for this function:

◆ Clone()

FormatterStdImpl * Clone ( )

Clones and returns a copy of this formatter.

If the formatter attached to field Formatter::Next is of type FormatterStdImpl, then that formatter is copied as well.

An object of type FormatterPythonStyle and with the same custom settings than this.

Implements Formatter.

Definition at line 50 of file formatterjavastyle.cpp.

Here is the call graph for this function:

◆ findPlaceholder()

integer findPlaceholder ( )

Searches for '%' which is not '%' or 'n'.

The index found, -1 if not found.

Implements FormatterStdImpl.

Definition at line 85 of file formatterjavastyle.cpp.

Here is the call graph for this function:

◆ parsePlaceholder()

bool parsePlaceholder ( )

Parses placeholder field in Java syntax. The portion format_spec is not set as this is not supported by the syntax.

true on success, false on errors.

Implements FormatterStdImpl.

Definition at line 158 of file formatterjavastyle.cpp.

Here is the call graph for this function:

◆ parseStdFormatSpec()

virtual bool parseStdFormatSpec ( )

Does nothing. Java does not support custom format specifications.

true to indicate success.

Implements FormatterStdImpl.

Definition at line 220 of file formatterjavastyle.hpp.

◆ preAndPostProcess()

bool preAndPostProcess ( integer startIdx,
AString * target )

All that this formatter does with this overridden method is to convert strings to upper case.

startIdxThe index of the start of the field written in targetString. -1 indicates pre-phase.
targetThe target string, only if different from field targetString, which indicates intermediate phase.
false, if the placeholder should be skipped (nothing is written for it). true otherwise.

Reimplemented from FormatterStdImpl.

Definition at line 407 of file formatterjavastyle.cpp.

Here is the call graph for this function:

◆ resetPlaceholder()

void resetPlaceholder ( )

Invokes parent implementation and then applies some changes to reflect what is defined as default in the Java string format specification.

Reimplemented from FormatterStdImpl.

Definition at line 66 of file formatterjavastyle.cpp.

Here is the call graph for this function:

◆ writeStringPortion()

void writeStringPortion ( integer length)

Implementation of abstract method FormatterStdImpl::writeStringPortion .
Replaces "%%" with '%' and "%n" with ascii 0x0a. In addition applies Format::Escape on target which replaces standard codes like "\\n", "\\r" or "\\t" with corresponding ascii codes. (The latter is an extension to the standard behavior of Java formatter.)

lengthThe number of characters to write.

Implements FormatterStdImpl.

Definition at line 99 of file formatterjavastyle.cpp.

Here is the call graph for this function:

The documentation for this class was generated from the following files: