File: ustr-replace-code.h

package info (click to toggle)
ustr 1.0.4-2
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 2,580 kB
  • ctags: 2,100
  • sloc: ansic: 19,191; makefile: 884; perl: 778; sh: 683; xml: 97
file content (342 lines) | stat: -rw-r--r-- 9,672 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/* Copyright (c) 2007 Paul Rosenfeld
                      James Antill -- See LICENSE file for terms. */
#ifndef USTR_REPLACE_H
#error " Include ustr-replace.h before this file."
#endif

USTR_CONF_i_PROTO
size_t ustrp__replace_inline_buf(struct Ustr_pool *p, struct Ustr **ps1,
                                 const void *optr, size_t olen,
                                 const void *nptr, size_t nlen, size_t lim)
{ /* "fast path" ... we can't fail, so ignore the return values */
  size_t num  = 0;
  size_t pos  = 0;
  
  USTR_ASSERT(ustr_owner(*ps1));
  USTR_ASSERT((nlen == olen) || !ustr_alloc(*ps1));

  while ((pos = ustr_srch_buf_fwd(*ps1, pos, optr, olen)))
  {
    USTR_ASSERT((nlen == olen) ||
                (ustr_fixed(*ps1) &&
                 (ustr_size(*ps1) >= (ustr_len(*ps1) + (nlen - olen)))));
    
    ustrp__sc_sub_buf(p, ps1, pos, olen, nptr, nlen);
    pos += nlen - 1;
    
    ++num;
    if (lim && (num == lim))
      break;
  }
  
  if (!num)
    errno = 0; /* only way to tell between FAILURE and NO REPLACEMENTS */
  return (num);
}


USTR_CONF_i_PROTO
size_t ustrp__replace_buf(struct Ustr_pool *p, struct Ustr **ps1,
                          const void *optr, size_t olen,
                          const void *nptr, size_t nlen, size_t lim)
{
  size_t num  = 0;
  size_t tlen = 0;
  size_t pos  = 0;
  struct Ustr *ret = USTR_NULL;
  const char *rptr;
  size_t lpos = 0;
  size_t roff = 0;
  
  USTR_ASSERT(ps1 && ustrp__assert_valid(!!p, *ps1));
  
  if ((nlen == olen) && ustr_owner(*ps1))
    return (ustrp__replace_inline_buf(p, ps1, optr, olen, nptr, nlen, lim));
  
  /* pre-calc size, and do single alloc and then memcpy.
   * Using dup()/ustr_sc_sub() is much simpler but very slow
   * for large strings. */
  tlen = ustr_len(*ps1);
  while ((pos = ustr_srch_buf_fwd(*ps1, pos, optr, olen)))
  {
    pos += olen - 1;

    if (nlen < olen) /* can go up or down */
      tlen -= (olen - nlen);
    else
    {
      if (tlen > (tlen + (nlen - olen)))
      {
        errno = USTR__ENOMEM;
        return (0);
      }
      tlen += (nlen - olen);
    }

    ++num;
    if (lim && (num == lim))
      break;
  }

  if (!num) /* minor speed hack */
  {
    errno = 0; /* only way to tell between FAILURE and NO REPLACEMENTS */
    return (0);
  }
  
  if (!tlen) /* minor speed hack */
    return (ustrp__del(p, ps1, ustr_len(*ps1)) ? num : 0);
  
  if (ustr_fixed(*ps1) && ((num <= 2) || ustr_limited(*ps1)))
  { /* if we will have to memmove() a lot, double copy */
    if (tlen <= ustr_size(*ps1))
      return (ustrp__replace_inline_buf(p, ps1, optr, olen, nptr, nlen, lim));
    if (ustr_limited(*ps1))
      goto fail_alloc;
  }
  
  if (!(ret = ustrp__dupx_undef(p, USTR__DUPX_FROM(*ps1), tlen)))
    goto fail_alloc;
  
  rptr = ustr_cstr(*ps1);
  lpos = 1;
  roff = 0;
  pos  = 0;
  num  = 0;
  while ((pos = ustr_srch_buf_fwd(*ps1, pos, optr, olen)))
  {
    const char *tptr = rptr + roff;
    size_t blen = pos - (roff + 1);
    
    pos += olen - 1;
    USTR_ASSERT(pos == (roff + blen + olen));
    
    ustrp__sub_buf(p, &ret, lpos, tptr, blen); lpos += blen;
    ustrp__sub_buf(p, &ret, lpos, nptr, nlen); lpos += nlen;

    roff = pos;
    
    ++num;
    if (lim && (num == lim))
      break;
  }
  ustrp__sub_buf(p, &ret, lpos, rptr + roff, ustr_len(*ps1) - roff);

  if (!ustr_fixed(*ps1) || (tlen > ustr_size(*ps1)))
    ustrp__sc_free2(p, ps1, ret);
  else
  { /* fixed buffer, with multiple replacements ... but fits */
    ustrp__set(p, ps1, ret);
    ustrp__free(p, ret);
  }
  
  return (num);

 fail_alloc:
  ustr_setf_enomem_err(*ps1);
  return (0);
}

