Subversion Repositories QNX 8.QNX8 IFS tool

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
26 pmbaty 1
/* ACC -- Automatic Compiler Configuration
2
 
3
   Copyright (C) 1996-2004 Markus Franz Xaver Johannes Oberhumer
4
   All Rights Reserved.
5
 
6
   This software is a copyrighted work licensed under the terms of
7
   the GNU General Public License. Please consult the file "ACC_LICENSE"
8
   for details.
9
 
10
   Markus F.X.J. Oberhumer
11
   <markus@oberhumer.com>
12
   http://www.oberhumer.com/
13
 */
14
 
15
 
16
#define __ACCLIB_HSTRING_CH_INCLUDED 1
17
#if !defined(ACCLIB_PUBLIC)
18
#  define ACCLIB_PUBLIC(r,f)    r __ACCLIB_FUNCNAME(f)
19
#endif
20
 
21
 
22
/***********************************************************************
23
// strlen
24
************************************************************************/
25
 
26
ACCLIB_PUBLIC(acc_hsize_t, acc_hstrlen) (const acc_hchar_p s)
27
{
28
    /* TODO: which one is the fastest generic version? */
29
#if 1
30
    const acc_hchar_p start = s; while (*s) ++s;
31
    return (acc_hsize_t) (s - start);
32
#elif 1
33
    acc_hsize_t n = 0; while (*s) ++s, ++n; return n;
34
#elif 1
35
    acc_hsize_t n = 0; while (s[n]) ++n; return n;
36
#endif
37
}
38
 
39
 
40
/***********************************************************************
41
// strcmp, strncmp, ascii_stricmp, ascii_strnicmp, ascii_memicmp
42
************************************************************************/
43
 
44
ACCLIB_PUBLIC(int, acc_hstrcmp) (const acc_hchar_p p, const acc_hchar_p s)
45
{
46
    for ( ; *p; ++p, ++s) {
47
        if (*p != *s)
48
            break;
49
    }
50
    return (unsigned char)*p - (unsigned char)*s;
51
}
52
 
53
 
54
ACCLIB_PUBLIC(int, acc_hstrncmp) (const acc_hchar_p p, const acc_hchar_p s, acc_hsize_t n)
55
{
56
    for ( ; *p && n > 0; ++p, ++s, --n) {
57
        if (*p != *s)
58
            break;
59
    }
60
    return (unsigned char)*p - (unsigned char)*s;
61
}
62
 
63
 
64
ACCLIB_PUBLIC(int, acc_ascii_hstricmp) (const acc_hchar_p p, const acc_hchar_p s)
65
{
66
    for ( ; *p; ++p, ++s) {
67
        if (*p != *s) {
68
            int d = acc_ascii_utolower(*p) - acc_ascii_utolower(*s);
69
            if (d != 0)
70
                return d;
71
        }
72
    }
73
    return acc_ascii_utolower(*p) - acc_ascii_utolower(*s);
74
}
75
 
76
 
77
ACCLIB_PUBLIC(int, acc_ascii_hstrnicmp) (const acc_hchar_p p, const acc_hchar_p s, acc_hsize_t n)
78
{
79
    for ( ; *p && n > 0; ++p, ++s, --n) {
80
        if (*p != *s) {
81
            int d = acc_ascii_utolower(*p) - acc_ascii_utolower(*s);
82
            if (d != 0)
83
                return d;
84
        }
85
    }
86
    return acc_ascii_utolower(*p) - acc_ascii_utolower(*s);
87
}
88
 
89
 
90
ACCLIB_PUBLIC(int, acc_ascii_hmemicmp) (const acc_hvoid_p pp, const acc_hvoid_p ss, acc_hsize_t n)
91
{
92
    const acc_hbyte_p p = (const acc_hbyte_p) pp;
93
    const acc_hbyte_p s = (const acc_hbyte_p) ss;
94
    for ( ; n > 0; ++p, ++s, --n) {
95
        if (*p != *s) {
96
            int d = acc_ascii_utolower(*p) - acc_ascii_utolower(*s);
97
            if (d != 0)
98
                return d;
99
        }
100
    }
101
    return acc_ascii_utolower(*p) - acc_ascii_utolower(*s);
102
}
103
 
104
 
105
/***********************************************************************
106
// strstr, ascii_stristr, memmem, ascii_memimem
107
************************************************************************/
108
 
