SQLite
Check-in [be24939052]
Not logged in
Overview
SHA1 Hash:be2493905281e12c7f4c146ab17c8872e52da350
Date: 2013-02-25 14:39:47
User: drh
Comment:Add new SQL functions unicode() and char().
Tags And Properties
Changes
hide diffs unified diffs patch

Changes to src/func.c

957 default: { 957 default: { 958 assert( sqlite3_value_type(argv[0])==SQLITE_NULL ); 958 assert( sqlite3_value_type(argv[0])==SQLITE_NULL ); 959 sqlite3_result_text(context, "NULL", 4, SQLITE_STATIC); 959 sqlite3_result_text(context, "NULL", 4, SQLITE_STATIC); 960 break; 960 break; 961 } 961 } 962 } 962 } 963 } 963 } > 964 > 965 /* > 966 ** The unicode() function. Return the integer unicode code-point value > 967 ** for the first character of the input string. > 968 */ > 969 static void unicodeFunc( > 970 sqlite3_context *context, > 971 int argc, > 972 sqlite3_value **argv > 973 ){ > 974 const unsigned char *z = sqlite3_value_text(argv[0]); > 975 if( z && z[0] ) sqlite3_result_int(context, sqlite3Utf8Read(&z)); > 976 } > 977 > 978 /* > 979 ** The char() function takes zero or more arguments, each of which is > 980 ** an integer. It constructs a string where each character of the string > 981 ** is the unicode character for the corresponding integer argument. > 982 */ > 983 static void charFunc( > 984 sqlite3_context *context, > 985 int argc, > 986 sqlite3_value **argv > 987 ){ > 988 unsigned char *z, *zOut; > 989 int i; > 990 zOut = z = sqlite3_malloc( argc*4 ); > 991 if( z==0 ){ > 992 sqlite3_result_error_nomem(context); > 993 return; > 994 } > 995 for(i=0; i<argc; i++){ > 996 sqlite3_int64 x = sqlite3_value_int64(argv[i]); > 997 unsigned c; > 998 x = sqlite3_value_int64(argv[i]); > 999 if( x<0 || x>0x10ffff ) x = 0xfffd; > 1000 c = (unsigned)(x & 0x1fffff); > 1001 if( c<=0xFFFF ){ > 1002 *zOut++ = (u8)(c&0x00FF); > 1003 *zOut++ = (u8)((c>>8)&0x00FF); > 1004 }else{ > 1005 if( c>=0xd800 && c<=0xdbff ) c = 0xfffd; > 1006 *zOut++ = (u8)(((c>>10)&0x003F) + (((c-0x10000)>>10)&0x00C0)); > 1007 *zOut++ = (u8)(0x00D8 + (((c-0x10000)>>18)&0x03)); > 1008 *zOut++ = (u8)(c&0x00FF); > 1009 *zOut++ = (u8)(0x00DC + ((c>>8)&0x03)); > 1010 } > 1011 } > 1012 sqlite3_result_text16le(context, (char*)z, (int)(zOut-z), sqlite3_free); > 1013 } 964 1014 965 /* 1015 /* 966 ** The hex() function. Interpret the argument as a blob. Return 1016 ** The hex() function. Interpret the argument as a blob. Return 967 ** a hexadecimal rendering as text. 1017 ** a hexadecimal rendering as text. 968 */ 1018 */ 969 static void hexFunc( 1019 static void hexFunc( 970 sqlite3_context *context, 1020 sqlite3_context *context, ................................................................................................................................................................................ 1585 FUNCTION(max, 0, 1, 1, 0 ), 1635 FUNCTION(max, 0, 1, 1, 0 ), 1586 AGGREGATE(max, 1, 1, 1, minmaxStep, minMaxFinalize ), 1636 AGGREGATE(max, 1, 1, 1, minmaxStep, minMaxFinalize ), 1587 FUNCTION2(typeof, 1, 0, 0, typeofFunc, SQLITE_FUNC_TYPEOF), 1637 FUNCTION2(typeof, 1, 0, 0, typeofFunc, SQLITE_FUNC_TYPEOF), 1588 FUNCTION2(length, 1, 0, 0, lengthFunc, SQLITE_FUNC_LENGTH), 1638 FUNCTION2(length, 1, 0, 0, lengthFunc, SQLITE_FUNC_LENGTH), 1589 FUNCTION(instr, 2, 0, 0, instrFunc ), 1639 FUNCTION(instr, 2, 0, 0, instrFunc ), 1590 FUNCTION(substr, 2, 0, 0, substrFunc ), 1640 FUNCTION(substr, 2, 0, 0, substrFunc ), 1591 FUNCTION(substr, 3, 0, 0, substrFunc ), 1641 FUNCTION(substr, 3, 0, 0, substrFunc ), > 1642 FUNCTION(unicode, 1, 0, 0, unicodeFunc ), > 1643 FUNCTION(char, -1, 0, 0, charFunc ), 1592 FUNCTION(abs, 1, 0, 0, absFunc ), 1644 FUNCTION(abs, 1, 0, 0, absFunc ), 1593 #ifndef SQLITE_OMIT_FLOATING_POINT 1645 #ifndef SQLITE_OMIT_FLOATING_POINT 1594 FUNCTION(round, 1, 0, 0, roundFunc ), 1646 FUNCTION(round, 1, 0, 0, roundFunc ), 1595 FUNCTION(round, 2, 0, 0, roundFunc ), 1647 FUNCTION(round, 2, 0, 0, roundFunc ), 1596 #endif 1648 #endif 1597 FUNCTION(upper, 1, 0, 0, upperFunc ), 1649 FUNCTION(upper, 1, 0, 0, upperFunc ), 1598 FUNCTION(lower, 1, 0, 0, lowerFunc ), 1650 FUNCTION(lower, 1, 0, 0, lowerFunc ),

Changes to test/func.test

1285 db eval {SELECT sum(length(x)) FROM t29} 1285 db eval {SELECT sum(length(x)) FROM t29} 1286 } {1000009} 1286 } {1000009} 1287 do_test func-29.6 { 1287 do_test func-29.6 { 1288 set x [lindex [sqlite3_db_status db CACHE_MISS 1] 1] 1288 set x [lindex [sqlite3_db_status db CACHE_MISS 1] 1] 1289 if {$x<5} {set x 1} 1289 if {$x<5} {set x 1} 1290 set x 1290 set x 1291 } {1} 1291 } {1} 1292 | 1292 > 1293 do_execsql_test func-30.1 {SELECT unicode('$');} 36 > 1294 do_execsql_test func-30.2 {SELECT unicode('¢');} 162 > 1295 do_execsql_test func-30.3 {SELECT unicode('€');} 8364 > 1296 do_execsql_test func-30.4 {SELECT char(36,162,8364);} {$¢€} 1293 1297 > 1298 for {set i 1} {$i<0xd800} {incr i 13} { > 1299 do_execsql_test func-30.5.$i {SELECT unicode(char($i))} $i > 1300 } > 1301 for {set i 57344} {$i<=0xfffd} {incr i 17} { > 1302 if {$i==0xfeff} continue > 1303 do_execsql_test func-30.5.$i {SELECT unicode(char($i))} $i > 1304 } > 1305 for {set i 65536} {$i<=0x10ffff} {incr i 139} { > 1306 do_execsql_test func-30.5.$i {SELECT unicode(char($i))} $i > 1307 } > 1308 1294 finish_test 1309 finish_test