2017-01-20 00:20:31 +00:00
|
|
|
// © 2016 and later: Unicode, Inc. and others.
|
2016-06-15 18:58:17 +00:00
|
|
|
// License & terms of use: http://www.unicode.org/copyright.html
|
2009-06-16 05:56:50 +00:00
|
|
|
/*
|
|
|
|
*******************************************************************************
|
2016-05-31 21:45:07 +00:00
|
|
|
* Copyright (C) 2010-2015, International Business Machines Corporation and
|
|
|
|
* others. All Rights Reserved.
|
2009-06-16 05:56:50 +00:00
|
|
|
*******************************************************************************
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* File NUMSYS.CPP
|
|
|
|
*
|
|
|
|
* Modification History:*
|
|
|
|
* Date Name Description
|
|
|
|
*
|
|
|
|
********************************************************************************
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "unicode/utypes.h"
|
2009-11-14 00:36:06 +00:00
|
|
|
#include "unicode/localpointer.h"
|
2009-06-16 05:56:50 +00:00
|
|
|
#include "unicode/uchar.h"
|
|
|
|
#include "unicode/unistr.h"
|
|
|
|
#include "unicode/ures.h"
|
|
|
|
#include "unicode/ustring.h"
|
|
|
|
#include "unicode/uloc.h"
|
|
|
|
#include "unicode/schriter.h"
|
|
|
|
#include "unicode/numsys.h"
|
2010-06-04 18:29:02 +00:00
|
|
|
#include "cstring.h"
|
2017-09-28 01:00:43 +00:00
|
|
|
#include "uassert.h"
|
2009-06-16 05:56:50 +00:00
|
|
|
#include "uresimp.h"
|
2011-08-08 16:03:37 +00:00
|
|
|
#include "numsys_impl.h"
|
2009-06-16 05:56:50 +00:00
|
|
|
|
|
|
|
#if !UCONFIG_NO_FORMATTING
|
|
|
|
|
|
|
|
U_NAMESPACE_BEGIN
|
|
|
|
|
|
|
|
// Useful constants
|
|
|
|
|
|
|
|
#define DEFAULT_DIGITS UNICODE_STRING_SIMPLE("0123456789");
|
|
|
|
static const char gNumberingSystems[] = "numberingSystems";
|
2010-06-04 18:29:02 +00:00
|
|
|
static const char gNumberElements[] = "NumberElements";
|
|
|
|
static const char gDefault[] = "default";
|
2012-02-15 22:01:43 +00:00
|
|
|
static const char gNative[] = "native";
|
|
|
|
static const char gTraditional[] = "traditional";
|
|
|
|
static const char gFinance[] = "finance";
|
2009-06-16 05:56:50 +00:00
|
|
|
static const char gDesc[] = "desc";
|
|
|
|
static const char gRadix[] = "radix";
|
|
|
|
static const char gAlgorithmic[] = "algorithmic";
|
2010-06-04 18:29:02 +00:00
|
|
|
static const char gLatn[] = "latn";
|
2009-06-16 05:56:50 +00:00
|
|
|
|
|
|
|
|
|
|
|
UOBJECT_DEFINE_RTTI_IMPLEMENTATION(NumberingSystem)
|
2011-08-08 16:03:37 +00:00
|
|
|
UOBJECT_DEFINE_RTTI_IMPLEMENTATION(NumsysNameEnumeration)
|
2009-06-16 05:56:50 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Default Constructor.
|
|
|
|
*
|
|
|
|
* @draft ICU 4.2
|
|
|
|
*/
|
|
|
|
|
|
|
|
NumberingSystem::NumberingSystem() {
|
|
|
|
radix = 10;
|
|
|
|
algorithmic = FALSE;
|
|
|
|
UnicodeString defaultDigits = DEFAULT_DIGITS;
|
|
|
|
desc.setTo(defaultDigits);
|
2010-06-04 18:29:02 +00:00
|
|
|
uprv_strcpy(name,gLatn);
|
2009-06-16 05:56:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Copy constructor.
|
|
|
|
* @draft ICU 4.2
|
|
|
|
*/
|
|
|
|
|
|
|
|
NumberingSystem::NumberingSystem(const NumberingSystem& other)
|
|
|
|
: UObject(other) {
|
|
|
|
*this=other;
|
|
|
|
}
|
|
|
|
|
|
|
|
NumberingSystem* U_EXPORT2
|
|
|
|
NumberingSystem::createInstance(int32_t radix_in, UBool isAlgorithmic_in, const UnicodeString & desc_in, UErrorCode &status) {
|
|
|
|
|
2010-07-01 21:41:48 +00:00
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2009-06-16 05:56:50 +00:00
|
|
|
if ( radix_in < 2 ) {
|
|
|
|
status = U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( !isAlgorithmic_in ) {
|
2015-08-27 20:23:27 +00:00
|
|
|
if ( desc_in.countChar32() != radix_in ) {
|
2009-06-16 05:56:50 +00:00
|
|
|
status = U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NumberingSystem *ns = new NumberingSystem();
|
|
|
|
|
|
|
|
ns->setRadix(radix_in);
|
|
|
|
ns->setDesc(desc_in);
|
|
|
|
ns->setAlgorithmic(isAlgorithmic_in);
|
2010-06-04 18:29:02 +00:00
|
|
|
ns->setName(NULL);
|
2009-06-16 05:56:50 +00:00
|
|
|
return ns;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NumberingSystem* U_EXPORT2
|
|
|
|
NumberingSystem::createInstance(const Locale & inLocale, UErrorCode& status) {
|
|
|
|
|
2010-07-01 21:41:48 +00:00
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
return NULL;
|
2012-02-15 22:01:43 +00:00
|
|
|
}
|
2010-07-01 21:41:48 +00:00
|
|
|
|
2012-02-15 22:01:43 +00:00
|
|
|
UBool nsResolved = TRUE;
|
|
|
|
UBool usingFallback = FALSE;
|
2009-06-16 05:56:50 +00:00
|
|
|
char buffer[ULOC_KEYWORDS_CAPACITY];
|
|
|
|
int32_t count = inLocale.getKeywordValue("numbers",buffer, sizeof(buffer),status);
|
2017-09-28 01:00:43 +00:00
|
|
|
if (U_FAILURE(status) || status == U_STRING_NOT_TERMINATED_WARNING) {
|
|
|
|
// the "numbers" keyword exceeds ULOC_KEYWORDS_CAPACITY; ignore and use default.
|
|
|
|
count = 0;
|
|
|
|
status = U_ZERO_ERROR;
|
|
|
|
}
|
2009-06-16 05:56:50 +00:00
|
|
|
if ( count > 0 ) { // @numbers keyword was specified in the locale
|
2017-09-28 01:00:43 +00:00
|
|
|
U_ASSERT(count < ULOC_KEYWORDS_CAPACITY);
|
2009-06-16 05:56:50 +00:00
|
|
|
buffer[count] = '\0'; // Make sure it is null terminated.
|
2012-02-15 22:01:43 +00:00
|
|
|
if ( !uprv_strcmp(buffer,gDefault) || !uprv_strcmp(buffer,gNative) ||
|
|
|
|
!uprv_strcmp(buffer,gTraditional) || !uprv_strcmp(buffer,gFinance)) {
|
|
|
|
nsResolved = FALSE;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
uprv_strcpy(buffer,gDefault);
|
|
|
|
nsResolved = FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!nsResolved) { // Resolve the numbering system ( default, native, traditional or finance ) into a "real" numbering system
|
|
|
|
UErrorCode localStatus = U_ZERO_ERROR;
|
|
|
|
UResourceBundle *resource = ures_open(NULL, inLocale.getName(), &localStatus);
|
|
|
|
UResourceBundle *numberElementsRes = ures_getByKey(resource,gNumberElements,NULL,&localStatus);
|
|
|
|
while (!nsResolved) {
|
|
|
|
localStatus = U_ZERO_ERROR;
|
|
|
|
count = 0;
|
|
|
|
const UChar *nsName = ures_getStringByKeyWithFallback(numberElementsRes, buffer, &count, &localStatus);
|
|
|
|
if ( count > 0 && count < ULOC_KEYWORDS_CAPACITY ) { // numbering system found
|
|
|
|
u_UCharsToChars(nsName,buffer,count);
|
|
|
|
buffer[count] = '\0'; // Make sure it is null terminated.
|
|
|
|
nsResolved = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!nsResolved) { // Fallback behavior per TR35 - traditional falls back to native, finance and native fall back to default
|
|
|
|
if (!uprv_strcmp(buffer,gNative) || !uprv_strcmp(buffer,gFinance)) {
|
|
|
|
uprv_strcpy(buffer,gDefault);
|
|
|
|
} else if (!uprv_strcmp(buffer,gTraditional)) {
|
|
|
|
uprv_strcpy(buffer,gNative);
|
|
|
|
} else { // If we get here we couldn't find even the default numbering system
|
|
|
|
usingFallback = TRUE;
|
|
|
|
nsResolved = TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-06-04 18:29:02 +00:00
|
|
|
ures_close(numberElementsRes);
|
|
|
|
ures_close(resource);
|
2012-02-15 22:01:43 +00:00
|
|
|
}
|
2010-06-04 18:29:02 +00:00
|
|
|
|
2012-02-15 22:01:43 +00:00
|
|
|
if (usingFallback) {
|
|
|
|
status = U_USING_FALLBACK_WARNING;
|
|
|
|
NumberingSystem *ns = new NumberingSystem();
|
|
|
|
return ns;
|
|
|
|
} else {
|
|
|
|
return NumberingSystem::createInstanceByName(buffer,status);
|
2009-06-16 05:56:50 +00:00
|
|
|
}
|
2012-02-15 22:01:43 +00:00
|
|
|
}
|
2009-06-16 05:56:50 +00:00
|
|
|
|
|
|
|
NumberingSystem* U_EXPORT2
|
|
|
|
NumberingSystem::createInstance(UErrorCode& status) {
|
|
|
|
return NumberingSystem::createInstance(Locale::getDefault(), status);
|
|
|
|
}
|
|
|
|
|
|
|
|
NumberingSystem* U_EXPORT2
|
|
|
|
NumberingSystem::createInstanceByName(const char *name, UErrorCode& status) {
|
2011-07-07 18:46:19 +00:00
|
|
|
UResourceBundle *numberingSystemsInfo = NULL;
|
|
|
|
UResourceBundle *nsTop, *nsCurrent;
|
|
|
|
int32_t radix = 10;
|
|
|
|
int32_t algorithmic = 0;
|
|
|
|
|
|
|
|
numberingSystemsInfo = ures_openDirect(NULL,gNumberingSystems, &status);
|
|
|
|
nsCurrent = ures_getByKey(numberingSystemsInfo,gNumberingSystems,NULL,&status);
|
|
|
|
nsTop = ures_getByKey(nsCurrent,name,NULL,&status);
|
|
|
|
UnicodeString nsd = ures_getUnicodeStringByKey(nsTop,gDesc,&status);
|
|
|
|
|
|
|
|
ures_getByKey(nsTop,gRadix,nsCurrent,&status);
|
|
|
|
radix = ures_getInt(nsCurrent,&status);
|
|
|
|
|
|
|
|
ures_getByKey(nsTop,gAlgorithmic,nsCurrent,&status);
|
|
|
|
algorithmic = ures_getInt(nsCurrent,&status);
|
|
|
|
|
|
|
|
UBool isAlgorithmic = ( algorithmic == 1 );
|
|
|
|
|
|
|
|
ures_close(nsCurrent);
|
|
|
|
ures_close(nsTop);
|
|
|
|
ures_close(numberingSystemsInfo);
|
|
|
|
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
status = U_UNSUPPORTED_ERROR;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
NumberingSystem* ns = NumberingSystem::createInstance(radix,isAlgorithmic,nsd,status);
|
|
|
|
ns->setName(name);
|
|
|
|
return ns;
|
2009-06-16 05:56:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Destructor.
|
|
|
|
* @draft ICU 4.2
|
|
|
|
*/
|
|
|
|
NumberingSystem::~NumberingSystem() {
|
|
|
|
}
|
|
|
|
|
2013-09-12 04:33:03 +00:00
|
|
|
int32_t NumberingSystem::getRadix() const {
|
2009-06-16 05:56:50 +00:00
|
|
|
return radix;
|
|
|
|
}
|
|
|
|
|
2013-09-12 04:33:03 +00:00
|
|
|
UnicodeString NumberingSystem::getDescription() const {
|
2009-06-16 05:56:50 +00:00
|
|
|
return desc;
|
|
|
|
}
|
|
|
|
|
2013-09-12 04:33:03 +00:00
|
|
|
const char * NumberingSystem::getName() const {
|
2010-06-04 18:29:02 +00:00
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
2009-06-16 05:56:50 +00:00
|
|
|
void NumberingSystem::setRadix(int32_t r) {
|
|
|
|
radix = r;
|
|
|
|
}
|
|
|
|
|
|
|
|
void NumberingSystem::setAlgorithmic(UBool c) {
|
|
|
|
algorithmic = c;
|
|
|
|
}
|
|
|
|
|
2016-12-14 18:41:04 +00:00
|
|
|
void NumberingSystem::setDesc(const UnicodeString &d) {
|
2009-06-16 05:56:50 +00:00
|
|
|
desc.setTo(d);
|
|
|
|
}
|
2010-06-04 18:29:02 +00:00
|
|
|
void NumberingSystem::setName(const char *n) {
|
|
|
|
if ( n == NULL ) {
|
|
|
|
name[0] = (char) 0;
|
|
|
|
} else {
|
|
|
|
uprv_strncpy(name,n,NUMSYS_NAME_CAPACITY);
|
|
|
|
name[NUMSYS_NAME_CAPACITY] = (char)0; // Make sure it is null terminated.
|
|
|
|
}
|
|
|
|
}
|
2009-06-16 05:56:50 +00:00
|
|
|
UBool NumberingSystem::isAlgorithmic() const {
|
|
|
|
return ( algorithmic );
|
|
|
|
}
|
|
|
|
|
2011-08-08 16:03:37 +00:00
|
|
|
StringEnumeration* NumberingSystem::getAvailableNames(UErrorCode &status) {
|
2015-09-15 18:03:08 +00:00
|
|
|
// TODO(ticket #11908): Init-once static cache, with u_cleanup() callback.
|
2011-08-08 16:03:37 +00:00
|
|
|
static StringEnumeration* availableNames = NULL;
|
|
|
|
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( availableNames == NULL ) {
|
2015-09-15 18:03:08 +00:00
|
|
|
// TODO: Simple array of UnicodeString objects, based on length of table resource?
|
2015-09-10 22:49:03 +00:00
|
|
|
LocalPointer<UVector> numsysNames(new UVector(uprv_deleteUObject, NULL, status), status);
|
2011-08-08 16:03:37 +00:00
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
UErrorCode rbstatus = U_ZERO_ERROR;
|
|
|
|
UResourceBundle *numberingSystemsInfo = ures_openDirect(NULL, "numberingSystems", &rbstatus);
|
|
|
|
numberingSystemsInfo = ures_getByKey(numberingSystemsInfo,"numberingSystems",numberingSystemsInfo,&rbstatus);
|
|
|
|
if(U_FAILURE(rbstatus)) {
|
|
|
|
status = U_MISSING_RESOURCE_ERROR;
|
|
|
|
ures_close(numberingSystemsInfo);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
while ( ures_hasNext(numberingSystemsInfo) ) {
|
|
|
|
UResourceBundle *nsCurrent = ures_getNextResource(numberingSystemsInfo,NULL,&rbstatus);
|
|
|
|
const char *nsName = ures_getKey(nsCurrent);
|
2015-09-10 22:49:03 +00:00
|
|
|
numsysNames->addElement(new UnicodeString(nsName, -1, US_INV),status);
|
2011-08-08 16:03:37 +00:00
|
|
|
ures_close(nsCurrent);
|
|
|
|
}
|
|
|
|
|
|
|
|
ures_close(numberingSystemsInfo);
|
2015-09-15 18:03:08 +00:00
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
availableNames = new NumsysNameEnumeration(numsysNames.getAlias(), status);
|
|
|
|
if (availableNames == NULL) {
|
|
|
|
status = U_MEMORY_ALLOCATION_ERROR;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
numsysNames.orphan(); // The names got adopted.
|
2011-08-08 16:03:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return availableNames;
|
|
|
|
}
|
2009-06-16 05:56:50 +00:00
|
|
|
|
2015-09-10 22:49:03 +00:00
|
|
|
NumsysNameEnumeration::NumsysNameEnumeration(UVector *numsysNames, UErrorCode& /*status*/) {
|
2011-08-08 16:03:37 +00:00
|
|
|
pos=0;
|
2015-09-10 22:49:03 +00:00
|
|
|
fNumsysNames = numsysNames;
|
2011-08-08 16:03:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const UnicodeString*
|
|
|
|
NumsysNameEnumeration::snext(UErrorCode& status) {
|
|
|
|
if (U_SUCCESS(status) && pos < fNumsysNames->size()) {
|
|
|
|
return (const UnicodeString*)fNumsysNames->elementAt(pos++);
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
NumsysNameEnumeration::reset(UErrorCode& /*status*/) {
|
|
|
|
pos=0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t
|
|
|
|
NumsysNameEnumeration::count(UErrorCode& /*status*/) const {
|
|
|
|
return (fNumsysNames==NULL) ? 0 : fNumsysNames->size();
|
|
|
|
}
|
|
|
|
|
|
|
|
NumsysNameEnumeration::~NumsysNameEnumeration() {
|
|
|
|
delete fNumsysNames;
|
|
|
|
}
|
2009-06-16 05:56:50 +00:00
|
|
|
U_NAMESPACE_END
|
|
|
|
|
|
|
|
#endif /* #if !UCONFIG_NO_FORMATTING */
|
|
|
|
|
|
|
|
//eof
|