Package com.ibm.icu.impl
Class SimpleFormatterImpl
java.lang.Object
com.ibm.icu.impl.SimpleFormatterImpl
Formats simple patterns like "{1} was born in {0}".
Internal version of
SimpleFormatter
with only static methods, to avoid wrapper objects.
This class "compiles" pattern strings into a binary format and implements formatting etc. based on that.
Format: Index 0: One more than the highest argument number. Followed by zero or more arguments or literal-text segments.
An argument is stored as its number, less than ARG_NUM_LIMIT. A literal-text segment is stored as its length (at least 1) offset by ARG_NUM_LIMIT, followed by that many chars.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic class
Internal iterator interface for maximum efficiency. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final int
Argument numbers must be smaller than this limit.private static final String[][]
"Intern" some common patterns.private static final char
private static final char
private static final char
private static final int
Maximum length of a text segment.private static final char
Initial and maximum char/UChar value set for a text segment. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic String
compileToStringMinMaxArguments
(CharSequence pattern, StringBuilder sb, int min, int max) Creates a compiled form of the pattern string, for use with appropriate static methods.private static StringBuilder
format
(String compiledPattern, CharSequence[] values, StringBuilder result, String resultCopy, boolean forbidResultAsValue, int[] offsets) static StringBuilder
formatAndAppend
(String compiledPattern, StringBuilder appendTo, int[] offsets, CharSequence... values) Formats the given values, appending to the appendTo builder.static StringBuilder
formatAndReplace
(String compiledPattern, StringBuilder result, int[] offsets, CharSequence... values) Formats the given values, replacing the contents of the result builder.static String
formatCompiledPattern
(String compiledPattern, CharSequence... values) Formats the given values.static int
formatPrefixSuffix
(String compiledPattern, Format.Field field, int start, int end, FormattedStringBuilder output) Special case for using FormattedStringBuilder with patterns with 0 or 1 argument.static String
formatRawPattern
(String pattern, int min, int max, CharSequence... values) Formats the not-compiled pattern with the given values.static int
getArgumentLimit
(String compiledPattern) static int
Returns the length of the pattern text with none of the arguments.static int
getPrefixLength
(String compiledPattern) Returns the length in code units of the pattern text up until the first argument.static String
getTextWithNoArguments
(String compiledPattern) Returns the pattern text with none of the arguments.
-
Field Details
-
ARG_NUM_LIMIT
private static final int ARG_NUM_LIMITArgument numbers must be smaller than this limit. Text segment lengths are offset by this much. This is currently the only unused char value in compiled patterns, except it is the maximum value of the first unit (max arg +1).- See Also:
-
LEN1_CHAR
private static final char LEN1_CHAR- See Also:
-
LEN2_CHAR
private static final char LEN2_CHAR- See Also:
-
LEN3_CHAR
private static final char LEN3_CHAR- See Also:
-
SEGMENT_LENGTH_ARGUMENT_CHAR
private static final char SEGMENT_LENGTH_ARGUMENT_CHARInitial and maximum char/UChar value set for a text segment. Segment length char values are from ARG_NUM_LIMIT+1 to this value here. Normally 0xffff, but can be as small as ARG_NUM_LIMIT+1 for testing.- See Also:
-
MAX_SEGMENT_LENGTH
private static final int MAX_SEGMENT_LENGTHMaximum length of a text segment. Longer segments are split into shorter ones.- See Also:
-
COMMON_PATTERNS
"Intern" some common patterns.
-
-
Constructor Details
-
SimpleFormatterImpl
private SimpleFormatterImpl()Use only static methods.
-
-
Method Details
-
compileToStringMinMaxArguments
public static String compileToStringMinMaxArguments(CharSequence pattern, StringBuilder sb, int min, int max) Creates a compiled form of the pattern string, for use with appropriate static methods. The number of arguments checked against the given limits is the highest argument number plus one, not the number of occurrences of arguments.- Parameters:
pattern
- The pattern string.sb
- A StringBuilder instance which may or may not be used.min
- The pattern must have at least this many arguments.max
- The pattern must have at most this many arguments.- Returns:
- The compiled-pattern string.
- Throws:
IllegalArgumentException
- for bad argument syntax and too few or too many arguments.
-
getArgumentLimit
- Parameters:
compiledPattern
- Compiled form of a pattern string.- Returns:
- The max argument number + 1.
-
formatCompiledPattern
Formats the given values.- Parameters:
compiledPattern
- Compiled form of a pattern string.
-
formatRawPattern
Formats the not-compiled pattern with the given values. Equivalent to compileToStringMinMaxArguments() followed by formatCompiledPattern(). The number of arguments checked against the given limits is the highest argument number plus one, not the number of occurrences of arguments.- Parameters:
pattern
- Not-compiled form of a pattern string.min
- The pattern must have at least this many arguments.max
- The pattern must have at most this many arguments.- Returns:
- The compiled-pattern string.
- Throws:
IllegalArgumentException
- for bad argument syntax and too few or too many arguments.
-
formatAndAppend
public static StringBuilder formatAndAppend(String compiledPattern, StringBuilder appendTo, int[] offsets, CharSequence... values) Formats the given values, appending to the appendTo builder.- Parameters:
compiledPattern
- Compiled form of a pattern string.appendTo
- Gets the formatted pattern and values appended.offsets
- offsets[i] receives the offset of where values[i] replaced pattern argument {i}. Can be null, or can be shorter or longer than values. If there is no {i} in the pattern, then offsets[i] is set to -1.values
- The argument values. An argument value must not be the same object as appendTo. values.length must be at least getArgumentLimit(). Can be null if getArgumentLimit()==0.- Returns:
- appendTo
-
formatAndReplace
public static StringBuilder formatAndReplace(String compiledPattern, StringBuilder result, int[] offsets, CharSequence... values) Formats the given values, replacing the contents of the result builder. May optimize by actually appending to the result if it is the same object as the value corresponding to the initial argument in the pattern.- Parameters:
compiledPattern
- Compiled form of a pattern string.result
- Gets its contents replaced by the formatted pattern and values.offsets
- offsets[i] receives the offset of where values[i] replaced pattern argument {i}. Can be null, or can be shorter or longer than values. If there is no {i} in the pattern, then offsets[i] is set to -1.values
- The argument values. An argument value may be the same object as result. values.length must be at least getArgumentLimit().- Returns:
- result
-
getTextWithNoArguments
Returns the pattern text with none of the arguments. Like formatting with all-empty string values.- Parameters:
compiledPattern
- Compiled form of a pattern string.
-
getLength
Returns the length of the pattern text with none of the arguments.- Parameters:
compiledPattern
- Compiled form of a pattern string.codePoints
- true to count code points; false to count code units.- Returns:
- The number of code points or code units.
-
getPrefixLength
Returns the length in code units of the pattern text up until the first argument.- Parameters:
compiledPattern
- Compiled form of a pattern string.- Returns:
- The number of code units.
-
formatPrefixSuffix
public static int formatPrefixSuffix(String compiledPattern, Format.Field field, int start, int end, FormattedStringBuilder output) Special case for using FormattedStringBuilder with patterns with 0 or 1 argument. With 1 argument, treat the current contents of the FormattedStringBuilder between start and end as the argument {0}. Insert the extra strings from compiledPattern to surround the argument in the output. With 0 arguments, overwrite the entire contents of the FormattedStringBuilder between start and end.- Parameters:
compiledPattern
- Compiled form of a pattern string.field
- Field to use when adding chars to the output.start
- The start index of the argument already in the output string.end
- The end index of the argument already in the output string.output
- Destination for formatted output.- Returns:
- Net number of characters added to the formatted string.
-
format
private static StringBuilder format(String compiledPattern, CharSequence[] values, StringBuilder result, String resultCopy, boolean forbidResultAsValue, int[] offsets)
-