⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 func.c

📁 sqlite的最新源码 This ZIP archive contains preprocessed C code for the SQLite library as individual sour
💻 C
📖 第 1 页 / 共 3 页
字号:
  const struct compareInfo *pInfo, /* Information about how to do the compare */  const int esc                    /* The escape character */){  int c, c2;  int invert;  int seen;  u8 matchOne = pInfo->matchOne;  u8 matchAll = pInfo->matchAll;  u8 matchSet = pInfo->matchSet;  u8 noCase = pInfo->noCase;   int prevEscape = 0;     /* True if the previous character was 'escape' */  while( (c = sqlite3Utf8Read(zPattern,0,&zPattern))!=0 ){    if( !prevEscape && c==matchAll ){      while( (c=sqlite3Utf8Read(zPattern,0,&zPattern)) == matchAll               || c == matchOne ){        if( c==matchOne && sqlite3Utf8Read(zString, 0, &zString)==0 ){          return 0;        }      }      if( c==0 ){        return 1;      }else if( c==esc ){        c = sqlite3Utf8Read(zPattern, 0, &zPattern);        if( c==0 ){          return 0;        }      }else if( c==matchSet ){        assert( esc==0 );         /* This is GLOB, not LIKE */        assert( matchSet<0x80 );  /* '[' is a single-byte character */        while( *zString && patternCompare(&zPattern[-1],zString,pInfo,esc)==0 ){          SQLITE_SKIP_UTF8(zString);        }        return *zString!=0;      }      while( (c2 = sqlite3Utf8Read(zString,0,&zString))!=0 ){        if( noCase ){          GlogUpperToLower(c2);          GlogUpperToLower(c);          while( c2 != 0 && c2 != c ){            c2 = sqlite3Utf8Read(zString, 0, &zString);            GlogUpperToLower(c2);          }        }else{          while( c2 != 0 && c2 != c ){            c2 = sqlite3Utf8Read(zString, 0, &zString);          }        }        if( c2==0 ) return 0;        if( patternCompare(zPattern,zString,pInfo,esc) ) return 1;      }      return 0;    }else if( !prevEscape && c==matchOne ){      if( sqlite3Utf8Read(zString, 0, &zString)==0 ){        return 0;      }    }else if( c==matchSet ){      int prior_c = 0;      assert( esc==0 );    /* This only occurs for GLOB, not LIKE */      seen = 0;      invert = 0;      c = sqlite3Utf8Read(zString, 0, &zString);      if( c==0 ) return 0;      c2 = sqlite3Utf8Read(zPattern, 0, &zPattern);      if( c2=='^' ){        invert = 1;        c2 = sqlite3Utf8Read(zPattern, 0, &zPattern);      }      if( c2==']' ){        if( c==']' ) seen = 1;        c2 = sqlite3Utf8Read(zPattern, 0, &zPattern);      }      while( c2 && c2!=']' ){        if( c2=='-' && zPattern[0]!=']' && zPattern[0]!=0 && prior_c>0 ){          c2 = sqlite3Utf8Read(zPattern, 0, &zPattern);          if( c>=prior_c && c<=c2 ) seen = 1;          prior_c = 0;        }else{          if( c==c2 ){            seen = 1;          }          prior_c = c2;        }        c2 = sqlite3Utf8Read(zPattern, 0, &zPattern);      }      if( c2==0 || (seen ^ invert)==0 ){        return 0;      }    }else if( esc==c && !prevEscape ){      prevEscape = 1;    }else{      c2 = sqlite3Utf8Read(zString, 0, &zString);      if( noCase ){        GlogUpperToLower(c);        GlogUpperToLower(c2);      }      if( c!=c2 ){        return 0;      }      prevEscape = 0;    }  }  return *zString==0;}/*** Count the number of times that the LIKE operator (or GLOB which is** just a variation of LIKE) gets called.  This is used for testing** only.*/#ifdef SQLITE_TESTint sqlite3_like_count = 0;#endif/*** Implementation of the like() SQL function.  This function implements** the build-in LIKE operator.  The first argument to the function is the** pattern and the second argument is the string.  So, the SQL statements:****       A LIKE B**** is implemented as like(B,A).**** This same function (with a different compareInfo structure) computes** the GLOB operator.*/static void likeFunc(  sqlite3_context *context,   int argc,   sqlite3_value **argv){  const unsigned char *zA, *zB;  int escape = 0;  sqlite3 *db = sqlite3_context_db_handle(context);  zB = sqlite3_value_text(argv[0]);  zA = sqlite3_value_text(argv[1]);  /* Limit the length of the LIKE or GLOB pattern to avoid problems  ** of deep recursion and N*N behavior in patternCompare().  */  if( sqlite3_value_bytes(argv[0]) >        db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH] ){    sqlite3_result_error(context, "LIKE or GLOB pattern too complex", -1);    return;  }  assert( zB==sqlite3_value_text(argv[0]) );  /* Encoding did not change */  if( argc==3 ){    /* The escape character string must consist of a single UTF-8 character.    ** Otherwise, return an error.    */    const unsigned char *zEsc = sqlite3_value_text(argv[2]);    if( zEsc==0 ) return;    if( sqlite3Utf8CharLen((char*)zEsc, -1)!=1 ){      sqlite3_result_error(context,           "ESCAPE expression must be a single character", -1);      return;    }    escape = sqlite3Utf8Read(zEsc, 0, &zEsc);  }  if( zA && zB ){    struct compareInfo *pInfo = sqlite3_user_data(context);#ifdef SQLITE_TEST    sqlite3_like_count++;#endif        sqlite3_result_int(context, patternCompare(zB, zA, pInfo, escape));  }}/*** Implementation of the NULLIF(x,y) function.  The result is the first** argument if the arguments are different.  The result is NULL if the** arguments are equal to each other.*/static void nullifFunc(  sqlite3_context *context,  int NotUsed,  sqlite3_value **argv){  CollSeq *pColl = sqlite3GetFuncCollSeq(context);  UNUSED_PARAMETER(NotUsed);  if( sqlite3MemCompare(argv[0], argv[1], pColl)!=0 ){    sqlite3_result_value(context, argv[0]);  }}/*** Implementation of the VERSION(*) function.  The result is the version** of the SQLite library that is running.*/static void versionFunc(  sqlite3_context *context,  int NotUsed,  sqlite3_value **NotUsed2){  UNUSED_PARAMETER2(NotUsed, NotUsed2);  sqlite3_result_text(context, sqlite3_version, -1, SQLITE_STATIC);}/* Array for converting from half-bytes (nybbles) into ASCII hex** digits. */static const char hexdigits[] = {  '0', '1', '2', '3', '4', '5', '6', '7',  '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };/*** EXPERIMENTAL - This is not an official function.  The interface may** change.  This function may disappear.  Do not write code that depends** on this function.**** Implementation of the QUOTE() function.  This function takes a single** argument.  If the argument is numeric, the return value is the same as** the argument.  If the argument is NULL, the return value is the string** "NULL".  Otherwise, the argument is enclosed in single quotes with** single-quote escapes.*/static void quoteFunc(sqlite3_context *context, int argc, sqlite3_value **argv){  if( argc<1 ) return;  switch( sqlite3_value_type(argv[0]) ){    case SQLITE_NULL: {      sqlite3_result_text(context, "NULL", 4, SQLITE_STATIC);      break;    }    case SQLITE_INTEGER:    case SQLITE_FLOAT: {      sqlite3_result_value(context, argv[0]);      break;    }    case SQLITE_BLOB: {      char *zText = 0;      char const *zBlob = sqlite3_value_blob(argv[0]);      int nBlob = sqlite3_value_bytes(argv[0]);      assert( zBlob==sqlite3_value_blob(argv[0]) ); /* No encoding change */      zText = (char *)contextMalloc(context, (2*(i64)nBlob)+4);       if( zText ){        int i;        for(i=0; i<nBlob; i++){          zText[(i*2)+2] = hexdigits[(zBlob[i]>>4)&0x0F];          zText[(i*2)+3] = hexdigits[(zBlob[i])&0x0F];        }        zText[(nBlob*2)+2] = '\'';        zText[(nBlob*2)+3] = '\0';        zText[0] = 'X';        zText[1] = '\'';        sqlite3_result_text(context, zText, -1, SQLITE_TRANSIENT);        sqlite3_free(zText);      }      break;    }    case SQLITE_TEXT: {      int i,j;      u64 n;      const unsigned char *zArg = sqlite3_value_text(argv[0]);      char *z;      if( zArg==0 ) return;      for(i=0, n=0; zArg[i]; i++){ if( zArg[i]=='\'' ) n++; }      z = contextMalloc(context, ((i64)i)+((i64)n)+3);      if( z ){        z[0] = '\'';        for(i=0, j=1; zArg[i]; i++){          z[j++] = zArg[i];          if( zArg[i]=='\'' ){            z[j++] = '\'';          }        }        z[j++] = '\'';        z[j] = 0;        sqlite3_result_text(context, z, j, sqlite3_free);      }    }  }}/*** The hex() function.  Interpret the argument as a blob.  Return** a hexadecimal rendering as text.*/static void hexFunc(  sqlite3_context *context,  int argc,  sqlite3_value **argv){  int i, n;  const unsigned char *pBlob;  char *zHex, *z;  assert( argc==1 );  UNUSED_PARAMETER(argc);  pBlob = sqlite3_value_blob(argv[0]);  n = sqlite3_value_bytes(argv[0]);  assert( pBlob==sqlite3_value_blob(argv[0]) );  /* No encoding change */  z = zHex = contextMalloc(context, ((i64)n)*2 + 1);  if( zHex ){    for(i=0; i<n; i++, pBlob++){      unsigned char c = *pBlob;      *(z++) = hexdigits[(c>>4)&0xf];      *(z++) = hexdigits[c&0xf];    }    *z = 0;    sqlite3_result_text(context, zHex, n*2, sqlite3_free);  }}/*** The zeroblob(N) function returns a zero-filled blob of size N bytes.*/static void zeroblobFunc(  sqlite3_context *context,  int argc,  sqlite3_value **argv){  i64 n;  assert( argc==1 );  UNUSED_PARAMETER(argc);  n = sqlite3_value_int64(argv[0]);  if( n>SQLITE_MAX_LENGTH ){    sqlite3_result_error_toobig(context);  }else{    sqlite3_result_zeroblob(context, n);  }}/*** The replace() function.  Three arguments are all strings: call** them A, B, and C. The result is also a string which is derived** from A by replacing every occurance of B with C.  The match** must be exact.  Collating sequences are not used.*/static void replaceFunc(  sqlite3_context *context,  int argc,  sqlite3_value **argv){  const unsigned char *zStr;        /* The input string A */  const unsigned char *zPattern;    /* The pattern string B */  const unsigned char *zRep;        /* The replacement string C */  unsigned char *zOut;              /* The output */  int nStr;                /* Size of zStr */  int nPattern;            /* Size of zPattern */  int nRep;                /* Size of zRep */  i64 nOut;                /* Maximum size of zOut */  int loopLimit;           /* Last zStr[] that might match zPattern[] */  int i, j;                /* Loop counters */  assert( argc==3 );  UNUSED_PARAMETER(argc);  zStr = sqlite3_value_text(argv[0]);  if( zStr==0 ) return;  nStr = sqlite3_value_bytes(argv[0]);  assert( zStr==sqlite3_value_text(argv[0]) );  /* No encoding change */  zPattern = sqlite3_value_text(argv[1]);  if( zPattern==0 || zPattern[0]==0 ) return;  nPattern = sqlite3_value_bytes(argv[1]);  assert( zPattern==sqlite3_value_text(argv[1]) );  /* No encoding change */  zRep = sqlite3_value_text(argv[2]);  if( zRep==0 ) return;  nRep = sqlite3_value_bytes(argv[2]);  assert( zRep==sqlite3_value_text(argv[2]) );  nOut = nStr + 1;  assert( nOut<SQLITE_MAX_LENGTH );  zOut = contextMalloc(context, (i64)nOut);  if( zOut==0 ){    return;  }  loopLimit = nStr - nPattern;    for(i=j=0; i<=loopLimit; i++){    if( zStr[i]!=zPattern[0] || memcmp(&zStr[i], zPattern, nPattern) ){      zOut[j++] = zStr[i];    }else{      u8 *zOld;      sqlite3 *db = sqlite3_context_db_handle(context);      nOut += nRep - nPattern;      if( nOut>=db->aLimit[SQLITE_LIMIT_LENGTH] ){        sqlite3_result_error_toobig(context);        sqlite3DbFree(db, zOut);        return;      }      zOld = zOut;      zOut = sqlite3_realloc(zOut, (int)nOut);      if( zOut==0 ){        sqlite3_result_error_nomem(context);        sqlite3DbFree(db, zOld);        return;      }      memcpy(&zOut[j], zRep, nRep);      j += nRep;      i += nPattern-1;    }  }  assert( j+nStr-i+1==nOut );  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;                        /* 1: trimleft  2: trimright  3: trim */  int i;                            /* Loop counter */  unsigned char *aLen;              /* Length of each character in zCharSet */  unsigned char **azChar;           /* Individual characters in zCharSet */  int nChar;                        /* Number of characters in zCharSet */  if( sqlite3_value_type(argv[0])==SQLITE_NULL ){    return;  }  zIn = sqlite3_value_text(argv[0]);  if( zIn==0 ) return;  nIn = sqlite3_value_bytes(argv[0]);  assert( zIn==sqlite3_value_text(argv[0]) );  if( argc==1 ){    static const unsigned char lenOne[] = { 1 };    static unsigned char * const azOne[] = { (u8*)" " };    nChar = 1;    aLen = (u8*)lenOne;    azChar = (unsigned char **)azOne;    zCharSet = 0;  }else if( (zCharSet = sqlite3_value_text(argv[1]))==0 ){    return;  }else{    const unsigned char *z;    for(z=zCharSet, nChar=0; *z; nChar++){      SQLITE_SKIP_UTF8(z);    }    if( nChar>0 ){      azChar = contextMalloc(context, ((i64)nChar)*(sizeof(char*)+1));      if( azChar==0 ){        return;      }      aLen = (unsigned char*)&azChar[nChar];      for(z=zCharSet, nChar=0; *z; nChar++){        azChar[nChar] = (unsigned char *)z;        SQLITE_SKIP_UTF8(z);        aLen[nChar] = z - azChar[nChar];      }    }  }  if( nChar>0 ){    flags = SQLITE_PTR_TO_INT(sqlite3_user_data(context));    if( flags & 1 ){      while( nIn>0 ){        int len;        for(i=0; i<nChar; i++){          len = aLen[i];          if( memcmp(zIn, azChar[i], len)==0 ) break;        }        if( i>=nChar ) break;        zIn += len;        nIn -= len;      }    }    if( flags & 2 ){

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -