File: str.c

package info (click to toggle)
libast 0.7-9
  • links: PTS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 2,732 kB
  • sloc: ansic: 13,136; sh: 10,332; makefile: 78
file content (797 lines) | stat: -rw-r--r-- 22,650 bytes parent folder | download | duplicates (6)
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
/*
 * Copyright (C) 1997-2004, Michael Jennings
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to
 * deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies of the Software, its documentation and marketing & publicity
 * materials, and acknowledgment shall be given in the documentation, materials
 * and software packages that this Software was used.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

static const char __attribute__((unused)) cvs_ident[] = "$Id: str.c,v 1.32 2004/12/15 00:00:22 mej Exp $";

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include <libast_internal.h>

/* *INDENT-OFF* */
static SPIF_CONST_TYPE(strclass) s_class = {
    {
        SPIF_DECL_CLASSNAME(str),
        (spif_func_t) spif_str_new,
        (spif_func_t) spif_str_init,
        (spif_func_t) spif_str_done,
        (spif_func_t) spif_str_del,
        (spif_func_t) spif_str_show,
        (spif_func_t) spif_str_comp,
        (spif_func_t) spif_str_dup,
        (spif_func_t) spif_str_type
    },
    (spif_func_t) spif_str_new_from_ptr,
    (spif_func_t) spif_str_new_from_buff,
    (spif_func_t) spif_str_new_from_fp,
    (spif_func_t) spif_str_new_from_fd,
    (spif_func_t) spif_str_new_from_num,
    (spif_func_t) spif_str_init_from_ptr,
    (spif_func_t) spif_str_init_from_buff,
    (spif_func_t) spif_str_init_from_fp,
    (spif_func_t) spif_str_init_from_fd,
    (spif_func_t) spif_str_init_from_num,
    (spif_func_t) spif_str_append,
    (spif_func_t) spif_str_append_char,
    (spif_func_t) spif_str_append_from_ptr,
    (spif_func_t) spif_str_casecmp,
    (spif_func_t) spif_str_casecmp_with_ptr,
    (spif_func_t) spif_str_clear,
    (spif_func_t) spif_str_cmp,
    (spif_func_t) spif_str_cmp_with_ptr,
    (spif_func_t) spif_str_downcase,
    (spif_func_t) spif_str_find,
    (spif_func_t) spif_str_find_from_ptr,
    (spif_func_t) spif_str_index,
    (spif_func_t) spif_str_ncasecmp,
    (spif_func_t) spif_str_ncasecmp_with_ptr,
    (spif_func_t) spif_str_ncmp,
    (spif_func_t) spif_str_ncmp_with_ptr,
    (spif_func_t) spif_str_prepend,
    (spif_func_t) spif_str_prepend_char,
    (spif_func_t) spif_str_prepend_from_ptr,
    (spif_func_t) spif_str_reverse,
    (spif_func_t) spif_str_rindex,
    (spif_func_t) spif_str_splice,
    (spif_func_t) spif_str_splice_from_ptr,
    (spif_func_t) spif_str_substr,
    (spif_func_t) spif_str_substr_to_ptr,
    (spif_func_t) spif_str_to_float,
    (spif_func_t) spif_str_to_num,
    (spif_func_t) spif_str_trim,
    (spif_func_t) spif_str_upcase
};
SPIF_TYPE(class) SPIF_CLASS_VAR(str) = SPIF_CAST(class) &s_class;
SPIF_TYPE(strclass) SPIF_STRCLASS_VAR(str) = &s_class;
/* *INDENT-ON* */

const size_t buff_inc = 4096;

spif_str_t
spif_str_new(void)
{
    spif_str_t self;

    self = SPIF_ALLOC(str);
    if (!spif_str_init(self)) {
        SPIF_DEALLOC(self);
        self = SPIF_NULL_TYPE(str);
    }
    return self;
}

