ICU-2329 use a single ASCII-EBCDIC table and add functions to check if a string contains only invariant characters

X-SVN-Rev: 12579
This commit is contained in:
Markus Scherer 2003-07-02 17:17:37 +00:00
parent a66ecb7536
commit 7c3cd6b0a5

View file

@ -65,6 +65,7 @@
/* include ICU headers */
#include "unicode/utypes.h"
#include "unicode/putil.h"
#include "uassert.h"
#include "umutex.h"
#include "cmemory.h"
#include "cstring.h"
@ -1898,97 +1899,55 @@ uprv_getDefaultCodepage()
#endif
}
#if U_CHARSET_FAMILY==U_EBCDIC_FAMILY
#ifdef OS390
/*
* These maps for ASCII to/from EBCDIC are from
* "UTF-EBCDIC - EBCDIC-Friendly Unicode (or UCS) Transformation Format"
* at http://www.unicode.org/unicode/reports/tr16/
* (which should reflect codepage 1047)
* but modified to explicitly exclude the variant
* control and graphical characters that are in ASCII-based
* codepages at 0x80 and above.
* Also, unlike in Version 6.0 of the UTR on UTF-EBCDIC,
* the Line Feed mapping varies according to the environment.
* These maps for ASCII to/from EBCDIC map invariant characters (see utypes.h)
* appropriately for most EBCDIC codepages.
*
* These tables do not establish a converter or a codepage.
*/
/* on S/390 Open Edition, ASCII 0xa (LF) maps to 0x15 and ISO-8 0x85 maps to 0x25 */
# define E_LF 0x15
# define A_15 0x0a
# define A_25 0x00
# if 0
/* the CDRA variation of 1047 is not currently used - see tables in #else below */
/* in standard EBCDIC (CDRA), ASCII 0xa (LF) maps to 0x25 and ISO-8 0x85 maps to 0x15 */
# define E_LF 0x25
# define A_15 0x00
# define A_25 0x0a
# endif
static const uint8_t asciiFromEbcdic[256]={
0x00, 0x01, 0x02, 0x03, 0x00, 0x09, 0x00, 0x7F, 0x00, 0x00, 0x00, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
0x10, 0x11, 0x12, 0x13, 0x00, A_15, 0x08, 0x00, 0x18, 0x19, 0x00, 0x00, 0x1C, 0x1D, 0x1E, 0x1F,
0x00, 0x00, 0x00, 0x00, 0x00, A_25, 0x17, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0x06, 0x07,
0x00, 0x00, 0x16, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x14, 0x15, 0x00, 0x1A,
0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x2E, 0x3C, 0x28, 0x2B, 0x7C,
0x26, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x21, 0x24, 0x2A, 0x29, 0x3B, 0x5E,
0x2D, 0x2F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x2C, 0x25, 0x5F, 0x3E, 0x3F,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x60, 0x3A, 0x23, 0x40, 0x27, 0x3D, 0x22,
0x00, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E, 0x6F, 0x70, 0x71, 0x72, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x7E, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7A, 0x00, 0x00, 0x00, 0x5B, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x5D, 0x00, 0x00,
0x7B, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x7D, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, 0x50, 0x51, 0x52, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x5C, 0x00, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5A, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0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0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
};
#else
/*
* These maps for ASCII to/from EBCDIC were generated
* using the ICU converter for codepage 37 on 2000-may-22.
* They explicitly exclude the variant
* They currently also map most other ASCII graphic characters,
* appropriately for codepages 37 and 1047.
* Exceptions: The characters for []^ have different codes in 37 & 1047.
* Both versions are mapped to ASCII.
*
* ASCII 37 1047
* [ 5B BA AD
* ] 5D BB BD
* ^ 5E B0 5F
*
* There are no mappings for variant characters from Unicode to EBCDIC.
*
* Currently, C0 control codes are also included in these maps.
* Exceptions: S/390 Open Edition swaps LF and NEL codes compared with other
* EBCDIC platforms; both codes (15 and 25) are mapped to ASCII LF (0A),
* but there is no mapping for ASCII LF back to EBCDIC.
*
* ASCII EBCDIC S/390-OE
* LF 0A 25 15
* NEL 85 15 25
*
* The maps below explicitly exclude the variant
* control and graphical characters that are in ASCII-based
* codepages at 0x80 and above.
* "No mapping" is expressed by mapping to a 00 byte.
*
* These tables do not establish a converter or a codepage.
*/
static const uint8_t asciiFromEbcdic[256]={
0x00, 0x01, 0x02, 0x03, 0x00, 0x09, 0x00, 0x7f, 0x00, 0x00, 0x00, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
0x10, 0x11, 0x12, 0x13, 0x00, 0x00, 0x08, 0x00, 0x18, 0x19, 0x00, 0x00, 0x1c, 0x1d, 0x1e, 0x1f,
0x10, 0x11, 0x12, 0x13, 0x00, 0x0a, 0x08, 0x00, 0x18, 0x19, 0x00, 0x00, 0x1c, 0x1d, 0x1e, 0x1f,
0x00, 0x00, 0x00, 0x00, 0x00, 0x0a, 0x17, 0x1b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0x06, 0x07,
0x00, 0x00, 0x16, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x14, 0x15, 0x00, 0x1a,
0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x2e, 0x3c, 0x28, 0x2b, 0x7c,
0x26, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x21, 0x24, 0x2a, 0x29, 0x3b, 0x00,
0x26, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x21, 0x24, 0x2a, 0x29, 0x3b, 0x5e,
0x2d, 0x2f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x2c, 0x25, 0x5f, 0x3e, 0x3f,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x60, 0x3a, 0x23, 0x40, 0x27, 0x3d, 0x22,
0x00, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x7e, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x5e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x5b, 0x5d, 0x00, 0x00, 0x00, 0x00,
0x00, 0x7e, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0x00, 0x00, 0x00, 0x5b, 0x00, 0x00,
0x5e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x5b, 0x5d, 0x00, 0x5d, 0x00, 0x00,
0x7b, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x7d, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, 0x50, 0x51, 0x52, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x5c, 0x00, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
@ -1996,54 +1955,189 @@ static const uint8_t asciiFromEbcdic[256]={
};
static const uint8_t ebcdicFromAscii[256]={
0x00, 0x01, 0x02, 0x03, 0x37, 0x2d, 0x2e, 0x2f, 0x16, 0x05, 0x25, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
0x00, 0x01, 0x02, 0x03, 0x37, 0x2d, 0x2e, 0x2f, 0x16, 0x05, 0x00, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
0x10, 0x11, 0x12, 0x13, 0x3c, 0x3d, 0x32, 0x26, 0x18, 0x19, 0x3f, 0x27, 0x1c, 0x1d, 0x1e, 0x1f,
0x40, 0x5a, 0x7f, 0x7b, 0x5b, 0x6c, 0x50, 0x7d, 0x4d, 0x5d, 0x5c, 0x4e, 0x6b, 0x60, 0x4b, 0x61,
0x40, 0x00, 0x7f, 0x00, 0x00, 0x6c, 0x50, 0x7d, 0x4d, 0x5d, 0x5c, 0x4e, 0x6b, 0x60, 0x4b, 0x61,
0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0x7a, 0x5e, 0x4c, 0x7e, 0x6e, 0x6f,
0x7c, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6,
0xd7, 0xd8, 0xd9, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xba, 0xe0, 0xbb, 0xb0, 0x6d,
0x79, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96,
0x97, 0x98, 0x99, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xa9, 0xc0, 0x4f, 0xd0, 0xa1, 0x07,
0x00, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6,
0xd7, 0xd8, 0xd9, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0x00, 0x00, 0x00, 0x00, 0x6d,
0x00, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96,
0x97, 0x98, 0x99, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xa9, 0x00, 0x00, 0x00, 0x00, 0x07,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
};
#endif
/*
* Bit sets indicating which characters of the ASCII repertoire
* (by ASCII/Unicode code) are "invariant".
* See utypes.h for more details.
*
* As invariant are considered the characters of the ASCII repertoire except
* for the following:
* 21 '!' <exclamation mark>
* 23 '#' <number sign>
* 24 '$' <dollar sign>
*
* 40 '@' <commercial at>
*
* 5b '[' <left bracket>
* 5c '\' <backslash>
* 5d ']' <right bracket>
* 5e '^' <circumflex>
*
* 60 '`' <grave accent>
*
* 7b '{' <left brace>
* 7c '|' <vertical line>
* 7d '}' <right brace>
* 7e '~' <tilde>
*/
static const uint32_t invariantChars[4]={
0xfffffbff, /* 00..1f but not 0a */
0xffffffe5, /* 20..3f but not 21 23 24 */
0x87fffffe, /* 40..5f but not 40 5b..5e */
0x87fffffe /* 60..7f but not 60 7b..7e */
};
#define CHAR_IS_INVARIANT(c) ((0<=(c)) && ((c)<=0x7f) && (invariantChars[(c)>>5]&((uint32_t)1<<((c)&0x1f)))!=0)
#endif
U_CAPI void U_EXPORT2
u_charsToUChars(const char *cs, UChar *us, int32_t length) {
UChar u;
uint8_t c;
UBool onlyInvariantChars;
/*
* Allow the entire ASCII repertoire to be mapped _to_ Unicode.
* For EBCDIC systems, this works for characters with codes from
* codepages 37 and 1047 or compatible.
*/
onlyInvariantChars=TRUE;
while(length>0) {
c=(uint8_t)(*cs++);
#if U_CHARSET_FAMILY==U_ASCII_FAMILY
*us++=(UChar)(uint8_t)(*cs++);
u=(UChar)c;
#elif U_CHARSET_FAMILY==U_EBCDIC_FAMILY
*us++=(UChar)asciiFromEbcdic[(uint8_t)(*cs++)];
u=(UChar)asciiFromEbcdic[c];
#else
# error U_CHARSET_FAMILY is not valid
#endif
if(u==0 && c!=0) {
onlyInvariantChars=FALSE;
}
*us++=u;
--length;
}
U_ASSERT(onlyInvariantChars); /* only invariant chars? */
}
U_CAPI void U_EXPORT2
u_UCharsToChars(const UChar *us, char *cs, int32_t length) {
UChar u;
UBool onlyInvariantChars;
onlyInvariantChars=TRUE;
while(length>0) {
u=*us++;
if(!CHAR_IS_INVARIANT(u)) {
onlyInvariantChars=FALSE;
u=0;
}
#if U_CHARSET_FAMILY==U_ASCII_FAMILY
*cs++=(char)(*us++);
*cs++=(char)u;
#elif U_CHARSET_FAMILY==U_EBCDIC_FAMILY
*cs++=(char)ebcdicFromAscii[(uint8_t)(*us++)];
*cs++=(char)ebcdicFromAscii[u];
#else
# error U_CHARSET_FAMILY is not valid
#endif
--length;
}
U_ASSERT(onlyInvariantChars); /* only invariant chars? */
}
U_CAPI UBool U_EXPORT2
uprv_isInvariantString(const char *s, int32_t length) {
char c;
for(;;) {
if(length<0) {
/* NUL-terminated */
c=*s++;
if(c==0) {
break;
}
} else {
/* count length */
if(length==0) {
break;
}
--length;
c=*s++;
if(c==0) {
continue; /* NUL is invariant */
}
}
/* c!=0 now, one branch below checks c==0 for variant characters */
/*
* no assertions here because these functions are legitimately called
* for strings with variant characters
*/
#if U_CHARSET_FAMILY==U_ASCII_FAMILY
if(!CHAR_IS_INVARIANT(c)) {
return FALSE; /* found a variant char */
}
#elif U_CHARSET_FAMILY==U_EBCDIC_FAMILY
c=(char)asciiFromEbcdic[(uint8_t)c];
if(c==0 || !CHAR_IS_INVARIANT(c)) {
return FALSE; /* found a variant char */
}
#else
# error U_CHARSET_FAMILY is not valid
#endif
}
return TRUE;
}
U_CAPI UBool U_EXPORT2
uprv_isInvariantUString(const UChar *s, int32_t length) {
UChar c;
for(;;) {
if(length<0) {
/* NUL-terminated */
c=*s++;
if(c==0) {
break;
}
} else {
/* count length */
if(length==0) {
break;
}
--length;
c=*s++;
}
/*
* no assertions here because these functions are legitimately called
* for strings with variant characters
*/
if(!CHAR_IS_INVARIANT(c)) {
return FALSE; /* found a variant char */
}
}
return TRUE;
}
/* end of platform-specific implementation */