forked from VowpalWabbit/vowpal_wabbit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse_primitives.h
72 lines (60 loc) · 1.51 KB
/
parse_primitives.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/*
Copyright (c) 2009 Yahoo! Inc. All rights reserved. The copyrights
embodied in the content of this file are licensed under the BSD
(revised) open source license
*/
#ifndef PP
#define PP
#include "v_array.h"
#include<iostream>
struct substring {
char *start;
char *end;
};
//chop up the string into a v_array of substring.
void tokenize(char delim, substring s, v_array<substring> &ret);
inline char* safe_index(char *start, char v, char *max)
{
while (start != max && *start != v)
start++;
return start;
}
inline void print_substring(substring s)
{
std::cout.write(s.start,s.end - s.start);
}
inline float float_of_substring(substring s)
{
char* endptr = s.end;
float f = strtof(s.start,&endptr);
if (endptr == s.start && s.start != s.end)
{
std::cout << "error: " << std::string(s.start, s.end-s.start).c_str() << " is not a float" << std::endl;
f = 0;
}
return f;
}
inline float double_of_substring(substring s)
{
char* endptr = s.end;
float f = strtod(s.start,&endptr);
if (endptr == s.start && s.start != s.end)
{
std::cout << "error: " << std::string(s.start, s.end-s.start).c_str() << " is not a double" << std::endl;
f = 0;
}
return f;
}
inline int int_of_substring(substring s)
{
return atoi(std::string(s.start, s.end-s.start).c_str());
}
inline unsigned long ulong_of_substring(substring s)
{
return strtoul(std::string(s.start, s.end-s.start).c_str(),NULL,10);
}
inline unsigned long ss_length(substring s)
{
return (s.end - s.start);
}
#endif