spif_str_t
spif_str_new_from_ptr(spif_charptr_t old)
{
    spif_str_t self;

    self = SPIF_ALLOC(str);
    if (!spif_str_init_from_ptr(self, old)) {
        SPIF_DEALLOC(self);
        self = SPIF_NULL_TYPE(str);
    }
    return self;
}

spif_str_t
spif_str_new_from_buff(spif_charptr_t buff, spif_stridx_t size)
{
    spif_str_t self;

    self = SPIF_ALLOC(str);
    if (!spif_str_init_from_buff(self, buff, size)) {
        SPIF_DEALLOC(self);
        self = SPIF_NULL_TYPE(str);
    }
    return self;
}

spif_str_t
spif_str_new_from_fp(FILE * fp)
{
    spif_str_t self;

    self = SPIF_ALLOC(str);
    if (!spif_str_init_from_fp(self, fp)) {
        SPIF_DEALLOC(self);
        self = SPIF_NULL_TYPE(str);
    }
    return self;
}

spif_str_t
spif_str_new_from_fd(int fd)
{
    spif_str_t self;

    self = SPIF_ALLOC(str);
    if (!spif_str_init_from_fd(self, fd)) {
        SPIF_DEALLOC(self);
        self = SPIF_NULL_TYPE(str);
    }
    return self;
}

spif_str_t
spif_str_new_from_num(long num)
{
    spif_str_t self;

    self = SPIF_ALLOC(str);
    if (!spif_str_init_from_num(self, num)) {
        SPIF_DEALLOC(self);
        self = SPIF_NULL_TYPE(str);
    }
    return self;
}

spif_bool_t
spif_str_init(spif_str_t self)
{
    ASSERT_RVAL(!SPIF_STR_ISNULL(self), FALSE);
    /* ***NOT NEEDED*** spif_obj_init(SPIF_OBJ(self)); */
    spif_obj_set_class(SPIF_OBJ(self), SPIF_CLASS(SPIF_STRCLASS_VAR(str)));
    self->s = SPIF_NULL_TYPE(charptr);
    self->len = 0;
    self->size = 0;
    return TRUE;
}

spif_bool_t
spif_str_init_from_ptr(spif_str_t self, spif_charptr_t old)
{
    ASSERT_RVAL(!SPIF_STR_ISNULL(self), FALSE);
    REQUIRE_RVAL((old != SPIF_NULL_TYPE(charptr)), spif_str_init(self));
    /* ***NOT NEEDED*** spif_obj_init(SPIF_OBJ(self)); */
    spif_obj_set_class(SPIF_OBJ(self), SPIF_CLASS(SPIF_STRCLASS_VAR(str)));
    self->len = strlen(SPIF_CONST_CAST_C(char *) old);
    self->size = self->len + 1;
    self->s = SPIF_CAST(charptr) MALLOC(self->size);
    memcpy(self->s, old, self->size);
    return TRUE;
}

spif_bool_t
spif_str_init_from_buff(spif_str_t self, spif_charptr_t buff, spif_stridx_t size)
{
    ASSERT_RVAL(!SPIF_STR_ISNULL(self), FALSE);
    /* ***NOT NEEDED*** spif_obj_init(SPIF_OBJ(self)); */
    spif_obj_set_class(SPIF_OBJ(self), SPIF_CLASS(SPIF_STRCLASS_VAR(str)));
    self->size = size;
    if (buff != SPIF_NULL_TYPE(charptr)) {
        self->len = strnlen(SPIF_CONST_CAST_C(char *) buff, size);
    } else {
        self->len = 0;
    }
    if (self->size == self->len) {
        self->size++;
    }
    self->s = SPIF_CAST(charptr) MALLOC(self->size);
    if (buff != SPIF_NULL_TYPE(charptr)) {
        memcpy(self->s, buff, self->len);
    }
    self->s[self->len] = 0;
    return TRUE;
}

spif_bool_t
spif_str_init_from_fp(spif_str_t self, FILE *fp)
{
    spif_charptr_t p, end = NULL;

    ASSERT_RVAL(!SPIF_STR_ISNULL(self), FALSE);
    ASSERT_RVAL((fp != SPIF_NULL_TYPE_C(FILE *)), FALSE);
    /* ***NOT NEEDED*** spif_obj_init(SPIF_OBJ(self)); */
    spif_obj_set_class(SPIF_OBJ(self), SPIF_CLASS(SPIF_STRCLASS_VAR(str)));
    self->size = buff_inc;
    self->len = 0;
    self->s = SPIF_CAST(charptr) MALLOC(self->size);

    for (p = self->s; fgets(SPIF_CAST_C(char *)p, buff_inc, fp); p += buff_inc) {
        if ((end = SPIF_CAST(charptr)
             strchr(SPIF_CONST_CAST_C(char *)p, '\n')) == NULL) {
            self->size += buff_inc;
            self->s = SPIF_CAST(charptr) REALLOC(self->s, self->size);
        } else {
            *end = 0;
            break;
        }
    }
    self->len = (spif_stridx_t) ((end)
                          ? (end - self->s)
                          : ((int) strlen(SPIF_CONST_CAST_C(char *)self->s)));
    self->size = self->len + 1;
    self->s = SPIF_CAST(charptr) REALLOC(self->s, self->size);
    return TRUE;
}

spif_bool_t
spif_str_init_from_fd(spif_str_t self, int fd)
{
    int n;
    spif_charptr_t p;

    ASSERT_RVAL(!SPIF_STR_ISNULL(self), FALSE);
    ASSERT_RVAL((fd >= 0), FALSE);
    /* ***NOT NEEDED*** spif_obj_init(SPIF_OBJ(self)); */
    spif_obj_set_class(SPIF_OBJ(self), SPIF_CLASS(SPIF_STRCLASS_VAR(str)));
    self->size = buff_inc;
    self->len = 0;
    self->s = SPIF_CAST(charptr) MALLOC(self->size);

    for (p = self->s; ((n = read(fd, p, buff_inc)) > 0) || (errno == EINTR);) {
        self->size += n;
        self->s = SPIF_CAST(charptr) REALLOC(self->s, self->size);
        p += n;
    }
    self->len = self->size - buff_inc;
    self->size = self->len + 1;
    self->s = SPIF_CAST(charptr) REALLOC(self->s, self->size);
    self->s[self->len] = 0;
    return TRUE;
}

spif_bool_t
spif_str_init_from_num(spif_str_t self, long num)
{
    spif_char_t buff[28];

    ASSERT_RVAL(!SPIF_STR_ISNULL(self), FALSE);
    /* ***NOT NEEDED*** spif_obj_init(SPIF_OBJ(self)); */
    spif_obj_set_class(SPIF_OBJ(self), SPIF_CLASS(SPIF_STRCLASS_VAR(str)));

    snprintf(SPIF_CHARPTR_C(buff), sizeof(buff), "%ld", num);
    self->len = strlen(SPIF_CHARPTR_C(buff));
    self->size = self->len + 1;
    self->s = SPIF_CAST(charptr) MALLOC(self->size);
    strcpy(SPIF_CHARPTR_C(self->s), SPIF_CHARPTR_C(buff));

    return TRUE;
}

spif_bool_t
spif_str_done(spif_str_t self)
{
    ASSERT_RVAL(!SPIF_STR_ISNULL(self), FALSE);
    if (self->size) {
        FREE(self->s);
        self->len = 0;
        self->size = 0;
        self->s = SPIF_NULL_TYPE(charptr);
    }
    return TRUE;
}

spif_bool_t
spif_str_del(spif_str_t self)
{
    ASSERT_RVAL(!SPIF_STR_ISNULL(self), FALSE);
    spif_str_done(self);
    SPIF_DEALLOC(self);
    return TRUE;
}

spif_str_t
spif_str_show(spif_str_t self, spif_charptr_t name, spif_str_t buff, size_t indent)
{
    spif_char_t tmp[4096];

    if (SPIF_STR_ISNULL(self)) {
        SPIF_OBJ_SHOW_NULL(str, name, buff, indent, tmp);
        return buff;
    }

    memset(tmp, ' ', indent);
    snprintf(SPIF_CHARPTR_C(tmp) + indent, sizeof(tmp) - indent,
             "(spif_str_t) %s:  %10p { \"",
             name, SPIF_CAST(ptr) self);
    if (SPIF_STR_ISNULL(buff)) {
        buff = spif_str_new_from_ptr(tmp);
    } else {
        spif_str_append_from_ptr(buff, tmp);
    }

    spif_str_append(buff, self);

    snprintf(SPIF_CHARPTR_C(tmp), sizeof(tmp), "\", len %lu, size %lu }\n", (unsigned long) self->len,
             (unsigned long) self->size);
    spif_str_append_from_ptr(buff, tmp);
    return buff;
}

spif_cmp_t
spif_str_comp(spif_str_t self, spif_str_t other)
{
    return spif_str_cmp(self, other);
}

spif_str_t
spif_str_dup(spif_str_t self)
{
    spif_str_t tmp;

    ASSERT_RVAL(!SPIF_STR_ISNULL(self), SPIF_NULL_TYPE(str));
    tmp = SPIF_ALLOC(str);
    memcpy(tmp, self, SPIF_SIZEOF_TYPE(str));
    tmp->s = SPIF_CAST(charptr) STRDUP(SPIF_CONST_CAST_C(char *) SPIF_STR_STR(self));
    tmp->len = self->len;
    tmp->size = self->size;
    return tmp;
}

spif_classname_t
spif_str_type(spif_str_t self)
{
    ASSERT_RVAL(!SPIF_STR_ISNULL(self), SPIF_CAST(classname) SPIF_NULLSTR_TYPE(classname));
    return SPIF_OBJ_CLASSNAME(self);
}

spif_bool_t
spif_str_append(spif_str_t self, spif_str_t other)
{
    ASSERT_RVAL(!SPIF_STR_ISNULL(self), FALSE);
    REQUIRE_RVAL(!SPIF_STR_ISNULL(other), FALSE);
    if (other->size && other->len) {
        self->size += other->size - 1;
        self->s = SPIF_CAST(charptr) REALLOC(self->s, self->size);
        memcpy(self->s + self->len, SPIF_STR_STR(other), other->len + 1);
        self->len += other->len;
    }
    return TRUE;
}

spif_bool_t
spif_str_append_char(spif_str_t self, spif_char_t c)
{
    ASSERT_RVAL(!SPIF_STR_ISNULL(self), FALSE);
    self->len++;
    if (self->size <= self->len) {
        self->size++;
        self->s = SPIF_CAST(charptr) REALLOC(self->s, self->size);
    }
    self->s[self->len - 1] = c;
    self->s[self->len] = 0;
    return TRUE;
}

spif_bool_t
spif_str_append_from_ptr(spif_str_t self, spif_charptr_t other)
{
    spif_stridx_t len;

    ASSERT_RVAL(!SPIF_STR_ISNULL(self), FALSE);
    REQUIRE_RVAL((other != SPIF_NULL_TYPE(charptr)), FALSE);
    len = strlen(SPIF_CONST_CAST_C(char *) other);
    if (len) {
        self->size += len;
        self->s = SPIF_CAST(charptr) REALLOC(self->s, self->size);
        memcpy(self->s + self->len, other, len + 1);
        self->len += len;
    }
    return TRUE;
}

