Cogs.Core
string_buffer.h
1// Copyright 2010 Google Inc. All Rights Reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14//
15// Author: jdtang@google.com (Jonathan Tang)
16//
17#ifndef GUMBO_STRING_BUFFER_H_
18#define GUMBO_STRING_BUFFER_H_
19
20#include <stdbool.h>
21#include <stddef.h>
22
23#include "gumbo.h"
24
25#ifdef __cplusplus
26extern "C" {
27#endif
28
30
31// A struct representing a mutable, growable string. This consists of a
32// heap-allocated buffer that may grow (by doubling) as necessary. When
33// converting to a string, this allocates a new buffer that is only as long as
34// it needs to be. Note that the internal buffer here is *not* nul-terminated,
35// so be sure not to use ordinary string manipulation functions on it.
36typedef struct {
37 // A pointer to the beginning of the string. NULL iff length == 0.
38 char* data;
39
40 // The length of the string fragment, in bytes. May be zero.
41 size_t length;
42
43 // The capacity of the buffer, in bytes.
44 size_t capacity;
46
47// Initializes a new GumboStringBuffer.
48void gumbo_string_buffer_init(
49 struct GumboInternalParser* parser, GumboStringBuffer* output);
50
51// Ensures that the buffer contains at least a certain amount of space. Most
52// useful with snprintf and the other length-delimited string functions, which
53// may want to write directly into the buffer.
54void gumbo_string_buffer_reserve(struct GumboInternalParser* parser,
55 size_t min_capacity, GumboStringBuffer* output);
56
57// Appends a single Unicode codepoint onto the end of the GumboStringBuffer.
58// This is essentially a UTF-8 encoder, and may add 1-4 bytes depending on the
59// value of the codepoint.
60void gumbo_string_buffer_append_codepoint(
61 struct GumboInternalParser* parser, int c, GumboStringBuffer* output);
62
63// Appends a string onto the end of the GumboStringBuffer.
64void gumbo_string_buffer_append_string(struct GumboInternalParser* parser,
66
67// Converts this string buffer to const char*, alloctaing a new buffer for it.
68char* gumbo_string_buffer_to_string(
69 struct GumboInternalParser* parser, GumboStringBuffer* input);
70
71// Reinitialize this string buffer. This clears it by setting length=0. It
72// does not zero out the buffer itself.
73void gumbo_string_buffer_clear(
74 struct GumboInternalParser* parser, GumboStringBuffer* input);
75
76// Deallocates this GumboStringBuffer.
77void gumbo_string_buffer_destroy(
78 struct GumboInternalParser* parser, GumboStringBuffer* buffer);
79
80#ifdef __cplusplus
81}
82#endif
83
84#endif // GUMBO_STRING_BUFFER_H_