forked from Sakrac/x65
-
Notifications
You must be signed in to change notification settings - Fork 0
/
struse.h
5258 lines (4663 loc) · 152 KB
/
struse.h
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
/*
String User Classes
The MIT License (MIT)
Copyright (c) 2015 Carl-Henrik Skårstedt
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 or
substantial portions of the Software.
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 OR COPYRIGHT HOLDERS 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.
https://github.com/sakrac/struse
Add this #define to *one* C++ file before #include "struse.h" to create the implementation:
#define STRUSE_IMPLEMENTATION
// in other words, this sequence should be at the top of one file:
#include ...
#define STRUSE_IMPLEMENTATION
#include "struse.h"
*/
#ifndef __STRUSE_H__
#define __STRUSE_H__
#include <inttypes.h> // uint32_t etc.
#include <string.h> // memcpy, memmove
#include <stdio.h> // printf, vsnprintf
#include <stdarg.h> // va_list
//
// Naming Rules:
//
// find* returns a position of a string where a match if found or -1 if not
// skip*/clip* trims beginning or end of string based on condition
// get_* returns a property, single character or substring
// before*/after* returns a substring matching the condition
// is_* returns a bool for a character or whole string test
// len_* return the number of characters matching the condition from the start of the string
// *_last indicates that search is done from the end of the string
// *_rh indicates a rolling hash search is used
// *_esc indicates that the search string allows for escape codes (\x => x)
// same_str* is a full string compare and returns true or false based on condtions
// *_range indicates that the result is filtered by a given set of valid characters
//
typedef unsigned int strl_t;
// helper defines for sprintf/printf with strop
// example: printf("string is " STROP_FMT "\n", STROP_ARG(strref))
#define STRREF_FMT "%.*s"
#define STRREF_ARG(s) (int)(s).get_len(), (s).get()
// internal helper functions for strref
int _find_rh(const char *text, strl_t len, const char *comp, strl_t comp_len);
int _find_rh_case(const char *text, strl_t len, const char *comp, strl_t comp_len);
// strref holds a reference to a constant substring (const char*)
class strref {
protected:
const char *string;
strl_t length;
public:
strref() { clear(); }
strref(const char *str) { set(str); }
strref(char *str) { set((const char*)str); }
strref(const char *str, strl_t len) : string(str), length(len) {}
strref(const char *str, int len) : string(str), length(strl_t(len)) {}
strref(char *str, int len) : string((const char*)str), length(strl_t(len)) {}
strref(char *str, strl_t len) : string((const char*)str), length(len) {}
bool valid() const { return string && length; }
void clear() { string = nullptr; length = 0; }
void set(const char *str);
const char* get() const { return string; }
const uint8_t* get_u() const { return (const uint8_t*)string; }
strl_t get_len() const { return length; }
char get_first() const { return (string && length) ? *string : 0; }
char get_last() const { return (string && length) ? string[length-1] : 0; }
char pop_first() { if (length && string) { char c = *string++; --length; return c; } return 0; }
strl_t limit_pos(strl_t pos) { return pos<length ? pos : length; }
bool is_substr(const char *sub) const { return sub>=string && sub<=(string+length); }
strl_t substr_offs(strref substr) const {
if (is_substr(substr.get())) return strl_t(substr.get()-get()); return 0; }
strl_t substr_end_offs(strref substr) const {
if (is_substr(substr.get())) return strl_t(substr.get()-get()) + substr.get_len(); return 0; }
bool is_empty() const { return length==0; }
// get fnv1a hash for string
unsigned int fnv1a(unsigned int seed = 2166136261) const;
unsigned int fnv1a_lower(unsigned int seed = 2166136261) const;
unsigned int fnv1a_append(unsigned int base_fnv1a_hash) const { return fnv1a(base_fnv1a_hash); }
unsigned short fnv1a_16( unsigned int seed = 2166136261 ) const;
uint64_t fnv1a_64(uint64_t seed = 14695981039346656037ULL) const;
// whitespace ignore fnv1a (any sequence whitespace is replaced by one space)
unsigned int fnv1a_ws(unsigned int seed = 2166136261) const;
// convert string to basic integer
int64_t atoi() const;
uint64_t atoui() const;
// convert string to floating point
float atof() const;
double atod() const;
// number of characters of basic integer in string
int atoi_skip();
// convert hexadecimal string to signed integer
int ahextoi() const;
// convert hexadecimal string to unsigned integer
size_t ahextoui() const;
uint64_t ahextou64() const;
size_t ahextoui_skip();
size_t abinarytoui_skip();
// output string with newline (printf)
void writeln();
// single digit number
static char num_to_char(uint8_t num) { return num<10 ? (num+'0'):(num+'a'-10); }
// is character empty such as space, tab, linefeed etc.?
static bool is_ws(uint8_t c) { return c <= ' '; }
static bool is_ws(char c) { return (uint8_t)c <= ' '; }
// is character a number?
static bool is_number(uint8_t c) { return c>='0' && c<='9'; }
static bool is_number(char c) { return (uint8_t)c>='0' && (uint8_t)c<='9'; }
// is character a hexadecimal number?
static bool is_hex(uint8_t c) { return is_number(c) || (c>='A' && c<='F') || (c>='a' && c<='f'); }
static bool is_hex(char c) { return is_hex(uint8_t(c)); }
// is character alphabetic (A-Z or a-z)?
static bool is_alphabetic(uint8_t c) { return (c>='a' && c<='z') || (c>='A' && c<='Z'); }
// is character alphabetic or numeric?
static bool is_alphanumeric(uint8_t c) { return is_number(c) || is_alphabetic(c); }
// is character valid as part of a label? (num, _, A-Z, a-z)
static bool is_valid_label(uint8_t c) { return c=='_' || is_alphanumeric(c); }
// word separators are non-alphanumeric characters except apostrophe.
static bool is_sep_ws(uint8_t c) { return c!='\'' && !is_alphanumeric(c); }
static bool is_sep_ws(char c) { return is_sep_ws(uint8_t(c)); }
// is control character? (!-/, ?-@, [-^, {-~)
static bool is_control(uint8_t c) { return !is_ws(c) && !is_alphanumeric(c) && c!='_'; }
// choice of upper/lowercase conversions
static char tolower(char c);
static char toupper(char c);
static char tolower_win(char c);
static char toupper_win(char c);
static char tolower_amiga(char c);
static char toupper_amiga(char c);
static char tolower_macos(char c);
static char toupper_macos(char c);
static size_t tolower_unicode(int c);
static size_t toupper_unicode(int c);
// operators
// strref += int: move string forward (skip)
void operator+=(const strl_t skip) { if (skip<length) { string += skip; length -= skip; } else clear(); }
void operator+=(const int skip) { if (strl_t(skip)<length) { string += skip; length -= strl_t(skip); } else clear(); }
// strref + int: return a string that is moved forward (skip)
strref operator+(const strl_t skip) { if (skip<length) return strref(string+skip, length-skip); return strref(); }
strref operator+(const int skip) { if (skip>=0 && strl_t(skip)<length) return strref(string+skip, length-skip); return strref(); }
// (bool): check if string is valid (0 length strings are invalid)
operator bool() const { return string && length; }
// strref++: skip string forward by one character
strref& operator++() { if (valid()) { string++; length--; } return *this; }
// strref > strref / strref < strref: greater than and lesser than operators
bool operator>(const strref o) const;
bool operator<(const strref o) const;
// strref[int]: get character at position
char operator[](strl_t pos) const { return pos<length ? string[pos] : 0; }
char operator[](int pos) const { return (strl_t)pos<length ? string[pos] : 0; }
// remove from start or end
void skip(strl_t count) { if (count<length) { string += count; length -= count; } else clear(); }
void clip(strl_t count) { if (count<length) { length -= count; } else clear(); }
// check if there are any potential non-text characters in the string
bool valid_ascii7() const;
// fetch a utf-8 character from the beginning of this string
size_t get_utf8() const;
// fetch a utf-8 character from the beginnning of this string and move cursor forward to the next utf-8 character
size_t pop_utf8();
// return character at position
char get_at(strl_t pos) const { return string && pos<length ? string[pos] : 0; }
char get_at(int pos) const { return string && (strl_t)pos<length ? string[pos] : 0; }
// return uint8_tacter at position
uint8_t get_u_at(strl_t pos) const { return (uint8_t)get_at(pos); }
// is there a whitespace at position offs in the string?
bool whitespace_at(strl_t pos) const { return is_ws(get_u_at(pos)); }
// count occurrences of char c
int count_char(char c) const;
// count characters to end of line
strl_t len_eol() const;
// characters start of next line (may be end of string)
strl_t len_next_line() const;
// count characters that can be converted to a floating point number
strl_t len_float_number() const;
// count characters that can be converted to a hexadecimal number
strl_t len_hex() const;
// check if string is a valid floating point number
bool is_float_number() const { return valid() && len_float_number() == length; }
// check if matching first char and skip if match
bool grab_char( char c ) { if(length && string[0]==c) { length--; string++; return true; } return false; }
// wildcard search
strref find_wildcard(const strref wild, strl_t pos = 0, bool case_sensitive = true) const;
strref next_wildcard(const strref wild, strref prev, bool case_sensitive = true) const {
return find_wildcard(wild, is_substr(prev.get()) ? (strl_t(prev.get()-get())+1) : 0, case_sensitive); }
strref wildcard_after(const strref wild, strref prev, bool case_sensitive = true) const {
return find_wildcard(wild, is_substr(prev.get()) ? strl_t(prev.get()+prev.get_len()-get()) : 0, case_sensitive); }
// character filter by string, as in a wildcard [] operator
bool char_matches_ranges(uint8_t c) const;
bool char_matches_ranges(char c) const { return char_matches_ranges((uint8_t)c); }
// whole string compare (_case indicates case sensitive)
bool same_str(const strref str) const;
bool same_str_case(const strref str) const;
bool same_str(const char *str) const;
bool same_str_case(const char *str);
// whole string compare, treat character same1 and same2 as equal
bool same_str(const strref str, char same1, char same2) const;
bool same_str_case(const strref str, char same1, char same2) const;
// mid string compare
bool same_substr(const strref str, strl_t pos) const;
bool same_substr_esc(const strref str, strl_t pos) const;
bool same_substr_case(const strref str, strl_t pos) const;
bool same_substr_case_esc(const strref str, strl_t pos) const;
bool same_substr_case_esc(const strref str, int pos) const { return same_substr_case_esc(str, (strl_t)pos); }
bool same_substr_case_esc_range(const strref str, const strref range, strl_t pos) const;
// prefix compare
strl_t prefix_len(const strref str) const;
strl_t prefix_len_case(const strref str) const;
strl_t prefix_len(const char *str) const;
strl_t prefix_len_case(const char *str) const;
strl_t prefix_len(const strref str, char same1, char same2) const;
bool has_prefix(const strref str) const { return prefix_len(str) == str.get_len(); }
bool has_prefix(const char* str) const { return str[prefix_len(str)]==0; }
bool is_prefix_of(const strref str) const { return prefix_len(str)==get_len(); }
bool is_prefix_of(const strref str, char same1, char same2) const {
return prefix_len(str, same1, same2)==get_len(); }
bool is_prefix_word(const strref str) const { return prefix_len(str)==get_len() &&
(str.get_len()==length || !is_alphanumeric((uint8_t)str[length])); }
bool is_prefix_case_of(const strref str) const { return prefix_len_case(str)==get_len(); }
bool is_prefix_float_number() const { return len_float_number() > 0; }
bool grab_prefix( const char* str ) { strl_t p = prefix_len( str ); if( !str[ p ] ) { skip( p ); return true; } return false; }
// suffix compare
strl_t suffix_len(const strref str) const;
strl_t suffix_len_case(const strref str) const;
bool is_suffix_of(const strref str) const { return suffix_len(str)==get_len(); }
bool is_suffix_case_of(const strref str) { return suffix_len_case(str)==get_len(); }
bool has_suffix(const char *str) const { return strref(str).is_suffix_of(*this); }
// whole word compare (prefix match + next char is whitespace or end of string)
bool is_word(const strref str) const { return prefix_len(str)==str.get_len() && whitespace_at(str.get_len()); }
bool is_word_case(const strref str) const { return prefix_len_case(str)==str.get_len() && whitespace_at(str.get_len()); }
// string search
// find first index of char c
int find(char c) const;
// find first index of char c after pos
int find_at(char c, strl_t pos) const;
// find first index of char c after index pos (find_at(c, pos+1))
int find_after(char c, strl_t pos) const;
// find first index of char c after index pos or return length for full string
strl_t find_or_full(char c, strl_t pos) const;
strl_t find_or_full_esc(char c, strl_t pos) const;
// find last position of char c
int find_last(char c) const;
// find first position of either char c or char d
int find(char c, char d) const;
// find last position of either char c or char d
int find_last(char c, char d) const;
// find first <b> after last <a> in string
int find_after_last(char a, char b) const { return find_after(b, (strl_t)(find_last(a)+1)); }
int find_after_last(char a1, char a2, char b) const {
int w = find_last(a1, a2)+1; int l = strref(string+w, length-w).find(b); return l>=0?l+w:-1; }
// return position in this string of the first occurrence of the argument or negative if not found, not case sensitive
int find(const strref str) const;
int find(const strref str, strl_t pos) const; // find first instance after pos
int find_bookend(const strref str, const strref bookend) const;
// return position in this string of the first occurrence of the argument or negative if not found, not case sensitive
int find(const char *str, strl_t pos = 0) const;
strl_t find_or_full(const char *str) const { int f = find(str); return f < 0 ? length : strl_t(f); }
// return position in this string of the first occurrence of the argument or negative if not found, case sensitive
int find_case(const strref str, strl_t pos = 0) const;
// return position in this string of the first occurrence of the argument or negative if not found, case sensitive
int find_case(const char *str) const;
int find_case_esc(const strref str, strl_t pos) const;
int find_case_esc_range(const strref str, const strref range, strl_t pos) const;
int find_esc_range(const strref str, const strref range, strl_t pos) const;
// return position in this string of the last occurrence of the argument or negative if not found, not case sensitive
int find_last(const strref str) const;
int find_last_bookend(const strref str, const strref bookend) const;
// return position in this string of the last occurrence of the argument or negative if not found, not case sensitive
int find_last(const char *str) const;
// return position in this string of the last occurrence of the argument or negative if not found, case sensitive
int find_last_case(const strref str) const;
// find first instance after pos allowing escape codes in search string
int find_esc(const strref str, strl_t pos) const;
// find any char from str in this string at position
int find_any_char_of(const strref range, strl_t pos = 0) const;
// find any char from str or char range or char - with backslash prefix
int find_any_char_or_range(const strref range, strl_t pos = 0) const;
int find_any_not_in_range(const strref range, strl_t pos = 0) const;
// find any char from str or char range or char - with backslash prefix
int find_range_char_within_range(const strref range_find, const strref range_within, strl_t pos = 0) const;
// find but not within parenthesis
int find_skip_parens(char token) const;
// counts
int substr_count(const strref str) const; // count the occurrences of the argument in this string
int substr_count_bookend(const strref str, const strref bookend) const;
int substr_case_count(const strref str) const; // count the occurrences of the argument in this string
int substr_label_case_count(const strref str) const;
int count_repeat(char c, strl_t pos) const;
int count_repeat_reverse(char c, strl_t pos) const;
int count_lines() const;
int count_lines(strl_t pos) const { return strref(string, pos<length ? pos : length).count_lines(); }
int count_lines(strref substring) const { return is_substr(substring.get()) ?
count_lines(strl_t(substring.get()-get())) : -1; }
strl_t prev_line_pos( strl_t pos );
strl_t start_line_pos( strl_t pos );
strl_t end_line_pos( strl_t pos );
// rolling hash find
int find_rh(strref str) const { return _find_rh(get(), get_len(), str.get(), str.get_len()); }
int find_rh_case(strref str) const {
return _find_rh_case(get(), get_len(), str.get(), str.get_len()); }
int find_rh(strref str, strl_t pos) const {
strref sub = *this; sub += pos; return sub.find_rh(str); }
int find_rh_case(strref str, strl_t pos) const {
strref sub = *this; sub += pos; return sub.find_rh_case(str); }
int find_rh_after(strref str, strref prev) const {
strref sub = *this; if (is_substr(prev.get())) sub.skip(strl_t(prev.get()-get()));
return sub.find_rh(str); }
int find_rh_case_after(strref str, strref prev) const {
strref sub = *this; if (is_substr(prev.get())) sub.skip(strl_t(prev.get()-get()));
return sub.find_rh_case(str); }
// whitespace management
// number of white space characters from start of string
strl_t len_whitespace() const { if (!valid()) return 0;
strl_t o = 0; while (o<length && is_ws((uint8_t)string[o])) o++; return o; }
// number of white space characters from pos
strl_t len_whitespace(strl_t pos) const {
strl_t o = pos; while (o<length && is_ws((uint8_t)string[o])) o++; return o-pos; }
// number of separator or white space characters from pos
strl_t len_sep_ws(strl_t pos) const {
strl_t o = pos; while (o<length && is_sep_ws((uint8_t)string[o])) o++; return o-pos; }
strl_t len_sep_ws(int pos) const { return len_sep_ws((strl_t)pos); }
// number of non white space characters from start of string
strl_t len_grayspace() const { if (!valid()) return 0;
strl_t o = 0; while (o<length && !is_ws((uint8_t)string[o])) o++; return o; }
// number of non white space characters from pos
strl_t len_grayspace(strl_t pos) const {
strl_t o = pos; while (o<length && !is_ws((uint8_t)string[o])) o++; return o-pos; }
// number of non separating non white space characters from pos
strl_t len_non_sep_ws(strl_t pos) const {
strl_t o = pos; while (o<length && !is_sep_ws((uint8_t)string[o])) o++; return o-pos; }
strl_t len_non_sep_ws(int pos) const { return len_non_sep_ws((strl_t)pos); }
// move string forward to the first non-whitespace character
void skip_whitespace() { skip(len_whitespace()); }
// move string forward to the first whitespace character
void skip_to_whitespace() { skip(len_grayspace()); }
// if bom detected skip it
strref skip_bom();
// cut white space characters at end of string
void clip_trailing_whitespace() { if (valid()) {
const char *e = string+length; while (*--e<=0x20 && length) { length--; } } }
// remove white space from start and end
void trim_whitespace() { clip_trailing_whitespace(); skip_whitespace(); }
strref get_trimmed_ws() const { strref ret = *this;
ret.clip_trailing_whitespace(); ret.skip_whitespace(); return ret; }
// find next whitespace or entire string
strl_t find_whitespace_or_full() const {
strl_t ws = (strl_t)len_grayspace(); return (ws==length) ? 0 : ws; }
// find next whitespace or -1
int find_whitespace() const {
strl_t ws = len_grayspace(); return (ws==length) ? -1 : (int)ws; }
// string content
// count number of alphanumeric characters from p
strl_t len_alphanumeric(strl_t p = 0) const { if (valid()) {
const char *s = string+p; strl_t r = length-p; while (is_alphanumeric((uint8_t)*s) && r) { s++; r--; }
return length-r; } return 0; }
// length of word consisting of alphanumeric characters
strl_t len_word() const { return len_alphanumeric(0); }
// length of string with escape characters
strl_t len_esc() const;
// return number of characters that can be a label
strl_t len_label() const { if (valid()) { const uint8_t *s = get_u(); strl_t r = length;
while (is_valid_label(*s) && r) { s++; r--; } return length-r; } return 0; }
// return whether or not this string is a number
bool is_number() const { if (strl_t r = length) { if (const uint8_t *s = get_u()) {
while (r) { if (!is_number(*s)) return false; s++; r--; } return true; } } return false; }
// go to the next word
void next_word_ws() { skip_whitespace(); skip_to_whitespace(); skip_whitespace(); }
// go to the end of this word
void end_word() { strl_t a = len_word(); string += a; length -= a; }
// get a portion of a string
strref get_substr(strl_t pos, strl_t len) const { return pos<length ?
(strref(string+pos, (pos+len)<length?len : (length-pos))) : strref(); }
strref get_substr(int pos, int len) const { return get_substr((strl_t)pos, (strl_t)len); }
strref get_substr(int pos, strl_t len) const { return get_substr((strl_t)pos, len); }
strref get_skipped(strl_t len) const { if (len<length)
return strref(string+len, length-len); return strref(); }
// get this strref without leading whitespace
strref get_skip_ws() const { return get_skipped(len_whitespace()); }
strref get_clipped(strl_t len) const { return strref(len>0?string:nullptr,
len>0?(strl_t(len)<length?len:length):0); }
strref get_word() const { return get_clipped(len_word()); }
// get a range of characters matching the range
strref get_range_word(const strref range, strl_t pos = 0) const;
// get the next block of characters separated by whitespace
strref get_word_ws() const {
strl_t w = len_whitespace(), g = len_grayspace(w); return get_substr(w, g - w); }
strref get_valid_json_string() const {
const uint8_t *s = get_u(); strl_t l = length; while (l) {
uint8_t c = *s++; if (!(c=='+' || c=='.' || c=='-' || is_number(c) || c>='A'))
break; l--; } return strref(string, length-l); }
strref before(char c) const {
int o = find(c); if (o>=0) return strref(string, o); return strref(); }
strref before(char c, char d) const {
int o = find(c, d); if (o>=0) return strref(string, o); return strref(); }
strref before_or_full(char c) const {
int o = find(c); if (o>=0) return strref(string, o); return *this; }
strref before_or_full_track_parens(char c) const {
int o = find_skip_parens(c); if (o >= 0) return strref(string, o); return *this;
}
strref before_last(char c) const {
int o = find_last(c); if (o>=0) return strref(string, o); return strref(); }
strref before_last(char c, char d) const {
int o = find_last(c, d); if (o>=0) return strref(string, o); return strref(); }
strref before_or_full(const strref str) const {
int o = find(str); if (o<0) return *this; return strref(string, o); }
strref after_or_full(const strref str) const {
int o = find(str); if (o<0) return *this; return strref(string+o, length-o); }
strref after_or_full(char c) const { int o = find(c);
if (o>=0) return strref(string+o+1, length-o-1); return *this; }
strref after_or_full(char c, char d) const { int o = find(c, d);
if (o>=0) return strref(string+o+1, length-o-1); return *this; }
strref after(char c) const { int o = find(c);
if (o>=0) return strref(string+o+1, length-o-1); return strref(); }
strref after_last_or_full(char c) const { int o = find_last(c);
if (o>=0) return strref(string+o+1, length-o-1); return *this; }
strref after_last_or_full(char c, char d) const {
int o = find_last(c, d); if (o>=0) return strref(string+o+1, length-o-1); return *this; }
strref after_last(char c) const { int o = find_last(c); if (o>=0)
return strref(string+o+1, length-o-1); return strref(); }
strref after_last(char c, char d) const { int o = find_last(c, d); if (o>=0)
return strref(string+o+1, length-o-1); return strref(); }
strref get_alphanumeric() const { strref r(*this); r.skip_whitespace();
if (strl_t l = r.len_alphanumeric()) return strref(string, l); return strref(); }
strref get_label() const { return strref(string, len_label()); }
strref before_or_full_case(const strref str) const { int o = find_case(str);
if (o<0) return *this; return strref(string, o); }
strref after_or_full_case(const strref str) const { int o = find_case(str);
if (o<0) return *this; return strref(string+o, length-o); }
strref between(char c, char d) { int s = find(c); if (s>=0) { int e = find_after(d, (strl_t)s);
if (e>=0) return get_substr(strl_t(s+1), strl_t(e-s-1)); } return strref(); }
// tokenization
strref split(strl_t pos);
strref split(int pos);
strref split_token(char c);
strref split_token_any(const strref chars);
strref split_token_trim(char c);
strref split_token_any_trim(const strref chars);
strref split_token_track_parens(char c);
strref split_token_track_parens_quote(char c);
strref split_token_trim_track_parens(char c);
strref split_range(const strref range, strl_t pos=0);
strref split_range_trim(const strref range, strl_t pos=0);
strref split_label();
strref split_lang();
strref split_num();
// get a snippet, previous and full current line around a position
strref get_snippet( strl_t pos );
// grab a block of text starting with (, [ or { and end with the corresponding number of ), ] or }
strref scoped_block_skip( bool quotes = false );
// scoped_block_skip with C style comments
strl_t scoped_block_comment_len();
strl_t scoped_block_utf8_comment_len();
strref scoped_block_comment_skip(bool include = false) { strref ret = split(scoped_block_comment_len()); if (!include) { ++ret; ret.clip(1); } return ret; }
strref scoped_block_utf8_comment_skip( bool include = false ) {
strref ret = split( scoped_block_utf8_comment_len() );
if( !include ) { ++ret; ret.clip( 1 ); }
return ret;
}
// check matching characters that are terminated by any character in term or ends
strl_t match_chars_str(const strref match, const strref term = strref());
strref get_line() const; // return the current line even if empty,t don't change this line
strref get_line(strl_t line) const; // return line by index
strref next_line(); // return the current line even if empty and skip this to line after
strref line() { strref ret; while (valid() && !ret.valid()) ret = next_line(); return ret; } // return the current or next valid line skip this to line after
strref next_token(char c) { int o = find(c); if (o<0) o = (int)get_len(); return split(o); }
strref token_chunk(char c) const { int o = find(c); if (o<0) return *this; return strref(string, o); }
void token_skip(const strref chunk) { skip(chunk.length+1); }
strref find_token(const char *substr, char token) const;
strref find_token_case(const char *substr, char token) const;
strref find_token(strref substr, char token) const;
strref find_token_case(strref substr, char token) const;
strref within_last(char a, char b) const { int f = find_last(a)+1;
int l = strref(string+f, length-f).find(b); if (l<0) l = 0; return strref(string+f, l); }
strref within_last(char a1, char a2, char b) const { int f = find_last(a1, a2)+1;
int l = strref(string+f, length-f).find(b); if (l<0) l = 0; return strref(string+f, l); }
strref get_quote_xml() const;
strref skip_quote_xml();
int find_quoted_xml(char d) const; // returns length up to the delimiter d with xml quotation rules, or -1 if delimiter not found
int find_quoted(char d) const; // returns length up to the delimiter d with c/c++ quotation rules, or -1 if delimiter not found
strref next_chunk_xml(char open, char close) const { int s = find_quoted_xml(open);
if (s<0) return strref(); strref left = get_skipped(strl_t(s+1)); return left.get_clipped(strl_t(left.find_quoted_xml(close))); }
strref next_chunk_quoted(char open, char close) const { int s = find_quoted(open);
if (s<0) return strref(); strref left = get_skipped(strl_t(s+1)); return left.get_clipped(strl_t(left.find_quoted(close))); }
void skip_chunk(const strref chunk) { strl_t add = strl_t(chunk.string-string)+chunk.length+1UL;
if (add<length) { string += add; length -= add; } else { clear(); } }
};
// internal helper functions for strmod
strl_t _strmod_copy(char *string, strl_t cap, const char *str);
strl_t _strmod_copy(char *string, strl_t cap, strref str);
strl_t _strmod_append(char *string, strl_t length, strl_t cap, const char *str);
strl_t _strmod_append(char *string, strl_t length, strl_t cap, strref str);
strl_t _strmod_insert(char *string, strl_t length, strl_t cap, const strref sub, strl_t pos);
strl_t _strmod_utf8_tolower(char *string, strl_t length, strl_t cap);
strl_t _strmod_write_utf8( char *string, strl_t cap, size_t code, strl_t pos );
void _strmod_substrcopy(char *string, strl_t length, strl_t cap, strl_t src, strl_t dst, strl_t chars);
void _strmod_tolower(char *string, strl_t length);
void _strmod_toupper(char *string, strl_t length);
strl_t _strmod_format_insert(char *string, strl_t length, strl_t cap, strl_t pos, strref format, const strref *args);
strl_t _strmod_append_num(char* str, strl_t left, uint32_t num, strl_t size, uint32_t radix);
strl_t _strmod_remove(char *string, strl_t length, char a);
strl_t _strmod_remove(char *string, strl_t length, strl_t start, strl_t len);
strl_t _strmod_exchange(char *string, strl_t length, strl_t cap, strl_t start, strl_t size, const strref insert);
strl_t _strmod_cleanup_path(char *file, strl_t len);
strl_t _strmod_relative_path(char *out, strl_t cap, strref orig, strref target);
// intermediate template class to support writeable string classes. use strown or strovl which inherits from this.
template <class B> class strmod : public B {
// mirror base class unsafe size operations (doesn't check capacity)
void add_len_int(strl_t l) { B::add_len_int(l); }
void sub_len_int(strl_t l) { B::sub_len_int(l); }
void set_len_int(strl_t l) { B::set_len_int(l); }
void dec_len_int() { B::dec_len_int(); }
void inc_len_int() { B::inc_len_int(); }
public:
strmod() { clear(); }
operator strref() { return strref(charstr(), len()); }
// mirror base template class
strl_t cap() const { return B::cap(); }
char* charstr() { return B::charstr(); }
const char* charstr() const { return B::charstr(); }
strl_t len() const { return B::len(); }
// get a strref version of this string
strref get_strref() { return strref(charstr(), len()); }
strref get_strref() const { return strref(charstr(), len()); }
strl_t get_len() const { return B::len(); }
// basic tests and operations
void clear() { set_len_int(0); }
bool valid() const { return charstr() && len(); }
operator bool() const { return valid(); }
bool empty() const { return !len(); }
bool full() const { return len() == cap(); }
const char* get() const { return charstr(); }
char get_first() const { return (charstr() && len()) ? *charstr() : 0; }
char get_last() const { return (charstr() && len()) ? charstr()[len()-1] : 0; }
void copy(strref o) { set_len_int(_strmod_copy(charstr(), cap(), o)); }
bool is_substr(const char *sub) const { return sub>=charstr() && sub<=(charstr()+len()); }
// public size operators (checks for capacity)
strl_t fit_add(strl_t desired) { return (desired+len()) < cap() ? desired : (cap()-len()); }
bool set_len(strl_t l) { if (l<=cap()) { set_len_int(l); return true; } set_len_int(cap()); return false; }
void add_len(strl_t l) { add_len_int(fit_add(l)); }
// offset operators will always return a strref
strref operator+(const strl_t skip) { if (skip<len())
return strref(charstr()+skip, len()-skip); return strref(); }
strref operator+(const int skip) { if (skip>=0 && strl_t(skip)<len())
return strref(charstr()+skip, len()-skip); return strref(); }
// get character at position
char operator[](size_t pos) { return pos<len() ? charstr()[pos] : 0; }
// mirror strref functionality
int count_char(char c) const { return get_strref().count_char(c); }
int len_eol() const { return get_strref().len_eol(); }
int len_next_line() const { return get_strref().len_next_line(); }
strl_t len_float_number() const { return get_strref().len_float_number(); }
bool is_float_number() const { return get_strref().is_float_number(); }
// get fnv1a hash for string
unsigned int fnv1a(unsigned int seed = 2166136261) const { return get_strref().fnv1a(seed); }
unsigned int fnv1a_append(unsigned int base_fnv1a_hash) const { return get_strref().fnv1a(base_fnv1a_hash); }
// whole string compare
bool same_str(const strref str) const { return get_strref().same_str_case(str); }
bool same_str_case(const strref str) const { return get_strref().same_str_case(str); }
bool same_str(const strref str, char same1, char same2) const {
return get_strref().same_str_case(str, same1, same2); }
bool same_str_case(const strref str, char same1, char same2) const {
return get_strref().same_str_case(str, same1, same2); }
bool same_str(const char *str) const { return get_strref().same_str(str); }
bool same_str_case(const char *str) const { return get_strref().same_str_case(str); }
// prefix compare
strl_t prefix_len(const strref str) const { return get_strref().prefix_len(str); }
strl_t prefix_len(const strref str, char same1, char same2) const {
return get_strref().prefix_len_case(str, same1, same2); }
strl_t prefix_len(const char *str) const { return get_strref().prefix_len(str); }
strl_t prefix_len_case(const strref str) const { return get_strref().prefix_len_case(str); }
strl_t prefix_len_case(const char *str) const { return get_strref().prefix_len_case(str); }
bool is_prefix_of(const strref str) const { return get_strref().is_prefix_of(str); }
bool is_prefix_of(const strref str, char same1, char same2) const {
return get_strref().is_prefix_of(str, same1, same2); }
bool is_prefix_word(const strref str) const { return get_strref().is_prefix_word(str); }
bool is_prefix_case_of(const strref str) const { return get_strref().is_prefix_case_of(str); }
bool is_prefix_float_number() const { return get_strref().is_prefix_float_number(); }
bool grab_prefix( const char* str ) { return get_strref().grab_prefix( str ); }
// whole word compare (prefix match + next char is whitespace or end of string)
bool is_word(const strref str) const { return get_strref().is_word(str); }
bool is_word_case(const strref str) const { return get_strref().is_word_case(str); }
// suffix compare
strl_t suffix_len(const strref str) const { return get_strref().suffix_len(str); }
strl_t suffix_len_case(const strref str) const { return get_strref().suffix_len_case(str); }
bool is_suffix_of(const strref str) const { return get_strref().is_suffix_of(str); }
bool is_suffix_case_of(const strref str) const { return get_strref().is_suffix_case_of(str); }
bool has_suffix(const char *str) const { return get_strref().has_suffix(str); }
// string search
int find(char c) const { return get_strref().find(c); }
int find_after(char c, strl_t pos) const { return get_strref().find_after(c, pos); }
int find_or_full(char c, strl_t pos) const { return get_strref().find_or_full(c, pos); }
int find_last(char c) const { return get_strref().find_last(c); }
int find(char c, char d) const { return get_strref().find(c, d); }
int find_last(char c, char d) const { return get_strref().find_last(c, d); }
int find_after_last(char a, char b) const { return get_strref().find_after_last(a, b); }
int find_after_last(char a1, char a2, char b) const { return get_strref().find_after_last(a1, a2, b); }
int find(const strref str) const { return get_strref().find(str); }
int find(const strref str, strl_t pos) const { return get_strref().find(str, pos); }
int find(const char *str, strl_t pos = 0) const { return get_strref().find(str, pos); }
int find_case(const strref str) const { return get_strref().find_case(str); }
int find_case(const char *str) const { return get_strref().find_case(str); }
int find_last(const strref str) const { return get_strref().find_last(str); }
int find_last(const char *str) const { return get_strref().find_last(str); }
int find_last_case(const strref str) const { return get_strref().find_last_case(str); }
int substr_count(const strref p) const { return get_strref().substr_count(p); }
// rolling hash string search
int find_rh(strref str) const { return get_strref().find_rh(str); }
int find_rh_case(strref str) const { return get_strref().find_rh_case(str); }
int find_rh(strref str, strl_t pos) const { return get_strref().find_rh(str, pos); }
int find_rh_case(strref str, strl_t pos) const { return get_strref().find_rh_case(str, pos); }
int find_rh_after(strref str, strref prev) const { return get_strref().find_rh_after(str, prev); }
int find_rh_case_after(strref str, strref prev) const { return get_strref().find_rh_case_after(str, prev); }
// whitespace management
int len_whitespace() const { return get_strref().len_whitespace(); }
int len_grayspace() const { return get_strref().len_grayspace(); }
strl_t find_whitespace_or_full() const { return get_strref().find_whitespace_or_full(); }
int find_whitespace() const { return get_strref().find_whitespace(); }
// string content
strl_t len_alphanumeric(strl_t p = 0) const { return get_strref().len_alphanumeric(p); }
strl_t len_word() const { return get_strref().len_word(); }
int len_label() const { return get_strref().len_label(); }
bool is_number() const { return get_strref().is_number(); }
// get a portion of a string
strref get_substr(strl_t pos, strl_t length) const { return get_strref().get_substr(pos, length); }
strref get_skipped(int skip_len) const { return get_strref().get_skipped(skip_len); }
strref get_clipped(int clip_len) const { return get_strref().get_clipped(clip_len); }
strref get_word() const { return get_strref().get_word(); }
strref get_json() const { return get_strref().get_json(); }
strref before(char c) const { return get_strref().before(c); }
strref before(char c, char d) const { return get_strref().before(c, d); }
strref before_last(char c) const { return get_strref().before_last(c); }
strref before_last(char c, char d) const { return get_strref().before_last(c, d); }
strref before_or_full(char c) const { return get_strref().before_or_full(c); }
strref before_or_full(const strref str) const { return get_strref().before_or_full(str); }
strref after_or_full(const strref str) const { return get_strref().after_or_full(str); }
strref after_or_full(char c) const { return get_strref().after_or_full(c); }
strref after(char c) const { return get_strref().after(c); }
strref after_last_or_full(char c) const { return get_strref().after_last_or_full(c); }
strref after_last(char c) const { return get_strref().after_last(c); }
strref get_alphanumeric() const { return get_strref().get_alphanumeric(); }
strref before_or_full_case(const strref str) const { return get_strref().before_or_full_case(str); }
strref after_or_full_case(const strref str) const { return get_strref().after_or_full_case(str); }
strref between(char c, char d) const { return get_strref().between(c, d); }
// tokenization
strref line(); // return the current line and skip this to next line
strref token_chunk(char c) const { return get_strref().token_chunk(c); }
strref find_token(const char *substr, char token) const { return get_strref().find_token(substr, token); }
strref find_token_case(const char *substr, char token) const { return get_strref().find_token_case(substr, token); }
strref find_token(strref substr, char token) const { return get_strref().find_token(substr, token); }
strref find_token_case(strref substr, char token) const { return get_strref().find_token_case(substr, token); }
strref within_last(char a, char b) const { return get_strref().within_last(a, b); }
strref within_last(char a1, char a2, char b) const { return get_strref().within_last(a1, a2, b); }
strref get_quote_xml() const { return get_strref().get_quote_xml(); }
int find_quoted_xml(char d) const { return get_strref().find_quoted_xml(d); }
int find_quoted(char d) const { return get_strref().find_quoted(d); }
// wildcard search
strref find_wildcard(const strref wild, strl_t pos = 0, bool case_sensitive = true) const {
return get_strref().find_wildcard(wild, pos, case_sensitive); }
// find a wildcard right after the start of the previous find
strref next_wildcard(const strref wild, strref prev, bool case_sensitive = true) const {
return get_strref().next_wildcard(wild, prev, case_sensitive); }
// find a wildcard right after the end of the previous find
strref wildcard_after(const strref wild, strref prev, bool case_sensitive = true) const {
return get_strref().next_wildcard(wild, prev, case_sensitive); }
// write a single utf-8 character to pos and return how many bytes was required
strl_t write_utf8(int code, strl_t pos) { return _strmod_write_utf8(charstr(), cap(), code, pos); }
// push a single utf-8 character to the end of this string
void push_utf8(int code) { add_len_int(write_utf8(code, len())); }
// get a single utf-8 character
int get_utf8(strl_t pos) { strl_t skip; return _strmod_read_utf8(charstr(), len(), pos, skip); }
// get a single utf-8 characer and the number of bytes that was used to store it
int get_utf8(strl_t pos, strl_t &skip) { return _strmod_read_utf8(charstr(), len(), pos, skip); }
// choice of uppercasing / lowercasing entire string
void tolower() { _strmod_tolower(charstr(), len()); }
void toupper() { _strmod_toupper(charstr(), len()); }
void tolower_win() { _strmod_tolower_win_ascii(charstr(), len()); }
void toupper_win() { _strmod_toupper_win_ascii(charstr(), len()); }
void tolower_amiga() { _strmod_tolower_amiga_ascii(charstr(), len()); }
void toupper_amiga() { _strmod_toupper_amiga_ascii(charstr(), len()); }
void tolower_macos() { _strmod_tolower_macos_ascii(charstr(), len()); }
void toupper_macos() { _strmod_toupper_macos_ascii(charstr(), len()); }
void tolower_utf8() { set_len_int(_strmod_utf8_tolower(charstr(), len(), cap())); }
void toupper_utf8() { set_len_int(_strmod_utf8_toupper(charstr(), len(), cap())); }
// get the end of the current string
char *end() { return charstr()+len(); }
// number of characters left until at capacity
strl_t left() const { return cap()-len(); }
// remove trailing whitespace
void clip_trailing_whitespace() { if (valid()) {
const char *e = end(); while (*--e<=0x20 && len()) { dec_len_int(); } } }
// copy a substring internally while checking for overlap
void substrcopy(strl_t pos, strl_t target, strl_t _length) {
_strmod_substrcopy(charstr(), len(), cap(), pos, target, _length); }
// insert a substring and expand the string to fit it
bool insert(const strref sub, strl_t pos) {
return set_len(_strmod_insert(charstr(), len(), cap(), sub, pos)); }
// append a substring at the end of this string
strmod& append(const strref o) { if (o) { strl_t a = fit_add(o.get_len());
if (a) { memcpy(end(), o.get(), a); add_len_int(a); } } return *this; }
// append a character at the end of this string
strmod& append(char c) { if (!full()) { charstr()[len()] = c; inc_len_int(); } return *this; }
// C#-ish string concatenation
strmod& operator+(char c) { return append(c); }
strmod& operator+(const char* str) { return append(str); }
strmod& operator+(strref str) { return append(str); }
// C++COut-ish string concatenation
strmod& operator<<(char c) { return append(c); }
strmod& operator<<(const char* str) { return append(str); }
strmod& operator<<(strref str) { return append(str); }
// append a character repeatedly
strmod& pad_to(char c, strl_t pos) { if (len() >= pos) set_len_int(pos); else {
strl_t ol = len(); set_len(pos); for (strl_t p = ol; p < len(); ++p) charstr()[p] = c; } return *this; }
// prepend this string with a substring
void prepend(const strref o) { insert(o, 0); }
// prepend this string with a c string
void prepend(const char *s) { insert(strref(s), 0); }
// format this string using {n} notation to index into the args list
void format(const strref format, const strref *args) {
set_len_int(_strmod_format_insert(charstr(), 0, cap(), 0, format, args)); }
// append a formatted string, return the appended part as a strref
strref format_append(const strref format, const strref *args) { strl_t l = len();
set_len_int(_strmod_format_insert(charstr(), len(), cap(), len(), format, args));
return strref(charstr()+l, len()-l); }
// prepend a formatted string, return the prepend part as a strref
strref format_prepend(const strref format, const strref *args) { strl_t l = len();
set_len_int(_strmod_format_insert(charstr(), len(), cap(), 0, format, args));
return strref(charstr(), len()-l); }
// insert a formatted string
void format_insert(const strref format, const strref *args, strl_t pos) {
set_len_int(_strmod_format_insert(charstr(), len(), cap(), pos, format, args)); }
strmod& append_num(uint32_t num, strl_t size, strl_t radix) {
add_len_int( _strmod_append_num( charstr() + len(), cap() - len(), num, size, radix ) );
return *this;
}
// c style sprintf (work around windows _s preference)
#ifdef _WIN32
int sprintf(const char *format, ...) { va_list args; va_start(args, format);
set_len_int((strl_t)vsnprintf_s(charstr(), cap(), _TRUNCATE, format, args)); va_end(args); return (int)len(); }
int sprintf_at(strl_t pos, const char *format, ...) { va_list args; va_start(args, format);
int l = vsnprintf_s(charstr()+pos, cap()-pos, _TRUNCATE, format, args);
if (l+pos>len()) set_len(l+pos); va_end(args); return l; }
int sprintf_append(const char *format, ...) { va_list args; va_start(args, format); strl_t l = 0;
if (len()<cap()) { l = (strl_t)vsnprintf_s(end(), cap()-len(), _TRUNCATE, format, args); va_end(args);
add_len_int(l); } return (int)l; }
#else
int sprintf(const char *format, ...) { va_list args; va_start(args, format);
set_len_int(vsnprintf(charstr(), cap(), format, args)); va_end(args); return len(); }
int sprintf_at(strl_t pos, const char *format, ...) { va_list args; va_start(args, format);
int l = vsnprintf(charstr()+pos, cap()-pos, format, args);
if (l+pos>len()) set_len(l+pos); va_end(args); return l; }
int sprintf_append(const char *format, ...) { va_list args; va_start(args, format);
int l = vsnprintf(end(), cap()-len(), format, args); va_end(args); add_len_int(l); return l; }
#endif
// replace instances of character c with character d
strref replace(char c, char d) { if (char *b = charstr()) {
for (strl_t i = len(); i; i--) { if (*b==c) *b = d; b++; } } return get_strref(); }
// replace instances of substring a with substring b
strref replace(const strref a, const strref b) {
set_len(_strmod_inplace_replace_int(charstr(), len(), cap(), a, b)); return get_strref(); }
// replace strings bookended by a specific string
strref replace_bookend(const strref a, const strref b, const strref bookend) { if (len() && get() && a && bookend)
set_len(_strmod_inplace_replace_bookend_int(charstr(), len(), cap(), a, b, bookend)); return get_strref(); }
// replace a string found within this string with another string
void exchange(strl_t pos, strl_t size, const strref insert) {
set_len_int(_strmod_exchange(charstr(), len(), cap(), pos, size, insert)); }
void exchange(const strref original, const strref insert) {
if (is_substr(original.get())) { exchange(strl_t(original.get()-get()), original.get_len(), insert); } }
// remove a part of this string
strref remove(strl_t start, strl_t _length) {
set_len_int(_strmod_remove(charstr(), len(), start, _length)); return get_strref(); }
// remove all instances of a character from this string
strref remove(char a) { set_len_int(_strmod_remove(charstr(), len(), a)); return get_strref(); }
// zero terminate this string and return it
const char *c_str() { charstr()[len()<cap()?len():(cap()-1)] = 0; return charstr(); }
// get the end of this string
char* charend() { return charstr()+len(); }
// remove a portion of this string
void erase(strl_t pos, strl_t length) { if (pos<len()) { if ((pos+length)>len())
length = len()-pos; if (length) { for (strl_t i = 0; i<length; i++)
charstr()[pos+i] = charstr()[pos+i+length]; } sub_len_int(length); } }
strmod& cleanup_path() {