spif_cmp_t
spif_str_casecmp(spif_str_t self, spif_str_t other)
{
    int c;

    SPIF_OBJ_COMP_CHECK_NULL(self, other);
    c = strcasecmp(SPIF_CHARPTR_C(SPIF_STR_STR(self)), SPIF_CHARPTR_C(SPIF_STR_STR(other)));
    return SPIF_CMP_FROM_INT(c);
}

spif_cmp_t
spif_str_casecmp_with_ptr(spif_str_t self, spif_charptr_t other)
{
    int c;

    SPIF_OBJ_COMP_CHECK_NULL(self, other);
    c = strcasecmp(SPIF_CHARPTR_C(SPIF_STR_STR(self)), SPIF_CHARPTR_C(other));
    return SPIF_CMP_FROM_INT(c);
}

spif_bool_t
spif_str_clear(spif_str_t self, spif_char_t c)
{
    ASSERT_RVAL(!SPIF_STR_ISNULL(self), FALSE);
    memset(self->s, c, self->size);
    self->s[self->len] = 0;
    return TRUE;
}

spif_cmp_t
spif_str_cmp(spif_str_t self, spif_str_t other)
{
    int c;

    SPIF_OBJ_COMP_CHECK_NULL(self, other);
    c = strcmp(SPIF_CHARPTR_C(SPIF_STR_STR(self)), SPIF_CHARPTR_C(SPIF_STR_STR(other)));
    return SPIF_CMP_FROM_INT(c);
}

spif_cmp_t
spif_str_cmp_with_ptr(spif_str_t self, spif_charptr_t other)
{
    int c;

    SPIF_OBJ_COMP_CHECK_NULL(self, other);
    c = strcmp(SPIF_CHARPTR_C(SPIF_STR_STR(self)), SPIF_CHARPTR_C(other));
    return SPIF_CMP_FROM_INT(c);
}

spif_bool_t
spif_str_downcase(spif_str_t self)
{
    spif_charptr_t tmp;

    ASSERT_RVAL(!SPIF_STR_ISNULL(self), FALSE);
    for (tmp = self->s; *tmp; tmp++) {
        *tmp = tolower(*tmp);
    }
    return TRUE;
}

spif_stridx_t
spif_str_find(spif_str_t self, spif_str_t other)
{
    char *tmp;

    ASSERT_RVAL(!SPIF_STR_ISNULL(self), (SPIF_CAST(stridx) -1));
    REQUIRE_RVAL(!SPIF_STR_ISNULL(other), (SPIF_CAST(stridx) -1));
    tmp = strstr(SPIF_CONST_CAST_C(char *) SPIF_STR_STR(self),
                 SPIF_CONST_CAST_C(char *) SPIF_STR_STR(other));
    if (tmp) {
        return SPIF_CAST(stridx) (SPIF_CAST(long) tmp - SPIF_CAST(long) (SPIF_STR_STR(self)));
    } else {
        return SPIF_CAST(stridx) (self->len);
    }
}

spif_stridx_t
spif_str_find_from_ptr(spif_str_t self, spif_charptr_t other)
{
    char *tmp;

    ASSERT_RVAL(!SPIF_STR_ISNULL(self), (SPIF_CAST(stridx) -1));
    REQUIRE_RVAL((other != SPIF_NULL_TYPE(charptr)), (SPIF_CAST(stridx) -1));
    tmp = strstr(SPIF_CONST_CAST_C(char *) SPIF_STR_STR(self),
                 SPIF_CONST_CAST_C(char *) other);
    if (tmp) {
        return SPIF_CAST(stridx) (SPIF_CAST(long) tmp - SPIF_CAST(long) (SPIF_STR_STR(self)));
    } else {
        return SPIF_CAST(stridx) (self->len);
    }
}

