Warning: Attempt to read property "date" on null in /usr/local/www/websvn.planix.org/blame.php on line 247

Warning: Attempt to read property "msg" on null in /usr/local/www/websvn.planix.org/blame.php on line 247
WebSVN – planix.SVN – Blame – /os/branches/feature_unix/sys/man/2/string – Rev 34

Subversion Repositories planix.SVN

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

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