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