aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/libpqxx/src/array.cxx
blob: c16c7ae586cf251426452555d1d5bbcd6b4ad4c1 (plain) (blame)
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/** Handling of SQL arrays.
 *
 * Copyright (c) 2000-2019, Jeroen T. Vermeulen.
 *
 * See COPYING for copyright license.  If you did not receive a file called
 * COPYING with this source code, please notify the distributor of this mistake,
 * or contact the author.
 */
#include "pqxx/compiler-internal.hxx"

#include <cassert>
#include <cstddef>
#include <cstring>
#include <utility>

#include "pqxx/array"
#include "pqxx/except"


namespace pqxx
{
/// Scan to next glyph in the buffer.  Assumes there is one.
std::string::size_type array_parser::scan_glyph(
	std::string::size_type pos) const
{
  assert(pos < m_end);
  return m_scan(m_input, m_end, pos);
}


/// Scan to next glyph in a substring.  Assumes there is one.
std::string::size_type array_parser::scan_glyph(
	std::string::size_type pos,
	std::string::size_type end) const
{
  assert(pos < end);
  assert(end <= m_end);
  return m_scan(m_input, end, pos);
}


/// Find the end of a single-quoted SQL string in an SQL array.
/** Returns the offset of the first character after the closing quote.
 */
std::string::size_type array_parser::scan_single_quoted_string() const
{
  auto here = m_pos, next = scan_glyph(here);
  assert(next < m_end);
  assert(next - here == 1);
  assert(m_input[here] == '\'');
  for (
	here = next, next = scan_glyph(here);
	here < m_end;
	here = next, next = scan_glyph(here)
  )
  {
    if (next - here == 1) switch (m_input[here])
    {
    case '\'':
      // SQL escapes single quotes by doubling them.  Terrible idea, but it's
      // what we have.  Inspect the next character to find out whether this is
      // the closing quote, or an escaped one inside the string.
      here = next;
      // (We can read beyond this quote because the array will always end in
      // a closing brace.)
      next = scan_glyph(here);

      if ((here + 1 < next) or (m_input[here] != '\''))
      {
        // Our lookahead character is not an escaped quote.  It's the first
        // character outside our string.  So, return it.
        return here;
      }

      // We've just scanned an escaped quote.  Keep going.
      break;

    case '\\':
      // Backslash escape.  Skip ahead by one more character.
      here = next;
      next = scan_glyph(here);
      break;
    }
  }
  throw argument_error{"Null byte in SQL string: " + std::string{m_input}};
}


/// Parse a single-quoted SQL string: un-quote it and un-escape it.
std::string array_parser::parse_single_quoted_string(
	std::string::size_type end) const
{
  // There have to be at least 2 characters: the opening and closing quotes.
  assert(m_pos + 1 < end);
  assert(m_input[m_pos] == '\'');
  assert(m_input[end - 1] == '\'');

  std::string output;
  // Maximum output size is same as the input size, minus the opening and
  // closing quotes.  In the worst case, the real number could be half that.
  // Usually it'll be a pretty close estimate.
  output.reserve(end - m_pos - 2);
  for (
	auto here = m_pos + 1, next = scan_glyph(here, end);
	here < end - 1;
	here = next, next = scan_glyph(here, end)
  )
  {
    if (
	next - here == 1 and
        (m_input[here] == '\'' or m_input[here] == '\\')
    )
    {
      // Skip escape.
      here = next;
      next = scan_glyph(here, end);
    }

    output.append(m_input + here, m_input + next);
  }

  return output;
}


/// Find the end of a double-quoted SQL string in an SQL array.
std::string::size_type array_parser::scan_double_quoted_string() const
{
  auto here = m_pos;
  assert(here < m_end);
  auto next = scan_glyph(here);
  assert(next - here == 1);
  assert(m_input[here] == '"');
  for (
	here = next, next = scan_glyph(here);
	here < m_end;
	here = next, next = scan_glyph(here)
  )
  {
    if (next - here == 1) switch (m_input[here])
    {
    case '\\':
      // Backslash escape.  Skip ahead by one more character.
      here = next;
      next = scan_glyph(here);
      break;

    case '"':
      // Closing quote.  Return the position right after.
      return next;
    }
  }
  throw argument_error{"Null byte in SQL string: " + std::string{m_input}};
}


/// Parse a double-quoted SQL string: un-quote it and un-escape it.
std::string array_parser::parse_double_quoted_string(
	std::string::size_type end) const
{
  // There have to be at least 2 characters: the opening and closing quotes.
  assert(m_pos + 1 < end);
  assert(m_input[m_pos] == '"');
  assert(m_input[end - 1] == '"');

  std::string output;
  // Maximum output size is same as the input size, minus the opening and
  // closing quotes.  In the worst case, the real number could be half that.
  // Usually it'll be a pretty close estimate.
  output.reserve(std::size_t(end - m_pos - 2));

  for (
	auto here = scan_glyph(m_pos, end), next = scan_glyph(here, end);
	here < end - 1;
	here = next, next = scan_glyph(here, end)
  )
  {
    if ((next - here == 1) and (m_input[here] == '\\'))
    {
      // Skip escape.
      here = next;
      next = scan_glyph(here, end);
    }

    output.append(m_input + here, m_input + next);
  }

  return output;
}


/// Find the end of an unquoted string in an SQL array.
/** Assumes UTF-8 or an ASCII-superset single-byte encoding.
 */
std::string::size_type array_parser::scan_unquoted_string() const
{
  auto here = m_pos, next = scan_glyph(here);
  assert(here < m_end);
  assert((next - here > 1) or (m_input[here] != '\''));
  assert((next - here > 1) or (m_input[here] != '"'));

  while (
        (next - here) > 1 or
        (
	  m_input[here] != ',' and
	  m_input[here] != ';' and
	  m_input[here] != '}'
        )
  )
  {
    here = next;
    next = scan_glyph(here);
  }
  return here;
}


/// Parse an unquoted SQL string.
/** Here, the special unquoted value NULL means a null value, not a string
 * that happens to spell "NULL".
 */
std::string array_parser::parse_unquoted_string(
	std::string::size_type end) const
{
  return std::string{m_input + m_pos, m_input + end};
}


array_parser::array_parser(
	const char input[],
	internal::encoding_group enc) :
  m_input(input),
  m_end(input == nullptr ? 0 : std::strlen(input)),
  m_scan(internal::get_glyph_scanner(enc)),
  m_pos(0)
{
}


std::pair<array_parser::juncture, std::string>
array_parser::get_next()
{
  juncture found;
  std::string value;
  std::string::size_type end;

  if (m_input == nullptr or (m_pos >= m_end))
    return std::make_pair(juncture::done, value);

  if (scan_glyph(m_pos) - m_pos > 1)
  {
    // Non-ASCII unquoted string.
    end = scan_unquoted_string();
    value = parse_unquoted_string(end);
    found = juncture::string_value;
  }
  else switch (m_input[m_pos])
  {
  case '\0':
    // TODO: Maybe just raise an error?
    found = juncture::done;
    end = m_pos;
    break;
  case '{':
    found = juncture::row_start;
    end = scan_glyph(m_pos);
    break;
  case '}':
    found = juncture::row_end;
    end = scan_glyph(m_pos);
    break;
  case '\'':
    found = juncture::string_value;
    end = scan_single_quoted_string();
    value = parse_single_quoted_string(end);
    break;
  case '"':
    found = juncture::string_value;
    end = scan_double_quoted_string();
    value = parse_double_quoted_string(end);
    break;
  default:
    end = scan_unquoted_string();
    value = parse_unquoted_string(end);
    if (value == "NULL")
    {
      // In this one situation, as a special case, NULL means a null field,
      // not a string that happens to spell "NULL".
      value.clear();
      found = juncture::null_value;
    }
    else
    {
      // The normal case: we just parsed an unquoted string.  The value is
      // what we need.
      found = juncture::string_value;
    }
    break;
  }

  // Skip a trailing field separator, if present.
  if (end < m_end)
  {
    auto next = scan_glyph(end);
    if (next - end == 1 and (m_input[end] == ',' or m_input[end] == ';'))
      end = next;
  }

  m_pos = end;
  return std::make_pair(found, value);
}
} // namespace pqxx