pike.git / src / builtin_functions.c

version» Context lines:

pike.git/src/builtin_functions.c:1:   /*   || This file is part of Pike. For copyright information see COPYRIGHT.   || Pike is distributed under GPL, LGPL and MPL. See the file COPYING   || for more information. - || $Id: builtin_functions.c,v 1.477 2003/02/16 04:23:18 mast Exp $ + || $Id: builtin_functions.c,v 1.478 2003/03/02 14:28:41 grubba Exp $   */      #include "global.h" - RCSID("$Id: builtin_functions.c,v 1.477 2003/02/16 04:23:18 mast Exp $"); + RCSID("$Id: builtin_functions.c,v 1.478 2003/03/02 14:28:41 grubba Exp $");   #include "interpret.h"   #include "svalue.h"   #include "pike_macros.h"   #include "object.h"   #include "program.h"   #include "array.h"   #include "pike_error.h"   #include "constants.h"   #include "mapping.h"   #include "stralloc.h"
pike.git/src/builtin_functions.c:107: Inside #if defined(PIKE_DEBUG)
   struct array *a;   #ifdef PIKE_DEBUG    if(args < 0) Pike_fatal("Negative args to f_aggregate() (%d)\n",args);   #endif       a=aggregate_array(args);    push_array(a); /* beware, macro */   }       + /*! @decl int hash_7_4(string s) +  *! @decl int hash_7_4(string s, int max) +  *! +  *! This function will return an @tt{int@} derived from the string @[s]. +  *! The same string will always hash to the same value. +  *! If @[max] is given, the result will be >= 0 and < @[max], otherwise +  *! the result will be >= 0 and <= 0x7fffffff. +  *! +  *! @note +  *! This function is provided for backward compatibility reasons. +  *! +  *! This function is byte-order dependant. +  *! +  *! @seealso +  *! @[hash()], @[hash_7_0()] +  */ + static void f_hash_7_4(INT32 args) + { +  size_t i = 0; +  struct pike_string *s = Pike_sp[-args].u.string; +  +  if(!args) +  SIMPLE_TOO_FEW_ARGS_ERROR("hash_7_4",1); +  +  if(Pike_sp[-args].type != T_STRING) +  SIMPLE_BAD_ARG_ERROR("hash_7_4", 1, "string"); +  +  i = simple_hashmem((unsigned char *)s->str, s->len<<s->size_shift, +  100<<s->size_shift); +  +  if(args > 1) +  { +  if(Pike_sp[1-args].type != T_INT) +  SIMPLE_BAD_ARG_ERROR("hash_7_4",2,"int"); +  +  if(!Pike_sp[1-args].u.integer) +  PIKE_ERROR("hash_7_4", "Modulo by zero.\n", Pike_sp, args); +  +  i%=(unsigned INT32)Pike_sp[1-args].u.integer; +  } +  pop_n_elems(args); +  push_int64(i); + } +    /*! @decl int hash_7_0(string s)    *! @decl int hash_7_0(string s, int max)    *!    *! This function will return an @tt{int@} derived from the string @[s].    *! The same string will always hash to the same value.    *! If @[max] is given, the result will be >= 0 and < @[max], otherwise    *! the result will be >= 0 and <= 0x7fffffff.    *!    *! @note    *! This function is provided for backward compatibility reasons.    *! -  +  *! This function is not NUL-safe, and is byte-order dependant. +  *!    *! @seealso -  *! @[hash()] +  *! @[hash()], @[hash_7_4()]    */ - void f_compat_hash( INT32 args ) + static void f_hash_7_0( INT32 args )   {    struct pike_string *s = Pike_sp[-args].u.string;    unsigned int i;    if(!args)    SIMPLE_TOO_FEW_ARGS_ERROR("hash_7_0",1);    if(Pike_sp[-args].type != T_STRING)    SIMPLE_BAD_ARG_ERROR("hash_7_0", 1, "string");       if( s->size_shift )    { -  f_hash( args ); +  f_hash_7_4( args );    return;    }       i = DO_NOT_WARN((unsigned int)hashstr( (unsigned char *)s->str,    MINIMUM(100,s->len)));    if(args > 1)    {    if(Pike_sp[1-args].type != T_INT)    SIMPLE_BAD_ARG_ERROR("hash_7_0",2,"int");   
