summaryrefslogtreecommitdiff
path: root/reference/C/CONTRIB/SNIP/str.h
blob: 07faead480e61d359bb0f82e089047cdcc6640f3 (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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
//
// Simple string class
// Public domain
//
// Written by david nugent
// davidn@csource.pronet.com
// 3:632/348@fidonet
//

# if !defined( _str_h )
# define _str_h 1

# if defined( __BORLANDC__ ) && ( __BORLANDC__ <= 0x0300 )
# define PLACEMENT_NEW_BUG
# define SIGNED_CHAR_BUG
# endif


struct refstr
{
    short _size;
    short _length;
    short _refs;
    refstr (short length, short size)
        : _length(length), _size(size), _refs(1)
        {}
    ~refstr (void) {}
# if !defined( PLACEMENT_NEW_BUG )
    void * operator new(unsigned sz, short allocsz);
# endif
    char * ptr (void)
    {
        return ((char *)this) + sizeof(refstr);
    }
};




class str
{

  public:

        // constructors/destructors

    str (void)
    {
        _strinit();
    }

    str (char const * s, short len =-1)
    {
        _strinit(s, len, -1);
    }

    str (unsigned char const * s, short len =-1)
    {
        _strinit((char const *)s, len, -1);
    }
# if !defined( SIGNED_CHAR_BUG )
    str (signed char const * s, short len =-1)
    {
        _strinit((char const *)s, len, -1);
    }
# endif

    str (char c)
    {
        _strinit (&c, 1, -1);
    }

    str (unsigned char c)
    {
        _strinit ((char const *)&c, 1, -1);
    }
# if !defined( SIGNED_CHAR_BUG )
    str (signed char c)
    {
        _strinit ((char const *)&c, 1, -1);
    }
# endif
    str (str const & s)
      : strdata(s.strdata)
    {
        ++strdata->_refs;
    }

    ~str (void)
    {
        if (!--strdata->_refs)
            delete strdata;
    }

        // assignment

    str & operator= (str const & s);
    str & operator= (char const * s);
    str & operator= (char c);

    str & operator= (unsigned char const * s)
    {
        return operator= ((char const *)s);
    }
# if !defined( SIGNED_CHAR_BUG )
    str & operator= (signed char const * s)
    {
        return operator= ((char const *)s);
    }
# endif

        // primitive members

    short length (void) const
    {
        return strdata->_length;
    }

    short size (void) const
    {
        return strdata->_size;
    }

        // operators

    str & operator<< (char const * s)   // concatenate
    {
        _concat (s);
        return *this;
    }

    str & operator<< (unsigned char const * s)
    {
        _concat ((char const *)s);
        return *this;
    }
# if !defined( SIGNED_CHAR_BUG )
    str & operator<< (signed char const * s)
    {
        _concat ((char const *)s);
        return *this;
    }
# endif
    str & operator<< (str const & s)
    {
        _concat (s);
        return *this;
    }

    str & operator<< (char c)
    {
        _concat (c);
        return *this;
    }

    str & operator<< (unsigned char c)
    {
        _concat (c);
        return *this;
    }

# if !defined( SIGNED_CHAR_BUG )
    str & operator<< (signed char c)
    {
        _concat (c);
        return *this;
    }
# endif

    char & operator[] (short pos)
    {
        if (pos < 0 || pos >= strdata->_length)
        {
            char * buf = c_ptr() + length();
            *buf = 0;
            return *buf;
        }
        return c_ptr()[pos];
    }

    char * c_ptr (void) const   // not necessarily NUL terminated!
    {                           // Use with caution...
        return strdata->ptr();
    }

    char const * c_str (void) const    // return char*
    {
        char * buf = c_ptr();
        buf[strdata->_length] = 0;
        return buf;
    }

    unsigned char const * u_str (void) const
    {
        return (unsigned char const *)c_str();
    }

        // manipulators

    short insert (short pos, char const * s, short len =-1);

    short insert (short pos, str const & s)
    {
        return insert (pos, s.c_ptr(), s.length());
    }

    short insert (short pos, unsigned char const * s,
                    short len =-1)
    {
        return insert (pos, (char const *)s, len);
    }
# if !defined( SIGNED_CHAR_BUG )
    short insert (short pos, signed char const * s,
                    short len =-1)
    {
        return insert (pos, (char const *)s, len);
    }
# endif
    short insert (short pos, char c)
    {
        return insert (pos, &c, 1);
    }

    short insert (short pos, unsigned char c)
    {
        return insert (pos, (char const *)&c, 1);
    }
# if !defined( SIGNED_CHAR_BUG )
    short insert (short pos, signed char c)
    {
        return insert (pos, (char const *)&c, 1);
    }
# endif

    short remove (short pos =0, short len =-1);

    short replace (short pos, char const * s, short clen =-1,
                    short len =-1);

    short replace (short pos, str & s, short clen =-1)
    {
        return replace (pos, s.c_ptr(), clen, s.length());
    }

    short replace (short pos, unsigned char const * s,
                    short clen =-1, short len =-1)
    {
        return replace (pos, (char const *)s, clen, len);
    }
# if !defined( SIGNED_CHAR_BUG )
    short replace (short pos, signed char const * s,
                    short clen =-1, short len =-1)
    {
        return replace (pos, (char const *)s, clen, len);
    }
# endif
    short replace (short pos, char c, short clen =-1)
    {
        return replace (pos, &c, clen, 1);
    }

    short replace (short pos, unsigned char c, short clen =-1)
    {
        return replace (pos, (char const *)&c, clen, 1);
    }
# if !defined( SIGNED_CHAR_BUG )
    short replace (short pos, signed char c, short clen =-1)
    {
        return replace (pos, (char const *)&c, clen, 1);
    }
# endif

    str & left (short len, char padch =' ');
    str & right (short len, char padch =' ');
    str & mid (short pos, short len, char padch =' ');

    short removech (char const * clist ="\r\n");
    short countch (char const * clist);

  protected:

    refstr * strdata;

    // Check to see if big enough for size
    int _chksize (short sz =0);

    int _concat (char const * s, short len =-1);

    int _concat (str const & s)
    {
        return _concat (s.c_ptr(), s.length());
    }

    int _concat (char ch)
    {
        return _concat (&ch, 1);
    }

    int _concat (unsigned char const * s, short len =-1)
    {
        return _concat ((char const *)s, len);
    }
# if !defined( SIGNED_CHAR_BUG )
    int _concat (signed char const * s, short len =-1)
    {
        return _concat ((char const *)s, len);
    }
# endif

  private:
        // Common constructor
    void _strinit (char const * s =0, short slen =0,
                    short siz =-1);

};


str left (str const & s, short len, char padch =' ');
str right (str const & s, short len, char padch =' ');
str mid (str const & s, short pos, short len, char padch =' ');

# endif