USTR_CONF_I_PROTO
size_t ustr_replace_buf(struct Ustr **ps1, const void *optr, size_t olen,
                        const void *nptr, size_t nlen, size_t lim)
{ return (ustrp__replace_buf(0, ps1, optr, olen, nptr, nlen, lim)); }
USTR_CONF_I_PROTO
size_t ustrp_replace_buf(struct Ustr_pool *p, struct Ustrp **ps1,
                         const void *optr, size_t olen,
                         const void *nptr, size_t nlen, size_t lim)
{
  struct Ustr *tmp = &(*ps1)->s;
  int ret = ustrp__replace_buf(p, &tmp, optr, olen, nptr, nlen, lim);
  *ps1 = USTRP(tmp);
  return (ret);
}

USTR_CONF_i_PROTO
size_t ustrp__replace(struct Ustr_pool *p, struct Ustr **ps1,
                      const struct Ustr *srch,
                      const struct Ustr *repl, size_t lim)
{
  struct Ustr *t1 = USTR_NULL;
  struct Ustr *t2 = USTR_NULL;
  size_t ret = 0;
  
  USTR_ASSERT(ustrp__assert_valid(!!p, srch));
  USTR_ASSERT(ustrp__assert_valid(!!p, repl));

  if (srch == *ps1) srch = t1 = ustrp__dup(p, *ps1);
  if (repl == *ps1) repl = t2 = ustrp__dup(p, *ps1);

  if (srch && repl)
    ret = ustrp__replace_buf(p, ps1,
                             ustr_cstr(srch), ustr_len(srch),
                             ustr_cstr(repl), ustr_len(repl), lim);
  
  ustrp__free(p, t1);
  ustrp__free(p, t2);
  
  return (ret);
}
USTR_CONF_I_PROTO
size_t ustr_replace(struct Ustr **ps1, const struct Ustr *srch,
                    const struct Ustr *repl, size_t lim)
{ return (ustrp__replace(0, ps1, srch, repl, lim)); }
USTR_CONF_I_PROTO
size_t ustrp_replace(struct Ustr_pool *p, struct Ustrp **ps1,
                     const struct Ustrp *srch,
                     const struct Ustrp *repl, size_t lim)
{
  struct Ustr *tmp = &(*ps1)->s;
  int ret = ustrp__replace(p, &tmp, &srch->s, &repl->s, lim);
  *ps1 = USTRP(tmp);
  return (ret);
}

USTR_CONF_i_PROTO
size_t ustrp__replace_inline_rep_chr(struct Ustr_pool *p, struct Ustr **ps1,
                                     char odata, size_t olen, 
                                     char ndata, size_t nlen, size_t lim)
{ /* "fast path" ... as we can't fail after we are the owner(). In theory
   * we can do nlen <= olen, but then we'll spend a lot of time calling
   * memmove(). Which might be painful, so let that fall through to dupx(). */
  size_t num  = 0;
  size_t pos  = 0;

  USTR_ASSERT(ustr_owner(*ps1));
  USTR_ASSERT((nlen == olen) || !ustr_alloc(*ps1));

  while ((pos = ustr_srch_rep_chr_fwd(*ps1, pos, odata, olen)))
  {
    USTR_ASSERT((nlen == olen) ||
                (ustr_fixed(*ps1) &&
                 (ustr_size(*ps1) >= (ustr_len(*ps1) + (nlen - olen)))));
    
    ustrp__sc_sub_rep_chr(p, ps1, pos, olen, ndata, nlen);
    pos += nlen - 1;
    
    ++num;
    if (lim && (num == lim))
      break;
  }

  if (!num)
    errno = 0; /* only way to tell between FAILURE and NO REPLACEMENTS */
  return (num);
}

