Diff for /parser3/src/classes/math.C between versions 1.35 and 1.44

version 1.35, 2003/11/20 15:35:29 version 1.44, 2006/11/14 17:24:49
Line 1 Line 1
 /** @file  /** @file
         Parser: @b math parser class.          Parser: @b math parser class.
   
         Copyright(c) 2001-2003 ArtLebedev Group(http://www.artlebedev.com)          Copyright(c) 2001-2005 ArtLebedev Group(http://www.artlebedev.com)
         Author: Alexandr Petrosian <paf@design.ru>(http://paf.design.ru)          Author: Alexandr Petrosian <paf@design.ru>(http://paf.design.ru)
   
         portions from gen_uuid.c,          portions from gen_uuid.c,
         Copyright (C) 1996, 1997, 1998, 1999 Theodore Ts'o.          Copyright (C) 1996, 1997, 1998, 1999 Theodore Ts'o.
 */  */
   
 static const char* IDENT_MATH_C="$Date$";  static const char * const IDENT_MATH_C="$Date$";
   
 #include "pa_vmethod_frame.h"  #include "pa_vmethod_frame.h"
 #include "pa_common.h"  #include "pa_common.h"
Line 164  static inline int _random(uint top) { Line 164  static inline int _random(uint top) {
 }  }
   
 static void _random(Request& r, MethodParams& params) {  static void _random(Request& r, MethodParams& params) {
         Value& range=params.as_junction(0, "range must be expression");          double top=params.as_double(0, "range must be expression", r);
         double top=r.process_to_value(range).as_double();  
         if(top<=0 || top>MAX_UINT)          if(top<=0 || top>MAX_UINT)
                 throw Exception("parser.runtime",                  throw Exception("parser.runtime",
                         0,                          0,
Line 181  static double degrees(double param) { re Line 180  static double degrees(double param) { re
 static double radians(double param) { return param /180 *PI; }  static double radians(double param) { return param /180 *PI; }
   
 static void math1(Request& r, MethodParams& params, math1_func_ptr func) {  static void math1(Request& r, MethodParams& params, math1_func_ptr func) {
         Value& param=params.as_junction(0, "parameter must be expression");          double param=params.as_double(0, "parameter must be expression", r);
           double result=func(param);
         double result=func(r.process_to_value(param).as_double());  
         r.write_no_lang(*new VDouble(result));          r.write_no_lang(*new VDouble(result));
 }  }
   
Line 208  MATH1(sqrt); Line 206  MATH1(sqrt);
   
 typedef double (*math2_func_ptr)(double, double);  typedef double (*math2_func_ptr)(double, double);
 static void math2(Request& r, MethodParams& params, math2_func_ptr func) {  static void math2(Request& r, MethodParams& params, math2_func_ptr func) {
         Value& a=params.as_junction(0, "parameter must be expression");          double a=params.as_double(0, "parameter must be expression", r);
         Value& b=params.as_junction(1, "parameter must be expression");          double b=params.as_double(1, "parameter must be expression", r);
           double result=func(a, b);
         double result=func(  
                 r.process_to_value(a).as_double(),  
                 r.process_to_value(b).as_double());  
         r.write_no_lang(*new VDouble(result));          r.write_no_lang(*new VDouble(result));
 }  }
   
Line 227  inline bool is_salt_body_char(int c) { Line 222  inline bool is_salt_body_char(int c) {
         return isalnum(c) || c == '.' || c=='/';          return isalnum(c) || c == '.' || c=='/';
 }  }
 static size_t calc_prefix_size(const char* salt) {  static size_t calc_prefix_size(const char* salt) {
         if(size_t salt_size=strlen(salt)) {          if(strlen(salt)) {
                 if(!is_salt_body_char(salt[0])) { // $...  {...                  if(!is_salt_body_char((unsigned char)salt[0])) { // $...  {...
                         const char* cur=salt+1; // skip                          const char* cur=salt+1; // skip
                         while(is_salt_body_char(*cur++)) // ...$  ...}                          while(is_salt_body_char((unsigned char)*cur++)) // ...$  ...}
                                 ;                                  ;
                         return cur-salt;                          return cur-salt;
                 } else                  } else
Line 286  static void _crypt(Request& r, MethodPar Line 281  static void _crypt(Request& r, MethodPar
         }          }
 }  }
   
 static const char* hex_string(unsigned char* bytes, size_t size, bool upcase) {  
         char *bytes_hex=new(PointerFreeGC) char [size*2/*byte->hh*/+1/*for zero-teminator*/];  
         unsigned char *src=bytes;  
         unsigned char *end=bytes+size;  
         char *dest=bytes_hex;  
   
         static const char *hex=upcase?"0123456789ABCDEF":"0123456789abcdef";  
   
         for(; src<end; src++) {  
                 *dest++=hex[*src/0x10];  
                 *dest++=hex[*src%0x10];  
         }  
         *dest=0;  
   
         return bytes_hex;  
 }  
   
 static void _md5(Request& r, MethodParams& params) {  static void _md5(Request& r, MethodParams& params) {
         const char *string=params.as_string(0, "parameter must be string").cstr();          const char *string=params.as_string(0, "parameter must be string").cstr();
   
   
         PA_MD5_CTX context;          PA_MD5_CTX context;
         unsigned char digest[16];          unsigned char digest[16];
         pa_MD5Init(&context);          pa_MD5Init(&context);
Line 367  static void _uid64(Request& r, MethodPar Line 344  static void _uid64(Request& r, MethodPar
         r.write_pass_lang(*new String(hex_string(id, sizeof(id), true)));          r.write_pass_lang(*new String(hex_string(id, sizeof(id), true)));
 }  }
   
   static void _crc32(Request& r, MethodParams& params) {
           const char *string=params.as_string(0, "parameter must be string").cstr();
           r.write_no_lang(*new VInt(pa_crc32(string, strlen(string))));
   }
   
 // constructor  // constructor
   
 MMath::MMath(): Methoded("math") {  MMath::MMath(): Methoded("math") {
Line 397  MMath::MMath(): Methoded("math") { Line 379  MMath::MMath(): Methoded("math") {
         // ^md5[string]          // ^md5[string]
         ADD1(md5);          ADD1(md5);
   
           ADD1(crc32);
   
         // ^uuid[]          // ^uuid[]
         ADD0(uuid);          ADD0(uuid);
   

Removed from v.1.35  
changed lines
  Added in v.1.44


E-mail: