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
11 .ta +\w'\fLSinstack* 'u
16 void s_free(String *s)
19 String* s_newalloc(int n)
22 String* s_array(char *p, int n)
25 String* s_grow(String *s, int n)
28 void s_putc(String *s, int c)
31 void s_terminate(String *s)
34 String* s_reset(String *s)
37 String* s_restart(String *s)
40 String* s_append(String *s, char *p)
43 String* s_nappend(String *s, char *p, int n)
46 String* s_memappend(String *s, char *p, int n)
49 String* s_copy(char *p)
52 String* s_parse(String *s1, String *s2)
56 void s_tolower(String *s)
59 String* s_incref(String *s)
62 String* s_unique(String *s)
65 Sinstack* s_allocinstack(char *file)
68 void s_freeinstack(Sinstack *stack)
71 char* s_rdinstack(Sinstack *stack, String *s)
77 int s_read(Biobuf *b, String *s, int n)
80 char* s_read_line(Biobuf *b, String *s)
83 char* s_getline(Biobuf *b, String *s)
86 These routines manipulate extensible strings.
89 which points to an array of characters. The string
90 maintains pointers to the beginning and end of the allocated
91 array. In addition a finger pointer keeps track of where
92 parsing will start (for
94 or new characters will be added (for
99 The structure, and a few useful macros are:
102 typedef struct String {
104 char *base; /* base of String */
105 char *end; /* end of allocated space+1 */
106 char *ptr; /* ptr into String */
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)
116 is used when code needs a reference to the character array.
119 directly is frowned upon since it exposes too much of the implementation.
120 .SS "Allocation and freeing
122 A string must be allocated before it can be used.
123 One normally does this using
125 giving the string an initial allocation of
127 If you know that the string will need to grow much
130 instead, specifying the number of bytes in the
134 causes both the string and its character array to be freed.
137 grows a string's allocation by a fixed amount. It is useful if
138 you are reading directly into a string's character array but should
139 be avoided if possible.
142 is used to create a constant array, that is, one whose contents
143 won't change. It points directly to the character array
144 given as an argument. Tread lightly when using this call.
145 .SS "Filling the string
146 After its initial allocation, the string points to the beginning
147 of an allocated array of characters starting with
151 writes a character into the string at the
152 pointer and advances the pointer to point after it.
157 at the pointer but doesn't advance it.
160 resets the pointer to the begining of the string but doesn't change the contents.
171 copy characters into the string at the pointer and
172 advance the pointer. They also write a
175 the pointer without advancing the pointer beyond it.
176 Both routines stop copying on encountering a
181 but doesn't stop at a
184 If you know the initial character array to be copied into a string,
185 you can allocate a string and copy in the bytes using
187 This is the equivalent of a
193 copies the next white space terminated token from
198 White space is defined as space, tab,
199 and newline. Both single and double quoted strings are treated as
200 a single token. The bounding quotes are not copied.
201 There is no escape mechanism.
206 characters in the string to lower case.
210 is used by multithreaded programs to avoid having the string memory
211 released until the last user of the string performs an
214 returns a unique copy of the string: if the reference count it
215 1 it returns the string, otherwise it returns an
221 reads the requested number of characters through a
223 into a string. The string is grown as necessary.
224 An eof or error terminates the read.
225 The number of bytes read is returned.
226 The string is null terminated.
229 reads up to and including the next newline and returns
230 a pointer to the beginning of the bytes read.
231 An eof or error terminates the read.
232 The string is null terminated.
235 reads up to the next newline, appends the input to
238 a pointer to the beginning of the bytes read. Leading
239 spaces and tabs and the trailing newline are all discarded.
241 discards blank lines and lines beginning with
245 newlines escaped by immediately-preceding backslashes.
248 allocates an input stack with the single file
252 frees an input stack.
254 reads a line from an input stack.
255 It follows the same rules as
257 except that when it encounters a line of the form
263 onto the input stack, postponing further reading of the current
267 The input stack has a maximum depth of 32 nested include files.