summaryrefslogtreecommitdiff
path: root/reference/C/CONTRIB/SNIP/str.cpp
blob: df4f11873cb782da4099d08a2ac1d3eb94644306 (plain)
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
//
// Implements simple string class 'str'
//

# include "str.h"
# include <string.h>
# include <memory.h>
# if defined( _MSC_VER )
# pragma warning(disable:4505)
# endif

# define STDLEN 32

extern "C" void * malloc (unsigned sz);
extern "C" void free (void * ptr);

# if defined( PLACEMENT_NEW_BUG )

inline void *
operator new (unsigned sz, short allocsz)
{
    return malloc (sz + allocsz);
}

# else

void *
refstr::operator new (unsigned sz, short allocsz)
{
    return malloc (sz + allocsz);
}

# endif

void
str::_strinit (char const * s, short len, short siz)
{
    if (len < 0)
        len = (short) ((s) ? strlen (s) : 0);
    if (siz < 0)
        siz = STDLEN;
    if (siz < len + 1)
        siz = short(len + 1);
    strdata = new(siz) refstr(len, siz);
    if (s && len)
        memcpy (c_ptr(), s, len);
}

        // Called whenever string is to be modified or grown
int
str::_chksize (short sz)
{
    refstr * old = 0;
    if (strdata->_refs > 1) // Need to dup memory
        --strdata->_refs;   // Dec existing string reference
    else if (sz >= size())
        old = strdata;
    else
        return 0;
    _strinit (c_ptr(), length(), sz);
    delete old;
    return 1;
}

str &
str::operator= (str const & s)
{
    if (&s != this)
    {
        if (!--strdata->_refs)
            delete strdata;
        strdata = s.strdata;
        ++strdata->_refs;
    }
    return *this;
}

str &
str::operator= (char const * s)
{
    if (s != c_ptr())
    {
        short len = (short) strlen (s);
        _chksize (len);
        memcpy (c_ptr(), s, len + 1);
        strdata->_length = len;
    }
    return *this;
}

str &
str::operator= (char c)
{
    _chksize (1);
    *c_ptr() = c;
    strdata->_length = 1;
    return *this;
}

short
str::insert (short pos, char const * s, short len)
{
    if (len < 0)
        len = (short) strlen (s);
    if (len)
    {
        short leng = strdata->_length;
        if (pos < 0 || pos > leng)
            pos = leng;
        _chksize (short(leng + len));
        char * buf = c_ptr();
        if (pos < leng)
            memmove (buf + pos + len, buf + pos, leng - pos);
        memcpy (buf + pos, s, len);
        strdata->_length += len;
    }
    return length();
}

short
str::remove (short pos, short len)
{
    if (pos >= 0 && pos < length())
    {
        short leng = strdata->_length;
        if (len < 0 || (pos + len) > leng)
            len = short(leng - pos);
        if (len)
        {
            _chksize (0);
            char * buf = c_ptr();
            memcpy (buf + pos, buf + pos + len, leng - (pos + len));
            strdata->_length -= len;
        }
    }
    return length();
}

short
str::replace (short pos, char const * s, short clen, short len)
{
    if (pos >= 0)
    {
        short leng = strdata->_length;
        if (clen < 0 || (pos + clen) > leng)
            clen = short(leng - pos);
        if (len < 0)
            len = (short) strlen (s);
        if (pos > leng)
            pos = leng;
        _chksize (short(leng - clen + len));
        char * buf = c_ptr();
        if (clen != len && clen)
            memmove (buf + pos + len, buf + pos + clen,
                     leng - (pos + clen - len));
        if (len)
            memcpy (buf + pos, s, len);
        strdata->_length += short(len - clen);
    }
    return length();
}


str &
str::left (short len, char padch)
{
    if (len < 0)
        return right (short(-len), padch);
    short leng = strdata->_length;
    if (len != leng)
    {
        _chksize (len);
        if (len > leng)
            memset (strdata->ptr() + leng, padch, leng - len);
        strdata->_length = len;
    }
    return *this;
}

str &
str::right (short len, char padch)
{
    if (len < 0)
        return left(-1, padch);
    short leng = strdata->_length;
    if (len != leng)
    {
        _chksize (len);
        if (len > leng)
        {
            char * buf = strdata->ptr();
            memmove (buf + len - leng, buf, leng);
            memset (buf, padch, len - leng);
        }
        strdata->_length = len;
    }
    return *this;
}

str &
str::mid (short pos, short len, char padch)
{
    if (pos <= 0)
        return left(len, padch);
    short leng = strdata->_length;
    if (pos > leng)
        pos = leng;
    if (leng < len)         // Are we padding?
    {
        _chksize (len);
        char * buf = strdata->ptr();
        short nlen = short((len - (leng - pos)) / 2);
        if (nlen > 0)
        {
            memmove (buf, buf + pos, leng - pos);
            memset (buf + leng - pos, padch, nlen);
            strdata->_length -= short(pos - nlen);
        }
    }
    return right (len, padch);
}


int
str::_concat (char const * s, short len)
{
    if (len < 0)
        len = (short) strlen (s);
    if (len)
    {
        _chksize (short(len + length()));
        memcpy (c_ptr() + length(), s, len);
        strdata->_length += len;
    }
    return length();
}

short
str::removech (char const * clist)
{
    short result = 0;
    if (*clist)
    {
        char * buf, * sub;
        buf = sub = strdata->ptr();
        short nlen = strdata->_length;
        for (short i = 0; i < nlen; ++i)
        {
            if (strchr (clist, *buf) == 0)
            {
                if (result)
                    *sub = *buf;
                ++sub;
            }
            else
            {
                if (!result)
                    _chksize (0);
                ++result;
            }
            ++buf;
        }
        strdata->_length = short(nlen - result);
    }
    return result;
}

short
str::countch (char const * clist)
{
    short result = 0;
    if (*clist)
    {
        char * buf = strdata->ptr();
        short nlen = strdata->_length;
        for (short i = 0; i < nlen; ++i, ++buf)
            if (strchr (clist, *buf) != 0)
                ++result;
    }
    return result;
}


str
left (str const & s, short len, char padch)
{
    str n(s);
    return n.left(len, padch);
}

str
right (str const & s, short len, char padch)
{
    str n(s);
    return n.right(len, padch);
}

str
mid (str const & s, short pos, short len, char padch)
{
    str n(s);
    return n.mid(pos, len, padch);
}