Index: src/func.c ================================================================== --- src/func.c +++ src/func.c @@ -979,11 +979,10 @@ /* ** The char() function takes zero or more arguments, each of which is ** 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, sqlite3_value **argv ){ @@ -998,24 +997,28 @@ sqlite3_int64 x; unsigned c; 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)); - } - } - sqlite3_result_text16le(context, (char*)z, (int)(zOut-z), sqlite3_free); -} -#endif + *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_text(context, (char*)z, (int)(zOut-z), sqlite3_free); +} /* ** The hex() function. Interpret the argument as a blob. Return ** a hexadecimal rendering as text. */ @@ -1641,13 +1644,11 @@ FUNCTION2(length, 1, 0, 0, lengthFunc, SQLITE_FUNC_LENGTH), FUNCTION(instr, 2, 0, 0, instrFunc ), 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 ), FUNCTION(round, 2, 0, 0, roundFunc ), #endif Index: test/func.test ================================================================== --- test/func.test +++ test/func.test @@ -1291,16 +1291,10 @@ } {1} do_execsql_test func-30.1 {SELECT unicode('$');} 36 do_execsql_test func-30.2 [subst {SELECT unicode('\u00A2');}] 162 do_execsql_test func-30.3 [subst {SELECT unicode('\u20AC');}] 8364 - -ifcapable !utf16 { - finish_test - return -} - do_execsql_test func-30.4 {SELECT char(36,162,8364);} [subst {$\u00A2\u20AC}] for {set i 1} {$i<0xd800} {incr i 13} { do_execsql_test func-30.5.$i {SELECT unicode(char($i))} $i }