aboutsummaryrefslogtreecommitdiff
path: root/external/glim/gstring.hpp
blob: eb0ea2cb7814ad62b164562fa8232950a9e4650e (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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
#ifndef _GSTRING_INCLUDED
#define _GSTRING_INCLUDED

/**
 * A C++ char string.\n
 * Can reuse (stack-allocated) buffers.\n
 * Can create zero-copy views.
 * @code
Copyright 2012 Kozarezov Artem Aleksandrovich

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
 * @endcode
 * @file
 */

#include <assert.h>
#include <stdlib.h>  // malloc, realloc, free
#include <stdint.h>
#include <string.h>  // memcpy, memmem
#include <stdio.h>  // snprintf
#include <stdexcept>
#include <iostream>
#include <iterator>

#include "exception.hpp"

/// Make a read-only gstring from a C string: `const gstring foo = C2GSTRING("foo")`.
#define C2GSTRING(CSTR) ::glim::gstring (::glim::gstring::ReferenceConstructor(), CSTR, sizeof (CSTR) - 1, true)
/// Usage: GSTRING_ON_STACK (buf, 64) << "foo" << "bar";
#define GSTRING_ON_STACK(NAME, SIZE) char NAME##Buf[SIZE]; ::glim::gstring NAME (SIZE, NAME##Buf, false, 0); NAME.self()

namespace glim {

/**
 * Based on: C++ version 0.4 char* style "itoa": Written by Lukás Chmela, http://www.strudel.org.uk/itoa/ (GPLv3).
 * Returns a pointer to the end of the string.
 * NB about `inline`: http://stackoverflow.com/a/1759575/257568
 * @param base Maximum is 36 (see http://en.wikipedia.org/wiki/Base_36).
 */
inline char* itoa (char* ptr, int64_t value, const int base = 10) {
  // check that the base is valid
  if (base < 2 || base > 36) {*ptr = '\0'; return ptr;}

  char *ptr1 = ptr;
  int64_t tmp_value;

  do {
    tmp_value = value;
    value /= base;
    *ptr++ = "zyxwvutsrqponmlkjihgfedcba9876543210123456789abcdefghijklmnopqrstuvwxyz" [35 + (tmp_value - value * base)];
  } while (value);

  // Apply negative sign
  if (tmp_value < 0) *ptr++ = '-';
  char* end = ptr;
  *ptr-- = '\0';
  char tmp_char;
  while (ptr1 < ptr) {
    tmp_char = *ptr;
    *ptr--= *ptr1;
    *ptr1++ = tmp_char;
  }
  return end;
}

class gstring_stream;

class gstring {
  enum Flags {
    FREE_FLAG = 0x80000000, // 1st bit; `_buf` needs `free`ing
    FREE_OFFSET = 31,
    REF_FLAG = 0x40000000, // 2nd bit; `_buf` has an extended life-time (such as C string literals) and can be shared (passed by reference)
    REF_OFFSET = 30,
    CAPACITY_MASK = 0x3F000000, // 3..8 bits; `_buf` size is 2^this
    CAPACITY_OFFSET = 24,
    LENGTH_MASK = 0x00FFFFFF, // 9th bit; allocated capacity
  };
  uint32_t _meta;
public:
  void* _buf;
public:
  constexpr gstring() noexcept: _meta (0), _buf (nullptr) {}
  /**
   * Reuse `buf` of size `bufSize`.
   * To fully use `buf` the `bufSize` should be the power of two.
   * @param bufSize The size of the memory allocated to `buf`.
   * @param buf The memory region to be reused.
   * @param free Whether the `buf` should be `free`d on resize or gstring destruction.
   * @param length String length inside the `buf`.
   * @param ref If true then the `buf` isn't copied by gstring's copy constructors.
   *            This is useful for wrapping C string literals.
   */
  explicit gstring (uint32_t bufSize, void* buf, bool free, uint32_t length, bool ref = false) noexcept {
    uint32_t power = 0; while (((uint32_t) 1 << (power + 1)) <= bufSize) ++power;
    _meta = ((uint32_t) free << FREE_OFFSET) |
            ((uint32_t) ref << REF_OFFSET) |
            (power << CAPACITY_OFFSET) |
            (length & LENGTH_MASK);
    _buf = buf;
  }

  struct ReferenceConstructor {};
  /// Make a view to the given cstring.
  /// @param buf The memory region to be reused.
  /// @param length String length inside the `buf`.
  /// @param ref If true then the `buf` isn't copied by gstring's copy constructors.
  ///            This is useful for wrapping C string literals.
  explicit constexpr gstring (ReferenceConstructor, const char* buf, uint32_t length, bool ref = false) noexcept:
    _meta (((uint32_t) ref << REF_OFFSET) | (length & LENGTH_MASK)), _buf ((void*) buf) {}

  /// Copy the characters into `gstring`.
  gstring (const char* chars): _meta (0), _buf (nullptr) {
    if (chars && *chars) {
      size_t length = ::strlen (chars);
      _buf = ::malloc (length);
      ::memcpy (_buf, chars, length);
      _meta = (uint32_t) FREE_FLAG |
              (length & LENGTH_MASK);
    }
  }

  /// Copy the characters into `gstring`.
  gstring (const char* chars, size_t length) {
    if (length != 0) {
      _buf = ::malloc (length);
      ::memcpy (_buf, chars, length);
    } else _buf = nullptr;
    _meta = (uint32_t) FREE_FLAG |
            (length & LENGTH_MASK);
  }

  /// Copy into `gstring`.
  gstring (const std::string& str): _meta (0), _buf (nullptr) {
    if (!str.empty()) {
      _buf = ::malloc (str.length());
      ::memcpy (_buf, str.data(), str.length());
      _meta = (uint32_t) FREE_FLAG |
              (str.length() & LENGTH_MASK);
    }
  }

  /// If `gstr` is `copiedByReference` then make a shallow copy of it,
  /// otherwise copy `gstr` contents into a `malloc`ed buffer.
  gstring (const gstring& gstr) {
    uint32_t glen = gstr.length();
    if (glen != 0) {
      if (gstr.copiedByReference()) {
        _meta = gstr._meta; _buf = gstr._buf;
      } else {
        _buf = ::malloc (glen);
        if (!_buf) GTHROW ("!malloc");
        ::memcpy (_buf, gstr._buf, glen);
        _meta = (uint32_t) FREE_FLAG |
                (glen & LENGTH_MASK);
      }
    } else {
      _meta = 0; _buf = nullptr;
    }
  }
  gstring (gstring&& gstr) noexcept: _meta (gstr._meta), _buf (gstr._buf) {
    gstr._meta = 0; gstr._buf = nullptr;
  }
  gstring& operator = (const gstring& gstr) {
    // cf. http://stackoverflow.com/questions/9322174/move-assignment-operator-and-if-this-rhs
    if (this != &gstr) {
      uint32_t glen = gstr.length();
      uint32_t power = 0;
      uint32_t capacity = this->capacity();
      if (glen <= capacity && capacity > 1) { // `capacity <= 1` means there is no _buf.
        // We reuse existing buffer. Keep capacity info.
        power = (_meta & CAPACITY_MASK) >> CAPACITY_OFFSET;
      } else {
        if (_buf != nullptr && needsFreeing()) ::free (_buf);
        if (gstr.copiedByReference()) {
          _meta = gstr._meta; _buf = gstr._buf;
          return *this;
        }
        _buf = ::malloc (glen);
        if (_buf == nullptr) GTHROW ("malloc failed");
      }
      ::memcpy (_buf, gstr._buf, glen);
      _meta = (uint32_t) FREE_FLAG |
              (power << CAPACITY_OFFSET) |
              (glen & LENGTH_MASK);
    }
    return *this;
  }
  gstring& operator = (gstring&& gstr) noexcept {
    assert (this != &gstr);
    if (_buf != nullptr && needsFreeing()) free (_buf);
    _meta = gstr._meta; _buf = gstr._buf;
    gstr._meta = 0; gstr._buf = nullptr;
    return *this;
  }

  /// Return a copy of the string.
  gstring clone() const {return gstring (data(), length());}
  /// If the gstring's buffer is not owned then copy the bytes into the owned one.
  /// Useful for turning a stack-allocated gstring into a heap-allocated gstring.
  gstring& owned() {if (!needsFreeing()) *this = gstring (data(), length()); return *this;}
  /** Returns a reference to the gstring: when the reference is copied the internal buffer is not copied but referenced (shallow copy).\n
   * This method should only be used if it is know that the life-time of the reference and its copies is less than the life-time of the buffer. */
  gstring ref() const noexcept {return gstring (0, _buf, false, length(), true);}

  bool needsFreeing() const noexcept {return _meta & FREE_FLAG;}
  bool copiedByReference() const noexcept {return _meta & REF_FLAG;}
  /// Current buffer capacity (memory allocated to the string). Returns 1 if no memory allocated.
  uint32_t capacity() const noexcept {return 1 << ((_meta & CAPACITY_MASK) >> CAPACITY_OFFSET);}
  uint32_t length() const noexcept {return _meta & LENGTH_MASK;}
  size_t size() const noexcept {return _meta & LENGTH_MASK;}
  bool empty() const noexcept {return (_meta & LENGTH_MASK) == 0;}
  std::string str() const {size_t len = size(); return len ? std::string ((const char*) _buf, len) : std::string();}
  /// NB: might move the string to a new buffer.
  const char* c_str() const {
    uint32_t len = length(); if (len == 0) return "";
    uint32_t cap = capacity();
    // c_str should work even for const gstring's, otherwise it's too much of a pain.
    if (cap < len + 1) const_cast<gstring*> (this) ->reserve (len + 1);
    char* buf = (char*) _buf; buf[len] = 0; return buf;
  }
  bool equals (const char* cstr) const noexcept {
    const char* cstr_; uint32_t clen_;
    if (cstr != nullptr) {cstr_ = cstr; clen_ = strlen (cstr);} else {cstr_ = ""; clen_ = 0;}
    const uint32_t len = length();
    if (len != clen_) return false;
    const char* gstr_ = _buf != nullptr ? (const char*) _buf : "";
    return memcmp (gstr_, cstr_, len) == 0;
  }
  bool equals (const gstring& gs) const noexcept {
    uint32_t llen = length(), olen = gs.length();
    if (llen != olen) return false;
    return memcmp ((const char*) _buf, (const char*) gs._buf, llen) == 0;
  }

  char& operator[] (unsigned index) noexcept {return ((char*)_buf)[index];}
  const char& operator[] (unsigned index) const noexcept {return ((const char*)_buf)[index];}

  /// Access `_buf` as `char*`. `_buf` might be nullptr.
  char* data() noexcept {return (char*)_buf;}
  const char* data() const noexcept {return (const char*)_buf;}

  char* endp() noexcept {return (char*)_buf + length();}
  const char* endp() const noexcept {return (const char*)_buf + length();}

  gstring view (uint32_t pos, int32_t count = -1) noexcept {
    return gstring (0, data() + pos, false, count >= 0 ? count : length() - pos, copiedByReference());}
  const gstring view (uint32_t pos, int32_t count = -1) const noexcept {
    return gstring (0, (void*)(data() + pos), false, count >= 0 ? count : length() - pos, copiedByReference());}

  // http://en.cppreference.com/w/cpp/concept/Iterator
  template<typename CT> struct iterator_t: public std::iterator<std::random_access_iterator_tag, CT, int32_t> {
    CT* _ptr;
    iterator_t () noexcept: _ptr (nullptr) {}
    iterator_t (CT* ptr) noexcept: _ptr (ptr) {}
    iterator_t (const iterator_t<CT>& it) noexcept: _ptr (it._ptr) {}

    CT& operator*() const noexcept {return *_ptr;}
    CT* operator->() const noexcept {return _ptr;}
    CT& operator[](int32_t ofs) const noexcept {return _ptr[ofs];}

    iterator_t<CT>& operator++() noexcept {++_ptr; return *this;}
    iterator_t<CT> operator++(int) noexcept {return iterator_t<CT> (_ptr++);};
    iterator_t<CT>& operator--() noexcept {--_ptr; return *this;}
    iterator_t<CT> operator--(int) noexcept {return iterator_t<CT> (_ptr--);};
    bool operator == (const iterator_t<CT>& i2) const noexcept {return _ptr == i2._ptr;}
    bool operator != (const iterator_t<CT>& i2) const noexcept {return _ptr != i2._ptr;}
    bool operator < (const iterator_t<CT>& i2) const noexcept {return _ptr < i2._ptr;}
    bool operator > (const iterator_t<CT>& i2) const noexcept {return _ptr > i2._ptr;}
    bool operator <= (const iterator_t<CT>& i2) const noexcept {return _ptr <= i2._ptr;}
    bool operator >= (const iterator_t<CT>& i2) const noexcept {return _ptr >= i2._ptr;}
    iterator_t<CT> operator + (int32_t ofs) const noexcept {return iterator (_ptr + ofs);}
    iterator_t<CT>& operator += (int32_t ofs) noexcept {_ptr += ofs; return *this;}
    iterator_t<CT> operator - (int32_t ofs) const noexcept {return iterator (_ptr - ofs);}
    iterator_t<CT>& operator -= (int32_t ofs) noexcept {_ptr -= ofs; return *this;}
  };
  // http://en.cppreference.com/w/cpp/concept/Container
  typedef char value_type;
  typedef char& reference;
  typedef const char& const_reference;
  typedef uint32_t size_type;
  typedef int32_t difference_type;
  typedef iterator_t<char> iterator;
  typedef iterator_t<const char> const_iterator;
  iterator begin() noexcept {return iterator ((char*) _buf);}
  const_iterator begin() const noexcept {return const_iterator ((char*) _buf);}
  iterator end() noexcept {return iterator ((char*) _buf + size());}
  const_iterator end() const noexcept {return const_iterator ((char*) _buf + size());}
  const_iterator cbegin() const noexcept {return const_iterator ((char*) _buf);}
  const_iterator cend() const noexcept {return const_iterator ((char*) _buf + size());}

  /** Returns -1 if not found. */
  int32_t find (const char* str, int32_t pos, int32_t count) const noexcept {
    const int32_t hlen = (int32_t) length() - pos;
    if (hlen <= 0) return -1;
    char* haystack = (char*) _buf + pos;
    void* mret = memmem (haystack, hlen, str, count);
    if (mret == 0) return -1;
    return (char*) mret - (char*) _buf;
  }
  int32_t find (const char* str, int32_t pos = 0) const noexcept {return find (str, pos, strlen (str));}

  /** Index of `ch` inside the string or -1 if not found. */
  int32_t indexOf (char ch) const noexcept {
    void* ret = memchr (_buf, ch, size());
    return ret == nullptr ? -1 : (char*) ret - (char*) _buf;
  }

  // Helps to workaround the "statement has no effect" warning in `GSTRING_ON_STACK`.
  gstring& self() noexcept {return *this;}

  /** Grow buffer to be at least `to` characters long. */
  void reserve (uint32_t to) {
    uint32_t power = (_meta & CAPACITY_MASK) >> CAPACITY_OFFSET;
    if (((uint32_t) 1 << power) < to) {
      ++power;
      while (((uint32_t) 1 << power) < to) ++power;
      if (power > 24) {GSTRING_ON_STACK (error, 64) << "gstring too large: " << (int) to; GTHROW (error.str());}
    } else if (power) {
      // No need to grow.
      return;
    }
    _meta = (_meta & ~CAPACITY_MASK) | (power << CAPACITY_OFFSET);
    if (needsFreeing() && _buf != nullptr) {
      _buf = ::realloc (_buf, capacity());
      if (_buf == nullptr) GTHROW ("realloc failed");
    } else {
      const char* oldBuf = (const char*) _buf;
      _buf = ::malloc (capacity());
      if (_buf == nullptr) GTHROW ("malloc failed");
      if (oldBuf != nullptr) ::memcpy (_buf, oldBuf, length());
      _meta |= FREE_FLAG;
    }
  }

  /** Length setter. Useful when you manually write into the buffer or to cut the string. */
  void length (uint32_t len) noexcept {
    _meta = (_meta & ~LENGTH_MASK) | (len & LENGTH_MASK);
  }

protected:
  friend class gstring_stream;
public:
  /** Appends an integer to the string.
   * @param base Radix, from 1 to 36 (default 10).
   * @param bytes How many bytes to reserve (24 by default). */
  void append64 (int64_t iv, int base = 10, uint_fast8_t bytes = 24) {
    uint32_t pos = length();
    if (capacity() < pos + bytes) reserve (pos + bytes);
    length (itoa ((char*) _buf + pos, iv, base) - (char*) _buf);
  }
  void append (char ch) {
    uint32_t pos = length();
    const uint32_t cap = capacity();
    if (pos >= cap || cap <= 1) reserve (pos + 1);
    ((char*)_buf)[pos] = ch;
    length (++pos);
  }
  void append (const char* cstr, uint32_t clen) {
    uint32_t len = length();
    uint32_t need = len + clen;
    const uint32_t cap = capacity();
    if (need > cap || cap <= 1) reserve (need);
    ::memcpy ((char*) _buf + len, cstr, clen);
    length (need);
  }
  /** This one is for http://code.google.com/p/re2/; `clear` then `append`. */
  bool ParseFrom (const char* cstr, int clen) {
    if (clen < 0 || clen > (int) LENGTH_MASK) return false;
    length (0); append (cstr, (uint32_t) clen); return true;}
  gstring& operator << (const gstring& gs) {append (gs.data(), gs.length()); return *this;}
  gstring& operator << (const std::string& str) {append (str.data(), str.length()); return *this;}
  gstring& operator << (const char* cstr) {if (cstr) append (cstr, ::strlen (cstr)); return *this;}
  gstring& operator << (char ch) {append (ch); return *this;}
  gstring& operator << (int iv) {append64 (iv, 10, sizeof (int) * 3); return *this;}
  gstring& operator << (long iv) {append64 (iv, 10, sizeof (long) * 3); return *this;}
  gstring& operator << (long long iv) {append64 (iv, 10, sizeof (long long) * 3); return *this;}
  gstring& operator << (double dv) {
    uint32_t len = length();
    reserve (len + 32);
    int rc = snprintf (endp(), 31, "%f", dv);
    if (rc > 0) {length (len + std::min (rc, 31));}
    return *this;
  }

  bool operator < (const gstring &gs) const noexcept {
    uint32_t len1 = length(); uint32_t len2 = gs.length();
    if (len1 == len2) return ::strncmp (data(), gs.data(), len1) < 0;
    int cmp = ::strncmp (data(), gs.data(), std::min (len1, len2));
    if (cmp) return cmp < 0;
    return len1 < len2;
  }

  /// Asks `strftime` to generate a time string. Capacity is increased if necessary (up to a limit of +1024 bytes).
  gstring& appendTime (const char* format, struct tm* tmv) {
    int32_t pos = length(), cap = capacity(), left = cap - pos;
    if (left < 8) {reserve (pos + 8); return appendTime (format, tmv);}
    size_t got = strftime ((char*) _buf + pos, left, format, tmv);
    if (got == 0) {
      if (left > 1024) return *this;  // Guard against perpetual growth.
      reserve (pos + left * 2); return appendTime (format, tmv);
    }
    length (pos + got);
    return *this;
  }

  /// Append the characters to this `gstring` wrapping them in the netstring format.
  gstring& appendNetstring (const char* cstr, uint32_t clen) {
    *this << (int) clen; append (':'); append (cstr, clen); append (','); return *this;}
  /// Append the `gstr` wrapping it in the netstring format.
  gstring& appendNetstring (const gstring& gstr) {return appendNetstring (gstr.data(), gstr.length());}

  std::ostream& writeAsNetstring (std::ostream& stream) const;

  /// Parse netstring at `pos` and return a `gstring` *pointing* at the parsed netstring.\n
  /// No heap space allocated.\n
  /// Throws std::runtime_error if netstring parsing fails.\n
  /// If parsing was successfull, then `after` is set to point after the parsed netstring.
  gstring netstringAt (uint32_t pos, uint32_t* after = nullptr) const {
    const uint32_t len = length(); char* buf = (char*) _buf;
    if (buf == nullptr) GTHROW ("gstring: netstringAt: nullptr");
    uint32_t next = pos;
    while (next < len && buf[next] >= '0' && buf[next] <= '9') ++next;
    if (next >= len || buf[next] != ':' || next - pos > 10) GTHROW ("gstring: netstringAt: no header");
    char* endptr = 0;
    long nlen = ::strtol (buf + pos, &endptr, 10);
    if (endptr != buf + next) GTHROW ("gstring: netstringAt: unexpected header end");
    pos = next + 1; next = pos + nlen;
    if (next >= len || buf[next] != ',') GTHROW ("gstring: netstringAt: no body");
    if (after) *after = next + 1;
    return gstring (0, buf + pos, false, next - pos);
  }

  /// Wrapper around strtol, not entirely safe (make sure the string is terminated with a non-digit, by calling c_str, for example).
  long intAt (uint32_t pos, uint32_t* after = nullptr, int base = 10) const {
    // BTW: http://www.kumobius.com/2013/08/c-string-to-int/
    const uint32_t len = length(); char* buf = (char*) _buf;
    if (pos >= len || buf == nullptr) GTHROW ("gstring: intAt: pos >= len");
    char* endptr = 0;
    long lv = ::strtol (buf + pos, &endptr, base);
    uint32_t next = endptr - buf;
    if (next > len) GTHROW ("gstring: intAt: endptr > len");
    if (after) *after = next;
    return lv;
  }

  /// Wrapper around strtol. Copies the string into a temporary buffer in order to pass it to strtol. Empty string returns 0.
  long toInt (int base = 10) const noexcept {
    const uint32_t len = length(); if (len == 0) return 0;
    char buf[len + 1]; memcpy (buf, _buf, len); buf[len] = 0;
    return ::strtol (buf, nullptr, base);
  }

  /// Get a single netstring from the `stream` and append it to the end of `gstring`.
  /// Throws an exception if the input is not a well-formed netstring.
  gstring& readNetstring (std::istream& stream) {
    int32_t nlen; stream >> nlen;
    if (!stream.good() || nlen < 0) GTHROW ("!netstring");
    int ch = stream.get();
    if (!stream.good() || ch != ':') GTHROW ("!netstring");
    uint32_t glen = length();
    const uint32_t cap = capacity();
    if (cap < glen + nlen || cap <= 1) reserve (glen + nlen);
    stream.read ((char*) _buf + glen, nlen);
    if (!stream.good()) GTHROW ("!netstring");
    ch = stream.get();
    if (ch != ',') GTHROW ("!netstring");
    length (glen + nlen);
    return *this;
  }

  /// Set length to 0. `_buf` not changed.
  gstring& clear() noexcept {length (0); return *this;}

  /// Removes `count` characters starting at `pos`.
  gstring& erase (uint32_t pos, uint32_t count = 1) noexcept {
    const char* buf = (const char*) _buf;
    const char* pt1 = buf + pos;
    const char* pt2 = pt1 + count;
    uint32_t len = length();
    const char* end = buf + len;
    if (pt2 <= end) {
      length (len - count);
      ::memmove ((void*) pt1, (void*) pt2, end - pt2);
    }
    return *this;
  }
  /// Remove characters [from,till) and return `from`.\n
  /// Compatible with "boost/algorithm/string/trim.hpp".
  iterator_t<char> erase (iterator_t<char> from, iterator_t<char> till) noexcept {
    intptr_t ipos = from._ptr - (char*) _buf;
    intptr_t count = till._ptr - from._ptr;
    if (ipos >= 0 && count > 0) erase (ipos, count);
    return from;
  }

  ~gstring() noexcept {
    if (_buf != nullptr && needsFreeing()) {::free (_buf); _buf = nullptr;}
  }
};

inline bool operator == (const gstring& gs1, const gstring& gs2) noexcept {return gs1.equals (gs2);}
inline bool operator == (const char* cstr, const gstring& gstr) noexcept {return gstr.equals (cstr);}
inline bool operator == (const gstring& gstr, const char* cstr) noexcept {return gstr.equals (cstr);}
inline bool operator != (const gstring& gs1, const gstring& gs2) noexcept {return !gs1.equals (gs2);}
inline bool operator != (const char* cstr, const gstring& gstr) noexcept {return !gstr.equals (cstr);}
inline bool operator != (const gstring& gstr, const char* cstr) noexcept {return !gstr.equals (cstr);}

inline bool operator == (const gstring& gstr, const std::string& str) noexcept {
  return gstr.equals (gstring (gstring::ReferenceConstructor(), str.data(), str.size()));}
inline bool operator != (const gstring& gstr, const std::string& str) noexcept {return !(gstr == str);}
inline bool operator == (const std::string& str, const gstring& gstr) noexcept {return gstr == str;}
inline bool operator != (const std::string& str, const gstring& gstr) noexcept {return !(gstr == str);}
inline std::string operator += (std::string& str, const gstring& gstr) {return str.append (gstr.data(), gstr.size());}
inline std::string operator + (const std::string& str, const gstring& gstr) {return std::string (str) .append (gstr.data(), gstr.size());}

inline std::ostream& operator << (std::ostream& os, const gstring& gstr) {
  if (gstr._buf != nullptr) os.write ((const char*) gstr._buf, gstr.length());
  return os;
}

/// Encode this `gstring` into `stream` as a netstring.
inline std::ostream& gstring::writeAsNetstring (std::ostream& stream) const {
  stream << length() << ':' << *this << ',';
  return stream;
}

// http://www.mr-edd.co.uk/blog/beginners_guide_streambuf
// http://www.dreamincode.net/code/snippet2499.htm
// http://spec.winprog.org/streams/
class gstring_stream: public std::basic_streambuf<char, std::char_traits<char> > {
  gstring& _gstr;
public:
  gstring_stream (gstring& gstr) noexcept: _gstr (gstr) {
    char* buf = (char*) gstr._buf;
    if (buf != nullptr) setg (buf, buf, buf + gstr.length());
  }
protected:
  virtual int_type overflow (int_type ch) {
    if (__builtin_expect (ch != traits_type::eof(), 1)) _gstr.append ((char) ch);
    return 0;
  }
  // no copying
  gstring_stream (const gstring_stream &);
  gstring_stream& operator = (const gstring_stream &);
};

} // namespace glim

// hash specialization
// cf. http://stackoverflow.com/questions/8157937/how-to-specialize-stdhashkeyoperator-for-user-defined-type-in-unordered
namespace std {
  template <> struct hash<glim::gstring> {
    size_t operator()(const glim::gstring& gs) const noexcept {
      // cf. http://stackoverflow.com/questions/7666509/hash-function-for-string
      // Would be nice to use https://131002.net/siphash/ here.
      uint32_t hash = 5381;
      uint32_t len = gs.length();
      if (len) {
        const char* str = (const char*) gs._buf;
        const char* end = str + len;
        while (str < end) hash = ((hash << 5) + hash) + *str++; /* hash * 33 + c */
      }
      return hash;
    }
  };
}

#endif // _GSTRING_INCLUDED