Loading...
Searching...
No Matches
as_string_builder.h
Go to the documentation of this file.
1/*
2 * Copyright 2008-2024 Aerospike, Inc.
3 *
4 * Portions may be licensed to Aerospike, Inc. under one or more contributor
5 * license agreements.
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
8 * use this file except in compliance with the License. You may obtain a copy of
9 * the License at http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14 * License for the specific language governing permissions and limitations under
15 * the License.
16 */
17#pragma once
18
19#include <aerospike/as_std.h>
20
21#ifdef __cplusplus
22extern "C" {
23#endif
24
25//---------------------------------
26// Types
27//---------------------------------
28
29/**
30 * Fast, non thread safe string builder implementation.
31 */
32typedef struct as_string_builder_s {
33 /**
34 * String Buffer
35 */
36 char* data;
37
38 /**
39 * Number of bytes allocated to the buffer
40 */
41 uint32_t capacity;
42
43 /**
44 * String length of buffer.
45 */
46 uint32_t length;
47
48 /**
49 * Allow resize.
50 */
51 bool resize;
52
53 /**
54 * Should buffer be freed on destroy.
55 */
56 bool free;
58
59//---------------------------------
60// Macros
61//---------------------------------
62
63/**
64 * Initialize string builder with a stack allocated buffer.
65 */
66#define as_string_builder_inita(__sb, __capacity, __resize)\
67(__sb)->data = alloca(__capacity);\
68(__sb)->data[0] = 0;\
69(__sb)->capacity = (__capacity);\
70(__sb)->length = 0;\
71(__sb)->resize = (__resize);\
72(__sb)->free = false;
73
74//---------------------------------
75// Functions
76//---------------------------------
77
78/**
79 * Initialize string builder with a heap allocated buffer.
80 */
81AS_EXTERN void
82as_string_builder_init(as_string_builder* sb, uint32_t capacity, bool resize);
83
84/**
85 * Assign external buffer to string builder. External buffers are not resized.
86 */
87static inline void
88as_string_builder_assign(as_string_builder* sb, uint32_t buffer_size, char* buffer)
89{
90 sb->data = buffer;
91 sb->data[0] = 0;
92 sb->capacity = buffer_size;
93 sb->length = 0;
94 sb->resize = false;
95 sb->free = false;
96}
97
98/**
99 * Free the resources allocated to the buffer.
100 */
101AS_EXTERN void
103
104/**
105 * Initialize to empty string from current state.
106 * Capacity remains unchanged.
107 */
108static inline void
110{
111 sb->data[0] = 0;
112 sb->length = 0;
113}
114
115/**
116 * Append null terminated string value to string buffer.
117 * Returns if successful or not.
118 */
119AS_EXTERN bool
121
122/**
123 * Append a single character to string buffer.
124 * Returns if successful or not.
125 */
126AS_EXTERN bool
128
129/**
130 * Append bytes in hex format to string buffer.
131 * Returns if successful or not.
132 */
133AS_EXTERN bool
134as_string_builder_append_bytes(as_string_builder* sb, uint8_t* src, uint32_t size);
135
136/**
137 * Append integer to string buffer.
138 * Returns if successful or not.
139 */
140AS_EXTERN bool
142
143/**
144 * Append unsigned integer to string buffer.
145 * Returns if successful or not.
146 */
147AS_EXTERN bool
149
150/**
151 * Append signed 64 bit integer to string buffer.
152 * Returns if successful or not.
153 */
154AS_EXTERN bool
156
157/**
158 * Append unsigned 64 bit integer to string buffer.
159 * Returns if successful or not.
160 */
161AS_EXTERN bool
163
164/**
165 * Append newline to string buffer.
166 * Returns if successful or not.
167 */
168static inline bool
173
174#ifdef __cplusplus
175} // end extern "C"
176#endif
#define AS_EXTERN
Definition as_std.h:25
AS_EXTERN bool as_string_builder_append_char(as_string_builder *sb, char value)
AS_EXTERN bool as_string_builder_append_uint(as_string_builder *sb, uint32_t val)
AS_EXTERN bool as_string_builder_append_bytes(as_string_builder *sb, uint8_t *src, uint32_t size)
static bool as_string_builder_append_newline(as_string_builder *sb)
AS_EXTERN bool as_string_builder_append(as_string_builder *sb, const char *value)
static void as_string_builder_reset(as_string_builder *sb)
AS_EXTERN void as_string_builder_destroy(as_string_builder *sb)
AS_EXTERN void as_string_builder_init(as_string_builder *sb, uint32_t capacity, bool resize)
AS_EXTERN bool as_string_builder_append_int64(as_string_builder *sb, int64_t val)
AS_EXTERN bool as_string_builder_append_uint64(as_string_builder *sb, uint64_t val)
static void as_string_builder_assign(as_string_builder *sb, uint32_t buffer_size, char *buffer)
AS_EXTERN bool as_string_builder_append_int(as_string_builder *sb, int val)