2004-05-08 08:48:01 +00:00
|
|
|
/*
|
|
|
|
*******************************************************************************
|
2014-02-25 21:21:49 +00:00
|
|
|
* Copyright (C) 2004-2014, International Business Machines
|
2004-05-08 08:48:01 +00:00
|
|
|
* Corporation and others. All Rights Reserved.
|
|
|
|
*******************************************************************************
|
|
|
|
* file name: ucol_sit.cpp
|
|
|
|
* encoding: US-ASCII
|
|
|
|
* tab size: 8 (not used)
|
|
|
|
* indentation:4
|
|
|
|
*
|
|
|
|
* Modification history
|
|
|
|
* Date Name Comments
|
|
|
|
* 03/12/2004 weiv Creation
|
|
|
|
*/
|
|
|
|
|
2005-06-21 20:46:04 +00:00
|
|
|
#include "unicode/ustring.h"
|
2008-01-05 01:27:56 +00:00
|
|
|
#include "unicode/udata.h"
|
2014-02-25 21:21:49 +00:00
|
|
|
#include "unicode/utf16.h"
|
2004-05-08 08:48:01 +00:00
|
|
|
#include "utracimp.h"
|
|
|
|
#include "ucol_imp.h"
|
2004-05-13 09:02:33 +00:00
|
|
|
#include "cmemory.h"
|
|
|
|
#include "cstring.h"
|
2008-01-05 01:27:56 +00:00
|
|
|
#include "uresimp.h"
|
2012-02-18 23:10:51 +00:00
|
|
|
#include "unicode/coll.h"
|
|
|
|
|
|
|
|
#ifdef UCOL_TRACE_SIT
|
|
|
|
# include <stdio.h>
|
|
|
|
#endif
|
2004-05-08 08:48:01 +00:00
|
|
|
|
2004-06-10 23:49:07 +00:00
|
|
|
#if !UCONFIG_NO_COLLATION
|
2004-05-08 08:48:01 +00:00
|
|
|
|
2014-02-25 21:21:49 +00:00
|
|
|
#include "unicode/tblcoll.h"
|
|
|
|
|
2004-05-13 09:02:33 +00:00
|
|
|
enum OptionsList {
|
|
|
|
UCOL_SIT_LANGUAGE = 0,
|
2012-02-18 23:10:51 +00:00
|
|
|
UCOL_SIT_SCRIPT = 1,
|
|
|
|
UCOL_SIT_REGION = 2,
|
|
|
|
UCOL_SIT_VARIANT = 3,
|
|
|
|
UCOL_SIT_KEYWORD = 4,
|
|
|
|
UCOL_SIT_PROVIDER = 5,
|
|
|
|
UCOL_SIT_LOCELEMENT_MAX = UCOL_SIT_PROVIDER, /* the last element that's part of LocElements */
|
|
|
|
|
2008-01-05 01:27:56 +00:00
|
|
|
UCOL_SIT_BCP47,
|
2004-05-13 09:02:33 +00:00
|
|
|
UCOL_SIT_STRENGTH,
|
|
|
|
UCOL_SIT_CASE_LEVEL,
|
|
|
|
UCOL_SIT_CASE_FIRST,
|
|
|
|
UCOL_SIT_NUMERIC_COLLATION,
|
|
|
|
UCOL_SIT_ALTERNATE_HANDLING,
|
|
|
|
UCOL_SIT_NORMALIZATION_MODE,
|
|
|
|
UCOL_SIT_FRENCH_COLLATION,
|
|
|
|
UCOL_SIT_HIRAGANA_QUATERNARY,
|
|
|
|
UCOL_SIT_VARIABLE_TOP,
|
2004-05-20 21:03:31 +00:00
|
|
|
UCOL_SIT_VARIABLE_TOP_VALUE,
|
2004-05-13 09:02:33 +00:00
|
|
|
UCOL_SIT_ITEMS_COUNT
|
|
|
|
};
|
|
|
|
|
2004-05-20 21:03:31 +00:00
|
|
|
/* option starters chars. */
|
|
|
|
static const char alternateHArg = 'A';
|
|
|
|
static const char variableTopValArg = 'B';
|
|
|
|
static const char caseFirstArg = 'C';
|
|
|
|
static const char numericCollArg = 'D';
|
|
|
|
static const char caseLevelArg = 'E';
|
|
|
|
static const char frenchCollArg = 'F';
|
|
|
|
static const char hiraganaQArg = 'H';
|
|
|
|
static const char keywordArg = 'K';
|
|
|
|
static const char languageArg = 'L';
|
|
|
|
static const char normArg = 'N';
|
2012-02-18 23:10:51 +00:00
|
|
|
static const char providerArg = 'P';
|
2004-05-20 21:03:31 +00:00
|
|
|
static const char regionArg = 'R';
|
|
|
|
static const char strengthArg = 'S';
|
|
|
|
static const char variableTopArg = 'T';
|
|
|
|
static const char variantArg = 'V';
|
|
|
|
static const char RFC3066Arg = 'X';
|
|
|
|
static const char scriptArg = 'Z';
|
|
|
|
|
2004-06-16 21:17:33 +00:00
|
|
|
static const char collationKeyword[] = "@collation=";
|
2012-02-18 23:10:51 +00:00
|
|
|
static const char providerKeyword[] = "@sp=";
|
|
|
|
|
2004-05-24 22:11:31 +00:00
|
|
|
|
2012-02-18 23:10:51 +00:00
|
|
|
static const int32_t locElementCount = UCOL_SIT_LOCELEMENT_MAX+1;
|
2004-05-20 21:03:31 +00:00
|
|
|
static const int32_t locElementCapacity = 32;
|
|
|
|
static const int32_t loc3066Capacity = 256;
|
2012-02-18 23:10:51 +00:00
|
|
|
static const int32_t locProviderCapacity = 10;
|
2004-05-20 21:03:31 +00:00
|
|
|
static const int32_t internalBufferSize = 512;
|
|
|
|
|
|
|
|
/* structure containing specification of a collator. Initialized
|
|
|
|
* from a short string. Also used to construct a short string from a
|
|
|
|
* collator instance
|
|
|
|
*/
|
2004-05-13 09:02:33 +00:00
|
|
|
struct CollatorSpec {
|
2004-05-20 21:03:31 +00:00
|
|
|
char locElements[locElementCount][locElementCapacity];
|
|
|
|
char locale[loc3066Capacity];
|
2012-02-18 23:10:51 +00:00
|
|
|
char provider[locProviderCapacity];
|
2004-05-13 09:02:33 +00:00
|
|
|
UColAttributeValue options[UCOL_ATTRIBUTE_COUNT];
|
2004-05-13 20:45:53 +00:00
|
|
|
uint32_t variableTopValue;
|
2004-05-20 21:03:31 +00:00
|
|
|
UChar variableTopString[locElementCapacity];
|
2004-06-14 21:00:06 +00:00
|
|
|
int32_t variableTopStringLen;
|
2004-05-20 21:03:31 +00:00
|
|
|
UBool variableTopSet;
|
|
|
|
struct {
|
|
|
|
const char *start;
|
|
|
|
int32_t len;
|
|
|
|
} entries[UCOL_SIT_ITEMS_COUNT];
|
2004-05-13 09:02:33 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2004-05-20 21:03:31 +00:00
|
|
|
/* structure for converting between character attribute
|
|
|
|
* representation and real collation attribute value.
|
|
|
|
*/
|
2004-05-13 09:02:33 +00:00
|
|
|
struct AttributeConversion {
|
|
|
|
char letter;
|
|
|
|
UColAttributeValue value;
|
|
|
|
};
|
|
|
|
|
2004-05-21 20:48:36 +00:00
|
|
|
static const AttributeConversion conversions[12] = {
|
2004-05-13 09:02:33 +00:00
|
|
|
{ '1', UCOL_PRIMARY },
|
|
|
|
{ '2', UCOL_SECONDARY },
|
|
|
|
{ '3', UCOL_TERTIARY },
|
|
|
|
{ '4', UCOL_QUATERNARY },
|
|
|
|
{ 'D', UCOL_DEFAULT },
|
|
|
|
{ 'I', UCOL_IDENTICAL },
|
|
|
|
{ 'L', UCOL_LOWER_FIRST },
|
|
|
|
{ 'N', UCOL_NON_IGNORABLE },
|
|
|
|
{ 'O', UCOL_ON },
|
|
|
|
{ 'S', UCOL_SHIFTED },
|
|
|
|
{ 'U', UCOL_UPPER_FIRST },
|
|
|
|
{ 'X', UCOL_OFF }
|
2004-05-08 08:48:01 +00:00
|
|
|
};
|
|
|
|
|
2004-05-24 22:11:31 +00:00
|
|
|
|
2009-04-22 23:10:07 +00:00
|
|
|
static UColAttributeValue
|
2004-05-24 22:11:31 +00:00
|
|
|
ucol_sit_letterToAttributeValue(char letter, UErrorCode *status) {
|
2004-06-03 20:07:39 +00:00
|
|
|
uint32_t i = 0;
|
2004-05-24 22:11:31 +00:00
|
|
|
for(i = 0; i < sizeof(conversions)/sizeof(conversions[0]); i++) {
|
|
|
|
if(conversions[i].letter == letter) {
|
|
|
|
return conversions[i].value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*status = U_ILLEGAL_ARGUMENT_ERROR;
|
2012-02-18 23:10:51 +00:00
|
|
|
#ifdef UCOL_TRACE_SIT
|
|
|
|
fprintf(stderr, "%s:%d: unknown letter %c: %s\n", __FILE__, __LINE__, letter, u_errorName(*status));
|
|
|
|
#endif
|
2004-05-24 22:11:31 +00:00
|
|
|
return UCOL_DEFAULT;
|
|
|
|
}
|
|
|
|
|
2004-05-20 21:03:31 +00:00
|
|
|
/* function prototype for functions used to parse a short string */
|
|
|
|
U_CDECL_BEGIN
|
|
|
|
typedef const char* U_CALLCONV
|
|
|
|
ActionFunction(CollatorSpec *spec, uint32_t value1, const char* string,
|
|
|
|
UErrorCode *status);
|
|
|
|
U_CDECL_END
|
2004-05-13 09:02:33 +00:00
|
|
|
|
|
|
|
U_CDECL_BEGIN
|
|
|
|
static const char* U_CALLCONV
|
2009-04-22 23:10:07 +00:00
|
|
|
_processLocaleElement(CollatorSpec *spec, uint32_t value, const char* string,
|
|
|
|
UErrorCode *status)
|
2004-05-20 21:03:31 +00:00
|
|
|
{
|
2004-05-13 09:02:33 +00:00
|
|
|
int32_t len = 0;
|
|
|
|
do {
|
2012-02-18 23:10:51 +00:00
|
|
|
if(value == UCOL_SIT_LANGUAGE || value == UCOL_SIT_KEYWORD || value == UCOL_SIT_PROVIDER) {
|
2004-05-20 21:03:31 +00:00
|
|
|
spec->locElements[value][len++] = uprv_tolower(*string);
|
|
|
|
} else {
|
|
|
|
spec->locElements[value][len++] = *string;
|
|
|
|
}
|
|
|
|
} while(*(++string) != '_' && *string && len < locElementCapacity);
|
|
|
|
if(len >= locElementCapacity) {
|
2004-05-13 09:02:33 +00:00
|
|
|
*status = U_BUFFER_OVERFLOW_ERROR;
|
|
|
|
return string;
|
|
|
|
}
|
2004-05-25 23:00:52 +00:00
|
|
|
// don't skip the underscore at the end
|
|
|
|
return string;
|
2004-05-13 09:02:33 +00:00
|
|
|
}
|
2004-05-13 20:45:53 +00:00
|
|
|
U_CDECL_END
|
2004-05-13 09:02:33 +00:00
|
|
|
|
2004-05-13 20:45:53 +00:00
|
|
|
U_CDECL_BEGIN
|
2004-05-13 09:02:33 +00:00
|
|
|
static const char* U_CALLCONV
|
2009-04-22 23:10:07 +00:00
|
|
|
_processRFC3066Locale(CollatorSpec *spec, uint32_t, const char* string,
|
|
|
|
UErrorCode *status)
|
2004-05-20 21:03:31 +00:00
|
|
|
{
|
|
|
|
char terminator = *string;
|
|
|
|
string++;
|
|
|
|
const char *end = uprv_strchr(string+1, terminator);
|
2004-06-14 21:00:06 +00:00
|
|
|
if(end == NULL || end - string >= loc3066Capacity) {
|
2004-05-20 21:03:31 +00:00
|
|
|
*status = U_BUFFER_OVERFLOW_ERROR;
|
|
|
|
return string;
|
|
|
|
} else {
|
|
|
|
uprv_strncpy(spec->locale, string, end-string);
|
2004-05-25 23:00:52 +00:00
|
|
|
return end+1;
|
2004-05-20 21:03:31 +00:00
|
|
|
}
|
2004-05-13 09:02:33 +00:00
|
|
|
}
|
2004-05-20 21:03:31 +00:00
|
|
|
|
2004-05-13 20:45:53 +00:00
|
|
|
U_CDECL_END
|
2004-05-13 09:02:33 +00:00
|
|
|
|
2004-05-13 20:45:53 +00:00
|
|
|
U_CDECL_BEGIN
|
2004-05-13 09:02:33 +00:00
|
|
|
static const char* U_CALLCONV
|
2009-04-22 23:10:07 +00:00
|
|
|
_processCollatorOption(CollatorSpec *spec, uint32_t option, const char* string,
|
|
|
|
UErrorCode *status)
|
2004-05-20 21:03:31 +00:00
|
|
|
{
|
2004-05-24 22:11:31 +00:00
|
|
|
spec->options[option] = ucol_sit_letterToAttributeValue(*string, status);
|
|
|
|
if((*(++string) != '_' && *string) || U_FAILURE(*status)) {
|
2012-02-18 23:10:51 +00:00
|
|
|
#ifdef UCOL_TRACE_SIT
|
|
|
|
fprintf(stderr, "%s:%d: unknown collator option at '%s': %s\n", __FILE__, __LINE__, string, u_errorName(*status));
|
|
|
|
#endif
|
2004-05-24 22:11:31 +00:00
|
|
|
*status = U_ILLEGAL_ARGUMENT_ERROR;
|
2004-05-13 09:02:33 +00:00
|
|
|
}
|
2004-05-25 23:00:52 +00:00
|
|
|
return string;
|
2004-05-13 09:02:33 +00:00
|
|
|
}
|
2004-05-13 20:45:53 +00:00
|
|
|
U_CDECL_END
|
|
|
|
|
2004-05-13 09:02:33 +00:00
|
|
|
|
2009-04-22 23:10:07 +00:00
|
|
|
static UChar
|
|
|
|
readHexCodeUnit(const char **string, UErrorCode *status)
|
2004-05-20 21:03:31 +00:00
|
|
|
{
|
2004-05-13 20:45:53 +00:00
|
|
|
UChar result = 0;
|
|
|
|
int32_t value = 0;
|
|
|
|
char c;
|
|
|
|
int32_t noDigits = 0;
|
|
|
|
while((c = **string) != 0 && noDigits < 4) {
|
|
|
|
if( c >= '0' && c <= '9') {
|
|
|
|
value = c - '0';
|
|
|
|
} else if ( c >= 'a' && c <= 'f') {
|
|
|
|
value = c - 'a' + 10;
|
|
|
|
} else if ( c >= 'A' && c <= 'F') {
|
|
|
|
value = c - 'A' + 10;
|
|
|
|
} else {
|
|
|
|
*status = U_ILLEGAL_ARGUMENT_ERROR;
|
2012-02-18 23:10:51 +00:00
|
|
|
#ifdef UCOL_TRACE_SIT
|
|
|
|
fprintf(stderr, "%s:%d: Bad hex char at '%s': %s\n", __FILE__, __LINE__, *string, u_errorName(*status));
|
|
|
|
#endif
|
2004-05-13 20:45:53 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2004-06-02 22:19:52 +00:00
|
|
|
result = (result << 4) | (UChar)value;
|
2004-05-20 21:03:31 +00:00
|
|
|
noDigits++;
|
2004-05-13 20:45:53 +00:00
|
|
|
(*string)++;
|
|
|
|
}
|
2004-06-14 21:00:06 +00:00
|
|
|
// if the string was terminated before we read 4 digits, set an error
|
|
|
|
if(noDigits < 4) {
|
|
|
|
*status = U_ILLEGAL_ARGUMENT_ERROR;
|
2012-02-18 23:10:51 +00:00
|
|
|
#ifdef UCOL_TRACE_SIT
|
|
|
|
fprintf(stderr, "%s:%d: Short (only %d digits, wanted 4) at '%s': %s\n", __FILE__, __LINE__, noDigits,*string, u_errorName(*status));
|
|
|
|
#endif
|
2004-06-14 21:00:06 +00:00
|
|
|
}
|
2004-05-20 21:03:31 +00:00
|
|
|
return result;
|
2004-05-13 20:45:53 +00:00
|
|
|
}
|
2004-05-13 09:02:33 +00:00
|
|
|
|
2004-05-13 20:45:53 +00:00
|
|
|
U_CDECL_BEGIN
|
2004-05-13 09:02:33 +00:00
|
|
|
static const char* U_CALLCONV
|
2009-04-22 23:10:07 +00:00
|
|
|
_processVariableTop(CollatorSpec *spec, uint32_t value1, const char* string, UErrorCode *status)
|
2004-05-20 21:03:31 +00:00
|
|
|
{
|
2004-05-13 20:45:53 +00:00
|
|
|
// get four digits
|
2004-05-20 21:03:31 +00:00
|
|
|
int32_t i = 0;
|
|
|
|
if(!value1) {
|
2004-06-14 21:00:06 +00:00
|
|
|
while(U_SUCCESS(*status) && i < locElementCapacity && *string != 0 && *string != '_') {
|
2004-05-20 21:03:31 +00:00
|
|
|
spec->variableTopString[i++] = readHexCodeUnit(&string, status);
|
|
|
|
}
|
2004-06-14 21:00:06 +00:00
|
|
|
spec->variableTopStringLen = i;
|
2011-06-02 22:08:21 +00:00
|
|
|
if(i == locElementCapacity && *string != 0 && *string != '_') {
|
2004-06-14 21:00:06 +00:00
|
|
|
*status = U_BUFFER_OVERFLOW_ERROR;
|
|
|
|
}
|
2004-05-20 21:03:31 +00:00
|
|
|
} else {
|
|
|
|
spec->variableTopValue = readHexCodeUnit(&string, status);
|
|
|
|
}
|
|
|
|
if(U_SUCCESS(*status)) {
|
|
|
|
spec->variableTopSet = TRUE;
|
2009-04-22 23:10:07 +00:00
|
|
|
}
|
2004-05-25 23:00:52 +00:00
|
|
|
return string;
|
2004-05-13 09:02:33 +00:00
|
|
|
}
|
|
|
|
U_CDECL_END
|
|
|
|
|
2004-05-20 21:03:31 +00:00
|
|
|
|
|
|
|
/* Table for parsing short strings */
|
|
|
|
struct ShortStringOptions {
|
|
|
|
char optionStart;
|
|
|
|
ActionFunction *action;
|
|
|
|
uint32_t attr;
|
|
|
|
};
|
|
|
|
|
2004-05-21 20:48:36 +00:00
|
|
|
static const ShortStringOptions options[UCOL_SIT_ITEMS_COUNT] =
|
2004-05-13 09:02:33 +00:00
|
|
|
{
|
2009-04-22 23:10:07 +00:00
|
|
|
/* 10 ALTERNATE_HANDLING */ {alternateHArg, _processCollatorOption, UCOL_ALTERNATE_HANDLING }, // alternate N, S, D
|
2004-05-20 21:03:31 +00:00
|
|
|
/* 15 VARIABLE_TOP_VALUE */ {variableTopValArg, _processVariableTop, 1 },
|
|
|
|
/* 08 CASE_FIRST */ {caseFirstArg, _processCollatorOption, UCOL_CASE_FIRST }, // case first L, U, X, D
|
|
|
|
/* 09 NUMERIC_COLLATION */ {numericCollArg, _processCollatorOption, UCOL_NUMERIC_COLLATION }, // codan O, X, D
|
|
|
|
/* 07 CASE_LEVEL */ {caseLevelArg, _processCollatorOption, UCOL_CASE_LEVEL }, // case level O, X, D
|
|
|
|
/* 12 FRENCH_COLLATION */ {frenchCollArg, _processCollatorOption, UCOL_FRENCH_COLLATION }, // french O, X, D
|
|
|
|
/* 13 HIRAGANA_QUATERNARY] */ {hiraganaQArg, _processCollatorOption, UCOL_HIRAGANA_QUATERNARY_MODE }, // hiragana O, X, D
|
2012-02-18 23:10:51 +00:00
|
|
|
/* 04 KEYWORD */ {keywordArg, _processLocaleElement, UCOL_SIT_KEYWORD }, // keyword
|
|
|
|
/* 00 LANGUAGE */ {languageArg, _processLocaleElement, UCOL_SIT_LANGUAGE }, // language
|
2004-05-20 21:03:31 +00:00
|
|
|
/* 11 NORMALIZATION_MODE */ {normArg, _processCollatorOption, UCOL_NORMALIZATION_MODE }, // norm O, X, D
|
2012-02-18 23:10:51 +00:00
|
|
|
/* 02 REGION */ {regionArg, _processLocaleElement, UCOL_SIT_REGION }, // region
|
2004-05-20 21:03:31 +00:00
|
|
|
/* 06 STRENGTH */ {strengthArg, _processCollatorOption, UCOL_STRENGTH }, // strength 1, 2, 3, 4, I, D
|
|
|
|
/* 14 VARIABLE_TOP */ {variableTopArg, _processVariableTop, 0 },
|
2012-02-18 23:10:51 +00:00
|
|
|
/* 03 VARIANT */ {variantArg, _processLocaleElement, UCOL_SIT_VARIANT }, // variant
|
2004-05-20 21:03:31 +00:00
|
|
|
/* 05 RFC3066BIS */ {RFC3066Arg, _processRFC3066Locale, 0 }, // rfc3066bis locale name
|
2012-02-18 23:10:51 +00:00
|
|
|
/* 01 SCRIPT */ {scriptArg, _processLocaleElement, UCOL_SIT_SCRIPT }, // script
|
|
|
|
/* PROVIDER */ {providerArg, _processLocaleElement, UCOL_SIT_PROVIDER }
|
2004-05-08 08:48:01 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2004-05-13 09:02:33 +00:00
|
|
|
static
|
2009-04-22 23:10:07 +00:00
|
|
|
const char* ucol_sit_readOption(const char *start, CollatorSpec *spec,
|
|
|
|
UErrorCode *status)
|
2004-05-20 21:03:31 +00:00
|
|
|
{
|
2004-06-02 22:19:52 +00:00
|
|
|
int32_t i = 0;
|
2004-05-13 09:02:33 +00:00
|
|
|
|
|
|
|
for(i = 0; i < UCOL_SIT_ITEMS_COUNT; i++) {
|
|
|
|
if(*start == options[i].optionStart) {
|
2004-05-20 21:03:31 +00:00
|
|
|
spec->entries[i].start = start;
|
|
|
|
const char* end = options[i].action(spec, options[i].attr, start+1, status);
|
2009-11-11 15:47:22 +00:00
|
|
|
spec->entries[i].len = (int32_t)(end - start);
|
2004-05-20 21:03:31 +00:00
|
|
|
return end;
|
2004-05-13 09:02:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
*status = U_ILLEGAL_ARGUMENT_ERROR;
|
2012-02-18 23:10:51 +00:00
|
|
|
#ifdef UCOL_TRACE_SIT
|
|
|
|
fprintf(stderr, "%s:%d: Unknown option at '%s': %s\n", __FILE__, __LINE__, start, u_errorName(*status));
|
|
|
|
#endif
|
2004-05-13 09:02:33 +00:00
|
|
|
return start;
|
|
|
|
}
|
|
|
|
|
|
|
|
static
|
2009-04-22 23:10:07 +00:00
|
|
|
void ucol_sit_initCollatorSpecs(CollatorSpec *spec)
|
2004-05-20 21:03:31 +00:00
|
|
|
{
|
2004-05-13 09:02:33 +00:00
|
|
|
// reset everything
|
|
|
|
uprv_memset(spec, 0, sizeof(CollatorSpec));
|
|
|
|
// set collation options to default
|
|
|
|
int32_t i = 0;
|
|
|
|
for(i = 0; i < UCOL_ATTRIBUTE_COUNT; i++) {
|
|
|
|
spec->options[i] = UCOL_DEFAULT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-04-22 23:10:07 +00:00
|
|
|
static const char*
|
|
|
|
ucol_sit_readSpecs(CollatorSpec *s, const char *string,
|
2004-05-20 21:03:31 +00:00
|
|
|
UParseError *parseError, UErrorCode *status)
|
|
|
|
{
|
|
|
|
const char *definition = string;
|
2009-04-22 23:10:07 +00:00
|
|
|
while(U_SUCCESS(*status) && *string) {
|
2004-05-20 21:03:31 +00:00
|
|
|
string = ucol_sit_readOption(string, s, status);
|
2004-05-25 23:00:52 +00:00
|
|
|
// advance over '_'
|
|
|
|
while(*string && *string == '_') {
|
|
|
|
string++;
|
|
|
|
}
|
2004-05-20 21:03:31 +00:00
|
|
|
}
|
|
|
|
if(U_FAILURE(*status)) {
|
2009-11-11 15:47:22 +00:00
|
|
|
parseError->offset = (int32_t)(string - definition);
|
2004-05-20 21:03:31 +00:00
|
|
|
}
|
2004-05-24 22:11:31 +00:00
|
|
|
return string;
|
2004-05-20 21:03:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static
|
2004-06-14 21:00:06 +00:00
|
|
|
int32_t ucol_sit_dumpSpecs(CollatorSpec *s, char *destination, int32_t capacity, UErrorCode *status)
|
2004-05-20 21:03:31 +00:00
|
|
|
{
|
|
|
|
int32_t i = 0, j = 0;
|
|
|
|
int32_t len = 0;
|
2009-04-22 23:10:07 +00:00
|
|
|
char optName;
|
2004-05-20 21:03:31 +00:00
|
|
|
if(U_SUCCESS(*status)) {
|
|
|
|
for(i = 0; i < UCOL_SIT_ITEMS_COUNT; i++) {
|
|
|
|
if(s->entries[i].start) {
|
|
|
|
if(len) {
|
2004-06-14 21:00:06 +00:00
|
|
|
if(len < capacity) {
|
|
|
|
uprv_strcat(destination, "_");
|
|
|
|
}
|
2004-05-20 21:03:31 +00:00
|
|
|
len++;
|
2009-04-22 23:10:07 +00:00
|
|
|
}
|
2004-05-20 21:03:31 +00:00
|
|
|
optName = *(s->entries[i].start);
|
2004-05-24 22:11:31 +00:00
|
|
|
if(optName == languageArg || optName == regionArg || optName == variantArg || optName == keywordArg) {
|
2004-05-20 21:03:31 +00:00
|
|
|
for(j = 0; j < s->entries[i].len; j++) {
|
2004-06-14 21:00:06 +00:00
|
|
|
if(len + j < capacity) {
|
|
|
|
destination[len+j] = uprv_toupper(*(s->entries[i].start+j));
|
|
|
|
}
|
2004-05-20 21:03:31 +00:00
|
|
|
}
|
2004-06-14 21:00:06 +00:00
|
|
|
len += s->entries[i].len;
|
2004-05-20 21:03:31 +00:00
|
|
|
} else {
|
|
|
|
len += s->entries[i].len;
|
2004-06-14 21:00:06 +00:00
|
|
|
if(len < capacity) {
|
|
|
|
uprv_strncat(destination,s->entries[i].start, s->entries[i].len);
|
|
|
|
}
|
2004-05-20 21:03:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return len;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-05-24 22:11:31 +00:00
|
|
|
static void
|
|
|
|
ucol_sit_calculateWholeLocale(CollatorSpec *s) {
|
|
|
|
// put the locale together, unless we have a done
|
|
|
|
// locale
|
|
|
|
if(s->locale[0] == 0) {
|
|
|
|
// first the language
|
2012-02-18 23:10:51 +00:00
|
|
|
uprv_strcat(s->locale, s->locElements[UCOL_SIT_LANGUAGE]);
|
2004-05-24 22:11:31 +00:00
|
|
|
// then the script, if present
|
2012-02-18 23:10:51 +00:00
|
|
|
if(*(s->locElements[UCOL_SIT_SCRIPT])) {
|
2004-05-24 22:11:31 +00:00
|
|
|
uprv_strcat(s->locale, "_");
|
2012-02-18 23:10:51 +00:00
|
|
|
uprv_strcat(s->locale, s->locElements[UCOL_SIT_SCRIPT]);
|
2004-05-24 22:11:31 +00:00
|
|
|
}
|
|
|
|
// then the region, if present
|
2012-02-18 23:10:51 +00:00
|
|
|
if(*(s->locElements[UCOL_SIT_REGION])) {
|
2004-05-24 22:11:31 +00:00
|
|
|
uprv_strcat(s->locale, "_");
|
2012-02-18 23:10:51 +00:00
|
|
|
uprv_strcat(s->locale, s->locElements[UCOL_SIT_REGION]);
|
|
|
|
} else if(*(s->locElements[UCOL_SIT_VARIANT])) { // if there is a variant, we need an underscore
|
2004-05-24 22:11:31 +00:00
|
|
|
uprv_strcat(s->locale, "_");
|
|
|
|
}
|
|
|
|
// add variant, if there
|
2012-02-18 23:10:51 +00:00
|
|
|
if(*(s->locElements[UCOL_SIT_VARIANT])) {
|
2004-05-24 22:11:31 +00:00
|
|
|
uprv_strcat(s->locale, "_");
|
2012-02-18 23:10:51 +00:00
|
|
|
uprv_strcat(s->locale, s->locElements[UCOL_SIT_VARIANT]);
|
2004-05-24 22:11:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// if there is a collation keyword, add that too
|
2012-02-18 23:10:51 +00:00
|
|
|
if(*(s->locElements[UCOL_SIT_KEYWORD])) {
|
2004-05-24 22:11:31 +00:00
|
|
|
uprv_strcat(s->locale, collationKeyword);
|
2012-02-18 23:10:51 +00:00
|
|
|
uprv_strcat(s->locale, s->locElements[UCOL_SIT_KEYWORD]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// if there is a provider keyword, add that too
|
|
|
|
if(*(s->locElements[UCOL_SIT_PROVIDER])) {
|
|
|
|
uprv_strcat(s->locale, providerKeyword);
|
|
|
|
uprv_strcat(s->locale, s->locElements[UCOL_SIT_PROVIDER]);
|
2004-05-24 22:11:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-06-21 20:46:04 +00:00
|
|
|
|
|
|
|
U_CAPI void U_EXPORT2
|
|
|
|
ucol_prepareShortStringOpen( const char *definition,
|
2005-07-15 22:11:40 +00:00
|
|
|
UBool,
|
2005-06-21 20:46:04 +00:00
|
|
|
UParseError *parseError,
|
|
|
|
UErrorCode *status)
|
|
|
|
{
|
|
|
|
if(U_FAILURE(*status)) return;
|
|
|
|
|
|
|
|
UParseError internalParseError;
|
|
|
|
|
|
|
|
if(!parseError) {
|
|
|
|
parseError = &internalParseError;
|
|
|
|
}
|
|
|
|
parseError->line = 0;
|
|
|
|
parseError->offset = 0;
|
|
|
|
parseError->preContext[0] = 0;
|
|
|
|
parseError->postContext[0] = 0;
|
|
|
|
|
|
|
|
|
|
|
|
// first we want to pick stuff out of short string.
|
|
|
|
// we'll end up with an UCA version, locale and a bunch of
|
|
|
|
// settings
|
|
|
|
|
|
|
|
// analyse the string in order to get everything we need.
|
|
|
|
CollatorSpec s;
|
|
|
|
ucol_sit_initCollatorSpecs(&s);
|
2006-10-06 18:08:06 +00:00
|
|
|
ucol_sit_readSpecs(&s, definition, parseError, status);
|
2005-06-21 20:46:04 +00:00
|
|
|
ucol_sit_calculateWholeLocale(&s);
|
2009-04-22 23:10:07 +00:00
|
|
|
|
2005-06-21 20:46:04 +00:00
|
|
|
char buffer[internalBufferSize];
|
|
|
|
uprv_memset(buffer, 0, internalBufferSize);
|
|
|
|
uloc_canonicalize(s.locale, buffer, internalBufferSize, status);
|
|
|
|
|
|
|
|
UResourceBundle *b = ures_open(U_ICUDATA_COLL, buffer, status);
|
|
|
|
/* we try to find stuff from keyword */
|
|
|
|
UResourceBundle *collations = ures_getByKey(b, "collations", NULL, status);
|
|
|
|
UResourceBundle *collElem = NULL;
|
|
|
|
char keyBuffer[256];
|
|
|
|
// if there is a keyword, we pick it up and try to get elements
|
|
|
|
if(!uloc_getKeywordValue(buffer, "collation", keyBuffer, 256, status)) {
|
|
|
|
// no keyword. we try to find the default setting, which will give us the keyword value
|
|
|
|
UResourceBundle *defaultColl = ures_getByKeyWithFallback(collations, "default", NULL, status);
|
|
|
|
if(U_SUCCESS(*status)) {
|
|
|
|
int32_t defaultKeyLen = 0;
|
|
|
|
const UChar *defaultKey = ures_getString(defaultColl, &defaultKeyLen, status);
|
|
|
|
u_UCharsToChars(defaultKey, keyBuffer, defaultKeyLen);
|
|
|
|
keyBuffer[defaultKeyLen] = 0;
|
|
|
|
} else {
|
|
|
|
*status = U_INTERNAL_PROGRAM_ERROR;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
ures_close(defaultColl);
|
|
|
|
}
|
|
|
|
collElem = ures_getByKeyWithFallback(collations, keyBuffer, collElem, status);
|
|
|
|
ures_close(collElem);
|
|
|
|
ures_close(collations);
|
|
|
|
ures_close(b);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-05-08 08:48:01 +00:00
|
|
|
U_CAPI UCollator* U_EXPORT2
|
|
|
|
ucol_openFromShortString( const char *definition,
|
2004-05-24 22:11:31 +00:00
|
|
|
UBool forceDefaults,
|
2004-05-08 08:48:01 +00:00
|
|
|
UParseError *parseError,
|
|
|
|
UErrorCode *status)
|
|
|
|
{
|
|
|
|
UTRACE_ENTRY_OC(UTRACE_UCOL_OPEN_FROM_SHORT_STRING);
|
|
|
|
UTRACE_DATA1(UTRACE_INFO, "short string = \"%s\"", definition);
|
|
|
|
|
|
|
|
if(U_FAILURE(*status)) return 0;
|
|
|
|
|
2004-05-24 22:11:31 +00:00
|
|
|
UParseError internalParseError;
|
|
|
|
|
|
|
|
if(!parseError) {
|
|
|
|
parseError = &internalParseError;
|
|
|
|
}
|
|
|
|
parseError->line = 0;
|
|
|
|
parseError->offset = 0;
|
|
|
|
parseError->preContext[0] = 0;
|
|
|
|
parseError->postContext[0] = 0;
|
|
|
|
|
|
|
|
|
2004-05-08 08:48:01 +00:00
|
|
|
// first we want to pick stuff out of short string.
|
|
|
|
// we'll end up with an UCA version, locale and a bunch of
|
|
|
|
// settings
|
|
|
|
|
|
|
|
// analyse the string in order to get everything we need.
|
2004-05-13 09:02:33 +00:00
|
|
|
const char *string = definition;
|
|
|
|
CollatorSpec s;
|
|
|
|
ucol_sit_initCollatorSpecs(&s);
|
2004-05-24 22:11:31 +00:00
|
|
|
string = ucol_sit_readSpecs(&s, definition, parseError, status);
|
|
|
|
ucol_sit_calculateWholeLocale(&s);
|
2009-04-22 23:10:07 +00:00
|
|
|
|
2004-05-20 21:03:31 +00:00
|
|
|
char buffer[internalBufferSize];
|
|
|
|
uprv_memset(buffer, 0, internalBufferSize);
|
|
|
|
uloc_canonicalize(s.locale, buffer, internalBufferSize, status);
|
2004-05-08 08:48:01 +00:00
|
|
|
|
2005-06-20 22:58:50 +00:00
|
|
|
UCollator *result = ucol_open(buffer, status);
|
2004-05-24 22:11:31 +00:00
|
|
|
int32_t i = 0;
|
2004-05-13 09:02:33 +00:00
|
|
|
|
|
|
|
for(i = 0; i < UCOL_ATTRIBUTE_COUNT; i++) {
|
|
|
|
if(s.options[i] != UCOL_DEFAULT) {
|
2004-06-14 21:00:06 +00:00
|
|
|
if(forceDefaults || ucol_getAttribute(result, (UColAttribute)i, status) != s.options[i]) {
|
2004-05-20 21:03:31 +00:00
|
|
|
ucol_setAttribute(result, (UColAttribute)i, s.options[i], status);
|
|
|
|
}
|
2004-05-24 22:11:31 +00:00
|
|
|
|
|
|
|
if(U_FAILURE(*status)) {
|
2009-11-11 15:47:22 +00:00
|
|
|
parseError->offset = (int32_t)(string - definition);
|
2004-05-24 22:11:31 +00:00
|
|
|
ucol_close(result);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2004-05-20 21:03:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if(s.variableTopSet) {
|
|
|
|
if(s.variableTopString[0]) {
|
2004-06-14 21:00:06 +00:00
|
|
|
ucol_setVariableTop(result, s.variableTopString, s.variableTopStringLen, status);
|
2004-05-20 21:03:31 +00:00
|
|
|
} else { // we set by value, using 'B'
|
|
|
|
ucol_restoreVariableTop(result, s.variableTopValue, status);
|
2004-05-13 09:02:33 +00:00
|
|
|
}
|
|
|
|
}
|
2004-05-20 21:03:31 +00:00
|
|
|
|
|
|
|
|
2004-05-13 09:02:33 +00:00
|
|
|
if(U_FAILURE(*status)) { // here it can only be a bogus value
|
|
|
|
ucol_close(result);
|
|
|
|
result = NULL;
|
|
|
|
}
|
2004-05-17 06:33:22 +00:00
|
|
|
|
2004-05-08 08:48:01 +00:00
|
|
|
UTRACE_EXIT_PTR_STATUS(result, *status);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2004-05-20 21:03:31 +00:00
|
|
|
|
2004-05-13 20:45:53 +00:00
|
|
|
U_CAPI int32_t U_EXPORT2
|
|
|
|
ucol_getShortDefinitionString(const UCollator *coll,
|
|
|
|
const char *locale,
|
2004-05-20 21:03:31 +00:00
|
|
|
char *dst,
|
2004-05-13 20:45:53 +00:00
|
|
|
int32_t capacity,
|
|
|
|
UErrorCode *status)
|
|
|
|
{
|
|
|
|
if(U_FAILURE(*status)) return 0;
|
2014-02-25 21:21:49 +00:00
|
|
|
if(coll == NULL) {
|
|
|
|
*status = U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
return 0;
|
2004-05-13 20:45:53 +00:00
|
|
|
}
|
2014-02-25 21:21:49 +00:00
|
|
|
return ((icu::Collator*)coll)->internalGetShortDefinitionString(locale,dst,capacity,*status);
|
2004-05-13 20:45:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
U_CAPI int32_t U_EXPORT2
|
2004-05-20 21:03:31 +00:00
|
|
|
ucol_normalizeShortDefinitionString(const char *definition,
|
2004-05-13 20:45:53 +00:00
|
|
|
char *destination,
|
|
|
|
int32_t capacity,
|
|
|
|
UParseError *parseError,
|
|
|
|
UErrorCode *status)
|
|
|
|
{
|
2004-05-20 21:03:31 +00:00
|
|
|
|
|
|
|
if(U_FAILURE(*status)) {
|
|
|
|
return 0;
|
|
|
|
}
|
2004-06-14 21:00:06 +00:00
|
|
|
|
|
|
|
if(destination) {
|
|
|
|
uprv_memset(destination, 0, capacity*sizeof(char));
|
2004-05-20 21:03:31 +00:00
|
|
|
}
|
|
|
|
|
2004-09-30 20:39:02 +00:00
|
|
|
UParseError pe;
|
|
|
|
if(!parseError) {
|
|
|
|
parseError = &pe;
|
|
|
|
}
|
|
|
|
|
2004-05-20 21:03:31 +00:00
|
|
|
// validate
|
|
|
|
CollatorSpec s;
|
|
|
|
ucol_sit_initCollatorSpecs(&s);
|
|
|
|
ucol_sit_readSpecs(&s, definition, parseError, status);
|
2004-06-14 21:00:06 +00:00
|
|
|
return ucol_sit_dumpSpecs(&s, destination, capacity, status);
|
2004-05-13 20:45:53 +00:00
|
|
|
}
|
|
|
|
|
2004-05-08 08:48:01 +00:00
|
|
|
/**
|
|
|
|
* Get a set containing the contractions defined by the collator. The set includes
|
|
|
|
* both the UCA contractions and the contractions defined by the collator
|
2004-05-17 06:33:22 +00:00
|
|
|
* @param coll collator
|
2004-05-08 08:48:01 +00:00
|
|
|
* @param conts the set to hold the result
|
|
|
|
* @param status to hold the error code
|
|
|
|
* @return the size of the contraction set
|
|
|
|
*/
|
|
|
|
U_CAPI int32_t U_EXPORT2
|
|
|
|
ucol_getContractions( const UCollator *coll,
|
|
|
|
USet *contractions,
|
2004-05-17 06:33:22 +00:00
|
|
|
UErrorCode *status)
|
2005-06-20 22:58:50 +00:00
|
|
|
{
|
|
|
|
ucol_getContractionsAndExpansions(coll, contractions, NULL, FALSE, status);
|
|
|
|
return uset_getItemCount(contractions);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a set containing the expansions defined by the collator. The set includes
|
|
|
|
* both the UCA expansions and the expansions defined by the tailoring
|
|
|
|
* @param coll collator
|
|
|
|
* @param conts the set to hold the result
|
|
|
|
* @param addPrefixes add the prefix contextual elements to contractions
|
|
|
|
* @param status to hold the error code
|
|
|
|
*
|
|
|
|
* @draft ICU 3.4
|
|
|
|
*/
|
|
|
|
U_CAPI void U_EXPORT2
|
|
|
|
ucol_getContractionsAndExpansions( const UCollator *coll,
|
|
|
|
USet *contractions,
|
|
|
|
USet *expansions,
|
|
|
|
UBool addPrefixes,
|
|
|
|
UErrorCode *status)
|
2004-05-08 08:48:01 +00:00
|
|
|
{
|
2004-06-14 21:00:06 +00:00
|
|
|
if(U_FAILURE(*status)) {
|
2005-06-20 22:58:50 +00:00
|
|
|
return;
|
2004-06-14 21:00:06 +00:00
|
|
|
}
|
2005-06-20 22:58:50 +00:00
|
|
|
if(coll == NULL) {
|
2004-06-14 21:00:06 +00:00
|
|
|
*status = U_ILLEGAL_ARGUMENT_ERROR;
|
2005-06-20 22:58:50 +00:00
|
|
|
return;
|
2004-06-14 21:00:06 +00:00
|
|
|
}
|
2014-02-25 21:21:49 +00:00
|
|
|
const icu::RuleBasedCollator *rbc = icu::RuleBasedCollator::rbcFromUCollator(coll);
|
|
|
|
if(rbc == NULL) {
|
|
|
|
*status = U_UNSUPPORTED_ERROR;
|
|
|
|
return;
|
2004-06-02 08:09:01 +00:00
|
|
|
}
|
2014-02-25 21:21:49 +00:00
|
|
|
rbc->internalGetContractionsAndExpansions(
|
|
|
|
icu::UnicodeSet::fromUSet(contractions),
|
|
|
|
icu::UnicodeSet::fromUSet(expansions),
|
|
|
|
addPrefixes, *status);
|
2004-06-02 08:09:01 +00:00
|
|
|
}
|
2004-06-10 23:49:07 +00:00
|
|
|
#endif
|