USTR_CONF_i_PROTO
size_t ustrp__replace_rep_chr(struct Ustr_pool *p, struct Ustr **ps1,
                              char odata, size_t olen, 
                              char ndata, size_t nlen, size_t lim)
{
  size_t num  = 0;
  size_t tlen = 0;
  size_t pos  = 0;
  struct Ustr *ret = USTR_NULL;
  const char *rptr;
  size_t lpos = 0;
  size_t roff = 0;

  USTR_ASSERT(ps1 && ustrp__assert_valid(!!p, *ps1));
  
  if ((nlen == olen) && ustr_owner(*ps1))
    return (ustrp__replace_inline_rep_chr(p, ps1, odata,olen, ndata,nlen, lim));

  /* pre-calc size, and do single alloc and then memcpy.
   * Using dup()/ustr_sc_sub() is much simpler but very slow
   * for large strings. */
  tlen = ustr_len(*ps1);
  while ((pos = ustr_srch_rep_chr_fwd(*ps1, pos, odata, olen)))
  {
    pos += olen - 1;

    if (nlen < olen) /* can go up or down */
      tlen -= (olen - nlen);
    else
    {
      if (tlen > (tlen + (nlen - olen)))
      {
        errno = USTR__ENOMEM;
        return (0);
      }
      tlen += (nlen - olen);
    }

    ++num;
    if (lim && (num == lim))
      break;
  }

  if (!num) /* minor speed hack */
  {
    errno = 0; /* only way to tell between FAILURE and NO REPLACEMENTS */
    return (0);
  }
  
  if (!tlen) /* minor speed hack */
    return (ustrp__del(p, ps1, ustr_len(*ps1)) ? num : 0);
  
  if (ustr_fixed(*ps1) && ((num <= 2) || ustr_limited(*ps1)))
  { /* if we will have to memmove() a lot, double copy */
    if (tlen <= ustr_size(*ps1))
      return (ustrp__replace_inline_rep_chr(p, ps1, odata,olen,ndata,nlen,lim));
    
    if (ustr_limited(*ps1))
      goto fail_alloc;
  }
  
  if (!(ret = ustrp__dupx_undef(p, USTR__DUPX_FROM(*ps1), tlen)))
    goto fail_alloc;
  
  rptr = ustr_cstr(*ps1);
  lpos = 1;
  roff = 0;
  pos  = 0;
  num  = 0;
  while ((pos = ustr_srch_rep_chr_fwd(*ps1, pos, odata, olen)))
  {
    const char *tptr = rptr + roff;
    size_t blen = pos - (roff + 1);
    
    pos += olen - 1;
    USTR_ASSERT(pos == (roff + blen + olen));
    
    ustrp__sub_buf(p, &ret,     lpos, tptr,  blen); lpos += blen;
    ustrp__sub_rep_chr(p, &ret, lpos, ndata, nlen); lpos += nlen;

    roff = pos;
    
    ++num;
    if (lim && (num == lim))
      break;
  }
  ustrp__sub_buf(p, &ret, lpos, rptr + roff, ustr_len(*ps1) - roff);
  
  if (!ustr_fixed(*ps1) || (tlen > ustr_size(*ps1)))
    ustrp__sc_free2(p, ps1, ret);
  else
  { /* fixed buffer, with multiple replacements ... but fits */
    ustrp__set(p, ps1, ret);
    ustrp__free(p, ret);
  }
  
  return (num);

 fail_alloc:
  ustr_setf_enomem_err(*ps1);
  return (0);
}
USTR_CONF_I_PROTO
size_t ustr_replace_rep_chr(struct Ustr **ps1, char odata, size_t olen, 
                            char ndata, size_t nlen, size_t lim)
{ return (ustrp__replace_rep_chr(0, ps1, odata, olen, ndata, nlen, lim)); }
USTR_CONF_I_PROTO
size_t ustrp_replace_rep_chr(struct Ustr_pool *p, struct Ustrp **ps1,
                             char odata, size_t olen, 
                             char ndata, size_t nlen, size_t lim)
{
  struct Ustr *tmp = &(*ps1)->s;
  int ret = ustrp__replace_rep_chr(p, &tmp, odata, olen, ndata, nlen, lim);
  *ps1 = USTRP(tmp);
  return (ret);
}