Back to index

php5  5.3.10
zend_operators.h
Go to the documentation of this file.
00001 /*
00002    +----------------------------------------------------------------------+
00003    | Zend Engine                                                          |
00004    +----------------------------------------------------------------------+
00005    | Copyright (c) 1998-2012 Zend Technologies Ltd. (http://www.zend.com) |
00006    +----------------------------------------------------------------------+
00007    | This source file is subject to version 2.00 of the Zend license,     |
00008    | that is bundled with this package in the file LICENSE, and is        |
00009    | available through the world-wide-web at the following url:           |
00010    | http://www.zend.com/license/2_00.txt.                                |
00011    | If you did not receive a copy of the Zend license and are unable to  |
00012    | obtain it through the world-wide-web, please send a note to          |
00013    | license@zend.com so we can mail you a copy immediately.              |
00014    +----------------------------------------------------------------------+
00015    | Authors: Andi Gutmans <andi@zend.com>                                |
00016    |          Zeev Suraski <zeev@zend.com>                                |
00017    +----------------------------------------------------------------------+
00018 */
00019 
00020 /* $Id: zend_operators.h 321634 2012-01-01 13:15:04Z felipe $ */
00021 
00022 #ifndef ZEND_OPERATORS_H
00023 #define ZEND_OPERATORS_H
00024 
00025 #include <errno.h>
00026 #include <math.h>
00027 #include <assert.h>
00028 
00029 #ifdef HAVE_IEEEFP_H
00030 #include <ieeefp.h>
00031 #endif
00032 
00033 #include "zend_strtod.h"
00034 
00035 #if 0&&HAVE_BCMATH
00036 #include "ext/bcmath/libbcmath/src/bcmath.h"
00037 #endif
00038 
00039 BEGIN_EXTERN_C()
00040 ZEND_API int add_function(zval *result, zval *op1, zval *op2 TSRMLS_DC);
00041 ZEND_API int sub_function(zval *result, zval *op1, zval *op2 TSRMLS_DC);
00042 ZEND_API int mul_function(zval *result, zval *op1, zval *op2 TSRMLS_DC);
00043 ZEND_API int div_function(zval *result, zval *op1, zval *op2 TSRMLS_DC);
00044 ZEND_API int mod_function(zval *result, zval *op1, zval *op2 TSRMLS_DC);
00045 ZEND_API int boolean_xor_function(zval *result, zval *op1, zval *op2 TSRMLS_DC);
00046 ZEND_API int boolean_not_function(zval *result, zval *op1 TSRMLS_DC);
00047 ZEND_API int bitwise_not_function(zval *result, zval *op1 TSRMLS_DC);
00048 ZEND_API int bitwise_or_function(zval *result, zval *op1, zval *op2 TSRMLS_DC);
00049 ZEND_API int bitwise_and_function(zval *result, zval *op1, zval *op2 TSRMLS_DC);
00050 ZEND_API int bitwise_xor_function(zval *result, zval *op1, zval *op2 TSRMLS_DC);
00051 ZEND_API int shift_left_function(zval *result, zval *op1, zval *op2 TSRMLS_DC);
00052 ZEND_API int shift_right_function(zval *result, zval *op1, zval *op2 TSRMLS_DC);
00053 ZEND_API int concat_function(zval *result, zval *op1, zval *op2 TSRMLS_DC);
00054 
00055 ZEND_API int is_equal_function(zval *result, zval *op1, zval *op2 TSRMLS_DC);
00056 ZEND_API int is_identical_function(zval *result, zval *op1, zval *op2 TSRMLS_DC);
00057 ZEND_API int is_not_identical_function(zval *result, zval *op1, zval *op2 TSRMLS_DC);
00058 ZEND_API int is_not_equal_function(zval *result, zval *op1, zval *op2 TSRMLS_DC);
00059 ZEND_API int is_smaller_function(zval *result, zval *op1, zval *op2 TSRMLS_DC);
00060 ZEND_API int is_smaller_or_equal_function(zval *result, zval *op1, zval *op2 TSRMLS_DC);
00061 
00062 ZEND_API zend_bool instanceof_function_ex(const zend_class_entry *instance_ce, const zend_class_entry *ce, zend_bool interfaces_only TSRMLS_DC);
00063 ZEND_API zend_bool instanceof_function(const zend_class_entry *instance_ce, const zend_class_entry *ce TSRMLS_DC);
00064 END_EXTERN_C()
00065 
00066 #if ZEND_DVAL_TO_LVAL_CAST_OK
00067 # define zend_dval_to_lval(d) ((long) (d))
00068 #elif SIZEOF_LONG == 4 && defined(HAVE_ZEND_LONG64)
00069 static zend_always_inline long zend_dval_to_lval(double d)
00070 {
00071        if (d > LONG_MAX || d < LONG_MIN) {
00072               return (long)(unsigned long)(zend_long64) d;
00073        }
00074        return (long) d;
00075 }
00076 #else
00077 static zend_always_inline long zend_dval_to_lval(double d)
00078 {
00079        if (d > LONG_MAX) {
00080               return (long)(unsigned long) d;
00081        }
00082        return (long) d;
00083 }
00084 #endif
00085 /* }}} */
00086 
00087 #define ZEND_IS_DIGIT(c) ((c) >= '0' && (c) <= '9')
00088 #define ZEND_IS_XDIGIT(c) (((c) >= 'A' && (c) <= 'F') || ((c) >= 'a' && (c) <= 'f'))
00089 
00102 static inline zend_uchar is_numeric_string(const char *str, int length, long *lval, double *dval, int allow_errors)
00103 {
00104        const char *ptr;
00105        int base = 10, digits = 0, dp_or_e = 0;
00106        double local_dval;
00107        zend_uchar type;
00108 
00109        if (!length) {
00110               return 0;
00111        }
00112 
00113        /* Skip any whitespace
00114         * This is much faster than the isspace() function */
00115        while (*str == ' ' || *str == '\t' || *str == '\n' || *str == '\r' || *str == '\v' || *str == '\f') {
00116               str++;
00117               length--;
00118        }
00119        ptr = str;
00120 
00121        if (*ptr == '-' || *ptr == '+') {
00122               ptr++;
00123        }
00124 
00125        if (ZEND_IS_DIGIT(*ptr)) {
00126               /* Handle hex numbers
00127                * str is used instead of ptr to disallow signs and keep old behavior */
00128               if (length > 2 && *str == '0' && (str[1] == 'x' || str[1] == 'X')) {
00129                      base = 16;
00130                      ptr += 2;
00131               }
00132 
00133               /* Skip any leading 0s */
00134               while (*ptr == '0') {
00135                      ptr++;
00136               }
00137 
00138               /* Count the number of digits. If a decimal point/exponent is found,
00139                * it's a double. Otherwise, if there's a dval or no need to check for
00140                * a full match, stop when there are too many digits for a long */
00141               for (type = IS_LONG; !(digits >= MAX_LENGTH_OF_LONG && (dval || allow_errors == 1)); digits++, ptr++) {
00142 check_digits:
00143                      if (ZEND_IS_DIGIT(*ptr) || (base == 16 && ZEND_IS_XDIGIT(*ptr))) {
00144                             continue;
00145                      } else if (base == 10) {
00146                             if (*ptr == '.' && dp_or_e < 1) {
00147                                    goto process_double;
00148                             } else if ((*ptr == 'e' || *ptr == 'E') && dp_or_e < 2) {
00149                                    const char *e = ptr + 1;
00150 
00151                                    if (*e == '-' || *e == '+') {
00152                                           ptr = e++;
00153                                    }
00154                                    if (ZEND_IS_DIGIT(*e)) {
00155                                           goto process_double;
00156                                    }
00157                             }
00158                      }
00159 
00160                      break;
00161               }
00162 
00163               if (base == 10) {
00164                      if (digits >= MAX_LENGTH_OF_LONG) {
00165                             dp_or_e = -1;
00166                             goto process_double;
00167                      }
00168               } else if (!(digits < SIZEOF_LONG * 2 || (digits == SIZEOF_LONG * 2 && ptr[-digits] <= '7'))) {
00169                      if (dval) {
00170                             local_dval = zend_hex_strtod(str, (char **)&ptr);
00171                      }
00172                      type = IS_DOUBLE;
00173               }
00174        } else if (*ptr == '.' && ZEND_IS_DIGIT(ptr[1])) {
00175 process_double:
00176               type = IS_DOUBLE;
00177 
00178               /* If there's a dval, do the conversion; else continue checking
00179                * the digits if we need to check for a full match */
00180               if (dval) {
00181                      local_dval = zend_strtod(str, (char **)&ptr);
00182               } else if (allow_errors != 1 && dp_or_e != -1) {
00183                      dp_or_e = (*ptr++ == '.') ? 1 : 2;
00184                      goto check_digits;
00185               }
00186        } else {
00187               return 0;
00188        }
00189 
00190        if (ptr != str + length) {
00191               if (!allow_errors) {
00192                      return 0;
00193               }
00194               if (allow_errors == -1) {
00195                      zend_error(E_NOTICE, "A non well formed numeric value encountered");
00196               }
00197        }
00198 
00199        if (type == IS_LONG) {
00200               if (digits == MAX_LENGTH_OF_LONG - 1) {
00201                      int cmp = strcmp(&ptr[-digits], long_min_digits);
00202 
00203                      if (!(cmp < 0 || (cmp == 0 && *str == '-'))) {
00204                             if (dval) {
00205                                    *dval = zend_strtod(str, NULL);
00206                             }
00207 
00208                             return IS_DOUBLE;
00209                      }
00210               }
00211 
00212               if (lval) {
00213                      *lval = strtol(str, NULL, base);
00214               }
00215 
00216               return IS_LONG;
00217        } else {
00218               if (dval) {
00219                      *dval = local_dval;
00220               }
00221 
00222               return IS_DOUBLE;
00223        }
00224 }
00225 
00226 static inline char *
00227 zend_memnstr(char *haystack, char *needle, int needle_len, char *end)
00228 {
00229        char *p = haystack;
00230        char ne = needle[needle_len-1];
00231 
00232        if (needle_len == 1) {
00233               return (char *)memchr(p, *needle, (end-p));
00234        }
00235 
00236        if (needle_len > end-haystack) {
00237               return NULL;
00238        }
00239 
00240        end -= needle_len;
00241 
00242        while (p <= end) {
00243               if ((p = (char *)memchr(p, *needle, (end-p+1))) && ne == p[needle_len-1]) {
00244                      if (!memcmp(needle, p, needle_len-1)) {
00245                             return p;
00246                      }
00247               }
00248 
00249               if (p == NULL) {
00250                      return NULL;
00251               }
00252 
00253               p++;
00254        }
00255 
00256        return NULL;
00257 }
00258 
00259 static inline void *zend_memrchr(const void *s, int c, size_t n)
00260 {
00261        register unsigned char *e;
00262 
00263        if (n <= 0) {
00264               return NULL;
00265        }
00266 
00267        for (e = (unsigned char *)s + n - 1; e >= (unsigned char *)s; e--) {
00268               if (*e == (unsigned char)c) {
00269                      return (void *)e;
00270               }
00271        }
00272 
00273        return NULL;
00274 }
00275 
00276 BEGIN_EXTERN_C()
00277 ZEND_API int increment_function(zval *op1);
00278 ZEND_API int decrement_function(zval *op2);
00279 
00280 ZEND_API void convert_scalar_to_number(zval *op TSRMLS_DC);
00281 ZEND_API void _convert_to_string(zval *op ZEND_FILE_LINE_DC);
00282 ZEND_API void convert_to_long(zval *op);
00283 ZEND_API void convert_to_double(zval *op);
00284 ZEND_API void convert_to_long_base(zval *op, int base);
00285 ZEND_API void convert_to_null(zval *op);
00286 ZEND_API void convert_to_boolean(zval *op);
00287 ZEND_API void convert_to_array(zval *op);
00288 ZEND_API void convert_to_object(zval *op);
00289 ZEND_API void multi_convert_to_long_ex(int argc, ...);
00290 ZEND_API void multi_convert_to_double_ex(int argc, ...);
00291 ZEND_API void multi_convert_to_string_ex(int argc, ...);
00292 ZEND_API int add_char_to_string(zval *result, const zval *op1, const zval *op2);
00293 ZEND_API int add_string_to_string(zval *result, const zval *op1, const zval *op2);
00294 #define convert_to_string(op) if ((op)->type != IS_STRING) { _convert_to_string((op) ZEND_FILE_LINE_CC); }
00295 
00296 ZEND_API double zend_string_to_double(const char *number, zend_uint length);
00297 
00298 ZEND_API int zval_is_true(zval *op);
00299 ZEND_API int compare_function(zval *result, zval *op1, zval *op2 TSRMLS_DC);
00300 ZEND_API int numeric_compare_function(zval *result, zval *op1, zval *op2 TSRMLS_DC);
00301 ZEND_API int string_compare_function(zval *result, zval *op1, zval *op2 TSRMLS_DC);
00302 #if HAVE_STRCOLL
00303 ZEND_API int string_locale_compare_function(zval *result, zval *op1, zval *op2 TSRMLS_DC);
00304 #endif
00305 
00306 ZEND_API void zend_str_tolower(char *str, unsigned int length);
00307 ZEND_API char *zend_str_tolower_copy(char *dest, const char *source, unsigned int length);
00308 ZEND_API char *zend_str_tolower_dup(const char *source, unsigned int length);
00309 
00310 ZEND_API int zend_binary_zval_strcmp(zval *s1, zval *s2);
00311 ZEND_API int zend_binary_zval_strncmp(zval *s1, zval *s2, zval *s3);
00312 ZEND_API int zend_binary_zval_strcasecmp(zval *s1, zval *s2);
00313 ZEND_API int zend_binary_zval_strncasecmp(zval *s1, zval *s2, zval *s3);
00314 ZEND_API int zend_binary_strcmp(const char *s1, uint len1, const char *s2, uint len2);
00315 ZEND_API int zend_binary_strncmp(const char *s1, uint len1, const char *s2, uint len2, uint length);
00316 ZEND_API int zend_binary_strcasecmp(const char *s1, uint len1, const char *s2, uint len2);
00317 ZEND_API int zend_binary_strncasecmp(const char *s1, uint len1, const char *s2, uint len2, uint length);
00318 
00319 ZEND_API void zendi_smart_strcmp(zval *result, zval *s1, zval *s2);
00320 ZEND_API void zend_compare_symbol_tables(zval *result, HashTable *ht1, HashTable *ht2 TSRMLS_DC);
00321 ZEND_API void zend_compare_arrays(zval *result, zval *a1, zval *a2 TSRMLS_DC);
00322 ZEND_API void zend_compare_objects(zval *result, zval *o1, zval *o2 TSRMLS_DC);
00323 
00324 ZEND_API int zend_atoi(const char *str, int str_len);
00325 ZEND_API long zend_atol(const char *str, int str_len);
00326 
00327 ZEND_API void zend_locale_sprintf_double(zval *op ZEND_FILE_LINE_DC);
00328 END_EXTERN_C()
00329 
00330 #define convert_to_ex_master(ppzv, lower_type, upper_type)     \
00331        if (Z_TYPE_PP(ppzv)!=IS_##upper_type) {                               \
00332               SEPARATE_ZVAL_IF_NOT_REF(ppzv);                                       \
00333               convert_to_##lower_type(*ppzv);                                       \
00334        }
00335 
00336 #define convert_to_explicit_type(pzv, type)             \
00337        do {                                                                  \
00338               switch (type) {                                                \
00339                      case IS_NULL:                                    \
00340                             convert_to_null(pzv);                     \
00341                             break;                                           \
00342                      case IS_LONG:                                    \
00343                             convert_to_long(pzv);                     \
00344                             break;                                           \
00345                      case IS_DOUBLE:                                         \
00346                             convert_to_double(pzv);                   \
00347                             break;                                           \
00348                      case IS_BOOL:                                    \
00349                             convert_to_boolean(pzv);           \
00350                             break;                                           \
00351                      case IS_ARRAY:                                          \
00352                             convert_to_array(pzv);                    \
00353                             break;                                           \
00354                      case IS_OBJECT:                                         \
00355                             convert_to_object(pzv);                   \
00356                             break;                                           \
00357                      case IS_STRING:                                         \
00358                             convert_to_string(pzv);                   \
00359                             break;                                           \
00360                      default:                                                \
00361                             assert(0);                                       \
00362                             break;                                           \
00363               }                                                                     \
00364        } while (0);
00365 
00366 #define convert_to_explicit_type_ex(ppzv, str_type)     \
00367        if (Z_TYPE_PP(ppzv) != str_type) {                      \
00368               SEPARATE_ZVAL_IF_NOT_REF(ppzv);                         \
00369               convert_to_explicit_type(*ppzv, str_type);       \
00370        }
00371 
00372 #define convert_to_boolean_ex(ppzv)       convert_to_ex_master(ppzv, boolean, BOOL)
00373 #define convert_to_long_ex(ppzv)   convert_to_ex_master(ppzv, long, LONG)
00374 #define convert_to_double_ex(ppzv) convert_to_ex_master(ppzv, double, DOUBLE)
00375 #define convert_to_string_ex(ppzv) convert_to_ex_master(ppzv, string, STRING)
00376 #define convert_to_array_ex(ppzv)  convert_to_ex_master(ppzv, array, ARRAY)
00377 #define convert_to_object_ex(ppzv) convert_to_ex_master(ppzv, object, OBJECT)
00378 #define convert_to_null_ex(ppzv)   convert_to_ex_master(ppzv, null, NULL)
00379 
00380 #define convert_scalar_to_number_ex(ppzv)                                           \
00381        if (Z_TYPE_PP(ppzv)!=IS_LONG && Z_TYPE_PP(ppzv)!=IS_DOUBLE) {  \
00382               if (!Z_ISREF_PP(ppzv)) {                                                            \
00383                      SEPARATE_ZVAL(ppzv);                                                         \
00384               }                                                                                                        \
00385               convert_scalar_to_number(*ppzv TSRMLS_CC);                                   \
00386        }
00387 
00388 
00389 #define Z_LVAL(zval)               (zval).value.lval
00390 #define Z_BVAL(zval)               ((zend_bool)(zval).value.lval)
00391 #define Z_DVAL(zval)               (zval).value.dval
00392 #define Z_STRVAL(zval)                    (zval).value.str.val
00393 #define Z_STRLEN(zval)                    (zval).value.str.len
00394 #define Z_ARRVAL(zval)                    (zval).value.ht
00395 #define Z_OBJVAL(zval)                    (zval).value.obj
00396 #define Z_OBJ_HANDLE(zval)         Z_OBJVAL(zval).handle
00397 #define Z_OBJ_HT(zval)                    Z_OBJVAL(zval).handlers
00398 #define Z_OBJCE(zval)                     zend_get_class_entry(&(zval) TSRMLS_CC)
00399 #define Z_OBJPROP(zval)                   Z_OBJ_HT((zval))->get_properties(&(zval) TSRMLS_CC)
00400 #define Z_OBJ_HANDLER(zval, hf) Z_OBJ_HT((zval))->hf
00401 #define Z_RESVAL(zval)                    (zval).value.lval
00402 #define Z_OBJDEBUG(zval,is_tmp)    (Z_OBJ_HANDLER((zval),get_debug_info)?Z_OBJ_HANDLER((zval),get_debug_info)(&(zval),&is_tmp TSRMLS_CC):(is_tmp=0,Z_OBJ_HANDLER((zval),get_properties)?Z_OBJPROP(zval):NULL))
00403 
00404 #define Z_LVAL_P(zval_p)           Z_LVAL(*zval_p)
00405 #define Z_BVAL_P(zval_p)           Z_BVAL(*zval_p)
00406 #define Z_DVAL_P(zval_p)           Z_DVAL(*zval_p)
00407 #define Z_STRVAL_P(zval_p)         Z_STRVAL(*zval_p)
00408 #define Z_STRLEN_P(zval_p)         Z_STRLEN(*zval_p)
00409 #define Z_ARRVAL_P(zval_p)         Z_ARRVAL(*zval_p)
00410 #define Z_OBJPROP_P(zval_p)        Z_OBJPROP(*zval_p)
00411 #define Z_OBJCE_P(zval_p)          Z_OBJCE(*zval_p)
00412 #define Z_RESVAL_P(zval_p)         Z_RESVAL(*zval_p)
00413 #define Z_OBJVAL_P(zval_p)         Z_OBJVAL(*zval_p)
00414 #define Z_OBJ_HANDLE_P(zval_p)     Z_OBJ_HANDLE(*zval_p)
00415 #define Z_OBJ_HT_P(zval_p)         Z_OBJ_HT(*zval_p)
00416 #define Z_OBJ_HANDLER_P(zval_p, h) Z_OBJ_HANDLER(*zval_p, h)
00417 #define Z_OBJDEBUG_P(zval_p,is_tmp)       Z_OBJDEBUG(*zval_p,is_tmp)
00418 
00419 #define Z_LVAL_PP(zval_pp)         Z_LVAL(**zval_pp)
00420 #define Z_BVAL_PP(zval_pp)         Z_BVAL(**zval_pp)
00421 #define Z_DVAL_PP(zval_pp)         Z_DVAL(**zval_pp)
00422 #define Z_STRVAL_PP(zval_pp)       Z_STRVAL(**zval_pp)
00423 #define Z_STRLEN_PP(zval_pp)       Z_STRLEN(**zval_pp)
00424 #define Z_ARRVAL_PP(zval_pp)       Z_ARRVAL(**zval_pp)
00425 #define Z_OBJPROP_PP(zval_pp)      Z_OBJPROP(**zval_pp)
00426 #define Z_OBJCE_PP(zval_pp)        Z_OBJCE(**zval_pp)
00427 #define Z_RESVAL_PP(zval_pp)       Z_RESVAL(**zval_pp)
00428 #define Z_OBJVAL_PP(zval_pp)       Z_OBJVAL(**zval_pp)
00429 #define Z_OBJ_HANDLE_PP(zval_p)    Z_OBJ_HANDLE(**zval_p)
00430 #define Z_OBJ_HT_PP(zval_p)        Z_OBJ_HT(**zval_p)
00431 #define Z_OBJ_HANDLER_PP(zval_p, h)              Z_OBJ_HANDLER(**zval_p, h)
00432 #define Z_OBJDEBUG_PP(zval_pp,is_tmp)     Z_OBJDEBUG(**zval_pp,is_tmp)
00433 
00434 #define Z_TYPE(zval)        (zval).type
00435 #define Z_TYPE_P(zval_p)    Z_TYPE(*zval_p)
00436 #define Z_TYPE_PP(zval_pp)  Z_TYPE(**zval_pp)
00437 
00438 #if HAVE_SETLOCALE && defined(ZEND_WIN32) && !defined(ZTS) && defined(_MSC_VER) && (_MSC_VER >= 1400)
00439 /* This is performance improvement of tolower() on Windows and VC2005
00440  * Gives 10-18% on bench.php
00441  */
00442 #define ZEND_USE_TOLOWER_L 1
00443 #endif
00444 
00445 #ifdef ZEND_USE_TOLOWER_L
00446 ZEND_API void zend_update_current_locale(void);
00447 #else
00448 #define zend_update_current_locale()
00449 #endif
00450 
00451 #endif
00452 
00453 /*
00454  * Local variables:
00455  * tab-width: 4
00456  * c-basic-offset: 4
00457  * indent-tabs-mode: t
00458  * End:
00459  */