-
Notifications
You must be signed in to change notification settings - Fork 0
/
str.c
9715 lines (7130 loc) · 258 KB
/
str.c
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
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* libslack - https://libslack.org
*
* Copyright (C) 1999-2004, 2010, 2020-2023 raf <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <https://www.gnu.org/licenses/>.
*
* 20230824 raf <[email protected]>
*/
/*
* $OpenBSD: strlcpy.c,v 1.4 1999/05/01 18:56:41 millert Exp $
* $OpenBSD: strlcat.c,v 1.5 2001/01/13 16:17:24 millert Exp $
* Modified by raf <[email protected]>
*
* Copyright (c) 1998 Todd C. Miller <[email protected]>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
=head1 NAME
I<libslack(str)> - string module
=head1 SYNOPSIS
#include <slack/std.h>
#include <slack/str.h>
typedef struct String String;
typedef struct StringTR StringTR;
enum StringAlignment
{
ALIGN_LEFT = '<',
ALIGN_RIGHT = '>',
ALIGN_CENTRE = '|',
ALIGN_CENTER = '|',
ALIGN_FULL = '='
};
enum StringTROption
{
TR_COMPLEMENT = 1,
TR_DELETE = 2,
TR_SQUASH = 4
};
typedef enum StringAlignment StringAlignment;
typedef enum StringTROption StringTROption;
String *str_create(const char *format, ...);
String *str_create_with_locker(Locker *locker, const char *format, ...);
String *str_vcreate(const char *format, va_list args);
String *str_vcreate_with_locker(Locker *locker, const char *format, va_list args);
String *str_create_sized(size_t size, const char *format, ...);
String *str_create_with_locker_sized(Locker *locker, size_t size, const char *format, ...);
String *str_vcreate_sized(size_t size, const char *format, va_list args);
String *str_vcreate_with_locker_sized(Locker *locker, size_t size, const char *format, va_list args);
String *str_copy(const String *str);
String *str_copy_unlocked(const String *str);
String *str_copy_with_locker(Locker *locker, const String *str);
String *str_copy_with_locker_unlocked(Locker *locker, const String *str);
String *str_fgetline(FILE *stream);
String *str_fgetline_with_locker(Locker *locker, FILE *stream);
void str_release(String *str);
void *str_destroy(String **str);
int str_rdlock(const String *str);
int str_wrlock(const String *str);
int str_unlock(const String *str);
int str_empty(const String *str);
int str_empty_unlocked(const String *str);
ssize_t str_length(const String *str);
ssize_t str_length_unlocked(const String *str);
char *cstr(const String *str);
ssize_t str_set_length(String *str, size_t length);
ssize_t str_set_length_unlocked(String *str, size_t length);
ssize_t str_recalc_length(String *str);
ssize_t str_recalc_length_unlocked(String *str);
String *str_clear(String *str);
String *str_clear_unlocked(String *str);
String *str_remove(String *str, ssize_t index);
String *str_remove_unlocked(String *str, ssize_t index);
String *str_remove_range(String *str, ssize_t index, ssize_t range);
String *str_remove_range_unlocked(String *str, ssize_t index, ssize_t range);
String *str_insert(String *str, ssize_t index, const char *format, ...);
String *str_insert_unlocked(String *str, ssize_t index, const char *format, ...);
String *str_vinsert(String *str, ssize_t index, const char *format, va_list args);
String *str_vinsert_unlocked(String *str, ssize_t index, const char *format, va_list args);
String *str_insert_str(String *str, ssize_t index, const String *src);
String *str_insert_str_unlocked(String *str, ssize_t index, const String *src);
String *str_append(String *str, const char *format, ...);
String *str_append_unlocked(String *str, const char *format, ...);
String *str_vappend(String *str, const char *format, va_list args);
String *str_vappend_unlocked(String *str, const char *format, va_list args);
String *str_append_str(String *str, const String *src);
String *str_append_str_unlocked(String *str, const String *src);
String *str_prepend(String *str, const char *format, ...);
String *str_prepend_unlocked(String *str, const char *format, ...);
String *str_vprepend(String *str, const char *format, va_list args);
String *str_vprepend_unlocked(String *str, const char *format, va_list args);
String *str_prepend_str(String *str, const String *src);
String *str_prepend_str_unlocked(String *str, const String *src);
String *str_replace(String *str, ssize_t index, ssize_t range, const char *format, ...);
String *str_replace_unlocked(String *str, ssize_t index, ssize_t range, const char *format, ...);
String *str_vreplace(String *str, ssize_t index, ssize_t range, const char *format, va_list args);
String *str_vreplace_unlocked(String *str, ssize_t index, ssize_t range, const char *format, va_list args);
String *str_replace_str(String *str, ssize_t index, ssize_t range, const String *src);
String *str_replace_str_unlocked(String *str, ssize_t index, ssize_t range, const String *src);
String *str_substr(const String *str, ssize_t index, ssize_t range);
String *str_substr_unlocked(const String *str, ssize_t index, ssize_t range);
String *str_substr_with_locker(Locker *locker, const String *str, ssize_t index, ssize_t range);
String *str_substr_with_locker_unlocked(Locker *locker, const String *str, ssize_t index, ssize_t range);
String *substr(const char *str, ssize_t index, ssize_t range);
String *substr_with_locker(Locker *locker, const char *str, ssize_t index, ssize_t range);
String *str_splice(String *str, ssize_t index, ssize_t range);
String *str_splice_unlocked(String *str, ssize_t index, ssize_t range);
String *str_splice_with_locker(Locker *locker, String *str, ssize_t index, ssize_t range);
String *str_splice_with_locker_unlocked(Locker *locker, String *str, ssize_t index, ssize_t range);
String *str_repeat(size_t count, const char *format, ...);
String *str_repeat_with_locker(Locker *locker, size_t count, const char *format, ...);
String *str_vrepeat(size_t count, const char *format, va_list args);
String *str_vrepeat_with_locker(Locker *locker, size_t count, const char *format, va_list args);
int str_tr(String *str, const char *from, const char *to, int option);
int str_tr_unlocked(String *str, const char *from, const char *to, int option);
int str_tr_str(String *str, const String *from, const String *to, int option);
int str_tr_str_unlocked(String *str, const String *from, const String *to, int option);
int tr(char *str, const char *from, const char *to, int option);
StringTR *tr_compile(const char *from, const char *to, int option);
StringTR *tr_compile_with_locker(Locker *locker, const char *from, const char *to, int option);
StringTR *str_tr_compile(const String *from, const String *to, int option);
StringTR *str_tr_compile_unlocked(const String *from, const String *to, int option);
StringTR *str_tr_compile_with_locker(Locker *locker, const String *from, const String *to, int option);
StringTR *str_tr_compile_with_locker_unlocked(Locker *locker, const String *from, const String *to, int option);
void tr_release(StringTR *table);
void *tr_destroy(StringTR **table);
int str_tr_compiled(String *str, StringTR *table);
int str_tr_compiled_unlocked(String *str, StringTR *table);
int tr_compiled(char *str, StringTR *table);
List *str_regexpr(const char *pattern, const String *text, int cflags, int eflags);
List *str_regexpr_unlocked(const char *pattern, const String *text, int cflags, int eflags);
List *str_regexpr_with_locker(Locker *locker, const char *pattern, const String *text, int cflags, int eflags);
List *str_regexpr_with_locker_unlocked(Locker *locker, const char *pattern, const String *text, int cflags, int eflags);
List *regexpr(const char *pattern, const char *text, int cflags, int eflags);
List *regexpr_with_locker(Locker *locker, const char *pattern, const char *text, int cflags, int eflags);
int regexpr_compile(regex_t *compiled, const char *pattern, int cflags);
void regexpr_release(regex_t *compiled);
List *str_regexpr_compiled(const regex_t *compiled, const String *text, int eflags);
List *str_regexpr_compiled_unlocked(const regex_t *compiled, const String *text, int eflags);
List *str_regexpr_compiled_with_locker(Locker *locker, const regex_t *compiled, const String *text, int eflags);
List *str_regexpr_compiled_with_locker_unlocked(Locker *locker, const regex_t *compiled, const String *text, int eflags);
List *regexpr_compiled(const regex_t *compiled, const char *text, int eflags);
List *regexpr_compiled_with_locker(Locker *locker, const regex_t *compiled, const char *text, int eflags);
String *str_regsub(const char *pattern, const char *replacement, String *text, int cflags, int eflags, int all);
String *str_regsub_unlocked(const char *pattern, const char *replacement, String *text, int cflags, int eflags, int all);
String *str_regsub_compiled(const regex_t *compiled, const char *replacement, String *text, int eflags, int all);
String *str_regsub_compiled_unlocked(const regex_t *compiled, const char *replacement, String *text, int eflags, int all);
List *str_fmt(const String *str, size_t line_width, StringAlignment alignment);
List *str_fmt_unlocked(const String *str, size_t line_width, StringAlignment alignment);
List *str_fmt_with_locker(Locker *locker, const String *str, size_t line_width, StringAlignment alignment);
List *str_fmt_with_locker_unlocked(Locker *locker, const String *str, size_t line_width, StringAlignment alignment);
List *fmt(const char *str, size_t line_width, StringAlignment alignment);
List *fmt_with_locker(Locker *locker, const char *str, size_t line_width, StringAlignment alignment);
List *str_split(const String *str, const char *delim);
List *str_split_unlocked(const String *str, const char *delim);
List *str_split_with_locker(Locker *locker, const String *str, const char *delim);
List *str_split_with_locker_unlocked(Locker *locker, const String *str, const char *delim);
List *split(const char *str, const char *delim);
List *split_with_locker(Locker *locker, const char *str, const char *delim);
List *str_regexpr_split(const String *str, const char *delim, int cflags, int eflags);
List *str_regexpr_split_unlocked(const String *str, const char *delim, int cflags, int eflags);
List *str_regexpr_split_with_locker(Locker *locker, const String *str, const char *delim, int cflags, int eflags);
List *str_regexpr_split_with_locker_unlocked(Locker *locker, const String *str, const char *delim, int cflags, int eflags);
List *regexpr_split(const char *str, const char *delim, int cflags, int eflags);
List *regexpr_split_with_locker(Locker *locker, const char *str, const char *delim, int cflags, int eflags);
String *str_join(const List *list, const char *delim);
String *str_join_unlocked(const List *list, const char *delim);
String *str_join_with_locker(Locker *locker, const List *list, const char *delim);
String *str_join_with_locker_unlocked(Locker *locker, const List *list, const char *delim);
String *join(const List *list, const char *delim);
String *join_with_locker(Locker *locker, const List *list, const char *delim);
int str_soundex(const String *str);
int str_soundex_unlocked(const String *str);
int soundex(const char *str);
String *str_trim(String *str);
String *str_trim_unlocked(String *str);
char *trim(char *str);
String *str_trim_left(String *str);
String *str_trim_left_unlocked(String *str);
char *trim_left(char *str);
String *str_trim_right(String *str);
String *str_trim_right_unlocked(String *str);
char *trim_right(char *str);
String *str_squeeze(String *str);
String *str_squeeze_unlocked(String *str);
char *squeeze(char *str);
String *str_quote(const String *str, const char *quotable, char quote_char);
String *str_quote_unlocked(const String *str, const char *quotable, char quote_char);
String *str_quote_with_locker(Locker *locker, const String *str, const char *quotable, char quote_char);
String *str_quote_with_locker_unlocked(Locker *locker, const String *str, const char *quotable, char quote_char);
String *quote(const char *str, const char *quotable, char quote_char);
String *quote_with_locker(Locker *locker, const char *str, const char *quotable, char quote_char);
String *str_unquote(const String *str, const char *quotable, char quote_char);
String *str_unquote_unlocked(const String *str, const char *quotable, char quote_char);
String *str_unquote_with_locker(Locker *locker, const String *str, const char *quotable, char quote_char);
String *str_unquote_with_locker_unlocked(Locker *locker, const String *str, const char *quotable, char quote_char);
String *unquote(const char *str, const char *quotable, char quote_char);
String *unquote_with_locker(Locker *locker, const char *str, const char *quotable, char quote_char);
String *str_encode(const String *str, const char *uncoded, const char *coded, char quote_char, int printable);
String *str_encode_unlocked(const String *str, const char *uncoded, const char *coded, char quote_char, int printable);
String *str_encode_with_locker(Locker *locker, const String *str, const char *uncoded, const char *coded, char quote_char, int printable);
String *str_encode_with_locker_unlocked(Locker *locker, const String *str, const char *uncoded, const char *coded, char quote_char, int printable);
String *str_decode(const String *str, const char *uncoded, const char *coded, char quote_char, int printable);
String *str_decode_unlocked(const String *str, const char *uncoded, const char *coded, char quote_char, int printable);
String *str_decode_with_locker(Locker *locker, const String *str, const char *uncoded, const char *coded, char quote_char, int printable);
String *str_decode_with_locker_unlocked(Locker *locker, const String *str, const char *uncoded, const char *coded, char quote_char, int printable);
String *encode(const char *str, const char *uncoded, const char *coded, char quote_char, int printable);
String *encode_with_locker(Locker *locker, const char *str, const char *uncoded, const char *coded, char quote_char, int printable);
String *decode(const char *str, const char *uncoded, const char *coded, char quote_char, int printable);
String *decode_with_locker(Locker *locker, const char *str, const char *uncoded, const char *coded, char quote_char, int printable);
String *str_lc(String *str);
String *str_lc_unlocked(String *str);
char *lc(char *str);
String *str_lcfirst(String *str);
String *str_lcfirst_unlocked(String *str);
char *lcfirst(char *str);
String *str_uc(String *str);
String *str_uc_unlocked(String *str);
char *uc(char *str);
String *str_ucfirst(String *str);
String *str_ucfirst_unlocked(String *str);
char *ucfirst(char *str);
int str_chop(String *str);
int str_chop_unlocked(String *str);
int chop(char *str);
int str_chomp(String *str);
int str_chomp_unlocked(String *str);
int chomp(char *str);
int str_bin(const String *str);
int str_bin_unlocked(const String *str);
int bin(const char *str);
int str_hex(const String *str);
int str_hex_unlocked(const String *str);
int hex(const char *str);
int str_oct(const String *str);
int str_oct_unlocked(const String *str);
int oct(const char *str);
int strcasecmp(const char *s1, const char *s2);
int strncasecmp(const char *s1, const char *s2, size_t n);
size_t strlcpy(char *dst, const char *src, size_t size);
size_t strlcat(char *dst, const char *src, size_t size);
char *cstrcpy(char *dst, const char *src);
char *cstrcat(char *dst, const char *src);
char *cstrchr(const char *str, int c);
char *cstrpbrk(const char *str, const char *brk);
char *cstrrchr(const char *str, int c);
char *cstrstr(const char *str, const char *srch);
int asprintf(char **str, const char *format, ...);
int vasprintf(char **str, const char *format, va_list args);
=head1 DESCRIPTION
This module provides text strings that grow and shrink automatically, and
functions for manipulating them. Some of the functions were modelled on the
I<list(3)> module. Others were modelled on the string functions and
operators in I<perlfunc(1)> and I<perlop(1)>. Others came from I<OpenBSD>.
=over 4
=cut
*/
#ifndef _BSD_SOURCE
#define _BSD_SOURCE /* For snprintf() on OpenBSD-4.7 */
#endif
#ifndef _DEFAULT_SOURCE
#define _DEFAULT_SOURCE /* New name for _BSD_SOURCE */
#endif
#ifndef __BSD_VISIBLE
#define __BSD_VISIBLE 1 /* For ntohl() on FreeBSD-8.0 */
#endif
#ifndef _NETBSD_SOURCE
#define _NETBSD_SOURCE /* For ntohl() on NetBSD-5.0.2 */
#endif
#include "config.h"
#include "std.h"
#include <netinet/in.h>
#include "err.h"
#include "str.h"
#include "mem.h"
#include "fio.h"
#ifndef HAVE_SNPRINTF
#include "snprintf.h"
#endif
struct String
{
size_t size; /* number of bytes allocated */
size_t length; /* number of bytes used (including nul) */
char *str; /* vector of characters */
Locker *locker; /* locking strategy for this string */
};
#define CHARSET 256
struct StringTR
{
int squash; /* whether or not to squash duplicate characters */
short table[CHARSET]; /* the translation table */
Locker *locker; /* locking strategy for this structure */
};
typedef enum
{
TRCODE_NOMAP = -1,
TRCODE_DELETE = -2
}
TRCode;
#define is_alpha(c) isalpha((int)(unsigned char)(c))
#define is_alnum(c) isalnum((int)(unsigned char)(c))
#define is_print(c) isprint((int)(unsigned char)(c))
#define is_space(c) isspace((int)(unsigned char)(c))
#define is_digit(c) isdigit((int)(unsigned char)(c))
#define is_xdigit(c) isxdigit((int)(unsigned char)(c))
#define to_lower(c) tolower((int)(unsigned char)(c))
#define to_upper(c) toupper((int)(unsigned char)(c))
#ifndef TEST
/* Minimum string length: must be a power of 2 */
static const size_t MIN_STRING_SIZE = 32;
/* Maximum bytes for an empty string: must be a power of 2 greater than MIN_STRING_SIZE */
static const size_t MIN_EMPTY_STRING_SIZE = 1024;
void (flockfile)(FILE *stream); /* Missing from old glibc headers */
void (funlockfile)(FILE *stream);
#ifndef HAVE_FLOCKFILE
#define flockfile(stream)
#define funlockfile(stream)
#define getc_unlocked(stream) getc(stream)
#endif
/*
C<int grow(String *str, size_t bytes)>
Allocates enough memory to add C<bytes> extra bytes to C<str> if necessary.
On success, returns C<0>. On error, returns C<-1>.
*/
static int grow(String *str, size_t bytes)
{
int grown = 0;
while (str->length + bytes > str->size)
{
if (str->size)
str->size <<= 1;
else
str->size = MIN_STRING_SIZE;
grown = 1;
}
if (grown)
return mem_resize(&str->str, str->size) ? 0 : -1;
return 0;
}
/*
C<int shrink(String *str, size_t bytes)>
Allocates less memory for removing C<bytes> bytes from C<str> if necessary.
On success, returns C<0>. On error, returns C<-1>.
*/
static int shrink(String *str, size_t bytes)
{
int shrunk = 0;
while (str->length - bytes < str->size >> 1)
{
if (str->size <= MIN_EMPTY_STRING_SIZE)
break;
str->size >>= 1;
shrunk = 1;
}
if (shrunk)
return mem_resize(&str->str, str->size) ? 0 : -1;
return 0;
}
/*
C<int expand(String *str, ssize_t index, size_t range)>
Slides C<str>'s bytes, starting at C<index>, C<range> positions to the right
to make room for more. On success, returns C<0>. On error, returns C<-1>.
*/
static int expand(String *str, ssize_t index, size_t range)
{
if (grow(str, range) == -1)
return -1;
memmove(str->str + index + range, str->str + index, (str->length - index) * sizeof(*str->str));
str->length += range;
return 0;
}
/*
C<int contract(String *str, ssize_t index, size_t range)>
Slides C<str>'s bytes, starting at C<index> + C<range>, C<range> positions
to the left to close a gap starting at C<index>. On success, returns C<0>.
On error, returns C<-1>.
*/
static int contract(String *str, ssize_t index, size_t range)
{
memmove(str->str + index, str->str + index + range, (str->length - index - range) * sizeof(*str->str));
if (shrink(str, range) == -1)
return -1;
str->length -= range;
return 0;
}
/*
C<int adjust(String *str, ssize_t index, size_t range, size_t length)>
Expands or contracts C<str> as required so that C<str[index + range ..]>
occupies C<str[index + length ..]>. On success, returns C<0>. On error,
returns C<-1>.
*/
static int adjust(String *str, ssize_t index, size_t range, size_t length)
{
if (range < length)
return expand(str, index + range, length - range);
if (range > length)
return contract(str, index + length, range - length);
return 0;
}
/*
=item C<String *str_create(const char *format, ...)>
Creates a I<String> specified by C<format> and the following arguments as in
I<sprintf(3)>. On success, returns the new string. It is the caller's
responsibility to deallocate the new string with I<str_release(3)> or
I<str_destroy(3)>. It is strongly recommended to use I<str_destroy(3)>,
because it also sets the pointer variable to C<null>. On error, returns
C<null> with C<errno> set appropriately.
B<Warning: Do not under any circumstances ever pass a non-literal string as
the format argument unless you know exactly how many conversions will take
place. Being careless with this is a very good way to build potential
security vulnerabilities into your programs. The same is true for all
functions that take a printf()-like format string as an argument.>
String *str = str_create(buf); // EVIL
String *str = str_create("%s", buf); // GOOD
=cut
*/
String *str_create(const char *format, ...)
{
String *str;
va_list args;
va_start(args, format);
str = str_vcreate_with_locker_sized(NULL, MIN_STRING_SIZE, format, args);
va_end(args);
return str;
}
/*
=item C<String *str_create_with_locker(Locker *locker, const char *format, ...)>
Equivalent to I<str_create(3)> except that multiple threads accessing the
new string will be synchronized by C<locker>.
=cut
*/
String *str_create_with_locker(Locker *locker, const char *format, ...)
{
String *str;
va_list args;
va_start(args, format);
str = str_vcreate_with_locker_sized(locker, MIN_STRING_SIZE, format, args);
va_end(args);
return str;
}
/*
=item C<String *str_vcreate(const char *format, va_list args)>
Equivalent to I<str_create(3)> with the variable argument list specified
directly as for I<vprintf(3)>.
=cut
*/
String *str_vcreate(const char *format, va_list args)
{
return str_vcreate_with_locker_sized(NULL, MIN_STRING_SIZE, format, args);
}
/*
=item C<String *str_vcreate_with_locker(Locker *locker, const char *format, va_list args)>
Equivalent to I<str_vcreate(3)> except that multiple threads accessing the
new string will be synchronized by C<locker>.
=cut
*/
String *str_vcreate_with_locker(Locker *locker, const char *format, va_list args)
{
return str_vcreate_with_locker_sized(locker, MIN_STRING_SIZE, format, args);
}
/*
=item C<String *str_create_sized(size_t size, const char *format, ...)>
Creates a I<String> specified by C<format> and the following arguments as in
I<sprintf(3)>. The initial allocation for the string data is at least
C<size> bytes. On success, returns the new string. It is the caller's
responsibility to deallocate the new string with I<str_release(3)> or
I<str_destroy(3)>. It is strongly recommended to use I<str_destroy(3)>,
because it also sets the pointer variable to C<null>. On error, returns
C<null> with C<errno> set appropriately.
=cut
*/
String *str_create_sized(size_t size, const char *format, ...)
{
String *str;
va_list args;
va_start(args, format);
str = str_vcreate_with_locker_sized(NULL, size, format, args);
va_end(args);
return str;
}
/*
=item C<String *str_create_with_locker_sized(Locker *locker, size_t size, const char *format, ...)>
Equivalent to I<str_create_sized(3)> except that multiple threads accessing
the new string will be synchronised by C<locker>.
=cut
*/
String *str_create_with_locker_sized(Locker *locker, size_t size, const char *format, ...)
{
String *str;
va_list args;
va_start(args, format);
str = str_vcreate_with_locker_sized(locker, size, format, args);
va_end(args);
return str;
}
/*
=item C<String *str_vcreate_sized(size_t size, const char *format, va_list args)>
Equivalent to I<str_create_sized(3)> with the variable argument list
specified directly as for I<vprintf(3)>.
=cut
*/
String *str_vcreate_sized(size_t size, const char *format, va_list args)
{
return str_vcreate_with_locker_sized(NULL, size, format, args);
}
/*
=item C<String *str_vcreate_with_locker_sized(Locker *locker, size_t size, const char *format, va_list args)>
Equivalent to I<str_vcreate_sized(3)> except that multiple threads accessing
the new string will be synchronised by C<locker>.
=cut
*/
#ifndef va_copy
#define va_copy(dst, src) __va_copy((dst), (src))
#endif
String *str_vcreate_with_locker_sized(Locker *locker, size_t size, const char *format, va_list args)
{
String *str;
char *buf = NULL;
ssize_t length;
unsigned int bit;
va_list args_copy;
for (bit = 1; bit; bit <<= 1)
{
if (bit >= size)
{
size = bit;
break;
}
}
if (!bit)
return set_errnull(EINVAL);
if (!format)
format = "";
for (;; size <<= 1)
{
if (!mem_resize(&buf, size))
{
mem_release(buf);
return NULL;
}
#ifdef va_copy
va_copy(args_copy, args);
length = vsnprintf(buf, size, format, args_copy);
va_end(args_copy);
#else
length = vsnprintf(buf, size, format, args);
#endif
if (length != -1 && length < size)
break;
}
if (!(str = mem_new(String))) /* XXX decouple */
{
mem_release(buf);
return NULL;
}
str->size = size;
str->length = length + 1;
str->str = buf;
str->locker = locker;
return str;
}
/*
=item C<String *str_copy(const String *str)>
Creates a copy of C<str>. On success, returns the copy. It is the caller's
responsibility to deallocate the new string with I<str_release(3)> or
I<str_destroy(3)>. It is strongly recommended to use I<str_destroy(3)>,
because it also sets the pointer variable to C<null>. On error, returns
C<null> with C<errno> set appropriately.
=cut
*/
String *str_copy(const String *str)
{
return str_copy_with_locker(NULL, str);
}
/*
=item C<String *str_copy_unlocked(const String *str)>
Equivalent to I<str_copy(3)> except that C<str> is not read-locked.
=cut
*/
String *str_copy_unlocked(const String *str)
{
return str_copy_with_locker_unlocked(NULL, str);
}
/*
=item C<String *str_copy_with_locker(Locker *locker, const String *str)>
Equivalent to I<str_copy(3)> except that multiple threads accessing the new
string will be synchronised by C<locker>.
=cut
*/
String *str_copy_with_locker(Locker *locker, const String *str)
{
return str_substr_with_locker(locker, str, 0, -1);
}
/*
=item C<String *str_copy_with_locker_unlocked(Locker *locker, const String *str)>
Equivalent to I<str_copy_with_locker(3)> except that C<str> is not
read-locked.
=cut
*/
String *str_copy_with_locker_unlocked(Locker *locker, const String *str)
{
return str_substr_with_locker_unlocked(locker, str, 0, -1);
}
/*
=item C<String *str_fgetline(FILE *stream)>
Similar to I<fgets(3)> except that it recognises UNIX (C<"\n">), DOS/Windows
(C<"\r\n">) and old Macintosh (C<"\r">) line endings (even different line
endings in the same file), and it can read a line of any size into the
I<String> that it returns. Reading stops after the C<EOF>, or after the end
of the line is reached. Line endings are always stored as a single C<"\n">
character. A C<nul> is placed after the last character in the buffer. On
success, returns a new I<String>. It is the caller's responsibility to
deallocate the new string with I<str_release(3)> or I<str_destroy(3)>. It is
strongly recommended to use I<str_destroy(3)>, because it also sets the
pointer variable to C<null>. On error, or when the end of file occurs while
no characters have been read, returns C<null>. Calls to this function can be
mixed with calls to other input functions from the I<stdio> library on the
same input stream.
=cut
*/
String *str_fgetline(FILE *stream)
{
return str_fgetline_with_locker(NULL, stream);
}
/*
=item C<String *str_fgetline_with_locker(Locker *locker, FILE *stream)>
Equivalent to I<str_fgetline(3)> except that multiple threads accessing the
new string will be synchronised by C<locker>.
=cut
*/
String *str_fgetline_with_locker(Locker *locker, FILE *stream)
{
String *ret = NULL;
char buf[BUFSIZ];
flockfile(stream);
while (fgetline_unlocked(buf, BUFSIZ, stream))
{
if (!ret)
{
if (!(ret = str_create_with_locker(locker, "%s", buf)))
break;
}
else if (!str_append(ret, "%s", buf))
{
str_destroy(&ret);
break;
}
if (cstr(ret)[ret->length - 2] == '\n')
break;
}
funlockfile(stream);
return ret;
}
/*
=item C<void str_release(String *str)>
Releases (deallocates) C<str>.
=cut
*/
void str_release(String *str)
{
Locker *locker;
if (!str)
return;
if (str_wrlock(str))
return;
locker = str->locker;
mem_release(str->str);
mem_release(str);
locker_unlock(locker);
}
/*
=item C<void *str_destroy(String **str)>
Destroys (deallocates and sets to C<null>) C<*str>. Returns C<null>.
B<Note:> strings shared by multiple threads must not be destroyed until
after all threads have finished with it.
=cut
*/
void *str_destroy(String **str)
{
if (str && *str)
{
str_release(*str);
*str = NULL;
}
return NULL;
}
/*
=item C<int str_rdlock(const String *str)>
Claims a read lock on C<str> (if C<str> was created with a I<Locker>).
Clients must call this before calling I<cstr(3)> (for the purpose of reading
the raw string data) on a string that was created with a I<Locker>. It is
the client's responsibility to call I<str_unlock(3)> when finished with the
raw string data. It is also needed when multiple read-only I<str(3)> module
functions need to be called atomically. It is the caller's responsibility to
call I<str_unlock(3)> after the atomic operation. The only functions that
may be called on C<str> between calls to I<str_rdlock(3)> and
I<str_unlock(3)> are I<cstr(3)> and any read-only I<str(3)> module functions
whose name ends with C<_unlocked>. On success, returns C<0>. On error,
returns an error code.
=cut
*/
#define str_rdlock(str) ((str) ? locker_rdlock((str)->locker) : EINVAL)
#define str_wrlock(str) ((str) ? locker_wrlock((str)->locker) : EINVAL)
#define str_unlock(str) ((str) ? locker_unlock((str)->locker) : EINVAL)
int (str_rdlock)(const String *str)
{
return str_rdlock(str);
}
/*
=item C<int str_wrlock(const String *str)>
Claims a write lock on C<str> (if C<str> was created with a I<Locker>).
Clients need to call this before calling I<cstr(3)> (for the purpose of
modifying the raw string data) on a string that was created with a
I<Locker>. It is the client's responsibility to call I<str_unlock(3)> when
finished with the raw string data. It is also needed when multiple
read/write I<str(3)> module functions need to be called atomically. It is
the caller's responsibility to call I<str_unlock(3)> after the atomic
operation. The only functions that may be called on C<str> between calls to
I<str_wrlock(3)> and I<str_unlock(3)> are I<cstr(3)> and any I<str(3)>
module functions whose name ends with C<_unlocked>. On success, returns
C<0>. On error, returns an error code.
=cut
*/
int (str_wrlock)(const String *str)
{
return str_wrlock(str);
}
/*
=item C<int str_unlock(const String *str)>
Unlocks a read lock or a write lock on C<str> obtained with I<str_rdlock(3)>
or I<str_wrlock(3)> (if C<str> was created with a I<Locker>). On success,
returns C<0>. On error, returns an error code.
=cut
*/
int (str_unlock)(const String *str)
{
return str_unlock(str);
}
/*
=item C<int str_empty(const String *str)>
Returns whether or not C<str> is the empty string. On error, returns C<-1>
with C<errno> set appropriately.
=cut
*/
int str_empty(const String *str)
{
int empty;
int err;
if (!str)
return set_errno(EINVAL);