Top Description Inners Fields Constructors Methods
com.ibm.icu.impl.number

public Class AffixUtils

extends Object
Class Inheritance
Imports
com.ibm.icu.impl.FormattedStringBuilder, com.ibm.icu.text.NumberFormat, .UnicodeSet

Performs manipulations on affix patterns: the prefix and suffix strings associated with a decimal format pattern. For example:
Affix Pattern Example Unescaped (Formatted) String
abc abc
ab- ab−
ab'-' ab-
ab'' ab'
To manually iterate over tokens in a literal string, use the following pattern, which is designed to be efficient.
long tag = 0L;
while (AffixPatternUtils.hasNext(tag, patternString)) {
    tag = AffixPatternUtils.nextToken(tag, patternString);
    int typeOrCp = AffixPatternUtils.getTypeOrCp(tag);
    switch (typeOrCp) {
    case AffixPatternUtils.TYPE_MINUS_SIGN:
        // Current token is a minus sign.
        break;
    case AffixPatternUtils.TYPE_PLUS_SIGN:
        // Current token is a plus sign.
        break;
    case AffixPatternUtils.TYPE_PERCENT:
        // Current token is a percent sign.
        break;
    // ... other types ...
    default:
        // Current token is an arbitrary code point.
        // The variable typeOrCp is the code point.
        break;
    }
}

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static interface
public static interface

Field Summary

Modifier and TypeField and Description
private static final int
private static final int
private static final int
private static final int
private static final int
private static final int
private static final int
private static final int
private static final int
private static final int
public static final int
private static final int
TYPE_CODEPOINT

Represents a literal character; the value is stored in the code point field.

public static final int
TYPE_CURRENCY_DOUBLE

Represents a double currency symbol '¤¤'.

public static final int
TYPE_CURRENCY_OVERFLOW

Represents a sequence of six or more currency symbols.

public static final int
TYPE_CURRENCY_QUAD

Represents a quadruple currency symbol '¤¤¤¤'.

public static final int
TYPE_CURRENCY_QUINT

Represents a quintuple currency symbol '¤¤¤¤¤'.

public static final int
TYPE_CURRENCY_SINGLE

Represents a single currency symbol '¤'.

public static final int
TYPE_CURRENCY_TRIPLE

Represents a triple currency symbol '¤¤¤'.

public static final int
TYPE_MINUS_SIGN

Represents a minus sign symbol '-'.

public static final int
TYPE_PERCENT

Represents a percent sign symbol '%'.

public static final int
TYPE_PERMILLE

Represents a permille sign symbol '‰'.

public static final int
TYPE_PLUS_SIGN

Represents a plus sign symbol '+'.

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public static boolean
containsOnlySymbolsAndIgnorables(CharSequence affixPattern, UnicodeSet ignorables)

Returns whether the given affix pattern contains only symbols and ignorables as defined by the given ignorables set.

public static boolean

Returns:

true if the affix pattern contains the given token type; false otherwise.
containsType
(CharSequence
The affix pattern to check.
affixPattern
,
int
The token type.
type
)

Checks whether the given affix pattern contains at least one token of the given type, which is one of the constants "TYPE_" in AffixUtils.

public static int

Returns:

The number of chars (UTF-16 code units) appended to the output.
escape
(CharSequence
The string to be escaped.
input
,
StringBuilder
The string builder to which to append the escaped string.
output
)

Takes a string and escapes (quotes) characters that have special meaning in the affix pattern syntax.

public static String
escape(CharSequence input)

Version of escape that returns a String, or null if input is null.

public static int

Returns:

The length of the unescaped string.
estimateLength
(CharSequence
The original string whose width will be estimated.
patternString
)

Estimates the number of code points present in an unescaped version of the affix pattern string (one that would be returned by unescape), assuming that all interpolated symbols consume one code point and that currencies consume as many code points as their symbol width.

private static int
getCodePoint(long tag)

public static final NumberFormat.Field
getFieldForType(int type)

private static int
getOffset(long tag)