spif_stridx_t
spif_str_index(spif_str_t self, spif_char_t c)
{
    char *tmp;

    ASSERT_RVAL(!SPIF_STR_ISNULL(self), (SPIF_CAST(stridx) -1));
    tmp = index(SPIF_CONST_CAST_C(char *) SPIF_STR_STR(self), c);
    if (tmp) {
        return SPIF_CAST(stridx) (SPIF_CAST(long) tmp - SPIF_CAST(long) (SPIF_STR_STR(self)));
    } else {
        return SPIF_CAST(stridx) (self->len);
    }
}

spif_cmp_t
spif_str_ncasecmp(spif_str_t self, spif_str_t other, spif_stridx_t cnt)
{
    int c;

    SPIF_OBJ_COMP_CHECK_NULL(self, other);
    c = strncasecmp(SPIF_CHARPTR_C(SPIF_STR_STR(self)), SPIF_CHARPTR_C(SPIF_STR_STR(other)), cnt);
    return SPIF_CMP_FROM_INT(c);
}

spif_cmp_t
spif_str_ncasecmp_with_ptr(spif_str_t self, spif_charptr_t other, spif_stridx_t cnt)
{
    int c;

    SPIF_OBJ_COMP_CHECK_NULL(self, other);
    c = strncasecmp(SPIF_CHARPTR_C(SPIF_STR_STR(self)), SPIF_CHARPTR_C(other), cnt);
    return SPIF_CMP_FROM_INT(c);
}

spif_cmp_t
spif_str_ncmp(spif_str_t self, spif_str_t other, spif_stridx_t cnt)
{
    int c;

    SPIF_OBJ_COMP_CHECK_NULL(self, other);
    c = strncmp(SPIF_CHARPTR_C(SPIF_STR_STR(self)), SPIF_CHARPTR_C(SPIF_STR_STR(other)), cnt);
    return SPIF_CMP_FROM_INT(c);
}

spif_cmp_t
spif_str_ncmp_with_ptr(spif_str_t self, spif_charptr_t other, spif_stridx_t cnt)
{
    int c;

    SPIF_OBJ_COMP_CHECK_NULL(self, other);
    c = strncmp(SPIF_CHARPTR_C(SPIF_STR_STR(self)), SPIF_CHARPTR_C(other), cnt);
    return SPIF_CMP_FROM_INT(c);
}

spif_bool_t
spif_str_prepend(spif_str_t self, spif_str_t other)
{
    ASSERT_RVAL(!SPIF_STR_ISNULL(self), FALSE);
    REQUIRE_RVAL(!SPIF_STR_ISNULL(other), FALSE);
    if (other->size && other->len) {
        self->size += other->size - 1;
        self->s = SPIF_CAST(charptr) REALLOC(self->s, self->size);
        memmove(self->s + other->len, self->s, self->len + 1);
        memcpy(self->s, SPIF_STR_STR(other), other->len);
        self->len += other->len;
    }
    return TRUE;
}

spif_bool_t
spif_str_prepend_char(spif_str_t self, spif_char_t c)
{
    ASSERT_RVAL(!SPIF_STR_ISNULL(self), FALSE);
    self->len++;
    if (self->size <= self->len) {
        self->size++;
        self->s = SPIF_CAST(charptr) REALLOC(self->s, self->size);
    }
    memmove(self->s + 1, self->s, self->len + 1);
    self->s[0] = SPIF_CAST(uchar) c;
    return TRUE;
}

spif_bool_t
spif_str_prepend_from_ptr(spif_str_t self, spif_charptr_t other)
{
    spif_stridx_t len;

    ASSERT_RVAL(!SPIF_STR_ISNULL(self), FALSE);
    REQUIRE_RVAL((other != SPIF_NULL_TYPE(charptr)), FALSE);
    len = strlen(SPIF_CONST_CAST_C(char *) other);
    if (len) {
        self->size += len;
        self->s = SPIF_CAST(charptr) REALLOC(self->s, self->size);
        memmove(self->s + len, self->s, self->len + 1);
        memcpy(self->s, other, len);
        self->len += len;
    }
    return TRUE;
}