109
ACCLIB_PUBLIC(acc_hchar_p, acc_hstrstr) (const acc_hchar_p p, const acc_hchar_p s)
110
{
111
    acc_hsize_t pn = __ACCLIB_FUNCNAME(acc_hstrlen)(p);
112
    acc_hsize_t sn = __ACCLIB_FUNCNAME(acc_hstrlen)(s);
113
    return (acc_hchar_p) __ACCLIB_FUNCNAME(acc_hmemmem)(p, pn, s, sn);
114
}
115
 
116
 
117
ACCLIB_PUBLIC(acc_hchar_p, acc_ascii_hstristr) (const acc_hchar_p p, const acc_hchar_p s)
118
{
119
    acc_hsize_t pn = __ACCLIB_FUNCNAME(acc_hstrlen)(p);
120
    acc_hsize_t sn = __ACCLIB_FUNCNAME(acc_hstrlen)(s);
121
    return (acc_hchar_p) __ACCLIB_FUNCNAME(acc_ascii_hmemimem)(p, pn, s, sn);
122
}
123
 
124
 
125
ACCLIB_PUBLIC(acc_hvoid_p, acc_hmemmem) (const acc_hvoid_p p, acc_hsize_t pn, const acc_hvoid_p s, acc_hsize_t sn)
126
{
127
#define __ACCLIB_REQUIRE_HMEMCPY_CH 1
128
    if (sn == 0) __ACCLIB_CONST_CAST_RETURN(acc_hvoid_p, p)
129
    for ( ; pn >= sn; --pn) {
130
        if (__ACCLIB_FUNCNAME(acc_hmemcmp)(p, s, sn) == 0)
131
            __ACCLIB_CONST_CAST_RETURN(acc_hvoid_p, p)
132
        p = (const acc_hbyte_p)p + 1;
133
    }
134
    return (acc_hvoid_p) 0;
135
}
136
 
137
 
138
ACCLIB_PUBLIC(acc_hvoid_p, acc_ascii_hmemimem) (const acc_hvoid_p p, acc_hsize_t pn, const acc_hvoid_p s, acc_hsize_t sn)
139
{
140
    if (sn == 0) __ACCLIB_CONST_CAST_RETURN(acc_hvoid_p, p)
141
    for ( ; pn >= sn; --pn) {
142
        if (__ACCLIB_FUNCNAME(acc_ascii_hmemicmp)(p, s, sn) == 0)
143
            __ACCLIB_CONST_CAST_RETURN(acc_hvoid_p, p)
144
        p = (const acc_hbyte_p)p + 1;
145
    }
146
    return (acc_hvoid_p) 0;
147
}
148
 
149
 
150
/***********************************************************************
151
// strcpy, strcat
152
************************************************************************/
153
 
154
ACCLIB_PUBLIC(acc_hchar_p, acc_hstrcpy) (acc_hchar_p d, const acc_hchar_p s)
155
{
156
    acc_hchar_p dest = d;
157
    while ((*d = *s) != 0) {
158
        ++d; ++s;
159
    }
160
    return dest;
161
}
162
 
163
 
164
ACCLIB_PUBLIC(acc_hchar_p, acc_hstrcat) (acc_hchar_p d, const acc_hchar_p s)
165
{
166
    acc_hchar_p dest = d;
167
    while (*d) ++d;
168
    while ((*d = *s) != 0) {
169
        ++d; ++s;
170
    }
171
    return dest;
172
}
173
 
174
 
175
/***********************************************************************
176
// strlcpy, strlcat
177
************************************************************************/
178
 
179
ACCLIB_PUBLIC(acc_hsize_t, acc_hstrlcpy) (acc_hchar_p d, const acc_hchar_p s, acc_hsize_t size)
180
{
181
    acc_hsize_t n = 0;
182
    if (n != size) {
183
        do {
184
            if ((*d = *s) == 0) return n;
185
            ++d; ++s; ++n;
186
        } while (n != size);
187
        d[-1] = 0;
188
    }
189
    while (*s) ++s, ++n;
190
    return n;
191
}
192
 
193
 
194
ACCLIB_PUBLIC(acc_hsize_t, acc_hstrlcat) (acc_hchar_p d, const acc_hchar_p s, acc_hsize_t size)
195
{
196
    acc_hsize_t n = 0;
197
    while (*d && n != size) ++d, ++n;
198
    if (n != size) {
199
        do {
200
            if ((*d = *s) == 0) return n;
201
            ++d; ++s; ++n;
202
        } while (n != size);
203
        d[-1] = 0;
204
    }
205
    while (*s) ++s, ++n;
206
    return n;
207
}
208
 
209
 
210
/***********************************************************************
211
// strscpy, strscat
212
// (same as strlcpy/strlcat, but just return -1 in case of failure)
213
************************************************************************/
214
 