private static int
getState(long tag)

private static int
getType(long tag)

private static int

Returns:

If less than zero, a symbol type corresponding to one of the TYPE_ constants, such as TYPE_MINUS_SIGN. If greater than or equal to zero, a literal code point.
getTypeOrCp
(long
The bitmask tag of the current token, as returned by nextToken.
tag
)

This function helps determine the identity of the token consumed by nextToken.

public static boolean

Returns:

true if the literal has at least one unquoted currency symbol; false otherwise.
hasCurrencySymbols
(CharSequence
The string to check for currency symbols.
affixPattern
)

Checks whether the specified affix pattern has any unquoted currency symbols ("¤").

private static boolean

Returns:

true if there are more tokens to consume; false otherwise.
hasNext
(long
The bitmask tag of the previous token, as returned by nextToken.
tag
,
CharSequence
The affix pattern.
string
)

Returns whether the affix pattern string has any more tokens to be retrieved from a call to nextToken.

public static void
iterateWithConsumer(CharSequence affixPattern, AffixUtils.TokenConsumer consumer)

Iterates over the affix pattern, calling the TokenConsumer for each token.

private static long
makeTag(int offset, int type, int state, int cp)

Encodes the given values into a 64-bit tag.

private static long

Returns:

The bitmask tag to pass to the next call of this method to retrieve the following token (never negative), or -1 if there were no more tokens in the affix pattern.
nextToken
(long
A bitmask used for keeping track of state from token to token. The initial value should be 0L.
tag
,
CharSequence
The affix pattern.
patternString
)

Returns the next token from the affix pattern.

public static String

Returns:

A string containing the new affix pattern.
replaceType
(CharSequence
The source affix pattern (does not get modified).
affixPattern
,
int
The token type.
type
,
char
The char to substitute in place of chars of the given token type.
replacementChar
)

Replaces all occurrences of tokens with the given type with the given replacement char.

public static int

Returns:

The length of the string added to affixPattern.
unescape
(CharSequence
The original string to be unescaped.
affixPattern
,
FormattedStringBuilder
The FormattedStringBuilder to mutate with the result.
output
,
int
The index into the FormattedStringBuilder to insert the the string.
position
,
AffixUtils.SymbolProvider
An object to generate locale symbols.
provider
,
NumberFormat.Field field)

Executes the unescape state machine.

public static int

Returns:

The number of code points in the unescaped string.
unescapedCount
(CharSequence
The original string to be unescaped.
affixPattern
,
boolean
true to count length (UTF-16 code units); false to count code points
lengthOrCount
,
AffixUtils.SymbolProvider
An object to generate locale symbols.
provider
)

Sames as unescape, but only calculates the length or code point count.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

STATE_AFTER_QUOTEback to summary
private static final int STATE_AFTER_QUOTE
STATE_BASEback to summary
private static final int STATE_BASE
STATE_FIFTH_CURRback to summary
private static final int STATE_FIFTH_CURR
STATE_FIRST_CURRback to summary
private static final int STATE_FIRST_CURR
STATE_FIRST_QUOTEback to summary
private static final int STATE_FIRST_QUOTE
STATE_FOURTH_CURRback to summary
private static final int STATE_FOURTH_CURR
STATE_INSIDE_QUOTEback to summary
private static final int STATE_INSIDE_QUOTE
STATE_OVERFLOW_CURRback to summary
private static final int STATE_OVERFLOW_CURR
STATE_SECOND_CURRback to summary
private static final int STATE_SECOND_CURR
STATE_THIRD_CURRback to summary
private static final int STATE_THIRD_CURR
TYPE_APPROXIMATELY_SIGNback to summary
public static final int TYPE_APPROXIMATELY_SIGN
TYPE_CODEPOINTback to summary
private static final int TYPE_CODEPOINT

Represents a literal character; the value is stored in the code point field.

TYPE_CURRENCY_DOUBLEback to summary
public static final int TYPE_CURRENCY_DOUBLE

