summaryrefslogtreecommitdiff
path: root/source/kit/string_builder.h
blob: 322f6e20ba26852e9de545ff0fa2741881c2d830 (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
#ifndef KIT_STRING_BUILDER_H
#define KIT_STRING_BUILDER_H

#include "status.h"
#include "string_ref.h"
#include "dynamic_array.h"

#include <assert.h>

#ifdef __cplusplus
extern "C" {
#endif

typedef KIT_DA(char) kit_str_builder_t;

#ifdef __GNUC__
#  pragma GCC diagnostic push
#  pragma GCC diagnostic ignored "-Wunused-function"
#  pragma GCC diagnostic ignored "-Wunknown-pragmas"
#  pragma GCC            push_options
#  pragma GCC            optimize("O3")
#endif

static kit_str_builder_t kit_str_build(kit_str_t        s,
                                       kit_allocator_t *alloc) {
  kit_str_builder_t builder;
  KIT_DA_INIT(builder, s.size, alloc);
  assert(builder.size == s.size);
  if (builder.size == s.size)
    memcpy(builder.values, s.values, s.size);
  return builder;
}

static kit_status_t kit_str_append(kit_str_builder_t *a,
                                   kit_str_t          b) {
  assert(a != NULL);
  if (a == NULL)
    return KIT_ERROR_INVALID_ARGUMENT;
  if (b.size <= 0)
    return KIT_OK;
  i64 n = a->size;
  KIT_DA_RESIZE(*a, n + b.size);
  if (a->size != n + b.size)
    return KIT_ERROR_BAD_ALLOC;
  memcpy(a->values + n, b.values, b.size);
  return KIT_OK;
}

#ifdef __GNUC__
#  pragma GCC            pop_options
#  pragma GCC diagnostic pop
#endif

#ifdef __cplusplus
}
#endif

#ifndef KIT_DISABLE_SHORT_NAMES
#  define str_builder_t kit_str_builder_t
#  define str_append kit_str_append
#endif

#endif