tags:

views:

28

answers:

2

Is there a way to probe the ICU library for all UChar's representing currency symbols supported by the library?

My current solution is iterating through all locales and for each locale, doing something like this:

const DecimalFormatSymbols *formatSymbols = formatter->getDecimalFormatSymbols();
UnicodeString currencySymbol = formatSymbols->getSymbol(DecimalFormatSymbols::kCurrencySymbol);

Then saving off each UChar in currencySymbol into a map (so no duplicates).

+2  A: 

All currency symbols have the category Sc (Symbol, Currency), so you can just enumerate all characters from that category.

#include <cstdio>
#include <icu/unicode/uchar.h>

UBool print_all_currency_symbols(const void* context, UChar32 start, UChar32 limit, UCharCategory type) {
    if (type == U_CURRENCY_SYMBOL) {
        for (UChar32 c = start; c < limit; ++ c)
            printf("%04x\n", c);
    }
    return TRUE;
}

int main() {
    u_enumCharTypes(print_all_currency_symbols, NULL);

        return 0;
}
KennyTM
Can you elaborate a little more? I'm not familiar with "category" as it applies to UnicodeStrings or the Locales.
Travis
@Travis: See update.
KennyTM
This looks great. I had no idea helper functions like the u_enumCharTypes() existed. I'll play with this. Thank you.
Travis
That API is an interesting one. You can use it to set something in your 'context' variable, and so use it as a callback. You could collect these UChar32s into a UnicodeSet for quick membership testing. Also, this solution will get the Unicode 'currency symbol' chars, but will NOT get the chars actually used for currency symbols, such as "$" or "Rs." etc.
Steven R. Loomis
@Steven: The `$` character *is* in the [Sc category](http://www.fileformat.info/info/unicode/category/Sc/list.htm). You are right about `Rs.` though as it is not a single character.
KennyTM
@KennyTM - you are right about $, I stand corrected. Not "Lm", "SFr", "N$" (the N part) or "USD" though, as well.
Steven R. Loomis
If I'm using this handler to populate a UnicodeSet of all currency symbols (instead of just printing them). How do I know when the enumeration is complete?
Travis
@Travis: It completes when the `u_enumCharTypes` function completes.
KennyTM
@KennyTM thank you! I didn't realize u_enumCharTypes blocked until it was done. Thank you very much
Travis
A: 

y3qeknctoh peclyqlnkacn mapka3nn qahkn

premnath