1 /*
2  *      cook - file construction tool
3  *      Copyright (C) 1998, 2006, 2007 Peter Miller;
4  *      All rights reserved.
5  *
6  *      This program is free software; you can redistribute it and/or modify
7  *      it under the terms of the GNU General Public License as published by
8  *      the Free Software Foundation; either version 3 of the License, or
9  *      (at your option) any later version.
10  *
11  *      This program is distributed in the hope that it will be useful,
12  *      but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *      GNU General Public License for more details.
15  *
16  *      You should have received a copy of the GNU General Public License
17  *      along with this program. If not, see
18  *      <http://www.gnu.org/licenses/>.
19  */
20 
21 #include <common/ac/string.h>
22 
23 #include <common/str.h>
24 #include <common/stracc.h>
25 
26 
27 string_ty *
str_substitute(string_ty * a,string_ty * b,string_ty * c)28 str_substitute(string_ty *a, string_ty *b, string_ty *c)
29 {
30     char            *cp;
31     char            *ep;
32     static stracc   sa;
33 
34     sa_open(&sa);
35     cp = c->str_text;
36     ep = cp + c->str_length;
37     while (cp < ep)
38     {
39         if
40         (
41             (size_t)(ep - cp) < a->str_length
42         ||
43             0 != memcmp(a->str_text, cp, a->str_length)
44         )
45         {
46             sa_char(&sa, *cp++);
47         }
48         else
49         {
50             sa_chars(&sa, b->str_text, b->str_length);
51             cp += a->str_length;
52         }
53     }
54     return sa_close(&sa);
55 }
56