2002-10-30 18:09:55 +00:00
|
|
|
/*
|
|
|
|
**********************************************************************
|
2014-04-23 23:22:13 +00:00
|
|
|
* Copyright (c) 2002-2014, International Business Machines
|
2002-10-30 18:09:55 +00:00
|
|
|
* Corporation and others. All Rights Reserved.
|
|
|
|
**********************************************************************
|
|
|
|
* Author: Alan Liu
|
|
|
|
* Created: October 30 2002
|
2002-11-27 20:08:13 +00:00
|
|
|
* Since: ICU 2.4
|
2010-12-31 18:21:36 +00:00
|
|
|
* 2010nov19 Markus Scherer Rewrite for formatVersion 2.
|
2002-10-30 18:09:55 +00:00
|
|
|
**********************************************************************
|
|
|
|
*/
|
|
|
|
#include "propname.h"
|
|
|
|
#include "unicode/uchar.h"
|
|
|
|
#include "unicode/udata.h"
|
2011-06-03 05:23:57 +00:00
|
|
|
#include "unicode/uscript.h"
|
2002-10-30 18:09:55 +00:00
|
|
|
#include "umutex.h"
|
2003-09-23 21:06:24 +00:00
|
|
|
#include "cmemory.h"
|
|
|
|
#include "cstring.h"
|
|
|
|
#include "uarrsort.h"
|
2010-12-31 18:21:36 +00:00
|
|
|
#include "uinvchar.h"
|
|
|
|
|
|
|
|
#define INCLUDED_FROM_PROPNAME_CPP
|
|
|
|
#include "propname_data.h"
|
2002-10-30 18:09:55 +00:00
|
|
|
|
2004-09-11 22:02:10 +00:00
|
|
|
U_CDECL_BEGIN
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the next non-ignorable ASCII character from a property name
|
|
|
|
* and lowercases it.
|
|
|
|
* @return ((advance count for the name)<<8)|character
|
|
|
|
*/
|
2004-09-14 00:52:37 +00:00
|
|
|
static inline int32_t
|
2004-09-11 22:02:10 +00:00
|
|
|
getASCIIPropertyNameChar(const char *name) {
|
|
|
|
int32_t i;
|
|
|
|
char c;
|
|
|
|
|
|
|
|
/* Ignore delimiters '-', '_', and ASCII White_Space */
|
|
|
|
for(i=0;
|
|
|
|
(c=name[i++])==0x2d || c==0x5f ||
|
|
|
|
c==0x20 || (0x09<=c && c<=0x0d);
|
|
|
|
) {}
|
|
|
|
|
|
|
|
if(c!=0) {
|
|
|
|
return (i<<8)|(uint8_t)uprv_asciitolower((char)c);
|
|
|
|
} else {
|
|
|
|
return i<<8;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the next non-ignorable EBCDIC character from a property name
|
|
|
|
* and lowercases it.
|
|
|
|
* @return ((advance count for the name)<<8)|character
|
|
|
|
*/
|
2004-09-14 00:52:37 +00:00
|
|
|
static inline int32_t
|
2004-09-11 22:02:10 +00:00
|
|
|
getEBCDICPropertyNameChar(const char *name) {
|
|
|
|
int32_t i;
|
|
|
|
char c;
|
|
|
|
|
|
|
|
/* Ignore delimiters '-', '_', and EBCDIC White_Space */
|
|
|
|
for(i=0;
|
|
|
|
(c=name[i++])==0x60 || c==0x6d ||
|
|
|
|
c==0x40 || c==0x05 || c==0x15 || c==0x25 || c==0x0b || c==0x0c || c==0x0d;
|
|
|
|
) {}
|
|
|
|
|
|
|
|
if(c!=0) {
|
|
|
|
return (i<<8)|(uint8_t)uprv_ebcdictolower((char)c);
|
|
|
|
} else {
|
|
|
|
return i<<8;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unicode property names and property value names are compared "loosely".
|
|
|
|
*
|
|
|
|
* UCD.html 4.0.1 says:
|
|
|
|
* For all property names, property value names, and for property values for
|
|
|
|
* Enumerated, Binary, or Catalog properties, use the following
|
|
|
|
* loose matching rule:
|
|
|
|
*
|
|
|
|
* LM3. Ignore case, whitespace, underscore ('_'), and hyphens.
|
|
|
|
*
|
|
|
|
* This function does just that, for (char *) name strings.
|
|
|
|
* It is almost identical to ucnv_compareNames() but also ignores
|
|
|
|
* C0 White_Space characters (U+0009..U+000d, and U+0085 on EBCDIC).
|
|
|
|
*
|
|
|
|
* @internal
|
|
|
|
*/
|
|
|
|
|
|
|
|
U_CAPI int32_t U_EXPORT2
|
|
|
|
uprv_compareASCIIPropertyNames(const char *name1, const char *name2) {
|
|
|
|
int32_t rc, r1, r2;
|
|
|
|
|
|
|
|
for(;;) {
|
|
|
|
r1=getASCIIPropertyNameChar(name1);
|
|
|
|
r2=getASCIIPropertyNameChar(name2);
|
|
|
|
|
|
|
|
/* If we reach the ends of both strings then they match */
|
|
|
|
if(((r1|r2)&0xff)==0) {
|
|
|
|
return 0;
|
|
|
|
}
|
2010-12-31 18:21:36 +00:00
|
|
|
|
2004-09-11 22:02:10 +00:00
|
|
|
/* Compare the lowercased characters */
|
|
|
|
if(r1!=r2) {
|
|
|
|
rc=(r1&0xff)-(r2&0xff);
|
|
|
|
if(rc!=0) {
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
name1+=r1>>8;
|
|
|
|
name2+=r2>>8;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
U_CAPI int32_t U_EXPORT2
|
|
|
|
uprv_compareEBCDICPropertyNames(const char *name1, const char *name2) {
|
|
|
|
int32_t rc, r1, r2;
|
|
|
|
|
|
|
|
for(;;) {
|
|
|
|
r1=getEBCDICPropertyNameChar(name1);
|
|
|
|
r2=getEBCDICPropertyNameChar(name2);
|
|
|
|
|
|
|
|
/* If we reach the ends of both strings then they match */
|
|
|
|
if(((r1|r2)&0xff)==0) {
|
|
|
|
return 0;
|
|
|
|
}
|
2010-12-31 18:21:36 +00:00
|
|
|
|
2004-09-11 22:02:10 +00:00
|
|
|
/* Compare the lowercased characters */
|
|
|
|
if(r1!=r2) {
|
|
|
|
rc=(r1&0xff)-(r2&0xff);
|
|
|
|
if(rc!=0) {
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
name1+=r1>>8;
|
|
|
|
name2+=r2>>8;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
U_CDECL_END
|
|
|
|
|
2002-12-14 09:05:17 +00:00
|
|
|
U_NAMESPACE_BEGIN
|
|
|
|
|
2010-12-31 18:21:36 +00:00
|
|
|
int32_t PropNameData::findProperty(int32_t property) {
|
|
|
|
int32_t i=1; // valueMaps index, initially after numRanges
|
|
|
|
for(int32_t numRanges=valueMaps[0]; numRanges>0; --numRanges) {
|
|
|
|
// Read and skip the start and limit of this range.
|
|
|
|
int32_t start=valueMaps[i];
|
|
|
|
int32_t limit=valueMaps[i+1];
|
|
|
|
i+=2;
|
|
|
|
if(property<start) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if(property<limit) {
|
|
|
|
return i+(property-start)*2;
|
|
|
|
}
|
|
|
|
i+=(limit-start)*2; // Skip all entries for this range.
|
2002-10-30 18:09:55 +00:00
|
|
|
}
|
2010-12-31 18:21:36 +00:00
|
|
|
return 0;
|
2002-10-30 18:09:55 +00:00
|
|
|
}
|
|
|
|
|
2010-12-31 18:21:36 +00:00
|
|
|
int32_t PropNameData::findPropertyValueNameGroup(int32_t valueMapIndex, int32_t value) {
|
|
|
|
if(valueMapIndex==0) {
|
|
|
|
return 0; // The property does not have named values.
|
|
|
|
}
|
2011-01-05 21:05:47 +00:00
|
|
|
++valueMapIndex; // Skip the BytesTrie offset.
|
2010-12-31 18:21:36 +00:00
|
|
|
int32_t numRanges=valueMaps[valueMapIndex++];
|
|
|
|
if(numRanges<0x10) {
|
|
|
|
// Ranges of values.
|
|
|
|
for(; numRanges>0; --numRanges) {
|
|
|
|
// Read and skip the start and limit of this range.
|
|
|
|
int32_t start=valueMaps[valueMapIndex];
|
|
|
|
int32_t limit=valueMaps[valueMapIndex+1];
|
|
|
|
valueMapIndex+=2;
|
|
|
|
if(value<start) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if(value<limit) {
|
|
|
|
return valueMaps[valueMapIndex+value-start];
|
|
|
|
}
|
|
|
|
valueMapIndex+=limit-start; // Skip all entries for this range.
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// List of values.
|
|
|
|
int32_t valuesStart=valueMapIndex;
|
|
|
|
int32_t nameGroupOffsetsStart=valueMapIndex+numRanges-0x10;
|
|
|
|
do {
|
|
|
|
int32_t v=valueMaps[valueMapIndex];
|
|
|
|
if(value<v) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if(value==v) {
|
|
|
|
return valueMaps[nameGroupOffsetsStart+valueMapIndex-valuesStart];
|
|
|
|
}
|
|
|
|
} while(++valueMapIndex<nameGroupOffsetsStart);
|
|
|
|
}
|
|
|
|
return 0;
|
2002-10-30 18:09:55 +00:00
|
|
|
}
|
|
|
|
|
2010-12-31 18:21:36 +00:00
|
|
|
const char *PropNameData::getName(const char *nameGroup, int32_t nameIndex) {
|
|
|
|
int32_t numNames=*nameGroup++;
|
|
|
|
if(nameIndex<0 || numNames<=nameIndex) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
// Skip nameIndex names.
|
|
|
|
for(; nameIndex>0; --nameIndex) {
|
|
|
|
nameGroup=uprv_strchr(nameGroup, 0)+1;
|
|
|
|
}
|
|
|
|
if(*nameGroup==0) {
|
|
|
|
return NULL; // no name (Property[Value]Aliases.txt has "n/a")
|
|
|
|
}
|
|
|
|
return nameGroup;
|
2002-10-30 18:09:55 +00:00
|
|
|
}
|
|
|
|
|
2011-01-05 21:05:47 +00:00
|
|
|
UBool PropNameData::containsName(BytesTrie &trie, const char *name) {
|
2010-12-31 18:21:36 +00:00
|
|
|
if(name==NULL) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
2011-01-05 21:05:47 +00:00
|
|
|
UStringTrieResult result=USTRINGTRIE_NO_VALUE;
|
2010-12-31 18:21:36 +00:00
|
|
|
char c;
|
|
|
|
while((c=*name++)!=0) {
|
|
|
|
c=uprv_invCharToLowercaseAscii(c);
|
|
|
|
// Ignore delimiters '-', '_', and ASCII White_Space.
|
|
|
|
if(c==0x2d || c==0x5f || c==0x20 || (0x09<=c && c<=0x0d)) {
|
|
|
|
continue;
|
|
|
|
}
|
2011-01-05 21:05:47 +00:00
|
|
|
if(!USTRINGTRIE_HAS_NEXT(result)) {
|
2010-12-31 18:21:36 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
result=trie.next((uint8_t)c);
|
|
|
|
}
|
2011-01-05 21:05:47 +00:00
|
|
|
return USTRINGTRIE_HAS_VALUE(result);
|
2002-10-30 18:09:55 +00:00
|
|
|
}
|
|
|
|
|
2010-12-31 18:21:36 +00:00
|
|
|
const char *PropNameData::getPropertyName(int32_t property, int32_t nameChoice) {
|
|
|
|
int32_t valueMapIndex=findProperty(property);
|
|
|
|
if(valueMapIndex==0) {
|
|
|
|
return NULL; // Not a known property.
|
2002-10-30 18:09:55 +00:00
|
|
|
}
|
2010-12-31 18:21:36 +00:00
|
|
|
return getName(nameGroups+valueMaps[valueMapIndex], nameChoice);
|
2002-10-30 18:09:55 +00:00
|
|
|
}
|
|
|
|
|
2010-12-31 18:21:36 +00:00
|
|
|
const char *PropNameData::getPropertyValueName(int32_t property, int32_t value, int32_t nameChoice) {
|
|
|
|
int32_t valueMapIndex=findProperty(property);
|
|
|
|
if(valueMapIndex==0) {
|
|
|
|
return NULL; // Not a known property.
|
|
|
|
}
|
|
|
|
int32_t nameGroupOffset=findPropertyValueNameGroup(valueMaps[valueMapIndex+1], value);
|
|
|
|
if(nameGroupOffset==0) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return getName(nameGroups+nameGroupOffset, nameChoice);
|
2002-10-30 18:09:55 +00:00
|
|
|
}
|
|
|
|
|
2011-01-05 21:05:47 +00:00
|
|
|
int32_t PropNameData::getPropertyOrValueEnum(int32_t bytesTrieOffset, const char *alias) {
|
|
|
|
BytesTrie trie(bytesTries+bytesTrieOffset);
|
2010-12-31 18:21:36 +00:00
|
|
|
if(containsName(trie, alias)) {
|
|
|
|
return trie.getValue();
|
|
|
|
} else {
|
|
|
|
return UCHAR_INVALID_CODE;
|
|
|
|
}
|
2002-10-30 18:09:55 +00:00
|
|
|
}
|
|
|
|
|
2010-12-31 18:21:36 +00:00
|
|
|
int32_t PropNameData::getPropertyEnum(const char *alias) {
|
|
|
|
return getPropertyOrValueEnum(0, alias);
|
2002-10-30 18:09:55 +00:00
|
|
|
}
|
|
|
|
|
2010-12-31 18:21:36 +00:00
|
|
|
int32_t PropNameData::getPropertyValueEnum(int32_t property, const char *alias) {
|
|
|
|
int32_t valueMapIndex=findProperty(property);
|
|
|
|
if(valueMapIndex==0) {
|
|
|
|
return UCHAR_INVALID_CODE; // Not a known property.
|
2003-05-08 22:17:03 +00:00
|
|
|
}
|
2010-12-31 18:21:36 +00:00
|
|
|
valueMapIndex=valueMaps[valueMapIndex+1];
|
|
|
|
if(valueMapIndex==0) {
|
|
|
|
return UCHAR_INVALID_CODE; // The property does not have named values.
|
2002-10-30 18:09:55 +00:00
|
|
|
}
|
2010-12-31 18:21:36 +00:00
|
|
|
// valueMapIndex is the start of the property's valueMap,
|
2011-01-05 21:05:47 +00:00
|
|
|
// where the first word is the BytesTrie offset.
|
2010-12-31 18:21:36 +00:00
|
|
|
return getPropertyOrValueEnum(valueMaps[valueMapIndex], alias);
|
2003-05-08 22:17:03 +00:00
|
|
|
}
|
2011-03-18 19:30:48 +00:00
|
|
|
U_NAMESPACE_END
|
2003-05-08 22:17:03 +00:00
|
|
|
|
2002-10-30 18:09:55 +00:00
|
|
|
//----------------------------------------------------------------------
|
|
|
|
// Public API implementation
|
|
|
|
|
|
|
|
U_CAPI const char* U_EXPORT2
|
|
|
|
u_getPropertyName(UProperty property,
|
|
|
|
UPropertyNameChoice nameChoice) {
|
2011-03-22 01:03:09 +00:00
|
|
|
U_NAMESPACE_USE
|
2010-12-31 18:21:36 +00:00
|
|
|
return PropNameData::getPropertyName(property, nameChoice);
|
2002-10-30 18:09:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
U_CAPI UProperty U_EXPORT2
|
|
|
|
u_getPropertyEnum(const char* alias) {
|
2011-03-22 01:03:09 +00:00
|
|
|
U_NAMESPACE_USE
|
2010-12-31 18:21:36 +00:00
|
|
|
return (UProperty)PropNameData::getPropertyEnum(alias);
|
2002-10-30 18:09:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
U_CAPI const char* U_EXPORT2
|
|
|
|
u_getPropertyValueName(UProperty property,
|
|
|
|
int32_t value,
|
|
|
|
UPropertyNameChoice nameChoice) {
|
2011-03-22 01:03:09 +00:00
|
|
|
U_NAMESPACE_USE
|
2010-12-31 18:21:36 +00:00
|
|
|
return PropNameData::getPropertyValueName(property, value, nameChoice);
|
2002-10-30 18:09:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
U_CAPI int32_t U_EXPORT2
|
|
|
|
u_getPropertyValueEnum(UProperty property,
|
|
|
|
const char* alias) {
|
2011-03-22 01:03:09 +00:00
|
|
|
U_NAMESPACE_USE
|
2010-12-31 18:21:36 +00:00
|
|
|
return PropNameData::getPropertyValueEnum(property, alias);
|
2003-09-23 21:06:24 +00:00
|
|
|
}
|
2011-06-03 05:23:57 +00:00
|
|
|
|
|
|
|
U_CAPI const char* U_EXPORT2
|
|
|
|
uscript_getName(UScriptCode scriptCode){
|
|
|
|
return u_getPropertyValueName(UCHAR_SCRIPT, scriptCode,
|
|
|
|
U_LONG_PROPERTY_NAME);
|
|
|
|
}
|
|
|
|
|
|
|
|
U_CAPI const char* U_EXPORT2
|
|
|
|
uscript_getShortName(UScriptCode scriptCode){
|
|
|
|
return u_getPropertyValueName(UCHAR_SCRIPT, scriptCode,
|
|
|
|
U_SHORT_PROPERTY_NAME);
|
|
|
|
}
|