summaryrefslogtreecommitdiff
path: root/storage/spider/hs_client/string_util.cpp
blob: 39934148cb8a3f66fb4dee65f3a41bc77a1cac7a (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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207

// vim:sw=2:ai

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

#include <my_global.h>
#include "mysql_version.h"
#include "hs_compat.h"
#if MYSQL_VERSION_ID < 50500
#include "mysql_priv.h"
#include <mysql/plugin.h>
#else
#include "sql_priv.h"
#include "probes_mysql.h"
#endif

#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);
}

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

long long
atoll_nocheck(const char *start, const char *finish)
{
  long long 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 -= (long long) (c - '0');
    } else {
      v += (long long) (c - '0');
    }
  }
  return v;
}

void
append_uint32(string_buffer& buf, uint32 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);
  }
}

/*
String *
to_stdstring(uint32 v)
{
  char buf[64];
  int str_len;
  String *str;
  str_len = snprintf(buf, sizeof(buf), "%lu", static_cast<unsigned long>(v));
  if ((str = new String(str_len + 1)))
    str->q_append(buf, str_len);
  return str;
}
*/

int
errno_string(const char *s, int en, String& err_r)
{
  char buf[64];
  int str_len;
  str_len = snprintf(buf, sizeof(buf), "%s: %d", s, en);
  if (!err_r.reserve(str_len + 1))
    err_r.q_append(buf, str_len);
  return en;
}

size_t
split(char delim, const string_ref& buf, string_ref *parts,
  size_t parts_len)
{
  size_t i = 0;
  const char *start = buf.begin();
  const char *const finish = buf.end();
  for (i = 0; i < parts_len; ++i) {
    const char *const p = memchr_char(start, delim, finish - start);
    if (p == 0) {
      parts[i] = string_ref(start, finish - start);
      ++i;
      break;
    }
    parts[i] = string_ref(start, p - start);
    start = p + 1;
  }
  const size_t r = i;
  for (; i < parts_len; ++i) {
    parts[i] = string_ref();
  }
  return r;
}

size_t
split(char delim, const string_wref& buf, string_wref *parts,
  size_t parts_len)
{
  size_t i = 0;
  char *start = buf.begin();
  char *const finish = buf.end();
  for (i = 0; i < parts_len; ++i) {
    char *const p = memchr_char(start, delim, finish - start);
    if (p == 0) {
      parts[i] = string_wref(start, finish - start);
      ++i;
      break;
    }
    parts[i] = string_wref(start, p - start);
    start = p + 1;
  }
  const size_t r = i;
  for (; i < parts_len; ++i) {
    parts[i] = string_wref();
  }
  return r;
}

size_t
split(char delim, const string_ref& buf, DYNAMIC_ARRAY& parts_r)
{
  size_t i = 0;
  const char *start = buf.begin();
  const char *finish = buf.end();
  while (true) {
    const char *p = memchr_char(start, delim, finish - start);
    if (p == 0) {
      string_ref param(start, finish - start);
      insert_dynamic(&parts_r, (uchar *) &param);
      break;
    }
    string_ref param(start, p - start);
    insert_dynamic(&parts_r, (uchar *) &param);
    start = p + 1;
  }
  const size_t r = i;
  return r;
}

size_t
split(char delim, const string_wref& buf, DYNAMIC_ARRAY& parts_r)
{
  size_t i = 0;
  char *start = buf.begin();
  char *finish = buf.end();
  while (true) {
    char *p = memchr_char(start, delim, finish - start);
    if (p == 0) {
      string_wref param(start, finish - start);
      insert_dynamic(&parts_r, (uchar *) &param);
      break;
    }
    string_wref param(start, p - start);
    insert_dynamic(&parts_r, (uchar *) &param);
    start = p + 1;
  }
  const size_t r = i;
  return r;
}

};