mirror of
https://github.com/gnustep/libs-base.git
synced 2025-04-22 16:33:29 +00:00
missed in last git commit
This commit is contained in:
parent
e60b2004af
commit
caedb21b51
1 changed files with 76 additions and 2 deletions
|
@ -815,6 +815,61 @@ else \
|
|||
bsize = grow / sizeof(unichar); \
|
||||
}
|
||||
|
||||
#define UTF8DECODE 1
|
||||
|
||||
#if defined(UTF8DECODE)
|
||||
/* This next data (utf8d) and function (decode()) copyright ...
|
||||
Copyright (c) 2008-2009 Bjoern Hoehrmann <bjoern@hoehrmann.de>
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#define UTF8_ACCEPT 0
|
||||
#define UTF8_REJECT 12
|
||||
|
||||
static const uint8_t utf8d[] = {
|
||||
// The first part of the table maps bytes to character classes that
|
||||
// to reduce the size of the transition table and create bitmasks.
|
||||
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,
|
||||
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,
|
||||
7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
|
||||
8,8,2,2,2,2,2,2,2,2,2,2,2,2,2,2, 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
|
||||
10,3,3,3,3,3,3,3,3,3,3,3,3,4,3,3, 11,6,6,6,5,8,8,8,8,8,8,8,8,8,8,8,
|
||||
|
||||
// The second part is a transition table that maps a combination
|
||||
// of a state of the automaton and a character class to a state.
|
||||
0,12,24,36,60,96,84,12,12,12,48,72, 12,12,12,12,12,12,12,12,12,12,12,12,
|
||||
12, 0,12,12,12,12,12, 0,12, 0,12,12, 12,24,12,12,12,12,12,24,12,24,12,12,
|
||||
12,12,12,12,12,12,12,24,12,12,12,12, 12,24,12,12,12,12,12,12,12,24,12,12,
|
||||
12,12,12,12,12,12,12,36,12,36,12,12, 12,36,12,12,12,12,12,36,12,36,12,12,
|
||||
12,36,12,12,12,12,12,12,12,12,12,12,
|
||||
};
|
||||
|
||||
static uint32_t inline
|
||||
decode(uint32_t* state, uint32_t* codep, uint32_t byte)
|
||||
{
|
||||
uint32_t type = utf8d[byte];
|
||||
|
||||
*codep = (*state != UTF8_ACCEPT)
|
||||
? (byte & 0x3fu) | (*codep << 6)
|
||||
: (0xff >> type) & (byte);
|
||||
|
||||
*state = utf8d[256 + *state + type];
|
||||
return *state;
|
||||
}
|
||||
|
||||
/* End of separately copyrighted section.
|
||||
*/
|
||||
#endif
|
||||
|
||||
|
||||
/**
|
||||
* Function to convert from 8-bit data to 16-bit unicode characters.
|
||||
* <p>The dst argument is a pointer to a pointer to a buffer in which the
|
||||
|
@ -917,11 +972,22 @@ GSToUnicode(unichar **dst, unsigned int *size, const unsigned char *src,
|
|||
{
|
||||
case NSUTF8StringEncoding:
|
||||
{
|
||||
#if defined(UTF8DECODE)
|
||||
uint32_t state = 0;
|
||||
#endif
|
||||
while (spos < slen)
|
||||
{
|
||||
unsigned char c = src[spos];
|
||||
unsigned long u = c;
|
||||
uint32_t u;
|
||||
|
||||
#if defined(UTF8DECODE)
|
||||
if (decode(&state, &u, src[spos++]))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
#else
|
||||
uint8_t c = src[spos];
|
||||
|
||||
u = c;
|
||||
if (c > 0x7f)
|
||||
{
|
||||
int i, sle = 0;
|
||||
|
@ -1010,6 +1076,7 @@ GSToUnicode(unichar **dst, unsigned int *size, const unsigned char *src,
|
|||
{
|
||||
spos++;
|
||||
}
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Add codepoint as either a single unichar for BMP
|
||||
|
@ -1040,6 +1107,13 @@ GSToUnicode(unichar **dst, unsigned int *size, const unsigned char *src,
|
|||
ptr[dpos++] = ul + 0xdc00;
|
||||
}
|
||||
}
|
||||
#if defined(UTF8DECODE)
|
||||
if (state != UTF8_ACCEPT)
|
||||
{
|
||||
result = NO; // Parse failure
|
||||
goto done;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
break;
|
||||
|
||||
|
|
Loading…
Reference in a new issue