/ Check-in [9ef4c24a]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Code cleanup: get rid of the sqlite3SetNString utility function. (CVS 1984)
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 9ef4c24a9acc2128891303de1ffd2ef4509d779c
User & Date: drh 2004-09-25 14:39:18
Context
2004-09-25
15:25
Optimizations in the tokenizer. (CVS 1985) check-in: 26898c57 user: drh tags: trunk
14:39
Code cleanup: get rid of the sqlite3SetNString utility function. (CVS 1984) check-in: 9ef4c24a user: drh tags: trunk
13:12
Code simplifications and size reductions. (CVS 1983) check-in: cb631a13 user: drh tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to src/build.c.

    19     19   **     DROP INDEX
    20     20   **     creating ID lists
    21     21   **     BEGIN TRANSACTION
    22     22   **     COMMIT
    23     23   **     ROLLBACK
    24     24   **     PRAGMA
    25     25   **
    26         -** $Id: build.c,v 1.253 2004/09/15 13:38:11 drh Exp $
           26  +** $Id: build.c,v 1.254 2004/09/25 14:39:18 drh Exp $
    27     27   */
    28     28   #include "sqliteInt.h"
    29     29   #include <ctype.h>
    30     30   
    31     31   /*
    32     32   ** This routine is called when a new SQL statement is beginning to
    33     33   ** be parsed.  Check to see if the schema for the database needs
................................................................................
   781    781     Column *pCol;
   782    782     if( (p = pParse->pNewTable)==0 ) return;
   783    783     i = p->nCol-1;
   784    784     if( i<0 ) return;
   785    785     pCol = &p->aCol[i];
   786    786     pz = &pCol->zType;
   787    787     n = pLast->n + Addr(pLast->z) - Addr(pFirst->z);
   788         -  sqlite3SetNString(pz, pFirst->z, n, (char*)0);
   789         -  z = *pz;
          788  +  assert( pCol->zType==0 );
          789  +  z = pCol->zType = sqlite3MPrintf("%.*s", n, pFirst->z);
   790    790     if( z==0 ) return;
   791    791     for(i=j=0; z[i]; i++){
   792    792       int c = z[i];
   793    793       if( isspace(c) ) continue;
   794    794       z[j++] = c;
   795    795     }
   796    796     z[j] = 0;
................................................................................
   804    804   **
   805    805   ** This routine is called by the parser while in the middle of
   806    806   ** parsing a CREATE TABLE statement.
   807    807   */
   808    808   void sqlite3AddDefaultValue(Parse *pParse, Token *pVal, int minusFlag){
   809    809     Table *p;
   810    810     int i;
   811         -  char **pz;
          811  +  char *z;
   812    812     if( (p = pParse->pNewTable)==0 ) return;
   813    813     i = p->nCol-1;
   814    814     if( i<0 ) return;
   815         -  pz = &p->aCol[i].zDflt;
   816         -  if( minusFlag ){
   817         -    sqlite3SetNString(pz, "-", 1, pVal->z, pVal->n, (char*)0);
   818         -  }else{
   819         -    sqlite3SetNString(pz, pVal->z, pVal->n, (char*)0);
   820         -  }
   821         -  sqlite3Dequote(*pz);
          815  +  assert( p->aCol[i].zDflt==0 );
          816  +  z = p->aCol[i].zDflt = sqlite3MPrintf("%s%T", minusFlag ? "-" : "", pVal);
          817  +  sqlite3Dequote(z);
   822    818   }
   823    819   
   824    820   /*
   825    821   ** Designate the PRIMARY KEY for the table.  pList is a list of names 
   826    822   ** of columns that form the primary key.  If pList is NULL, then the
   827    823   ** most recently added column of the table is the primary key.
   828    824   **
................................................................................
  1012   1008       pColl2 = sqlite3FindCollSeq(db, aEnc[i], z, n, 0);
  1013   1009       if( pColl2->xCmp!=0 ){
  1014   1010         memcpy(pColl, pColl2, sizeof(CollSeq));
  1015   1011         return SQLITE_OK;
  1016   1012       }
  1017   1013     }
  1018   1014     if( pParse->nErr==0 ){
  1019         -    sqlite3SetNString(&pParse->zErrMsg, "no such collation sequence: ", 
  1020         -        -1, z, n, (char*)0);
         1015  +    sqlite3ErrorMsg(pParse, "no such collation sequence: %.*s", n, z);
  1021   1016     }
  1022   1017     pParse->nErr++;
  1023   1018     return SQLITE_ERROR;
  1024   1019   }
  1025   1020   
  1026   1021   /*
  1027   1022   ** This routine is called on a collation sequence before it is used to
................................................................................
  1078   1073   ** another error occurs, NULL is returned and an error message written into
  1079   1074   ** pParse.
  1080   1075   */
  1081   1076   CollSeq *sqlite3LocateCollSeq(Parse *pParse, const char *zName, int nName){
  1082   1077     u8 enc = pParse->db->enc;
  1083   1078     u8 initbusy = pParse->db->init.busy;
  1084   1079     CollSeq *pColl = sqlite3FindCollSeq(pParse->db, enc, zName, nName, initbusy);
         1080  +  if( nName<0 ) nName = strlen(zName);
  1085   1081     if( !initbusy && (!pColl || !pColl->xCmp) ){
  1086   1082       /* No collation sequence of this type for this encoding is registered.
  1087   1083       ** Call the collation factory to see if it can supply us with one.
  1088   1084       */
  1089   1085       callCollNeeded(pParse->db, zName, nName);
  1090   1086       pColl = sqlite3FindCollSeq(pParse->db, enc, zName, nName, 0);
  1091   1087       if( pColl && !pColl->xCmp ){
................................................................................
  1097   1093         }
  1098   1094       }
  1099   1095     }
  1100   1096   
  1101   1097     /* If nothing has been found, write the error message into pParse */
  1102   1098     if( !initbusy && (!pColl || !pColl->xCmp) ){
  1103   1099       if( pParse->nErr==0 ){
  1104         -      sqlite3SetNString(&pParse->zErrMsg, "no such collation sequence: ", -1,
  1105         -          zName, nName, (char*)0);
         1100  +      sqlite3ErrorMsg(pParse, "no such collation sequence: %.*s", nName, zName);
  1106   1101       }
  1107         -    pParse->nErr++;
  1108   1102       pColl = 0;
  1109   1103     }
  1110   1104     return pColl;
  1111   1105   }
  1112   1106   
  1113   1107   
  1114   1108   

