summaryrefslogtreecommitdiffstats
path: root/plugin/handler_socket/libhsclient/string_util.cpp
blob: 8ee6000f3d01ae23e412f6f466c50db6a762fca8 (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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
// vim:sw=2:ai

/*
 * Copyright (C) 2010 DeNA Co.,Ltd.. All rights reserved.
 * See COPYRIGHT.txt for details.
 */

#include <errno.h>
#include <stdio.h>

#include "string_util.hpp"

namespace dena {

string_wref
get_token(char *& wp, char *wp_end, char delim)
{
  char *const wp_begin = wp;
  char *const p = memchr_char(wp_begin, delim, wp_end - wp_begin);
  if (p == 0) {
    wp = wp_end;
    return string_wref(wp_begin, wp_end - wp_begin);
  }
  wp = p + 1;
  return string_wref(wp_begin, p - wp_begin);
}

template <typename T> T
atoi_tmpl_nocheck(const char *start, const char *finish)
{
  T v = 0;
  for (; start != finish; ++start) {
    const char c = *start;
    if (c < '0' || c > '9') {
      break;
    }
    v *= 10;
    v += static_cast<T>(c - '0');
  }
  return v;
}

template <typename T> T
atoi_signed_tmpl_nocheck(const char *start, const char *finish)
{
  T v = 0;
  bool negative = false;
  if (start != finish) {
    if (start[0] == '-') {
      ++start;
      negative = true;
    } else if (start[0] == '+') {
      ++start;
    }
  }
  for (; start != finish; ++start) {
    const char c = *start;
    if (c < '0' || c > '9') {
      break;
    }
    v *= 10;
    if (negative) {
      v -= static_cast<T>(c - '0');
    } else {
      v += static_cast<T>(c - '0');
    }
  }
  return v;
}

uint32_t
atoi_uint32_nocheck(const char *start, const char *finish)
{
  return atoi_tmpl_nocheck<uint32_t>(start, finish);
}

long long
atoll_nocheck(const char *start, const char *finish)
{
  return atoi_signed_tmpl_nocheck<long long>(start, finish);
}

void
append_uint32(string_buffer& buf, uint32_t v)
{
  char *const wp = buf.make_space(64);
  const int len = snprintf(wp, 64, "%lu", static_cast<unsigned long>(v));
  if (len > 0) {
    buf.space_wrote(len);
  }
}

std::string
to_stdstring(uint32_t v)
{
  char buf[64];
  snprintf(buf, sizeof(buf), "%lu", static_cast<unsigned long>(v));
  return std::string(buf);
}

int
errno_string(const char *s, int en, std::string& err_r)
{
  char buf[64];
  snprintf(buf, sizeof(buf), "%s: %d", s, en);
  err_r = std::string(buf);
  return en;
}

template <typename T> size_t
split_tmpl_arr(char delim, const T& buf, T *parts, size_t parts_len)
{
  typedef typename T::value_type value_type;
  size_t i = 0;
  value_type *start = buf.begin();
  value_type *const finish = buf.end();
  for (i = 0; i < parts_len; ++i) {
    value_type *const p = memchr_char(start, delim, finish - start);
    if (p == 0) {
      parts[i] = T(start, finish - start);
      ++i;
      break;
    }
    parts[i] = T(start, p - start);
    start = p + 1;
  }
  const size_t r = i;
  for (; i < parts_len; ++i) {
    parts[i] = T();
  }
  return r;
}

size_t
split(char delim, const string_ref& buf, string_ref *parts,
  size_t parts_len)
{
  return split_tmpl_arr(delim, buf, parts, parts_len);
}

size_t
split(char delim, const string_wref& buf, string_wref *parts,
  size_t parts_len)
{
  return split_tmpl_arr(delim, buf, parts, parts_len);
}

template <typename T, typename V> size_t
split_tmpl_vec(char delim, const T& buf, V& parts)
{
  typedef typename T::value_type value_type;
  size_t i = 0;
  value_type *start = buf.begin();
  value_type *const finish = buf.end();
  while (true) {
    value_type *const p = memchr_char(start, delim, finish - start);
    if (p == 0) {
      parts.push_back(T(start, finish - start));
      break;
    }
    parts.push_back(T(start, p - start));
    start = p + 1;
  }
  const size_t r = i;
  return r;
}

size_t
split(char delim, const string_ref& buf, std::vector<string_ref>& parts_r)
{
  return split_tmpl_vec(delim, buf, parts_r);
}

size_t
split(char delim, const string_wref& buf, std::vector<string_wref>& parts_r)
{
  return split_tmpl_vec(delim, buf, parts_r);
}

};