215
ACCLIB_PUBLIC(int, acc_hstrscpy) (acc_hchar_p d, const acc_hchar_p s, acc_hsize_t size)
216
{
217
    acc_hsize_t n = 0;
218
    if (n != size) {
219
        do {
220
            if ((*d = *s) == 0) return 0;
221
            ++d; ++s; ++n;
222
        } while (n != size);
223
        d[-1] = 0;
224
    }
225
    return -1;
226
}
227
 
228
 
229
ACCLIB_PUBLIC(int, acc_hstrscat) (acc_hchar_p d, const acc_hchar_p s, acc_hsize_t size)
230
{
231
    acc_hsize_t n = 0;
232
    while (*d && n != size) ++d, ++n;
233
    if (n != size) {
234
        do {
235
            if ((*d = *s) == 0) return 0;
236
            ++d; ++s; ++n;
237
        } while (n != size);
238
        d[-1] = 0;
239
    }
240
    return -1;
241
}
242
 
243
 
244
/***********************************************************************
245
// strccpy, memccpy
246
************************************************************************/
247
 
248
ACCLIB_PUBLIC(acc_hchar_p, acc_hstrccpy) (acc_hchar_p d, const acc_hchar_p s, int c)
249
{
250
    for (;;) {
251
        if ((*d = *s) == 0) break;
252
        if (*d++ == (char) c) return d;
253
        ++s;
254
    }
255
    return (acc_hchar_p) 0;
256
}
257
 
258
 
259
ACCLIB_PUBLIC(acc_hvoid_p, acc_hmemccpy) (acc_hvoid_p d, const acc_hvoid_p s, int c, acc_hsize_t n)
260
{
261
    acc_hbyte_p a = (acc_hbyte_p) d;
262
    const acc_hbyte_p p = (const acc_hbyte_p) s;
263
    const acc_hbyte_p e = (const acc_hbyte_p) s + n;
264
    while (p != e) {
265
        if ((*a++ = *p++) == (unsigned char) c)
266
            return a;
267
    }
268
    return (acc_hvoid_p) 0;
269
}
270
 
271
 
272
/***********************************************************************
273
// strchr, strrchr, ascii_strichr, ascii_strrichr
274
************************************************************************/
275
 
276
ACCLIB_PUBLIC(acc_hchar_p, acc_hstrchr) (const acc_hchar_p s, int c)
277
{
278
    for ( ; *s; ++s) {
279
        if (*s == (char) c)
280
            __ACCLIB_CONST_CAST_RETURN(acc_hchar_p, s)
281
    }
282
    if ((char) c == 0) __ACCLIB_CONST_CAST_RETURN(acc_hchar_p, s)
283
    return (acc_hchar_p) 0;
284
}
285
 
286
 
287
ACCLIB_PUBLIC(acc_hchar_p, acc_hstrrchr) (const acc_hchar_p s, int c)
288
{
289
    const acc_hchar_p start = s;
290
    while (*s) ++s;
291
    if ((char) c == 0) __ACCLIB_CONST_CAST_RETURN(acc_hchar_p, s)
292
    for (;;) {
293
        if (s == start) break; --s;
294
        if (*s == (char) c)
295
            __ACCLIB_CONST_CAST_RETURN(acc_hchar_p, s)
296
    }
297
    return (acc_hchar_p) 0;
298
}
299
 
300
 
301
ACCLIB_PUBLIC(acc_hchar_p, acc_ascii_hstrichr) (const acc_hchar_p s, int c)
302
{
303
    c = acc_ascii_utolower(c);
304
    for ( ; *s; ++s) {
305
        if (acc_ascii_utolower(*s) == c)
306
            __ACCLIB_CONST_CAST_RETURN(acc_hchar_p, s)
307
    }
308
    if (c == 0) __ACCLIB_CONST_CAST_RETURN(acc_hchar_p, s)
309
    return (acc_hchar_p) 0;
310
}
311
 
312
 
313
ACCLIB_PUBLIC(acc_hchar_p, acc_ascii_hstrrichr) (const acc_hchar_p s, int c)
314
{
315
    const acc_hchar_p start = s;
316
    c = acc_ascii_utolower(c);
317
    while (*s) ++s;
318
    if (c == 0) __ACCLIB_CONST_CAST_RETURN(acc_hchar_p, s)
319
    for (;;) {
320
        if (s == start) break; --s;
321
        if (acc_ascii_utolower(*s) == c)
322
            __ACCLIB_CONST_CAST_RETURN(acc_hchar_p, s)
323
    }
324
    return (acc_hchar_p) 0;
325
}
326
 
327
 