Represents a double currency symbol '¤¤'.

TYPE_CURRENCY_OVERFLOWback to summary
public static final int TYPE_CURRENCY_OVERFLOW

Represents a sequence of six or more currency symbols.

TYPE_CURRENCY_QUADback to summary
public static final int TYPE_CURRENCY_QUAD

Represents a quadruple currency symbol '¤¤¤¤'.

TYPE_CURRENCY_QUINTback to summary
public static final int TYPE_CURRENCY_QUINT

Represents a quintuple currency symbol '¤¤¤¤¤'.

TYPE_CURRENCY_SINGLEback to summary
public static final int TYPE_CURRENCY_SINGLE

Represents a single currency symbol '¤'.

TYPE_CURRENCY_TRIPLEback to summary
public static final int TYPE_CURRENCY_TRIPLE

Represents a triple currency symbol '¤¤¤'.

TYPE_MINUS_SIGNback to summary
public static final int TYPE_MINUS_SIGN

Represents a minus sign symbol '-'.

TYPE_PERCENTback to summary
public static final int TYPE_PERCENT

Represents a percent sign symbol '%'.

TYPE_PERMILLEback to summary
public static final int TYPE_PERMILLE

Represents a permille sign symbol '‰'.

TYPE_PLUS_SIGNback to summary
public static final int TYPE_PLUS_SIGN

Represents a plus sign symbol '+'.

Constructor Detail

AffixUtilsback to summary
public AffixUtils()

Method Detail

containsOnlySymbolsAndIgnorablesback to summary
public static boolean containsOnlySymbolsAndIgnorables(CharSequence affixPattern, UnicodeSet ignorables)

Returns whether the given affix pattern contains only symbols and ignorables as defined by the given ignorables set.

containsTypeback to summary
public static boolean containsType(CharSequence affixPattern, int type)

Checks whether the given affix pattern contains at least one token of the given type, which is one of the constants "TYPE_" in AffixUtils.

Parameters
affixPattern:CharSequence

The affix pattern to check.

type:int

The token type.

Returns:boolean

true if the affix pattern contains the given token type; false otherwise.

escapeback to summary
public static int escape(CharSequence input, StringBuilder output)

Takes a string and escapes (quotes) characters that have special meaning in the affix pattern syntax. This function does not reverse-lookup symbols.

Example input: "-$x"; example output: "'-'$x"

Parameters
input:CharSequence

The string to be escaped.

output:StringBuilder

The string builder to which to append the escaped string.

Returns:int

The number of chars (UTF-16 code units) appended to the output.

escapeback to summary
public static String escape(CharSequence input)

Version of escape that returns a String, or null if input is null.

estimateLengthback to summary
public static int estimateLength(CharSequence patternString)

Estimates the number of code points present in an unescaped version of the affix pattern string (one that would be returned by unescape), assuming that all interpolated symbols consume one code point and that currencies consume as many code points as their symbol width. Used for computing padding width.

Parameters
patternString:CharSequence

The original string whose width will be estimated.

Returns:int

The length of the unescaped string.

getCodePointback to summary
private static int getCodePoint(long tag)
getFieldForTypeback to summary
public static final NumberFormat.Field getFieldForType(int type)
getOffsetback to summary
private static int getOffset(long tag)
getStateback to summary
private static int getState(long tag)
getTypeback to summary
private static int getType(long tag)
getTypeOrCpback to summary
private static int getTypeOrCp(long tag)

This function helps determine the identity of the token consumed by nextToken. Converts from a bitmask tag, based on a call to nextToken, to its corresponding symbol type or code point.

Parameters
tag:long

The bitmask tag of the current token, as returned by nextToken.

Returns:int

If less than zero, a symbol type corresponding to one of the TYPE_ constants, such as TYPE_MINUS_SIGN. If greater than or equal to zero, a literal code point.

hasCurrencySymbolsback to summary
public static boolean hasCurrencySymbols(CharSequence affixPattern)

Checks whether the specified affix pattern has any unquoted currency symbols ("¤").

