1
2// vim:sw=2:ai
3
4/*
5 * Copyright (C) 2010-2011 DeNA Co.,Ltd.. All rights reserved.
6 * Copyright (C) 2011-2017 Kentoku SHIBA
7 * See COPYRIGHT.txt for details.
8 */
9
10#include <my_global.h>
11#include "mysql_version.h"
12#include "hs_compat.h"
13#if MYSQL_VERSION_ID < 50500
14#include "mysql_priv.h"
15#include <mysql/plugin.h>
16#else
17#include "sql_priv.h"
18#include "probes_mysql.h"
19#endif
20
21#include "string_util.hpp"
22
23namespace dena {
24
25string_wref
26get_token(char *& wp, char *wp_end, char delim)
27{
28 char *const wp_begin = wp;
29 char *const p = memchr_char(wp_begin, delim, wp_end - wp_begin);
30 if (p == 0) {
31 wp = wp_end;
32 return string_wref(wp_begin, wp_end - wp_begin);
33 }
34 wp = p + 1;
35 return string_wref(wp_begin, p - wp_begin);
36}
37
38uint32
39atoi_uint32_nocheck(const char *start, const char *finish)
40{
41 uint32 v = 0;
42 for (; start != finish; ++start) {
43 const char c = *start;
44 if (c < '0' || c > '9') {
45 break;
46 }
47 v *= 10;
48 v += (uint32) (c - '0');
49 }
50 return v;
51}
52
53long long
54atoll_nocheck(const char *start, const char *finish)
55{
56 long long v = 0;
57 bool negative = false;
58 if (start != finish) {
59 if (start[0] == '-') {
60 ++start;
61 negative = true;
62 } else if (start[0] == '+') {
63 ++start;
64 }
65 }
66 for (; start != finish; ++start) {
67 const char c = *start;
68 if (c < '0' || c > '9') {
69 break;
70 }
71 v *= 10;
72 if (negative) {
73 v -= (long long) (c - '0');
74 } else {
75 v += (long long) (c - '0');
76 }
77 }
78 return v;
79}
80
81void
82append_uint32(string_buffer& buf, uint32 v)
83{
84 char *const wp = buf.make_space(64);
85 const int len = snprintf(wp, 64, "%lu", static_cast<unsigned long>(v));
86 if (len > 0) {
87 buf.space_wrote(len);
88 }
89}
90
91/*
92String *
93to_stdstring(uint32 v)
94{
95 char buf[64];
96 int str_len;
97 String *str;
98 str_len = snprintf(buf, sizeof(buf), "%lu", static_cast<unsigned long>(v));
99 if ((str = new String(str_len + 1)))
100 str->q_append(buf, str_len);
101 return str;
102}
103*/
104
105int
106errno_string(const char *s, int en, String& err_r)
107{
108 char buf[64];
109 int str_len;
110 str_len = snprintf(buf, sizeof(buf), "%s: %d", s, en);
111 if (!err_r.reserve(str_len + 1))
112 err_r.q_append(buf, str_len);
113 return en;
114}
115
116size_t
117split(char delim, const string_ref& buf, string_ref *parts,
118 size_t parts_len)
119{
120 size_t i = 0;
121 const char *start = buf.begin();
122 const char *const finish = buf.end();
123 for (i = 0; i < parts_len; ++i) {
124 const char *const p = memchr_char(start, delim, finish - start);
125 if (p == 0) {
126 parts[i] = string_ref(start, finish - start);
127 ++i;
128 break;
129 }
130 parts[i] = string_ref(start, p - start);
131 start = p + 1;
132 }
133 const size_t r = i;
134 for (; i < parts_len; ++i) {
135 parts[i] = string_ref();
136 }
137 return r;
138}
139
140size_t
141split(char delim, const string_wref& buf, string_wref *parts,
142 size_t parts_len)
143{
144 size_t i = 0;
145 char *start = buf.begin();
146 char *const finish = buf.end();
147 for (i = 0; i < parts_len; ++i) {
148 char *const p = memchr_char(start, delim, finish - start);
149 if (p == 0) {
150 parts[i] = string_wref(start, finish - start);
151 ++i;
152 break;
153 }
154 parts[i] = string_wref(start, p - start);
155 start = p + 1;
156 }
157 const size_t r = i;
158 for (; i < parts_len; ++i) {
159 parts[i] = string_wref();
160 }
161 return r;
162}
163
164size_t
165split(char delim, const string_ref& buf, DYNAMIC_ARRAY& parts_r)
166{
167 size_t i = 0;
168 const char *start = buf.begin();
169 const char *finish = buf.end();
170 while (true) {
171 const char *p = memchr_char(start, delim, finish - start);
172 if (p == 0) {
173 string_ref param(start, finish - start);
174 insert_dynamic(&parts_r, (uchar *) &param);
175 break;
176 }
177 string_ref param(start, p - start);
178 insert_dynamic(&parts_r, (uchar *) &param);
179 start = p + 1;
180 }
181 const size_t r = i;
182 return r;
183}
184
185size_t
186split(char delim, const string_wref& buf, DYNAMIC_ARRAY& parts_r)
187{
188 size_t i = 0;
189 char *start = buf.begin();
190 char *finish = buf.end();
191 while (true) {
192 char *p = memchr_char(start, delim, finish - start);
193 if (p == 0) {
194 string_wref param(start, finish - start);
195 insert_dynamic(&parts_r, (uchar *) &param);
196 break;
197 }
198 string_wref param(start, p - start);
199 insert_dynamic(&parts_r, (uchar *) &param);
200 start = p + 1;
201 }
202 const size_t r = i;
203 return r;
204}
205
206};
207
208