File: api-strbuf.txt

package info (click to toggle)
sheepdog 0.8.3-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 3,364 kB
  • ctags: 3,951
  • sloc: ansic: 30,552; sh: 3,573; perl: 2,924; asm: 453; makefile: 391; python: 192
file content (211 lines) | stat: -rw-r--r-- 6,638 bytes parent folder | download | duplicates (3)
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
strbuf API
==========

strbuf's are meant to be used with all the usual C string and memory
APIs. Given that the length of the buffer is known, it's often better to
use the mem* functions than a str* one (memchr vs. strchr e.g.).
Though, one has to be careful about the fact that str* functions often
stop on NULs and that strbufs may have embedded NULs.

An strbuf is NUL terminated for convenience, but no function in the
strbuf API actually relies on the string being free of NULs.

strbufs has some invariants that are very important to keep in mind:

. The `buf` member is never NULL, so it can be used in any usual C
string operations safely. strbuf's _have_ to be initialized either by
`strbuf_init()` or by `= STRBUF_INIT` before the invariants, though.
+
Do *not* assume anything on what `buf` really is (e.g. if it is
allocated memory or not), use `strbuf_detach()` to unwrap a memory
buffer from its strbuf shell in a safe way. That is the sole supported
way. This will give you a malloced buffer that you can later `free()`.
+
However, it is totally safe to modify anything in the string pointed by
the `buf` member, between the indices `0` and `len-1` (inclusive).

. The `buf` member is a byte array that has at least `len + 1` bytes
  allocated. The extra byte is used to store a `'\0'`, allowing the
  `buf` member to be a valid C-string. Every strbuf function ensure this
  invariant is preserved.
+
NOTE: It is OK to "play" with the buffer directly if you work it this
      way:
+
----
strbuf_grow(sb, SOME_SIZE); <1>
strbuf_setlen(sb, sb->len + SOME_OTHER_SIZE);
----
<1> Here, the memory array starting at `sb->buf`, and of length
`strbuf_avail(sb)` is all yours, and you can be sure that
`strbuf_avail(sb)` is at least `SOME_SIZE`.
+
NOTE: `SOME_OTHER_SIZE` must be smaller or equal to `strbuf_avail(sb)`.
+
Doing so is safe, though if it has to be done in many places, adding the
missing API to the strbuf module is the way to go.
+
WARNING: Do _not_ assume that the area that is yours is of size `alloc
- 1` even if it's true in the current implementation. Alloc is somehow a
"private" member that should not be messed with. Use `strbuf_avail()`
instead.

Data structures
---------------

* `struct strbuf`

This is the string buffer structure. The `len` member can be used to
determine the current length of the string, and `buf` member provides access to
the string itself.

Functions
---------

* Life cycle

`strbuf_init`::

	Initialize the structure. The second parameter can be zero or a bigger
	number to allocate memory, in case you want to prevent further reallocs.

`strbuf_release`::

	Release a string buffer and the memory it used. You should not use the
	string buffer after using this function, unless you initialize it again.

`strbuf_detach`::

	Detach the string from the strbuf and returns it; you now own the
	storage the string occupies and it is your responsibility from then on
	to release it with `free(3)` when you are done with it.

`strbuf_attach`::

	Attach a string to a buffer. You should specify the string to attach,
	the current length of the string and the amount of allocated memory.
	The amount must be larger than the string length, because the string you
	pass is supposed to be a NUL-terminated string.  This string _must_ be
	malloc()ed, and after attaching, the pointer cannot be relied upon
	anymore, and neither be free()d directly.

* Related to the size of the buffer

`strbuf_avail`::

	Determine the amount of allocated but unused memory.

`strbuf_grow`::

	Ensure that at least this amount of unused memory is available after
	`len`. This is used when you know a typical size for what you will add
	and want to avoid repetitive automatic resizing of the underlying buffer.
	This is never a needed operation, but can be critical for performance in
	some cases.

`strbuf_setlen`::

	Set the length of the buffer to a given value. This function does *not*
	allocate new memory, so you should not perform a `strbuf_setlen()` to a
	length that is larger than `len + strbuf_avail()`. `strbuf_setlen()` is
	just meant as a 'please fix invariants from this strbuf I just messed
	with'.

`strbuf_reset`::

	Empty the buffer by setting the size of it to zero.

* Related to the contents of the buffer

`strbuf_rtrim`::

	Strip whitespace from the end of a string.

* Adding data to the buffer

NOTE: All of the functions in this section will grow the buffer as necessary.
If they fail for some reason other than memory shortage and the buffer hadn't
been allocated before (i.e. the `struct strbuf` was set to `STRBUF_INIT`),
then they will free() it.

`strbuf_addch`::

	Add a single character to the buffer.

`strbuf_insert`::

	Insert data to the given position of the buffer. The remaining contents
	will be shifted, not overwritten.

`strbuf_remove`::

	Remove given amount of data from a given position of the buffer.

`strbuf_splice`::

	Remove the bytes between `pos..pos+len` and replace it with the given
	data.

`strbuf_add`::

	Add data of given length to the buffer.

`strbuf_addstr`::

Add a NUL-terminated string to the buffer.
+
NOTE: This function will *always* be implemented as an inline or a macro
that expands to:
+
----
strbuf_add(..., s, strlen(s));
----
+
Meaning that this is efficient to write things like:
+
----
strbuf_addstr(sb, "immediate string");
----

`strbuf_addbuf`::

	Copy the contents of an other buffer at the end of the current one.

`strbuf_addf`::

	Add a formatted string to the buffer.

`strbuf_fread`::

	Read a given size of data from a FILE* pointer to the buffer.
+
NOTE: The buffer is rewound if the read fails. If -1 is returned,
`errno` must be consulted, like you would do for `read(3)`.
`strbuf_read()`, `strbuf_read_file()` and `strbuf_getline()` has the
same behaviour as well.

`strbuf_read`::

	Read the contents of a given file descriptor. The third argument can be
	used to give a hint about the file size, to avoid reallocs.

`strbuf_getline`::

	Read a line from a FILE *, overwriting the existing contents
	of the strbuf. The second argument specifies the line
	terminator character, typically `'\n'`.
	Reading stops after the terminator or at EOF.  The terminator
	is removed from the buffer before returning.  Returns 0 unless
	there was nothing left before EOF, in which case it returns `EOF`.

`strbuf_copyout`::

	Copy the contents of the strbuf to the second argument 'buf'.
	The number of bytes to be copied is at most the third argument
	'len'.

`strbuf_stripout`::

	Strip out the contents of the strbuf to the second argument 'buf'.
	The number of bytes to be copied is at most the third argument
	'len'.