diff options
author | John Langford <jl@hunch.net> | 2012-06-20 22:14:47 +0400 |
---|---|---|
committer | John Langford <jl@hunch.net> | 2012-06-20 22:14:47 +0400 |
commit | 6ed1335520d691432ff651cf6ecc943b3bbae3e5 (patch) | |
tree | 04244b3a1ba9be44ebacf05b151537eb014f14d4 /vowpalwabbit/parse_primitives.h | |
parent | d4075cb58fd7110e409182de6b09bfca15a71629 (diff) |
incorporated read twice parser from Jeremie
Diffstat (limited to 'vowpalwabbit/parse_primitives.h')
-rw-r--r-- | vowpalwabbit/parse_primitives.h | 48 |
1 files changed, 47 insertions, 1 deletions
diff --git a/vowpalwabbit/parse_primitives.h b/vowpalwabbit/parse_primitives.h index 1200f5fe..0ed374eb 100644 --- a/vowpalwabbit/parse_primitives.h +++ b/vowpalwabbit/parse_primitives.h @@ -9,6 +9,7 @@ embodied in the content of this file are licensed under the BSD #include<iostream> #include <stdint.h> +#include <math.h> #include "v_array.h" #include "io.h" #include "example.h" @@ -100,10 +101,55 @@ inline void print_substring(substring s) std::cout.write(s.begin,s.end - s.begin); } +// The following function is a home made strtof. The +// differences are : +// - much faster (around 50% but depends on the string to parse) +// - less error control, but utilised inside a very strict parser +// in charge of error detection. +inline float parseFloat(char * p, char **end) +{ + if (!*p || *p == '?') + return 0; + int s = 1; + while (*p == ' ') p++; + + if (*p == '-') { + s = -1; p++; + } + + double acc = 0; + while (*p >= '0' && *p <= '9') + acc = acc * 10 + *p++ - '0'; + + int num_dec = 0; + if (*p == '.') { + p++; + while (*p >= '0' && *p <= '9') { + acc = acc *10 + (*p++ - '0') ; + num_dec++; + } + } + int exp_acc = 0; + if(*p == 'e' || *p == 'E'){ + p++; + int exp_s = 1; + if (*p == '-') { + exp_s = -1; p++; + } + while (*p >= '0' && *p <= '9') + exp_acc = exp_acc * 10 + *p++ - '0'; + exp_acc *= exp_s; + + } + acc *= pow(10,exp_acc-num_dec); + *end = p; + return s * acc; +} + inline float float_of_substring(substring s) { char* endptr = s.end; - float f = strtof(s.begin,&endptr); + float f = parseFloat(s.begin,&endptr); if (endptr == s.begin && s.begin != s.end) { std::cout << "error: " << std::string(s.begin, s.end-s.begin).c_str() << " is not a float" << std::endl; |