Parameters
affixPattern:CharSequence

The string to check for currency symbols.

Returns:boolean

true if the literal has at least one unquoted currency symbol; false otherwise.

hasNextback to summary
private static boolean hasNext(long tag, CharSequence string)

Returns whether the affix pattern string has any more tokens to be retrieved from a call to nextToken.

Parameters
tag:long

The bitmask tag of the previous token, as returned by nextToken.

string:CharSequence

The affix pattern.

Returns:boolean

true if there are more tokens to consume; false otherwise.

iterateWithConsumerback to summary
public static void iterateWithConsumer(CharSequence affixPattern, AffixUtils.TokenConsumer consumer)

Iterates over the affix pattern, calling the TokenConsumer for each token.

makeTagback to summary
private static long makeTag(int offset, int type, int state, int cp)

Encodes the given values into a 64-bit tag.

  • Bits 0-31 => offset (int32)
  • Bits 32-35 => type (uint4)
  • Bits 36-39 => state (uint4)
  • Bits 40-60 => code point (uint21)
  • Bits 61-63 => unused
nextTokenback to summary
private static long nextToken(long tag, CharSequence patternString)

Returns the next token from the affix pattern.

Parameters
tag:long

A bitmask used for keeping track of state from token to token. The initial value should be 0L.

patternString:CharSequence

The affix pattern.

Returns:long

The bitmask tag to pass to the next call of this method to retrieve the following token (never negative), or -1 if there were no more tokens in the affix pattern.

See Also
hasNext
replaceTypeback to summary
public static String replaceType(CharSequence affixPattern, int type, char replacementChar)

Replaces all occurrences of tokens with the given type with the given replacement char.

Parameters
affixPattern:CharSequence

The source affix pattern (does not get modified).

type:int

The token type.

replacementChar:char

The char to substitute in place of chars of the given token type.

Returns:String

A string containing the new affix pattern.

unescapeback to summary
public static int unescape(CharSequence affixPattern, FormattedStringBuilder output, int position, AffixUtils.SymbolProvider provider, NumberFormat.Field field)

Executes the unescape state machine. Replaces the unquoted characters "-", "+", "%", "‰", and "¤" with the corresponding symbols provided by the SymbolProvider, and inserts the result into the FormattedStringBuilder at the requested location.

Example input: "'-'¤x"; example output: "-$x"

Parameters
affixPattern:CharSequence

The original string to be unescaped.

output:FormattedStringBuilder

The FormattedStringBuilder to mutate with the result.

position:int

The index into the FormattedStringBuilder to insert the the string.

provider:AffixUtils.SymbolProvider

An object to generate locale symbols.

Returns:int

The length of the string added to affixPattern.

unescapedCountback to summary
public static int unescapedCount(CharSequence affixPattern, boolean lengthOrCount, AffixUtils.SymbolProvider provider)

Sames as unescape, but only calculates the length or code point count. More efficient than unescape if you only need the length but not the string itself.

Parameters
affixPattern:CharSequence

The original string to be unescaped.

lengthOrCount:boolean

true to count length (UTF-16 code units); false to count code points

provider:AffixUtils.SymbolProvider

An object to generate locale symbols.

Returns:int

The number of code points in the unescaped string.

com.ibm.icu.impl.number back to summary

public Interface AffixUtils.SymbolProvider

Known Direct Implementers
com.ibm.icu.impl.number.MutablePatternModifier

Method Summary

Modifier and TypeMethod and Description
public CharSequence
getSymbol(int type)

Method Detail

getSymbolback to summary
public CharSequence getSymbol(int type)
com.ibm.icu.impl.number back to summary

public Interface AffixUtils.TokenConsumer

Known Direct Implementers
com.ibm.icu.impl.number.parse.AffixPatternMatcher

Method Summary

Modifier and TypeMethod and Description
public void
consumeToken(int typeOrCp)

Method Detail

consumeTokenback to summary
public void consumeToken(int typeOrCp)