File: parse_primitives.h

package info (click to toggle)
vowpal-wabbit 6.1-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 20,200 kB
  • sloc: cpp: 6,724; perl: 449; makefile: 71; sh: 57
file content (72 lines) | stat: -rw-r--r-- 1,548 bytes parent folder | download
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 *begin;
  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.begin,s.end - s.begin);
}

inline float float_of_substring(substring s)
{
  char* endptr = s.end;
  float f = strtof(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;
      f = 0;
    }
  return f;
}

inline float double_of_substring(substring s)
{
  char* endptr = s.end;
  float f = strtod(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 double" << std::endl;
      f = 0;
    }
  return f;
}

inline int int_of_substring(substring s)
{
  return atoi(std::string(s.begin, s.end-s.begin).c_str());
}

inline unsigned long ulong_of_substring(substring s)
{
  return strtoul(std::string(s.begin, s.end-s.begin).c_str(),NULL,10);
}

inline unsigned long ss_length(substring s)
{
  return (s.end - s.begin);
}

#endif