Many hyperlinks are disabled.
Use anonymous login
to enable hyperlinks.
Overview
Comment: | Added TRIM, LTRIM, and RTRIM functions. (CVS 3698) |
---|---|
Downloads: | Tarball | ZIP archive |
Timelines: | family | ancestors | descendants | both | trunk |
Files: | files | file ages | folders |
SHA1: |
6fe13eeade4fc7099fbda1e652064092 |
User & Date: | drh 2007-03-17 17:52:42.000 |
Context
2007-03-17
| ||
18:22 | Add documentation of the REPLACE, TRIM, LTRIM, and RTRIM functions. (CVS 3699) (check-in: d42c963620 user: drh tags: trunk) | |
17:52 | Added TRIM, LTRIM, and RTRIM functions. (CVS 3698) (check-in: 6fe13eeade user: drh tags: trunk) | |
13:27 | First cut at an implementation of the REPLACE() function. We might yet make this a compile-time option or move it into a separate source file. (CVS 3697) (check-in: c2fe746ea7 user: drh tags: trunk) | |
Changes
Changes to src/func.c.
︙ | ︙ | |||
12 13 14 15 16 17 18 | ** This file contains the C functions that implement various SQL ** functions of SQLite. ** ** There is only one exported symbol in this file - the function ** sqliteRegisterBuildinFunctions() found at the bottom of the file. ** All other code has file scope. ** | | | 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | ** This file contains the C functions that implement various SQL ** functions of SQLite. ** ** There is only one exported symbol in this file - the function ** sqliteRegisterBuildinFunctions() found at the bottom of the file. ** All other code has file scope. ** ** $Id: func.c,v 1.138 2007/03/17 17:52:42 drh Exp $ */ #include "sqliteInt.h" #include <ctype.h> /* #include <math.h> */ #include <stdlib.h> #include <assert.h> #include "vdbeInt.h" |
︙ | ︙ | |||
693 694 695 696 697 698 699 | assert( argc==3 ); if( sqlite3_value_type(argv[0])==SQLITE_NULL || sqlite3_value_type(argv[1])==SQLITE_NULL || sqlite3_value_type(argv[2])==SQLITE_NULL ){ return; } | < | | > | 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 | assert( argc==3 ); if( sqlite3_value_type(argv[0])==SQLITE_NULL || sqlite3_value_type(argv[1])==SQLITE_NULL || sqlite3_value_type(argv[2])==SQLITE_NULL ){ return; } zStr = sqlite3_value_text(argv[0]); nStr = sqlite3_value_bytes(argv[0]); zPattern = sqlite3_value_text(argv[1]); nPattern = sqlite3_value_bytes(argv[1]); zRep = sqlite3_value_text(argv[2]); nRep = sqlite3_value_bytes(argv[2]); if( nPattern>=nRep ){ nOut = nStr; }else{ nOut = (nStr/nPattern + 1)*nRep; } zOut = sqlite3_malloc(nOut+1); if( zOut==0 ) return; |
︙ | ︙ | |||
723 724 725 726 727 728 729 730 731 732 733 734 735 736 | memcpy(&zOut[j], &zStr[i], nStr-i); j += nStr - i; assert( j<=nOut ); zOut[j] = 0; sqlite3_result_text(context, (char*)zOut, j, sqlite3_free); } #ifdef SQLITE_SOUNDEX /* ** Compute the soundex encoding of a word. */ static void soundexFunc( sqlite3_context *context, | > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 | memcpy(&zOut[j], &zStr[i], nStr-i); j += nStr - i; assert( j<=nOut ); zOut[j] = 0; sqlite3_result_text(context, (char*)zOut, j, sqlite3_free); } /* ** Implementation of the TRIM(), LTRIM(), and RTRIM() functions. ** The userdata is 0x1 for left trim, 0x2 for right trim, 0x3 for both. */ static void trimFunc( sqlite3_context *context, int argc, sqlite3_value **argv ){ const unsigned char *zIn; /* Input string */ const unsigned char *zCharSet; /* Set of characters to trim */ int nIn; /* Number of bytes in input */ int flags; int i; unsigned char cFirst, cNext; if( sqlite3_value_type(argv[0])==SQLITE_NULL ){ return; } zIn = sqlite3_value_text(argv[0]); nIn = sqlite3_value_bytes(argv[0]); if( argc==1 ){ static const unsigned char zSpace[] = " "; zCharSet = zSpace; }else if( sqlite3_value_type(argv[1])==SQLITE_NULL ){ return; }else{ zCharSet = sqlite3_value_text(argv[1]); } cFirst = zCharSet[0]; if( cFirst ){ flags = (int)sqlite3_user_data(context); if( flags & 1 ){ for(; nIn>0; nIn--, zIn++){ if( cFirst==zIn[0] ) continue; for(i=1; zCharSet[i] && zCharSet[i]!=zIn[0]; i++){} if( zCharSet[i]==0 ) break; } } if( flags & 2 ){ for(; nIn>0; nIn--){ cNext = zIn[nIn-1]; if( cFirst==cNext ) continue; for(i=1; zCharSet[i] && zCharSet[i]!=cNext; i++){} if( zCharSet[i]==0 ) break; } } } sqlite3_result_text(context, (char*)zIn, nIn, SQLITE_TRANSIENT); } #ifdef SQLITE_SOUNDEX /* ** Compute the soundex encoding of a word. */ static void soundexFunc( sqlite3_context *context, |
︙ | ︙ | |||
1075 1076 1077 1078 1079 1080 1081 | ** the only difference between the two being that the sense of the ** comparison is inverted. For the max() aggregate, the ** sqlite3_user_data() function returns (void *)-1. For min() it ** returns (void *)db, where db is the sqlite3* database pointer. ** Therefore the next statement sets variable 'max' to 1 for the max() ** aggregate, or 0 for min(). */ | | | 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 | ** the only difference between the two being that the sense of the ** comparison is inverted. For the max() aggregate, the ** sqlite3_user_data() function returns (void *)-1. For min() it ** returns (void *)db, where db is the sqlite3* database pointer. ** Therefore the next statement sets variable 'max' to 1 for the max() ** aggregate, or 0 for min(). */ max = sqlite3_user_data(context)!=0; cmp = sqlite3MemCompare(pBest, pArg, pColl); if( (max && cmp<0) || (!max && cmp>0) ){ sqlite3VdbeMemCopy(pBest, pArg); } }else{ sqlite3VdbeMemCopy(pBest, pArg); } |
︙ | ︙ | |||
1105 1106 1107 1108 1109 1110 1111 | ** functions. This should be the only routine in this file with ** external linkage. */ void sqlite3RegisterBuiltinFunctions(sqlite3 *db){ static const struct { char *zName; signed char nArg; | | | | | 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 | ** functions. This should be the only routine in this file with ** external linkage. */ void sqlite3RegisterBuiltinFunctions(sqlite3 *db){ static const struct { char *zName; signed char nArg; u8 argType; /* ff: db 1: 0, 2: 1, 3: 2,... N: N-1. */ u8 eTextRep; /* 1: UTF-16. 0: UTF-8 */ u8 needCollSeq; void (*xFunc)(sqlite3_context*,int,sqlite3_value **); } aFuncs[] = { { "min", -1, 0, SQLITE_UTF8, 1, minmaxFunc }, { "min", 0, 0, SQLITE_UTF8, 1, 0 }, { "max", -1, 1, SQLITE_UTF8, 1, minmaxFunc }, { "max", 0, 1, SQLITE_UTF8, 1, 0 }, { "typeof", 1, 0, SQLITE_UTF8, 0, typeofFunc }, { "length", 1, 0, SQLITE_UTF8, 0, lengthFunc }, { "substr", 3, 0, SQLITE_UTF8, 0, substrFunc }, #ifndef SQLITE_OMIT_UTF16 { "substr", 3, 0, SQLITE_UTF16LE, 0, sqlite3utf16Substr }, #endif { "abs", 1, 0, SQLITE_UTF8, 0, absFunc }, |
︙ | ︙ | |||
1135 1136 1137 1138 1139 1140 1141 | { "hex", 1, 0, SQLITE_UTF8, 0, hexFunc }, { "ifnull", 2, 0, SQLITE_UTF8, 1, ifnullFunc }, { "random", -1, 0, SQLITE_UTF8, 0, randomFunc }, { "randomblob", 1, 0, SQLITE_UTF8, 0, randomBlob }, { "nullif", 2, 0, SQLITE_UTF8, 1, nullifFunc }, { "sqlite_version", 0, 0, SQLITE_UTF8, 0, versionFunc}, { "quote", 1, 0, SQLITE_UTF8, 0, quoteFunc }, | | | | > > > > > > | | | | | | | | | | | > | > | | < < < < | 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 | { "hex", 1, 0, SQLITE_UTF8, 0, hexFunc }, { "ifnull", 2, 0, SQLITE_UTF8, 1, ifnullFunc }, { "random", -1, 0, SQLITE_UTF8, 0, randomFunc }, { "randomblob", 1, 0, SQLITE_UTF8, 0, randomBlob }, { "nullif", 2, 0, SQLITE_UTF8, 1, nullifFunc }, { "sqlite_version", 0, 0, SQLITE_UTF8, 0, versionFunc}, { "quote", 1, 0, SQLITE_UTF8, 0, quoteFunc }, { "last_insert_rowid", 0, 0xff, SQLITE_UTF8, 0, last_insert_rowid }, { "changes", 0, 0xff, SQLITE_UTF8, 0, changes }, { "total_changes", 0, 0xff, SQLITE_UTF8, 0, total_changes }, { "replace", 3, 0, SQLITE_UTF8, 0, replaceFunc }, { "ltrim", 1, 1, SQLITE_UTF8, 0, trimFunc }, { "ltrim", 2, 1, SQLITE_UTF8, 0, trimFunc }, { "rtrim", 1, 2, SQLITE_UTF8, 0, trimFunc }, { "rtrim", 2, 2, SQLITE_UTF8, 0, trimFunc }, { "trim", 1, 3, SQLITE_UTF8, 0, trimFunc }, { "trim", 2, 3, SQLITE_UTF8, 0, trimFunc }, #ifdef SQLITE_SOUNDEX { "soundex", 1, 0, SQLITE_UTF8, 0, soundexFunc}, #endif #ifndef SQLITE_OMIT_LOAD_EXTENSION { "load_extension", 1, 0xff, SQLITE_UTF8, 0, loadExt }, { "load_extension", 2, 0xff, SQLITE_UTF8, 0, loadExt }, #endif #ifdef SQLITE_TEST { "randstr", 2, 0, SQLITE_UTF8, 0, randStr }, { "test_destructor", 1, 0xff, SQLITE_UTF8, 0, test_destructor}, { "test_destructor_count", 0, 0, SQLITE_UTF8, 0, test_destructor_count}, { "test_auxdata", -1, 0, SQLITE_UTF8, 0, test_auxdata}, { "test_error", 1, 0, SQLITE_UTF8, 0, test_error}, #endif }; static const struct { char *zName; signed char nArg; u8 argType; u8 needCollSeq; void (*xStep)(sqlite3_context*,int,sqlite3_value**); void (*xFinalize)(sqlite3_context*); } aAggs[] = { { "min", 1, 0, 1, minmaxStep, minMaxFinalize }, { "max", 1, 1, 1, minmaxStep, minMaxFinalize }, { "sum", 1, 0, 0, sumStep, sumFinalize }, { "total", 1, 0, 0, sumStep, totalFinalize }, { "avg", 1, 0, 0, sumStep, avgFinalize }, { "count", 0, 0, 0, countStep, countFinalize }, { "count", 1, 0, 0, countStep, countFinalize }, }; int i; for(i=0; i<sizeof(aFuncs)/sizeof(aFuncs[0]); i++){ void *pArg; u8 argType = aFuncs[i].argType; if( argType==0xff ){ pArg = db; }else{ pArg = (void*)(int)argType; } sqlite3CreateFunc(db, aFuncs[i].zName, aFuncs[i].nArg, aFuncs[i].eTextRep, pArg, aFuncs[i].xFunc, 0, 0); if( aFuncs[i].needCollSeq ){ FuncDef *pFunc = sqlite3FindFunction(db, aFuncs[i].zName, strlen(aFuncs[i].zName), aFuncs[i].nArg, aFuncs[i].eTextRep, 0); if( pFunc && aFuncs[i].needCollSeq ){ pFunc->needCollSeq = 1; } } } #ifndef SQLITE_OMIT_ALTERTABLE sqlite3AlterFunctions(db); #endif #ifndef SQLITE_OMIT_PARSER sqlite3AttachFunctions(db); #endif for(i=0; i<sizeof(aAggs)/sizeof(aAggs[0]); i++){ void *pArg = (void*)(int)aAggs[i].argType; sqlite3CreateFunc(db, aAggs[i].zName, aAggs[i].nArg, SQLITE_UTF8, pArg, 0, aAggs[i].xStep, aAggs[i].xFinalize); if( aAggs[i].needCollSeq ){ FuncDef *pFunc = sqlite3FindFunction( db, aAggs[i].zName, strlen(aAggs[i].zName), aAggs[i].nArg, SQLITE_UTF8, 0); if( pFunc && aAggs[i].needCollSeq ){ pFunc->needCollSeq = 1; |
︙ | ︙ |
Changes to test/func.test.
1 2 3 4 5 6 7 8 9 10 11 12 13 | # 2001 September 15 # # The author disclaims copyright to this source code. In place of # a legal notice, here is a blessing: # # May you do good and not evil. # May you find forgiveness for yourself and forgive others. # May you share freely, never taking more than you give. # #*********************************************************************** # This file implements regression tests for SQLite library. The # focus of this file is testing built-in functions. # | | | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | # 2001 September 15 # # The author disclaims copyright to this source code. In place of # a legal notice, here is a blessing: # # May you do good and not evil. # May you find forgiveness for yourself and forgive others. # May you share freely, never taking more than you give. # #*********************************************************************** # This file implements regression tests for SQLite library. The # focus of this file is testing built-in functions. # # $Id: func.test,v 1.59 2007/03/17 17:52:42 drh Exp $ set testdir [file dirname $argv0] source $testdir/tester.tcl # Create a table to work with. # do_test func-0.0 { |
︙ | ︙ | |||
436 437 438 439 440 441 442 | DROP TABLE t4; } } {} # Test that the auxdata API for scalar functions works. This test uses # a special user-defined function only available in test builds, # test_auxdata(). Function test_auxdata() takes any number of arguments. | < | 436 437 438 439 440 441 442 443 444 445 446 447 448 449 | DROP TABLE t4; } } {} # Test that the auxdata API for scalar functions works. This test uses # a special user-defined function only available in test builds, # test_auxdata(). Function test_auxdata() takes any number of arguments. do_test func-13.1 { execsql { SELECT test_auxdata('hello world'); } } {0} do_test func-13.2 { |
︙ | ︙ | |||
768 769 770 771 772 773 774 | } {{This is the larger-main test string}} do_test func-21.8 { execsql { SELECT replace("aaaaaaa", "a", "0123456789"); } } {0123456789012345678901234567890123456789012345678901234567890123456789} | > | > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 | } {{This is the larger-main test string}} do_test func-21.8 { execsql { SELECT replace("aaaaaaa", "a", "0123456789"); } } {0123456789012345678901234567890123456789012345678901234567890123456789} # Tests for the TRIM, LTRIM and RTRIM functions. # do_test func-22.1 { catchsql {SELECT trim(1,2,3)} } {1 {wrong number of arguments to function trim()}} do_test func-22.2 { catchsql {SELECT ltrim(1,2,3)} } {1 {wrong number of arguments to function ltrim()}} do_test func-22.3 { catchsql {SELECT rtrim(1,2,3)} } {1 {wrong number of arguments to function rtrim()}} do_test func-22.4 { execsql {SELECT trim(' hi ');} } {hi} do_test func-22.5 { execsql {SELECT ltrim(' hi ');} } {{hi }} do_test func-22.6 { execsql {SELECT rtrim(' hi ');} } {{ hi}} do_test func-22.7 { execsql {SELECT trim(' hi ','xyz');} } {{ hi }} do_test func-22.8 { execsql {SELECT ltrim(' hi ','xyz');} } {{ hi }} do_test func-22.9 { execsql {SELECT rtrim(' hi ','xyz');} } {{ hi }} do_test func-22.10 { execsql {SELECT trim('xyxzy hi zzzy','xyz');} } {{ hi }} do_test func-22.11 { execsql {SELECT ltrim('xyxzy hi zzzy','xyz');} } {{ hi zzzy}} do_test func-22.12 { execsql {SELECT rtrim('xyxzy hi zzzy','xyz');} } {{xyxzy hi }} do_test func-22.13 { execsql {SELECT trim(' hi ','');} } {{ hi }} do_test func-22.20 { execsql {SELECT typeof(trim(NULL));} } {null} do_test func-22.21 { execsql {SELECT typeof(trim(NULL,'xyz'));} } {null} do_test func-22.22 { execsql {SELECT typeof(trim('hello',NULL));} } {null} finish_test |