rsc | cfa37a7 | 2004-04-10 18:53:55 +0000 | [diff] [blame] | 1 | .TH STRING 3 |
| 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 \- 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 | .SH DESCRIPTION |
| 75 | .PP |
| 76 | These routines manipulate extensible strings. |
| 77 | The basic type is |
| 78 | .BR String , |
| 79 | which points to an array of characters. The string |
| 80 | maintains pointers to the beginning and end of the allocated |
| 81 | array. In addition a finger pointer keeps track of where |
| 82 | parsing will start (for |
| 83 | .IR s_parse ) |
| 84 | or new characters will be added (for |
| 85 | .IR s_putc , |
| 86 | .IR s_append , |
| 87 | and |
| 88 | .IR s_nappend ). |
| 89 | The structure, and a few useful macros are: |
| 90 | .sp |
| 91 | .EX |
| 92 | typedef 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 |
| 106 | is used when code needs a reference to the character array. |
| 107 | Using |
| 108 | .B s->base |
| 109 | directly is frowned upon since it exposes too much of the implementation. |
| 110 | .SS "allocation and freeing |
| 111 | .PP |
| 112 | A string must be allocated before it can be used. |
| 113 | One normally does this using |
| 114 | .IR s_new , |
| 115 | giving the string an initial allocation of |
| 116 | 128 bytes. |
| 117 | If you know that the string will need to grow much |
| 118 | longer, you can use |
| 119 | .I s_newalloc |
| 120 | instead, specifying the number of bytes in the |
| 121 | initial allocation. |
| 122 | .PP |
| 123 | .I S_free |
| 124 | causes both the string and its character array to be freed. |
| 125 | .PP |
| 126 | .I S_grow |
| 127 | grows a string's allocation by a fixed amount. It is useful if |
| 128 | you are reading directly into a string's character array but should |
| 129 | be avoided if possible. |
| 130 | .PP |
| 131 | .I S_array |
| 132 | is used to create a constant array, that is, one whose contents |
| 133 | won't change. It points directly to the character array |
| 134 | given as an argument. Tread lightly when using this call. |
| 135 | .SS "Filling the string |
| 136 | After its initial allocation, the string points to the beginning |
| 137 | of an allocated array of characters starting with |
| 138 | .SM NUL. |
| 139 | .PP |
| 140 | .I S_putc |
| 141 | writes a character into the string at the |
| 142 | pointer and advances the pointer to point after it. |
| 143 | .PP |
| 144 | .I S_terminate |
| 145 | writes a |
| 146 | .SM NUL |
| 147 | at the pointer but doesn't advance it. |
| 148 | .PP |
| 149 | .I S_restart |
| 150 | resets the pointer to the begining of the string but doesn't change the contents. |
| 151 | .PP |
| 152 | .I S_reset |
| 153 | is equivalent to |
| 154 | .I s_restart |
| 155 | followed by |
| 156 | .IR s_terminate . |
| 157 | .PP |
| 158 | .I S_append |
| 159 | and |
| 160 | .I s_nappend |
| 161 | copy characters into the string at the pointer and |
| 162 | advance the pointer. They also write a |
| 163 | .SM NUL |
| 164 | at |
| 165 | the pointer without advancing the pointer beyond it. |
| 166 | Both routines stop copying on encountering a |
| 167 | .SM NUL. |
| 168 | .I S_memappend |
| 169 | is like |
| 170 | .I s_nappend |
| 171 | but doesn't stop at a |
| 172 | .SM NUL. |
| 173 | .PP |
| 174 | If you know the initial character array to be copied into a string, |
| 175 | you can allocate a string and copy in the bytes using |
| 176 | .IR s_copy . |
| 177 | This is the equivalent of a |
| 178 | .I s_new |
| 179 | followed by an |
| 180 | .IR s_append . |
| 181 | .PP |
| 182 | .I S_parse |
| 183 | copies the next white space terminated token from |
| 184 | .I s1 |
| 185 | to |
| 186 | the end of |
| 187 | .IR s2 . |
| 188 | White space is defined as space, tab, |
| 189 | and newline. Both single and double quoted strings are treated as |
| 190 | a single token. The bounding quotes are not copied. |
| 191 | There is no escape mechanism. |
| 192 | .PP |
| 193 | .I S_tolower |
| 194 | converts all |
| 195 | .SM ASCII |
| 196 | characters in the string to lower case. |
| 197 | .SS Multithreading |
| 198 | .PP |
| 199 | .I S_incref |
| 200 | is used by multithreaded programs to avoid having the string memory |
| 201 | released until the last user of the string performs an |
| 202 | .IR s_free . |
| 203 | .I S_unique |
| 204 | returns a unique copy of the string: if the reference count it |
| 205 | 1 it returns the string, otherwise it returns an |
| 206 | .I s_clone |
| 207 | of the string. |
| 208 | .SS "Bio interaction |
| 209 | .PP |
| 210 | .I S_read |
| 211 | reads the requested number of characters through a |
| 212 | .I Biobuf |
| 213 | into a string. The string is grown as necessary. |
| 214 | An eof or error terminates the read. |
| 215 | The number of bytes read is returned. |
| 216 | The string is null terminated. |
| 217 | .PP |
| 218 | .I S_read_line |
| 219 | reads up to and including the next newline and returns |
| 220 | a pointer to the beginning of the bytes read. |
| 221 | An eof or error terminates the read. |
| 222 | The string is null terminated. |
| 223 | .PP |
| 224 | .I S_getline |
| 225 | reads up to the next newline and returns |
| 226 | a pointer to the beginning of the bytes read. Leading |
| 227 | spaces and tabs and the trailing newline are all discarded. |
| 228 | .I S_getline |
| 229 | will recursively read through files included with |
| 230 | .B #include |
| 231 | and discard all other lines beginning with |
| 232 | .BR # . |
| 233 | .SH SOURCE |
rsc | b5fdffe | 2004-04-19 19:22:56 +0000 | [diff] [blame] | 234 | .B /usr/local/plan9/src/libString |
rsc | cfa37a7 | 2004-04-10 18:53:55 +0000 | [diff] [blame] | 235 | .SH SEE ALSO |
rsc | bf8a59f | 2004-04-11 03:42:27 +0000 | [diff] [blame] | 236 | .IR bio (3) |