LEFT | RIGHT |
1 // Copyright 2012 The Go Authors. All rights reserved. | 1 // Copyright 2012 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 // Byte buffers and string vectors. | 5 // Byte buffers and string vectors. |
6 | 6 |
7 #include "a.h" | 7 #include "a.h" |
8 | 8 |
9 // binit prepares an uninitialized buffer for use. | 9 // binit prepares an uninitialized buffer for use. |
10 void | 10 void |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
92 bwrite(dst, src->p, src->len); | 92 bwrite(dst, src->p, src->len); |
93 } | 93 } |
94 | 94 |
95 // bequal reports whether the buffers have the same content. | 95 // bequal reports whether the buffers have the same content. |
96 bool | 96 bool |
97 bequal(Buf *s, Buf *t) | 97 bequal(Buf *s, Buf *t) |
98 { | 98 { |
99 return s->len == t->len && xmemcmp(s->p, t->p, s->len) == 0; | 99 return s->len == t->len && xmemcmp(s->p, t->p, s->len) == 0; |
100 } | 100 } |
101 | 101 |
102 // bsubst rewites b to replace the first occurrence of x with y. | 102 // bsubst rewites b to replace all occurrences of x with y. |
103 void | 103 void |
104 bsubst(Buf *b, char *x, char *y) | 104 bsubst(Buf *b, char *x, char *y) |
105 { | 105 { |
106 char *p; | 106 char *p; |
107 » int nx, ny; | 107 » int nx, ny, pos; |
108 | 108 |
109 » p = xstrstr(bstr(b), x); | |
110 » if(p == nil) | |
111 » » return; | |
112 nx = xstrlen(x); | 109 nx = xstrlen(x); |
113 ny = xstrlen(y); | 110 ny = xstrlen(y); |
114 » if(nx != ny) { | 111 |
115 » » if(nx < ny) | 112 » pos = 0; |
116 » » » bgrow(b, ny-nx); | 113 » for(;;) { |
117 » » xmemmove(p+ny, p+nx, (b->p+b->len)-(p+nx)); | 114 » » p = xstrstr(bstr(b)+pos, x); |
118 » } | 115 » » if(p == nil) |
119 » xmemmove(p, y, ny); | 116 » » » break; |
120 » b->len += ny - nx; | 117 » » if(nx != ny) { |
| 118 » » » if(nx < ny) |
| 119 » » » » bgrow(b, ny-nx); |
| 120 » » » xmemmove(p+ny, p+nx, (b->p+b->len)-(p+nx)); |
| 121 » » } |
| 122 » » xmemmove(p, y, ny); |
| 123 » » pos = p+ny - b->p; |
| 124 » » b->len += ny - nx; |
| 125 » } |
121 } | 126 } |
122 | 127 |
123 // The invariant with the vectors is that v->p[0:v->len] is allocated | 128 // The invariant with the vectors is that v->p[0:v->len] is allocated |
124 // strings that are owned by the vector. The data beyond v->len may | 129 // strings that are owned by the vector. The data beyond v->len may |
125 // be garbage. | 130 // be garbage. |
126 | 131 |
127 // vinit prepares an uninitialized vector for use. | 132 // vinit prepares an uninitialized vector for use. |
128 void | 133 void |
129 vinit(Vec *v) | 134 vinit(Vec *v) |
130 { | 135 { |
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
262 while(*p == ' ' || *p == '\t' || *p == '\r' || *p == '\n') | 267 while(*p == ' ' || *p == '\t' || *p == '\r' || *p == '\n') |
263 p++; | 268 p++; |
264 if(*p == '\0') | 269 if(*p == '\0') |
265 break; | 270 break; |
266 start = p; | 271 start = p; |
267 while(*p != ' ' && *p != '\t' && *p != '\r' && *p != '\n' && *p
!= '\0') | 272 while(*p != ' ' && *p != '\t' && *p != '\r' && *p != '\n' && *p
!= '\0') |
268 p++; | 273 p++; |
269 vaddn(v, start, p-start); | 274 vaddn(v, start, p-start); |
270 } | 275 } |
271 } | 276 } |
LEFT | RIGHT |