Changes to src/main.c.

    10     10   **
    11     11   *************************************************************************
    12     12   ** Main file for the SQLite library.  The routines in this file
    13     13   ** implement the programmer interface to the library.  Routines in
    14     14   ** other files are for internal use by SQLite and should not be
    15     15   ** accessed by users of the library.
    16     16   **
    17         -** $Id: main.c,v 1.260 2004/09/15 13:38:11 drh Exp $
           17  +** $Id: main.c,v 1.261 2004/09/25 14:39:18 drh Exp $
    18     18   */
    19     19   #include "sqliteInt.h"
    20     20   #include "os.h"
    21     21   #include <ctype.h>
    22     22   
    23     23   /*
    24     24   ** The following constant value is used by the SQLITE_BIGENDIAN and
................................................................................
   665    665   
   666    666   /*
   667    667   ** Windows systems should call this routine to free memory that
   668    668   ** is returned in the in the errmsg parameter of sqlite3_open() when
   669    669   ** SQLite is a DLL.  For some reason, it does not work to call free()
   670    670   ** directly.
   671    671   **
   672         -** Note that we need to call free() not sqliteFree() here, since every
   673         -** string that is exported from SQLite should have already passed through
   674         -** sqlite3StrRealloc().
          672  +** Note that we need to call free() not sqliteFree() here.
   675    673   */
   676    674   void sqlite3_free(char *p){ free(p); }
   677    675   
   678    676   /*
   679    677   ** Create new user functions.
   680    678   */
   681    679   int sqlite3_create_function(

Changes to src/pragma.c.

     7      7   **    May you do good and not evil.
     8      8   **    May you find forgiveness for yourself and forgive others.
     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12     12   ** This file contains code used to implement the PRAGMA command.
    13     13   **
    14         -** $Id: pragma.c,v 1.66 2004/09/17 20:25:25 drh Exp $
           14  +** $Id: pragma.c,v 1.67 2004/09/25 14:39:19 drh Exp $
    15     15   */
    16     16   #include "sqliteInt.h"
    17     17   #include <ctype.h>
    18     18   
    19     19   #if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
    20     20   # include "pager.h"
    21     21   # include "btree.h"
................................................................................
   203    203     iDb = sqlite3TwoPartName(pParse, pId1, pId2, &pId);
   204    204     if( iDb<0 ) return;
   205    205     pDb = &db->aDb[iDb];
   206    206   
   207    207     zLeft = sqlite3NameFromToken(pId);
   208    208     if( !zLeft ) return;
   209    209     if( minusFlag ){
   210         -    zRight = 0;
   211         -    sqlite3SetNString(&zRight, "-", 1, pValue->z, pValue->n, (char*)0);
          210  +    zRight = sqlite3MPrintf("-%T", pValue);
   212    211     }else{
   213    212       zRight = sqlite3NameFromToken(pValue);
   214    213     }
   215    214   
   216    215     zDb = ((iDb>0)?pDb->zName:0);
   217    216     if( sqlite3AuthCheck(pParse, SQLITE_PRAGMA, zLeft, zRight, zDb) ){
   218    217       goto pragma_out;

Changes to src/select.c.

     8      8   **    May you find forgiveness for yourself and forgive others.
     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12     12   ** This file contains C code routines that are called by the parser
    13     13   ** to handle SELECT statements in SQLite.
    14     14   **
    15         -** $Id: select.c,v 1.210 2004/09/25 13:12:16 drh Exp $
           15  +** $Id: select.c,v 1.211 2004/09/25 14:39:19 drh Exp $
    16     16   */
    17     17   #include "sqliteInt.h"
    18     18   
    19     19   
    20     20   /*
    21     21   ** Allocate a new Select structure and return a pointer to that
    22     22   ** structure.
................................................................................
   112    112         break;
   113    113       }
   114    114     }
   115    115     if(
   116    116        (jointype & (JT_INNER|JT_OUTER))==(JT_INNER|JT_OUTER) ||
   117    117        (jointype & JT_ERROR)!=0
   118    118     ){
   119         -    static Token dummy = { 0, 0 };
   120         -    char *zSp1 = " ", *zSp2 = " ";
   121         -    if( pB==0 ){ pB = &dummy; zSp1 = 0; }
   122         -    if( pC==0 ){ pC = &dummy; zSp2 = 0; }
   123         -    sqlite3SetNString(&pParse->zErrMsg, "unknown or unsupported join type: ", 0,
   124         -       pA->z, pA->n, zSp1, 1, pB->z, pB->n, zSp2, 1, pC->z, pC->n, (char*)0);
   125         -    pParse->nErr++;
          119  +    const char *zSp1 = " ";
          120  +    const char *zSp2 = " ";
          121  +    if( pB==0 ){ zSp1++; }
          122  +    if( pC==0 ){ zSp2++; }
          123  +    sqlite3ErrorMsg(pParse, "unknown or unsupported join type: "
          124  +       "%T%s%T%s%T", pA, zSp1, pB, zSp2, pC);
   126    125       jointype = JT_INNER;
   127    126     }else if( jointype & JT_RIGHT ){
   128    127       sqlite3ErrorMsg(pParse, 
   129    128         "RIGHT and FULL OUTER JOINs are not currently supported");
   130    129       jointype = JT_INNER;
   131    130     }
   132    131     return jointype;
................................................................................
   476    475         assert( nColumn==1 );
   477    476         sqlite3VdbeAddOp(v, OP_NotNull, -1, addr1+3);
   478    477         sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
   479    478         addr2 = sqlite3VdbeAddOp(v, OP_Goto, 0, 0);
   480    479         if( pOrderBy ){
   481    480           pushOntoSorter(pParse, v, pOrderBy);
   482    481         }else{
   483         -        char const *affStr;
   484    482           char aff = (iParm>>16)&0xFF;
   485    483           aff = sqlite3CompareAffinity(pEList->a[0].pExpr, aff);
   486    484           sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, &aff, 1);
   487    485           sqlite3VdbeAddOp(v, OP_String8, 0, 0);
   488    486           sqlite3VdbeAddOp(v, OP_PutStrKey, (iParm&0x0000FFFF), 0);
   489    487         }
   490    488         sqlite3VdbeChangeP2(v, addr2, sqlite3VdbeCurrentAddr(v));

Changes to src/sqliteInt.h.

     7      7   **    May you do good and not evil.
     8      8   **    May you find forgiveness for yourself and forgive others.
     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12     12   ** Internal interface definitions for SQLite.
    13     13   **
    14         -** @(#) $Id: sqliteInt.h,v 1.323 2004/09/25 13:12:16 drh Exp $
           14  +** @(#) $Id: sqliteInt.h,v 1.324 2004/09/25 14:39:19 drh Exp $
    15     15   */
    16     16   #ifndef _SQLITEINT_H_
    17     17   #define _SQLITEINT_H_
    18     18   
    19     19   #include "config.h"
    20     20   #include "sqlite3.h"
    21     21   #include "hash.h"
................................................................................
   193    193   #ifdef SQLITE_DEBUG
   194    194   # define sqliteMalloc(X)    sqlite3Malloc_(X,1,__FILE__,__LINE__)
   195    195   # define sqliteMallocRaw(X) sqlite3Malloc_(X,0,__FILE__,__LINE__)
   196    196   # define sqliteFree(X)      sqlite3Free_(X,__FILE__,__LINE__)
   197    197   # define sqliteRealloc(X,Y) sqlite3Realloc_(X,Y,__FILE__,__LINE__)
   198    198   # define sqliteStrDup(X)    sqlite3StrDup_(X,__FILE__,__LINE__)
   199    199   # define sqliteStrNDup(X,Y) sqlite3StrNDup_(X,Y,__FILE__,__LINE__)
   200         -  void sqlite3StrRealloc(char**);
   201    200   #else
   202    201   # define sqliteFree          sqlite3FreeX
   203    202   # define sqliteMalloc        sqlite3Malloc
   204    203   # define sqliteMallocRaw     sqlite3MallocRaw
   205    204   # define sqliteRealloc       sqlite3Realloc
   206         -/* # define sqliteRealloc_(X,Y) sqlite3Realloc(X,Y) */
   207         -# define sqlite3StrRealloc(X)
   208         -# define sqliteStrDup         sqlite3StrDup
   209         -# define sqliteStrNDup        sqlite3StrNDup
          205  +# define sqliteStrDup        sqlite3StrDup
          206  +# define sqliteStrNDup       sqlite3StrNDup
   210    207   #endif
   211    208   
   212    209   /*
   213    210   ** This variable gets set if malloc() ever fails.  After it gets set,
   214    211   ** the SQLite library shuts down permanently.
   215    212   */
   216    213   extern int sqlite3_malloc_failed;
................................................................................
  1204   1201   #endif
  1205   1202   void sqlite3FreeX(void*);
  1206   1203   char *sqlite3MPrintf(const char*, ...);
  1207   1204   char *sqlite3VMPrintf(const char*, va_list);
  1208   1205   void sqlite3DebugPrintf(const char*, ...);
  1209   1206   void *sqlite3TextToPtr(const char*);
  1210   1207   void sqlite3SetString(char **, const char *, ...);
  1211         -void sqlite3SetNString(char **, ...);
  1212   1208   void sqlite3ErrorMsg(Parse*, const char*, ...);
  1213   1209   void sqlite3Dequote(char*);
  1214   1210   int sqlite3KeywordCode(const char*, int);
  1215   1211   int sqlite3RunParser(Parse*, const char*, char **);
  1216   1212   void sqlite3FinishCoding(Parse*);
  1217   1213   Expr *sqlite3Expr(int, Expr*, Expr*, Token*);
  1218   1214   Expr *sqlite3ExprAnd(Expr*, Expr*);

Changes to src/table.c.

   146    146       res.azResult[0] = (char*)res.nData;
   147    147     }
   148    148     if( rc==SQLITE_ABORT ){
   149    149       sqlite3_free_table(&res.azResult[1]);
   150    150       if( res.zErrMsg ){
   151    151         if( pzErrMsg ){
   152    152           free(*pzErrMsg);
   153         -        *pzErrMsg = res.zErrMsg;
   154         -        sqlite3StrRealloc(pzErrMsg);
   155         -      }else{
   156         -        sqliteFree(res.zErrMsg);
          153  +        *pzErrMsg = sqlite3_mprintf("%s",res.zErrMsg);
   157    154         }
          155  +      sqliteFree(res.zErrMsg);
   158    156       }
   159    157       return res.rc;
   160    158     }
   161    159     sqliteFree(res.zErrMsg);
   162    160     if( rc!=SQLITE_OK ){
   163    161       sqlite3_free_table(&res.azResult[1]);
   164    162       return rc;

Changes to src/tokenize.c.

    11     11   *************************************************************************
    12     12   ** An tokenizer for SQL
    13     13   **
    14     14   ** This file contains C code that splits an SQL input string up into
    15     15   ** individual tokens and sends those tokens one-by-one over to the
    16     16   ** parser for analysis.
    17     17   **
    18         -** $Id: tokenize.c,v 1.87 2004/09/08 15:09:41 drh Exp $
           18  +** $Id: tokenize.c,v 1.88 2004/09/25 14:39:19 drh Exp $
    19     19   */
    20     20   #include "sqliteInt.h"
    21     21   #include "os.h"
    22     22   #include <ctype.h>
    23     23   #include <stdlib.h>
    24     24   
    25     25   /*
................................................................................
   496    496             pParse->rc = SQLITE_INTERRUPT;
   497    497             sqlite3SetString(pzErrMsg, "interrupt", (char*)0);
   498    498             goto abort_parse;
   499    499           }
   500    500           break;
   501    501         }
   502    502         case TK_ILLEGAL: {
   503         -        sqlite3SetNString(pzErrMsg, "unrecognized token: \"", -1, 
   504         -           pParse->sLastToken.z, pParse->sLastToken.n, "\"", 1, (char*)0);
          503  +        if( pzErrMsg ){
          504  +          sqliteFree(*pzErrMsg);
          505  +          *pzErrMsg = sqlite3MPrintf("unrecognized token: \"%T\"",
          506  +                          &pParse->sLastToken);
          507  +        }
   505    508           nErr++;
   506    509           goto abort_parse;
   507    510         }
   508    511         case TK_SEMI: {
   509    512           pParse->zTail = &zSql[i];
   510    513           /* Fall thru into the default case */
   511    514         }

Changes to src/util.c.

    10     10   **
    11     11   *************************************************************************
    12     12   ** Utility functions used throughout sqlite.
    13     13   **
    14     14   ** This file contains functions for allocating memory, comparing
    15     15   ** strings, and stuff like that.
    16     16   **
    17         -** $Id: util.c,v 1.117 2004/09/08 20:13:06 drh Exp $
           17  +** $Id: util.c,v 1.118 2004/09/25 14:39:19 drh Exp $
    18     18   */
    19     19   #include "sqliteInt.h"
    20     20   #include <stdarg.h>
    21     21   #include <ctype.h>
    22     22   
    23     23   #if SQLITE_DEBUG>2 && defined(__GLIBC__)
    24     24   #include <execinfo.h>
................................................................................
   210    210     print_stack_trace();
   211    211     fprintf(stderr,"%06d realloc %d to %d bytes at 0x%x to 0x%x at %s:%d\n",
   212    212       ++memcnt, oldN, n, (int)oldP, (int)p, zFile, line);
   213    213   #endif
   214    214     return p;
   215    215   }
   216    216   
   217         -/*
   218         -** Make a duplicate of a string into memory obtained from malloc()
   219         -** Free the original string using sqliteFree().
   220         -**
   221         -** This routine is called on all strings that are passed outside of
   222         -** the SQLite library.  That way clients can free the string using free()
   223         -** rather than having to call sqliteFree().
   224         -*/
   225         -void sqlite3StrRealloc(char **pz){
   226         -  char *zNew;
   227         -  if( pz==0 || *pz==0 ) return;
   228         -  zNew = malloc( strlen(*pz) + 1 );
   229         -  if( zNew==0 ){
   230         -    sqlite3_malloc_failed++;
   231         -    sqliteFree(*pz);
   232         -    *pz = 0;
   233         -  }
   234         -  strcpy(zNew, *pz);
   235         -  sqliteFree(*pz);
   236         -  *pz = zNew;
   237         -}
   238         -
   239    217   /*
   240    218   ** Make a copy of a string in memory obtained from sqliteMalloc()
   241    219   */
   242    220   char *sqlite3StrDup_(const char *z, char *zFile, int line){
   243    221     char *zNew;
   244    222     if( z==0 ) return 0;
   245    223     zNew = sqlite3Malloc_(strlen(z)+1, 0, zFile, line);
................................................................................
   385    363   #ifdef SQLITE_DEBUG
   386    364   #if SQLITE_DEBUG>1
   387    365     fprintf(stderr,"string at 0x%x is %s\n", (int)*pz, *pz);
   388    366   #endif
   389    367   #endif
   390    368   }
   391    369   
   392         -/*
   393         -** Works like sqlite3SetString, but each string is now followed by
   394         -** a length integer which specifies how much of the source string 
   395         -** to copy (in bytes).  -1 means use the whole string.  The 1st 
   396         -** argument must either be NULL or point to memory obtained from 
   397         -** sqliteMalloc().
   398         -*/
   399         -void sqlite3SetNString(char **pz, ...){
   400         -  va_list ap;
   401         -  int nByte;
   402         -  const char *z;
   403         -  char *zResult;
   404         -  int n;
   405         -
   406         -  if( pz==0 ) return;
   407         -  nByte = 0;
   408         -  va_start(ap, pz);
   409         -  while( (z = va_arg(ap, const char*))!=0 ){
   410         -    n = va_arg(ap, int);
   411         -    if( n<=0 ) n = strlen(z);
   412         -    nByte += n;
   413         -  }
   414         -  va_end(ap);
   415         -  sqliteFree(*pz);
   416         -  *pz = zResult = sqliteMallocRaw( nByte + 1 );
   417         -  if( zResult==0 ) return;
   418         -  va_start(ap, pz);
   419         -  while( (z = va_arg(ap, const char*))!=0 ){
   420         -    n = va_arg(ap, int);
   421         -    if( n<=0 ) n = strlen(z);
   422         -    memcpy(zResult, z, n);
   423         -    zResult += n;
   424         -  }
   425         -  *zResult = 0;
   426         -#ifdef SQLITE_DEBUG
   427         -#if SQLITE_DEBUG>1
   428         -  fprintf(stderr,"string at 0x%x is %s\n", (int)*pz, *pz);
   429         -#endif
   430         -#endif
   431         -  va_end(ap);
   432         -}
   433         -
   434    370   /*
   435    371   ** Set the most recent error code and error string for the sqlite
   436    372   ** handle "db". The error code is set to "err_code".
   437    373   **
   438    374   ** If it is not NULL, string zFormat specifies the format of the
   439    375   ** error string in the style of the printf functions: The following
   440    376   ** format characters are allowed:

Changes to src/vdbeaux.c.

   300    300     }else if( n==P3_KEYINFO_HANDOFF ){
   301    301       pOp->p3 = (char*)zP3;
   302    302       pOp->p3type = P3_KEYINFO;
   303    303     }else if( n<0 ){
   304    304       pOp->p3 = (char*)zP3;
   305    305       pOp->p3type = n;
   306    306     }else{
   307         -    sqlite3SetNString(&pOp->p3, zP3, n, (char*)0);
          307  +    if( n==0 ) n = strlen(zP3);
          308  +    pOp->p3 = sqliteStrNDup(zP3, n);
   308    309       pOp->p3type = P3_DYNAMIC;
   309    310     }
   310    311   }
   311    312   
   312    313   #ifndef NDEBUG
   313    314   /*
   314    315   ** Replace the P3 field of the most recently coded instruction with