spif_bool_t
spif_str_reverse(spif_str_t self)
{
    ASSERT_RVAL(!SPIF_STR_ISNULL(self), FALSE);
    return ((strrev(SPIF_CAST_C(char *) self->s)) ? TRUE : FALSE);
}

spif_stridx_t
spif_str_rindex(spif_str_t self, spif_char_t c)
{
    char *tmp;

    ASSERT_RVAL(!SPIF_STR_ISNULL(self), (SPIF_CAST(stridx) -1));
    tmp = rindex(SPIF_CONST_CAST_C(char *) SPIF_STR_STR(self), c);
    if (tmp) {
        return SPIF_CAST(stridx) (SPIF_CAST(long) tmp - SPIF_CAST(long) (SPIF_STR_STR(self)));
    } else {
        return SPIF_CAST(stridx) (self->len);
    }
}

spif_bool_t
spif_str_splice(spif_str_t self, spif_stridx_t idx, spif_stridx_t cnt, spif_str_t other)
{
    spif_charptr_t tmp, ptmp;
    spif_stridx_t newsize;

    ASSERT_RVAL(!SPIF_STR_ISNULL(self), FALSE);
    if (idx < 0) {
        idx = self->len + idx;
    }
    REQUIRE_RVAL(idx >= 0, FALSE);
    REQUIRE_RVAL(idx < self->len, FALSE);
    if (cnt < 0) {
        cnt = idx + self->len + cnt;
    }
    REQUIRE_RVAL(cnt >= 0, FALSE);
    REQUIRE_RVAL(cnt <= (self->len - idx), FALSE);

    newsize = self->len + ((SPIF_STR_ISNULL(other)) ? (0) : (other->len)) - cnt + 1;
    ptmp = tmp = SPIF_CAST(charptr) MALLOC(newsize);
    if (idx > 0) {
        memcpy(tmp, self->s, idx);
        ptmp += idx;
    }
    if (!SPIF_OBJ_ISNULL(other)) {
        memcpy(ptmp, other->s, other->len);
        ptmp += other->len;
    }
    memcpy(ptmp, self->s + idx + cnt, self->len - idx - cnt + 1);
    if (self->size < newsize) {
        self->s = SPIF_CAST(charptr) REALLOC(self->s, newsize);
        self->size = newsize;
    }
    self->len = newsize - 1;
    memcpy(self->s, tmp, newsize);
    FREE(tmp);
    return TRUE;
}

spif_bool_t
spif_str_splice_from_ptr(spif_str_t self, spif_stridx_t idx, spif_stridx_t cnt, spif_charptr_t other)
{
    spif_charptr_t tmp, ptmp;
    spif_stridx_t len, newsize;

    ASSERT_RVAL(!SPIF_STR_ISNULL(self), FALSE);
    len = (other ? strlen(SPIF_CONST_CAST_C(char *) other) : 0);
    if (idx < 0) {
        idx = self->len + idx;
    }
    REQUIRE_RVAL(idx >= 0, FALSE);
    REQUIRE_RVAL(idx < self->len, FALSE);
    if (cnt < 0) {
        cnt = idx + self->len + cnt;
    }
    REQUIRE_RVAL(cnt >= 0, FALSE);
    REQUIRE_RVAL(cnt <= (self->len - idx), FALSE);

    newsize = self->len + len - cnt + 1;
    ptmp = tmp = SPIF_CAST(charptr) MALLOC(newsize);
    if (idx > 0) {
        memcpy(tmp, self->s, idx);
        ptmp += idx;
    }
    if (len) {
        memcpy(ptmp, other, len);
        ptmp += len;
    }
    memcpy(ptmp, self->s + idx + cnt, self->len - idx - cnt + 1);
    if (self->size < newsize) {
        self->s = SPIF_CAST(charptr) REALLOC(self->s, newsize);
        self->size = newsize;
    }
    self->len = newsize - 1;
    memcpy(self->s, tmp, newsize);
    FREE(tmp);
    return TRUE;
}