pike.git/src/builtin_functions.c:161:      /*! @decl int hash(string s)    *! @decl int hash(string s, int max)    *!    *! This function will return an @tt{int@} derived from the string @[s].    *! The same string will always hash to the same value.    *! If @[max] is given, the result will be >= 0 and < @[max], otherwise    *! the result will be >= 0 and <= 0x7fffffff.    *!    *! @note -  *! The hash algorithm was changed in Pike 7.1. If you want a hash +  *! The hash algorithm was changed in Pike 7.5. If you want a hash +  *! that is compatible with Pike 7.4 and earlier, use @[hash_7_4()]. +  *! +  *! The hash algorithm was also changed in Pike 7.1. If you want a hash    *! that is compatible with Pike 7.0 and earlier, use @[hash_7_0()].    *!    *! @seealso -  *! @[hash_7_0()] +  *! @[hash_7_0()], @[hash_7_4()]    */ - void f_hash(INT32 args) + PMOD_EXPORT void f_hash(INT32 args)   {    size_t i = 0; -  struct pike_string *s = Pike_sp[-args].u.string; +  struct pike_string *s;       if(!args)    SIMPLE_TOO_FEW_ARGS_ERROR("hash",1);       if(Pike_sp[-args].type != T_STRING)    SIMPLE_BAD_ARG_ERROR("hash", 1, "string");    -  i = simple_hashmem((unsigned char *)s->str, s->len<<s->size_shift, -  100<<s->size_shift); +  s = Pike_sp[-args].u.string; +  switch(s->size_shift) { +  case 0: +  i = simple_hashmem(STR0(s), s->len, 100); +  break; +  case 1: +  i = simple_hashmem1(STR1(s), s->len, 100); +  break; +  case 2: +  i = simple_hashmem2(STR2(s), s->len, 100); +  break; +  default: +  Pike_fatal("hash(): Unsupported string shift: %d\n", s->size_shift); +  break; +  }       if(args > 1)    {    if(Pike_sp[1-args].type != T_INT)    SIMPLE_BAD_ARG_ERROR("hash",2,"int");       if(!Pike_sp[1-args].u.integer)    PIKE_ERROR("hash", "Modulo by zero.\n", Pike_sp, args);    -  +  if(Pike_sp[1-args].u.integer < 0) +  PIKE_ERROR("hash", "Negative modulo.\n", Pike_sp, args); +     i%=(unsigned INT32)Pike_sp[1-args].u.integer;    }    pop_n_elems(args);    push_int64(i);   }      /*! @decl mixed copy_value(mixed value)    *!    *! Copy a value recursively.    *!
pike.git/src/builtin_functions.c:7694:    ADD_EFUN("callablep", f_callablep,tFunc(tMix,tInt),OPT_TRY_OPTIMIZE);      /* function(string,string:int)|function(string,string*:array(string)) */    ADD_EFUN("glob",f_glob,    tOr(tFunc(tStr tStr,tInt),tFunc(tStr tArr(tStr),tArr(tStr))),    OPT_TRY_OPTIMIZE);      /* function(string,int|void:int) */    ADD_EFUN("hash",f_hash,tFunc(tStr tOr(tInt,tVoid),tInt),OPT_TRY_OPTIMIZE);    -  ADD_EFUN("hash_7_0",f_compat_hash, +  ADD_EFUN("hash_7_0",f_hash_7_0,    tFunc(tStr tOr(tInt,tVoid),tInt),OPT_TRY_OPTIMIZE);    -  +  ADD_EFUN("hash_7_4",f_hash_7_4, +  tFunc(tStr tOr(tInt,tVoid),tInt),OPT_TRY_OPTIMIZE); +    /* function(string|array:int*)|function(mapping(1=mixed:mixed)|multiset(1=mixed):array(1))|function(object|program:string*) */    ADD_EFUN2("indices",f_indices,    tOr3(tFunc(tOr(tStr,tArray),tArr(tInt)),    tFunc(tOr(tMap(tSetvar(1,tMix),tMix),tSet(tSetvar(1,tMix))),    tArr(tVar(1))),    tFunc(tOr(tObj,tPrg(tObj)),tArr(tStr))),    OPT_TRY_OPTIMIZE,fix_indices_type,0);      /* function(mixed:int) */    ADD_EFUN("intp", f_intp,tFunc(tMix,tInt),OPT_TRY_OPTIMIZE);