blob: 0782b260fdbe9b81fe2031564ab9ec753b42c9fa [file] [log] [blame]
rsccfa37a72004-04-10 18:53:55 +00001.TH STRING 3
2.SH NAME
3s_alloc, s_append, s_array, s_copy, s_error, s_free, s_incref, s_memappend, s_nappend, s_new, s_newalloc, s_parse, s_reset, s_restart, s_terminate, s_tolower, s_putc, s_unique, s_grow, s_read, s_read_line, s_getline \- extensible strings
4.SH SYNOPSIS
5.B #include <u.h>
6.br
7.B #include <libc.h>
8.br
9.B #include <String.h>
10.PP
11.B
12String* s_new(void)
13.br
14.B
15void s_free(String *s)
16.br
17.B
18String* s_newalloc(int n)
19.br
20.B
21String* s_array(char *p, int n)
22.br
23.B
24String* s_grow(String *s, int n)
25.PP
26.B
27void s_putc(String *s, int c)
28.br
29.B
30void s_terminate(String *s)
31.br
32.B
33String* s_reset(String *s)
34.br
35.B
36String* s_restart(String *s)
37.br
38.B
39String* s_append(String *s, char *p)
40.br
41.B
42String* s_nappend(String *s, char *p, int n)
43.br
44.B
45String* s_memappend(String *s, char *p, int n)
46.br
47.B
48String* s_copy(char *p)
49.br
50.B
51String* s_parse(String *s1, String *s2)
52.br
53.PP
54.B
55void s_tolower(String *s)
56.PP
57.B
58String* s_incref(String *s)
59.br
60.B
61String* s_unique(String *s)
62.PP
63.B
64#include <bio.h>
65.PP
66.B
67int s_read(Biobuf *b, String *s, int n)
68.br
69.B
70char* s_read_line(Biobuf *b, String *s)
71.br
72.B
73char* s_getline(Biobuf *b, String *s)
74.SH DESCRIPTION
75.PP
76These routines manipulate extensible strings.
77The basic type is
78.BR String ,
79which points to an array of characters. The string
80maintains pointers to the beginning and end of the allocated
81array. In addition a finger pointer keeps track of where
82parsing will start (for
83.IR s_parse )
84or new characters will be added (for
85.IR s_putc ,
86.IR s_append ,
87and
88.IR s_nappend ).
89The structure, and a few useful macros are:
90.sp
91.EX
92typedef struct String {
93 Lock;
94 char *base; /* base of String */
95 char *end; /* end of allocated space+1 */
96 char *ptr; /* ptr into String */
97 ...
98} String;
99
100#define s_to_c(s) ((s)->base)
101#define s_len(s) ((s)->ptr-(s)->base)
102#define s_clone(s) s_copy((s)->base)
103.EE
104.PP
105.I S_to_c
106is used when code needs a reference to the character array.
107Using
108.B s->base
109directly is frowned upon since it exposes too much of the implementation.
110.SS "allocation and freeing
111.PP
112A string must be allocated before it can be used.
113One normally does this using
114.IR s_new ,
115giving the string an initial allocation of
116128 bytes.
117If you know that the string will need to grow much
118longer, you can use
119.I s_newalloc
120instead, specifying the number of bytes in the
121initial allocation.
122.PP
123.I S_free
124causes both the string and its character array to be freed.
125.PP
126.I S_grow
127grows a string's allocation by a fixed amount. It is useful if
128you are reading directly into a string's character array but should
129be avoided if possible.
130.PP
131.I S_array
132is used to create a constant array, that is, one whose contents
133won't change. It points directly to the character array
134given as an argument. Tread lightly when using this call.
135.SS "Filling the string
136After its initial allocation, the string points to the beginning
137of an allocated array of characters starting with
138.SM NUL.
139.PP
140.I S_putc
141writes a character into the string at the
142pointer and advances the pointer to point after it.
143.PP
144.I S_terminate
145writes a
146.SM NUL
147at the pointer but doesn't advance it.
148.PP
149.I S_restart
150resets the pointer to the begining of the string but doesn't change the contents.
151.PP
152.I S_reset
153is equivalent to
154.I s_restart
155followed by
156.IR s_terminate .
157.PP
158.I S_append
159and
160.I s_nappend
161copy characters into the string at the pointer and
162advance the pointer. They also write a
163.SM NUL
164at
165the pointer without advancing the pointer beyond it.
166Both routines stop copying on encountering a
167.SM NUL.
168.I S_memappend
169is like
170.I s_nappend
171but doesn't stop at a
172.SM NUL.
173.PP
174If you know the initial character array to be copied into a string,
175you can allocate a string and copy in the bytes using
176.IR s_copy .
177This is the equivalent of a
178.I s_new
179followed by an
180.IR s_append .
181.PP
182.I S_parse
183copies the next white space terminated token from
184.I s1
185to
186the end of
187.IR s2 .
188White space is defined as space, tab,
189and newline. Both single and double quoted strings are treated as
190a single token. The bounding quotes are not copied.
191There is no escape mechanism.
192.PP
193.I S_tolower
194converts all
195.SM ASCII
196characters in the string to lower case.
197.SS Multithreading
198.PP
199.I S_incref
200is used by multithreaded programs to avoid having the string memory
201released until the last user of the string performs an
202.IR s_free .
203.I S_unique
204returns a unique copy of the string: if the reference count it
2051 it returns the string, otherwise it returns an
206.I s_clone
207of the string.
208.SS "Bio interaction
209.PP
210.I S_read
211reads the requested number of characters through a
212.I Biobuf
213into a string. The string is grown as necessary.
214An eof or error terminates the read.
215The number of bytes read is returned.
216The string is null terminated.
217.PP
218.I S_read_line
219reads up to and including the next newline and returns
220a pointer to the beginning of the bytes read.
221An eof or error terminates the read.
222The string is null terminated.
223.PP
224.I S_getline
225reads up to the next newline and returns
226a pointer to the beginning of the bytes read. Leading
227spaces and tabs and the trailing newline are all discarded.
228.I S_getline
229will recursively read through files included with
230.B #include
231and discard all other lines beginning with
232.BR # .
233.SH SOURCE
rscb5fdffe2004-04-19 19:22:56 +0000234.B /usr/local/plan9/src/libString
rsccfa37a72004-04-10 18:53:55 +0000235.SH SEE ALSO
rscbf8a59f2004-04-11 03:42:27 +0000236.IR bio (3)