spif_str_t
spif_str_substr(spif_str_t self, spif_stridx_t idx, spif_stridx_t cnt)
{
    ASSERT_RVAL(!SPIF_STR_ISNULL(self), SPIF_NULL_TYPE(str));
    if (idx < 0) {
        idx = self->len + idx;
    }
    REQUIRE_RVAL(idx >= 0, SPIF_NULL_TYPE(str));
    REQUIRE_RVAL(idx < self->len, SPIF_NULL_TYPE(str));
    if (cnt <= 0) {
        cnt = self->len - idx + cnt;
    }
    REQUIRE_RVAL(cnt >= 0, SPIF_NULL_TYPE(str));
    UPPER_BOUND(cnt, self->len - idx);
    return spif_str_new_from_buff(SPIF_STR_STR(self) + idx, cnt);
}

spif_charptr_t
spif_str_substr_to_ptr(spif_str_t self, spif_stridx_t idx, spif_stridx_t cnt)
{
    spif_charptr_t newstr;

    ASSERT_RVAL(!SPIF_STR_ISNULL(self), SPIF_NULL_TYPE(charptr));
    if (idx < 0) {
        idx = self->len + idx;
    }
    REQUIRE_RVAL(idx >= 0, SPIF_NULL_TYPE(charptr));
    REQUIRE_RVAL(idx < self->len, SPIF_NULL_TYPE(charptr));
    if (cnt <= 0) {
        cnt = self->len - idx + cnt;
    }
    REQUIRE_RVAL(cnt >= 0, SPIF_NULL_TYPE(charptr));
    UPPER_BOUND(cnt, self->len - idx);

    newstr = SPIF_CAST(charptr) MALLOC(cnt + 1);
    memcpy(newstr, SPIF_STR_STR(self) + idx, cnt);
    newstr[cnt] = 0;
    return newstr;
}

double
spif_str_to_float(spif_str_t self)
{
    ASSERT_RVAL(!SPIF_STR_ISNULL(self), SPIF_CAST_C(double) NAN);
    return (double) (strtod(SPIF_CONST_CAST_C(char *)SPIF_STR_STR(self), (char **) NULL));
}

size_t
spif_str_to_num(spif_str_t self, int base)
{
    ASSERT_RVAL(!SPIF_STR_ISNULL(self), (SPIF_CAST_C(size_t) -1));
    return (size_t) (strtoul(SPIF_CONST_CAST_C(char *) SPIF_STR_STR(self), (char **) NULL, base));
}

spif_bool_t
spif_str_trim(spif_str_t self)
{
    spif_charptr_t start, end;

    ASSERT_RVAL(!SPIF_STR_ISNULL(self), FALSE);
    start = self->s;
    end = self->s + self->len - 1;
    for (; isspace((spif_uchar_t) (*start)) && (start < end); start++);
    for (; isspace((spif_uchar_t) (*end)) && (start < end); end--);
    if (start > end) {
        return spif_str_done(self);
    }
    *(++end) = 0;
    self->len = (spif_stridx_t) (end - start);
    self->size = self->len + 1;
    memmove(self->s, start, self->size);
    self->s = SPIF_CAST(charptr) REALLOC(self->s, self->size);
    return TRUE;
}

spif_bool_t
spif_str_upcase(spif_str_t self)
{
    spif_charptr_t tmp;

    ASSERT_RVAL(!SPIF_STR_ISNULL(self), FALSE);
    for (tmp = self->s; *tmp; tmp++) {
        *tmp = toupper(*tmp);
    }
    return TRUE;
}

SPIF_DEFINE_PROPERTY_FUNC_C(str, spif_stridx_t, size)
SPIF_DEFINE_PROPERTY_FUNC_C(str, spif_stridx_t, len)