Welcome to mirror list, hosted at ThFree Co, Russian Federation.

parse_primitives.h - github.com/moses-smt/vowpal_wabbit.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e2d19d415ae5d6529d4fd3fa0780f688b8b8cf09 (plain)
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
73
/*
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>
using namespace std;

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)
{
  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)
    {
      cout << "error: " << string(s.start, s.end-s.start).c_str() << " is not a float" << 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)
    {
      cout << "error: " << string(s.start, s.end-s.start).c_str() << " is not a double" << endl;
      f = 0;
    }
  return f;
}

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

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

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

#endif