Fix the char() function so that it works even if SQLITE_OMIT_UTF16 is defined.

FossilOrigin-Name: af542c82e8e7f0415530b639fa397429c5f377f6
This commit is contained in:
drh
2013-03-07 14:00:04 +00:00
parent 5acefe7dbe
commit fe7a5d11b6
4 changed files with 24 additions and 29 deletions
+15 -14
View File
@@ -981,7 +981,6 @@ static void unicodeFunc(
** an integer. It constructs a string where each character of the string
** is the unicode character for the corresponding integer argument.
*/
#ifndef SQLITE_OMIT_UTF16
static void charFunc(
sqlite3_context *context,
int argc,
@@ -1000,20 +999,24 @@ static void charFunc(
x = sqlite3_value_int64(argv[i]);
if( x<0 || x>0x10ffff ) x = 0xfffd;
c = (unsigned)(x & 0x1fffff);
if( c<=0xFFFF ){
if( c>=0xd800 && c<=0xdfff ) c = 0xfffd;
*zOut++ = (u8)(c&0x00FF);
*zOut++ = (u8)((c>>8)&0x00FF);
if( c<0x00080 ){
*zOut++ = (u8)(c&0xFF);
}else if( c<0x00800 ){
*zOut++ = 0xC0 + (u8)((c>>6)&0x1F);
*zOut++ = 0x80 + (u8)(c & 0x3F);
}else if( c<0x10000 ){
*zOut++ = 0xE0 + (u8)((c>>12)&0x0F);
*zOut++ = 0x80 + (u8)((c>>6) & 0x3F);
*zOut++ = 0x80 + (u8)(c & 0x3F);
}else{
*zOut++ = (u8)(((c>>10)&0x003F) + (((c-0x10000)>>10)&0x00C0));
*zOut++ = (u8)(0x00D8 + (((c-0x10000)>>18)&0x03));
*zOut++ = (u8)(c&0x00FF);
*zOut++ = (u8)(0x00DC + ((c>>8)&0x03));
}
*zOut++ = 0xF0 + (u8)((c>>18) & 0x07);
*zOut++ = 0x80 + (u8)((c>>12) & 0x3F);
*zOut++ = 0x80 + (u8)((c>>6) & 0x3F);
*zOut++ = 0x80 + (u8)(c & 0x3F);
} \
}
sqlite3_result_text16le(context, (char*)z, (int)(zOut-z), sqlite3_free);
sqlite3_result_text(context, (char*)z, (int)(zOut-z), sqlite3_free);
}
#endif
/*
** The hex() function. Interpret the argument as a blob. Return
@@ -1643,9 +1646,7 @@ void sqlite3RegisterGlobalFunctions(void){
FUNCTION(substr, 2, 0, 0, substrFunc ),
FUNCTION(substr, 3, 0, 0, substrFunc ),
FUNCTION(unicode, 1, 0, 0, unicodeFunc ),
#ifndef SQLITE_OMIT_UTF16
FUNCTION(char, -1, 0, 0, charFunc ),
#endif
FUNCTION(abs, 1, 0, 0, absFunc ),
#ifndef SQLITE_OMIT_FLOATING_POINT
FUNCTION(round, 1, 0, 0, roundFunc ),