com.ibm.icu.text.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.
| Modifier and Type | Class and Description |
|---|---|
| public static class | SimpleFormatterImpl.IterInternal
Internal iterator interface for maximum efficiency. |
| Modifier and Type | Field and Description |
|---|---|
| private static final int | ARG_NUM_LIMIT
Argument numbers must be smaller than this limit. |
| private static final String[][] | COMMON_PATTERNS
"Intern" some common patterns. |
| private static final char | |
| private static final char | |
| private static final char | |
| private static final int | MAX_SEGMENT_LENGTH
Maximum length of a text segment. |
| private static final char | SEGMENT_LENGTH_ARGUMENT_CHAR
Initial and maximum char/UChar value set for a text segment. |
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| public static String | Returns: The compiled-pattern string.The pattern string. pattern, StringBuilder A StringBuilder instance which may or may not be used. sb, int The pattern must have at least this many arguments. min, int The pattern must have at most this many arguments. 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)
|
| public static StringBuilder | Returns: appendToCompiled form of a pattern string. compiledPattern, StringBuilder Gets the formatted pattern and values appended. appendTo, int[] 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. offsets, CharSequence... 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. values)Formats the given values, appending to the appendTo builder. |
| public static StringBuilder | Returns: resultCompiled form of a pattern string. compiledPattern, StringBuilder Gets its contents replaced by the formatted pattern and values. result, int[] 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. offsets, CharSequence... The argument values.
An argument value may be the same object as result.
values.length must be at least getArgumentLimit(). values)Formats the given values, replacing the contents of the result builder. |
| public static String | formatCompiledPattern(String
Compiled form of a pattern string. compiledPattern, CharSequence... values)Formats the given values. |
| public static int | Returns: Net number of characters added to the formatted string.Compiled form of a pattern string. compiledPattern, Format.Field to use when adding chars to the output. field,The start index of the argument already in the output string. start, int The end index of the argument already in the output string. end, FormattedStringBuilder Destination for formatted output. output)Special case for using FormattedStringBuilder with patterns with 0 or 1 argument. |
| public static String | Returns: The compiled-pattern string.Not-compiled form of a pattern string. pattern, int The pattern must have at least this many arguments. min, int The pattern must have at most this many arguments. max, CharSequence... values)Formats the not-compiled pattern with the given values. |
| public static int | Returns: The max argument number + 1.Compiled form of a pattern string. compiledPattern)
|
| public static int | |
| public static int | Returns: The number of code units.Compiled form of a pattern string. compiledPattern)Returns the length in code units of the pattern text up until the first argument. |
| public static String | getTextWithNoArguments(String
Compiled form of a pattern string. compiledPattern)Returns the pattern text with none of the arguments. |
| ARG_NUM_LIMIT | back to summary |
|---|---|
| private static final int ARG_NUM_LIMIT Argument 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). | |
| COMMON_PATTERNS | back to summary |
|---|---|
| private static final String[][] COMMON_PATTERNS "Intern" some common patterns. | |
| LEN1_CHAR | back to summary |
|---|---|
| private static final char LEN1_CHAR | |
| LEN2_CHAR | back to summary |
|---|---|
| private static final char LEN2_CHAR | |
| LEN3_CHAR | back to summary |
|---|---|
| private static final char LEN3_CHAR | |
| MAX_SEGMENT_LENGTH | back to summary |
|---|---|
| private static final int MAX_SEGMENT_LENGTH Maximum length of a text segment. Longer segments are split into shorter ones. | |
| SEGMENT_LENGTH_ARGUMENT_CHAR | back to summary |
|---|---|
| private static final char SEGMENT_LENGTH_ARGUMENT_CHAR Initial 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. | |
| SimpleFormatterImpl | back to summary |
|---|---|
| private SimpleFormatterImpl() Use only static methods. | |
| compileToStringMinMaxArguments | back to summary |
|---|---|
| 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.
| |
| format | back to summary |
|---|---|
| private static StringBuilder format(String compiledPattern, CharSequence[] values, StringBuilder result, String resultCopy, boolean forbidResultAsValue, int[] offsets) | |
| formatAndAppend | back to summary |
|---|---|
| public static StringBuilder formatAndAppend(String compiledPattern, StringBuilder appendTo, int[] offsets, CharSequence... values) Formats the given values, appending to the appendTo builder.
| |
| formatAndReplace | back to summary |
|---|---|
| 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.
| |
| formatCompiledPattern | back to summary |
|---|---|
| public static String formatCompiledPattern(String compiledPattern, CharSequence... values) Formats the given values.
| |
| formatPrefixSuffix | back to summary |
|---|---|
| public static int formatPrefixSuffix(String compiledPattern, Format. 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.
| |
| formatRawPattern | back to summary |
|---|---|
| public static String formatRawPattern(String pattern, int min, int max, CharSequence... values) 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.
| |
| getArgumentLimit | back to summary |
|---|---|
public static int getArgumentLimit(String compiledPattern)
| |
| getLength | back to summary |
|---|---|
| public static int getLength(String compiledPattern, boolean codePoints) Returns the length of the pattern text with none of the arguments.
| |
| getPrefixLength | back to summary |
|---|---|
| public static int getPrefixLength(String compiledPattern) Returns the length in code units of the pattern text up until the first argument.
| |
| getTextWithNoArguments | back to summary |
|---|---|
| public static String getTextWithNoArguments(String compiledPattern) Returns the pattern text with none of the arguments. Like formatting with all-empty string values.
| |
long state = 0;
while (true) {
state = IterInternal.step(state, compiledPattern, output);
if (state == IterInternal.DONE) {
break;
}
int argIndex = IterInternal.getArgIndex(state);
// Append the string corresponding to argIndex to output
}
| Modifier and Type | Field and Description |
|---|---|
| public static final long |
| Access | Constructor and Description |
|---|---|
| public |
| Modifier and Type | Method and Description |
|---|---|
| public static int | |
| public static long |
| DONE | back to summary |
|---|---|
| public static final long DONE | |
| IterInternal | back to summary |
|---|---|
| public IterInternal() | |
| getArgIndex | back to summary |
|---|---|
| public static int getArgIndex(long state) | |
| step | back to summary |
|---|---|
| public static long step(long state, CharSequence compiledPattern, Appendable output) | |