328
/***********************************************************************
329
// memchr, memrchr, ascii_memichr, ascii_memrichr
330
************************************************************************/
331
 
332
ACCLIB_PUBLIC(acc_hvoid_p, acc_hmemchr) (const acc_hvoid_p s, int c, acc_hsize_t n)
333
{
334
    const acc_hbyte_p p = (const acc_hbyte_p) s;
335
    const acc_hbyte_p e = (const acc_hbyte_p) s + n;
336
    while (p != e) {
337
        if (*p == (unsigned char) c)
338
            __ACCLIB_CONST_CAST_RETURN(acc_hvoid_p, p)
339
        ++p;
340
    }
341
    return (acc_hvoid_p) 0;
342
}
343
 
344
 
345
ACCLIB_PUBLIC(acc_hvoid_p, acc_hmemrchr) (const acc_hvoid_p s, int c, acc_hsize_t n)
346
{
347
    const acc_hbyte_p p = (const acc_hbyte_p) s + n;
348
    const acc_hbyte_p e = (const acc_hbyte_p) s;
349
    while (p != e) {
350
        --p;
351
        if (*p == (unsigned char) c)
352
            __ACCLIB_CONST_CAST_RETURN(acc_hvoid_p, p)
353
    }
354
    return (acc_hvoid_p) 0;
355
}
356
 
357
 
358
ACCLIB_PUBLIC(acc_hvoid_p, acc_ascii_hmemichr) (const acc_hvoid_p s, int c, acc_hsize_t n)
359
{
360
    const acc_hbyte_p p = (const acc_hbyte_p) s;
361
    const acc_hbyte_p e = (const acc_hbyte_p) s + n;
362
    c = acc_ascii_utolower(c);
363
    while (p != e) {
364
        if (acc_ascii_utolower(*p) == c)
365
            __ACCLIB_CONST_CAST_RETURN(acc_hvoid_p, p)
366
        ++p;
367
    }
368
    return (acc_hvoid_p) 0;
369
}
370
 
371
 
372
ACCLIB_PUBLIC(acc_hvoid_p, acc_ascii_hmemrichr) (const acc_hvoid_p s, int c, acc_hsize_t n)
373
{
374
    const acc_hbyte_p p = (const acc_hbyte_p) s + n;
375
    const acc_hbyte_p e = (const acc_hbyte_p) s;
376
    c = acc_ascii_utolower(c);
377
    while (p != e) {
378
        --p;
379
        if (acc_ascii_utolower(*p) == c)
380
            __ACCLIB_CONST_CAST_RETURN(acc_hvoid_p, p)
381
    }
382
    return (acc_hvoid_p) 0;
383
}
384
 
385
 
386
/***********************************************************************
387
// strspn, strrspn
388
************************************************************************/
389
 
390
ACCLIB_PUBLIC(acc_hsize_t, acc_hstrspn) (const acc_hchar_p s, const acc_hchar_p accept)
391
{
392
    acc_hsize_t n = 0;
393
    for ( ; *s; ++s) {
394
        const acc_hchar_p a;
395
        for (a = accept; *a; ++a)
396
            if (*s == *a)
397
                goto acc_label_next;
398
        break;
399
    acc_label_next:
400
        ++n;
401
    }
402
    return n;
403
}
404
 
405
 
406
ACCLIB_PUBLIC(acc_hsize_t, acc_hstrrspn) (const acc_hchar_p s, const acc_hchar_p accept)
407
{
408
    acc_hsize_t n = 0;
409
    const acc_hchar_p start = s;
410
    while (*s) ++s;
411
    for (;;) {
412
        const acc_hchar_p a;
413
        if (s == start) break; --s;
414
        for (a = accept; *a; ++a)
415
            if (*s == *a)
416
                goto acc_label_next;
417
        break;
418
    acc_label_next:
419
        ++n;
420
    }
421
    return n;
422
}
423
 
424
 
425
/***********************************************************************
426
// strcspn, strrcspn
427
************************************************************************/
428
 
429
ACCLIB_PUBLIC(acc_hsize_t, acc_hstrcspn) (const acc_hchar_p s, const acc_hchar_p reject)
430
{
431
    acc_hsize_t n = 0;
432
    for ( ; *s; ++s, ++n) {
433
        const acc_hchar_p r;
434
        for (r = reject; *r; ++r)
435
            if (*s == *r)
436
                return n;
437
    }
438
    return n;
439
}
440
 
441
 
442
ACCLIB_PUBLIC(acc_hsize_t, acc_hstrrcspn) (const acc_hchar_p s, const acc_hchar_p reject)
443
{
444
    acc_hsize_t n = 0;
445
    const acc_hchar_p start = s;
446
    while (*s) ++s;
447
    for ( ; ; ++n) {
448
        const acc_hchar_p r;
449
        if (s == start) break; --s;
450
        for (r = reject; *r; ++r)
451
            if (*s == *r)
452
                return n;
453
    }
454
    return n;
455
}
456
 
457
 
458
/***********************************************************************
459
// strpbrk, strrpbrk
460
************************************************************************/
461
 
462
ACCLIB_PUBLIC(acc_hchar_p, acc_hstrpbrk) (const acc_hchar_p s, const acc_hchar_p accept)
463
{
464
    for ( ; *s; ++s) {
465
        const acc_hchar_p a;
466
        for (a = accept; *a; ++a) {
467
            if (*s == *a)
468
                __ACCLIB_CONST_CAST_RETURN(acc_hchar_p, s)
469
        }
470
    }
471
    return (acc_hchar_p) 0;
472
}
473
 
474
 
475
ACCLIB_PUBLIC(acc_hchar_p, acc_hstrrpbrk) (const acc_hchar_p s, const acc_hchar_p accept)
476
{
477
    const acc_hchar_p start = s;
478
    while (*s) ++s;
479
    for (;;) {
480
        const acc_hchar_p a;
481
        if (s == start) break; --s;
482
        for (a = accept; *a; ++a) {
483
            if (*s == *a)
484
                __ACCLIB_CONST_CAST_RETURN(acc_hchar_p, s)
485
        }
486
    }
487
    return (acc_hchar_p) 0;
488
}
489
 
490
 
491
/***********************************************************************
492
// strsep, strrsep
493
************************************************************************/
494
 
495
ACCLIB_PUBLIC(acc_hchar_p, acc_hstrsep) (acc_hchar_pp ss, const acc_hchar_p delim)
496
{
497
    acc_hchar_p s = *ss;
498
    if (s) {
499
        acc_hchar_p p = __ACCLIB_FUNCNAME(acc_hstrpbrk)(s, delim);
500
        if (p) *p++ = 0;
501
        *ss = p;
502
    }
503
    return s;
504
}
505
 
506
 
507
ACCLIB_PUBLIC(acc_hchar_p, acc_hstrrsep) (acc_hchar_pp ss, const acc_hchar_p delim)
508
{
509
    acc_hchar_p s = *ss;
510
    if (s) {
511
        acc_hchar_p p = __ACCLIB_FUNCNAME(acc_hstrrpbrk)(s, delim);
512
        if (p) {
513
            *p++ = 0;
514
            return p;
515
        }
516
        *ss = (acc_hchar_p) 0;
517
    }
518
    return s;
519
}
520
 
521
 
522
/***********************************************************************
523
// ascii_strlwr, ascii_strupr, ascii_memlwr, ascii_memupr
524
************************************************************************/
525
 
526
ACCLIB_PUBLIC(acc_hchar_p, acc_ascii_hstrlwr) (acc_hchar_p s)
527
{
528
    acc_hbyte_p p = (acc_hbyte_p) s;
529
    for ( ; *p; ++p)
530
        *p = (unsigned char) acc_ascii_utolower(*p);
531
    return s;
532
}
533
 
534
 
535
ACCLIB_PUBLIC(acc_hchar_p, acc_ascii_hstrupr) (acc_hchar_p s)
536
{
537
    acc_hbyte_p p = (acc_hbyte_p) s;
538
    for ( ; *p; ++p)
539
        *p = (unsigned char) acc_ascii_utoupper(*p);
540
    return s;
541
}
542
 
543
 
544
ACCLIB_PUBLIC(acc_hvoid_p, acc_ascii_hmemlwr) (acc_hvoid_p s, acc_hsize_t n)
545
{
546
    acc_hbyte_p p = (acc_hbyte_p) s;
547
    acc_hbyte_p e = (acc_hbyte_p) s + n;
548
    for ( ; p != e; ++p)
549
        *p = (unsigned char) acc_ascii_utolower(*p);
550
    return s;
551
}
552
 
553
 
554
ACCLIB_PUBLIC(acc_hvoid_p, acc_ascii_hmemupr) (acc_hvoid_p s, acc_hsize_t n)
555
{
556
    acc_hbyte_p p = (acc_hbyte_p) s;
557
    acc_hbyte_p e = (acc_hbyte_p) s + n;
558
    for ( ; p != e; ++p)
559
        *p = (unsigned char) acc_ascii_utoupper(*p);
560
    return s;
561
}
562
 
563
 
564
/*
565
vi:ts=4:et
566
*/