-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstr_view.c
1785 lines (1644 loc) · 53.8 KB
/
str_view.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
/* Author: Alexander G. Lopez
==========================
This file implements the str_view interface as an interpretation of C++
string_view type. There are some minor differences and C flavor thrown
in. Additionally, there is a provided reimplementation of the Two-Way
String-Searching algorithm, similar to glibc. */
#include "str_view.h"
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
/* This is the old way I did this in the str_view lib. But I banned vla's in
the ccc library and compilers don't seem to like vla params even though
they are technically not the same issue. So I'll just disable them here.
Clang and GCC support static array parameter declarations while
MSVC does not. This is how to solve the differing declaration
signature requirements.
#if defined(__GNUC__) || defined(__clang__) || defined(__INTEL_LLVM_COMPILER)
Clang and GCC support static array parameter declarations while
MSVC does not. This is how to solve the differing declaration
signature requirements.
A static array parameter declaration helper. Function parameters
may specify an array of a type of at least SIZE elements large,
allowing compiler optimizations and safety errors. Specify
a parameter such as `void func(int size, ARR_GEQ(arr, size))`.
# define ARR_GEQ(str, size) str[static(size)]
A static array parameter declaration helper. Function parameters
may specify an array of a type of at least SIZE elements large,
allowing compiler optimizations and safety errors. Specify
a parameter such as `void func(int size, int ARR_GEQ(arr,size))`.
This declarations adds the additional constraint that the pointer
to the begginning of the array of types will not move.
# define ARR_CONST_GEQ(str, size) str[static const(size)]
#else
Dummy macro for MSVC compatibility. Specifies a function parameter shall
have at least one element. Compiler warnings may differ from GCC/Clang.
# define ARR_GEQ(str, size) *str
Dummy macro for MSVC compatibility. Specifies a function parameter shall
have at least one element. MSVC does not allow specification of a const
pointer to the begginning of an array function parameter when using array
size parameter syntax. Compiler warnings may differ from GCC/Clang.
# define ARR_CONST_GEQ(str, size) *const str
#endif
*/
#define ARR_GEQ(str, size) *str
#define ARR_CONST_GEQ(str, size) *const str
/* ======================== Type Definitions =========================== */
/* Return the factorization step of two-way search in precompute phase. */
struct sv_factorization
{
/* Position in the needle at which (local period = period). */
ptrdiff_t critical_pos;
/* A distance in the needle such that two letters always coincide. */
ptrdiff_t period_dist;
};
/* Avoid giving the user a chance to dereference null as much as posssible
by returning this for various edgecases when it makes sense to communicate
empty, null, invalid, not found etc. Used on cases by case basis.
The function interfaces protect us from null pointers but not always. */
static str_view const nil = {.s = "", .len = 0};
/* ========================= Prototypes =============================== */
static size_t sv_after_find(str_view, str_view);
static size_t sv_before_rfind(str_view, str_view);
static size_t sv_min(size_t, size_t);
static sv_threeway_cmp sv_char_cmp(char, char);
static ptrdiff_t sv_signed_max(ptrdiff_t, ptrdiff_t);
/* Once the user facing API has verified the lengths of strings provided to
views as inputs, internal code can take advantage of compiler optimizations
by assuming the strings are GREATER than or EQUAL TO certain lenghts
allowing for processing by larger units than 1 in compiled code. */
static size_t sv_pos_memo(ptrdiff_t hay_sz, char const ARR_GEQ(, hay_sz),
ptrdiff_t needle_sz, char const ARR_GEQ(, needle_sz),
ptrdiff_t, ptrdiff_t);
static size_t sv_pos_normal(ptrdiff_t hay_sz, char const ARR_GEQ(, hay_sz),
ptrdiff_t needle_sz,
char const ARR_GEQ(, needle_sz), ptrdiff_t,
ptrdiff_t);
static size_t sv_rpos_memo(ptrdiff_t hay_sz, char const ARR_GEQ(, hay_sz),
ptrdiff_t needle_sz, char const ARR_GEQ(, needle_sz),
ptrdiff_t, ptrdiff_t);
static size_t sv_rpos_normal(ptrdiff_t hay_sz, char const ARR_GEQ(, hay_sz),
ptrdiff_t needle_sz,
char const ARR_GEQ(, needle_sz), ptrdiff_t,
ptrdiff_t);
static size_t sv_tw_match(ptrdiff_t hay_sz, char const ARR_GEQ(, hay_sz),
ptrdiff_t needle_sz, char const ARR_GEQ(, needle_sz));
static size_t sv_tw_rmatch(ptrdiff_t hay_sz, char const ARR_GEQ(, hay_sz),
ptrdiff_t needle_sz,
char const ARR_GEQ(, needle_sz));
static struct sv_factorization
sv_maximal_suffix(ptrdiff_t needle_sz, char const ARR_GEQ(, needle_sz));
static struct sv_factorization
sv_maximal_suffix_rev(ptrdiff_t needle_sz, char const ARR_GEQ(, needle_sz));
static struct sv_factorization
sv_rmaximal_suffix(ptrdiff_t needle_sz, char const ARR_GEQ(, needle_sz));
static struct sv_factorization
sv_rmaximal_suffix_rev(ptrdiff_t needle_sz, char const ARR_GEQ(, needle_sz));
static size_t sv_twobyte_strnstrn(size_t hay_sz,
unsigned char const ARR_GEQ(, hay_sz),
size_t n_sz,
unsigned char const ARR_GEQ(, n_sz));
static size_t sv_threebyte_strnstrn(size_t sz,
unsigned char const ARR_GEQ(, sz),
size_t n_sz,
unsigned char const ARR_GEQ(, n_sz));
static size_t sv_fourbyte_strnstrn(size_t sz, unsigned char const ARR_GEQ(, sz),
size_t n_sz,
unsigned char const ARR_GEQ(, n_sz));
static size_t sv_strcspn(size_t str_sz, char const ARR_GEQ(, str_sz),
size_t set_sz, char const ARR_GEQ(, set_sz));
static size_t sv_strspn(size_t str_sz, char const ARR_GEQ(, str_sz),
size_t set_sz, char const ARR_GEQ(, set_sz));
static size_t sv_strnstrn(ptrdiff_t hay_sz, char const ARR_GEQ(, hay_sz),
ptrdiff_t needle_sz, char const ARR_GEQ(, needle_sz));
static size_t sv_strnchr(size_t n, char const ARR_GEQ(, n), char);
static size_t sv_rstrnchr(size_t n, char const ARR_GEQ(, n), char);
static size_t sv_rstrnstrn(ptrdiff_t hay_sz, char const ARR_GEQ(, hay_sz),
ptrdiff_t needle_sz,
char const ARR_GEQ(, needle_sz));
static size_t sv_rtwobyte_strnstrn(size_t sz, unsigned char const ARR_GEQ(, sz),
size_t n_sz,
unsigned char const ARR_GEQ(, n_sz));
static size_t sv_rthreebyte_strnstrn(size_t sz,
unsigned char const ARR_GEQ(, sz),
size_t n_sz,
unsigned char const ARR_GEQ(, n_sz));
static size_t sv_rfourbyte_strnstrn(size_t sz,
unsigned char const ARR_GEQ(, sz),
size_t n_sz,
unsigned char const ARR_GEQ(, n_sz));
/* =================== Interface Implementation ====================== */
str_view
sv(char const *const str)
{
if (!str)
{
return nil;
}
return (str_view){.s = str, .len = strlen(str)};
}
str_view
sv_n(size_t n, char const *const str)
{
if (!str)
{
return nil;
}
return (str_view){.s = str, .len = strnlen(str, n)};
}
str_view
sv_delim(char const *const str, char const *const delim)
{
if (!str)
{
return nil;
}
if (!delim)
{
return (str_view){.s = str, .len = strlen(str)};
}
return sv_begin_tok((str_view){.s = str, .len = strlen(str)},
(str_view){.s = delim, .len = strlen(delim)});
}
void
sv_print(FILE *f, str_view const sv)
{
if (!f || !sv.s || nil.s == sv.s || !sv.len)
{
return;
}
/* printf does not output the null terminator in normal strings so
as long as we output correct number of characters we do the same */
(void)fwrite(sv.s, sizeof(char), sv.len, f);
}
str_view
sv_copy(size_t const str_sz, char const *const src_str)
{
return sv_n(str_sz, src_str);
}
size_t
sv_fill(size_t const dest_sz, char *const dest_buf, str_view const src)
{
if (!dest_buf || !dest_sz || !src.s || !src.len)
{
return 0;
}
size_t const bytes = sv_min(dest_sz, sv_size(src));
memmove(dest_buf, src.s, bytes);
dest_buf[bytes - 1] = '\0';
return bytes;
}
bool
sv_empty(str_view const sv)
{
return !sv.len;
}
size_t
sv_len(str_view const sv)
{
return sv.len;
}
size_t
sv_size(str_view const sv)
{
return sv.len + 1;
}
size_t
sv_strsize(char const *const str)
{
if (!str)
{
return 0;
}
return strlen(str) + 1;
}
size_t
sv_minlen(char const *const str, size_t n)
{
return strnlen(str, n);
}
char
sv_at(str_view const sv, size_t const i)
{
if (i >= sv.len)
{
return *nil.s;
}
return sv.s[i];
}
char const *
sv_null(void)
{
return nil.s;
}
void
sv_swap(str_view *const a, str_view *const b)
{
if (a == b || !a || !b)
{
return;
}
str_view const tmp_b = (str_view){.s = b->s, .len = b->len};
b->s = a->s;
b->len = a->len;
a->s = tmp_b.s;
a->len = tmp_b.len;
}
sv_threeway_cmp
sv_cmp(str_view const lhs, str_view const rhs)
{
if (!lhs.s || !rhs.s)
{
return SV_ERR;
}
size_t const sz = sv_min(lhs.len, rhs.len);
size_t i = 0;
for (; i < sz && lhs.s[i] == rhs.s[i]; ++i)
{}
if (i == lhs.len && i == rhs.len)
{
return SV_EQL;
}
if (i < lhs.len && i < rhs.len)
{
return (uint8_t)lhs.s[i] < (uint8_t)rhs.s[i] ? SV_LES : SV_GRT;
}
return (i < lhs.len) ? SV_GRT : SV_LES;
}
sv_threeway_cmp
sv_strcmp(str_view const lhs, char const *const rhs)
{
if (!lhs.s || !rhs)
{
return SV_ERR;
}
size_t const sz = lhs.len;
size_t i = 0;
for (; i < sz && rhs[i] && lhs.s[i] == rhs[i]; ++i)
{}
if (i == lhs.len && !rhs[i])
{
return SV_EQL;
}
if (i < lhs.len && rhs[i])
{
return (uint8_t)lhs.s[i] < (uint8_t)rhs[i] ? SV_LES : SV_GRT;
}
return (i < lhs.len) ? SV_GRT : SV_LES;
}
sv_threeway_cmp
sv_strncmp(str_view const lhs, char const *const rhs, size_t const n)
{
if (!lhs.s || !rhs)
{
return SV_ERR;
}
size_t const sz = sv_min(lhs.len, n);
size_t i = 0;
for (; i < sz && rhs[i] && lhs.s[i] == rhs[i]; ++i)
{}
if (i == lhs.len && sz == n)
{
return SV_EQL;
}
/* strncmp compares the first at most n bytes inclusive */
if (i < lhs.len && sz <= n)
{
return (uint8_t)lhs.s[i] < (uint8_t)rhs[i] ? SV_LES : SV_GRT;
}
return (i < lhs.len) ? SV_GRT : SV_LES;
}
char
sv_front(str_view const sv)
{
if (!sv.s || !sv.len)
{
return *nil.s;
}
return *sv.s;
}
char
sv_back(str_view const sv)
{
if (!sv.s || !sv.len)
{
return *nil.s;
}
return sv.s[sv.len - 1];
}
char const *
sv_begin(str_view const sv)
{
if (!sv.s)
{
return nil.s;
}
return sv.s;
}
char const *
sv_end(str_view const sv)
{
if (!sv.s || sv.s == nil.s)
{
return nil.s;
}
return sv.s + sv.len;
}
char const *
sv_next(char const *c)
{
if (!c)
{
return nil.s;
}
return ++c;
}
char const *
sv_rbegin(str_view const sv)
{
if (!sv.s)
{
return nil.s;
}
if (!sv.len)
{
return sv.s;
}
return sv.s + sv.len - 1;
}
char const *
sv_rend(str_view const sv)
{
if (!sv.s || sv.s == nil.s)
{
return nil.s;
}
if (!sv.len)
{
return sv.s;
}
return sv.s - 1;
}
char const *
sv_rnext(char const *c)
{
if (!c)
{
return nil.s;
}
return --c;
}
char const *
sv_pos(str_view const sv, size_t const i)
{
if (!sv.s)
{
return nil.s;
}
if (i > sv.len)
{
return sv_end(sv);
}
return sv.s + i;
}
str_view
sv_begin_tok(str_view src, str_view const delim)
{
if (!src.s)
{
return nil;
}
if (!delim.s)
{
return (str_view){.s = src.s + src.len, 0};
}
char const *const begin = src.s;
size_t const sv_not = sv_after_find(src, delim);
src.s += sv_not;
if (begin + src.len == src.s)
{
return (str_view){.s = src.s, .len = 0};
}
src.len -= sv_not;
return (str_view){.s = src.s, .len = sv_find(src, 0, delim)};
}
bool
sv_end_tok(str_view const src, str_view const tok)
{
return !tok.len || tok.s >= (src.s + src.len);
}
str_view
sv_next_tok(str_view const src, str_view const tok, str_view const delim)
{
if (!tok.s)
{
return nil;
}
if (!delim.s || !tok.s || !tok.s[tok.len])
{
return (str_view){.s = tok.s + tok.len, .len = 0};
}
str_view next = {.s = tok.s + tok.len, .len = src.len - tok.len};
if (next.s >= src.s + src.len)
{
return (str_view){.s = src.s + src.len, .len = 0};
}
next.s += delim.len;
next.len = src.len - (next.s - src.s);
/* There is a cheap easy way to skip repeating delimiters before the
next search that should be faster than string comparison. */
size_t const after_delim = sv_after_find(next, delim);
next.s += after_delim;
next.len -= after_delim;
if (next.s >= src.s + src.len)
{
return (str_view){.s = src.s + src.len, .len = 0};
}
size_t const found = sv_strnstrn((ptrdiff_t)next.len, next.s,
(ptrdiff_t)delim.len, delim.s);
return (str_view){.s = next.s, .len = found};
}
str_view
sv_rbegin_tok(str_view src, str_view const delim)
{
if (!src.s)
{
return nil;
}
if (!delim.s)
{
return (str_view){.s = src.s + src.len, 0};
}
size_t before_delim = sv_before_rfind(src, delim);
src.len = sv_min(src.len, before_delim + 1);
size_t start = sv_rfind(src, src.len, delim);
if (start == src.len)
{
return src;
}
start += delim.len;
return (str_view){.s = src.s + start, .len = before_delim - start + 1};
}
str_view
sv_rnext_tok(str_view const src, str_view const tok, str_view const delim)
{
if (!tok.s)
{
return nil;
}
if (!tok.len | !delim.s || tok.s == src.s || tok.s - delim.len <= src.s)
{
return (str_view){.s = src.s, .len = 0};
}
str_view const shorter = {.s = src.s, .len = (tok.s - delim.len) - src.s};
/* Same as in the forward version, this method is a quick way to skip
any number of repeating delimiters before starting the next search
for a delimiter before a token. */
size_t const before_delim = sv_before_rfind(shorter, delim);
if (before_delim == shorter.len)
{
return shorter;
}
size_t start = sv_rstrnstrn((ptrdiff_t)before_delim, shorter.s,
(ptrdiff_t)delim.len, delim.s);
if (start == before_delim)
{
return (str_view){.s = shorter.s, .len = before_delim + 1};
}
start += delim.len;
return (str_view){.s = src.s + start, .len = before_delim - start + 1};
}
bool
sv_rend_tok(str_view const src, str_view const tok)
{
return !tok.len && tok.s == src.s;
}
str_view
sv_extend(str_view sv)
{
if (!sv.s)
{
return nil;
}
char const *i = sv.s;
while (*i++)
{}
sv.len = i - sv.s - 1;
return sv;
}
bool
sv_starts_with(str_view const sv, str_view const prefix)
{
if (prefix.len > sv.len)
{
return false;
}
return sv_cmp(sv_substr(sv, 0, prefix.len), prefix) == SV_EQL;
}
str_view
sv_remove_prefix(str_view const sv, size_t const n)
{
size_t const remove = sv_min(sv.len, n);
return (str_view){.s = sv.s + remove, .len = sv.len - remove};
}
bool
sv_ends_with(str_view const sv, str_view const suffix)
{
if (suffix.len > sv.len)
{
return false;
}
return sv_cmp(sv_substr(sv, sv.len - suffix.len, suffix.len), suffix)
== SV_EQL;
}
str_view
sv_remove_suffix(str_view const sv, size_t const n)
{
if (!sv.s)
{
return nil;
}
return (str_view){.s = sv.s, .len = sv.len - sv_min(sv.len, n)};
}
str_view
sv_substr(str_view const sv, size_t const pos, size_t const count)
{
if (pos > sv.len)
{
return (str_view){.s = sv.s + sv.len, .len = 0};
}
return (str_view){.s = sv.s + pos, .len = sv_min(count, sv.len - pos)};
}
bool
sv_contains(str_view const hay, str_view const needle)
{
if (needle.len > hay.len)
{
return false;
}
if (sv_empty(hay))
{
return false;
}
if (sv_empty(needle))
{
return true;
}
return hay.len
!= sv_strnstrn((ptrdiff_t)hay.len, hay.s, (ptrdiff_t)needle.len,
needle.s);
}
str_view
sv_match(str_view const hay, str_view const needle)
{
if (!hay.s || !needle.s)
{
return nil;
}
if (needle.len > hay.len || sv_empty(hay) || sv_empty(needle))
{
return (str_view){.s = hay.s + hay.len, .len = 0};
}
size_t const found = sv_strnstrn((ptrdiff_t)hay.len, hay.s,
(ptrdiff_t)needle.len, needle.s);
return found == hay.len ? (str_view){.s = hay.s + hay.len, .len = 0}
: (str_view){.s = hay.s + found, .len = needle.len};
}
str_view
sv_rmatch(str_view const hay, str_view const needle)
{
if (!hay.s)
{
return nil;
}
if (sv_empty(hay) || sv_empty(needle))
{
return (str_view){.s = hay.s + hay.len, .len = 0};
}
size_t const found = sv_rstrnstrn((ptrdiff_t)hay.len, hay.s,
(ptrdiff_t)needle.len, needle.s);
return found == hay.len ? (str_view){.s = hay.s + hay.len, .len = 0}
: (str_view){.s = hay.s + found, .len = needle.len};
}
size_t
sv_find(str_view const hay, size_t const pos, str_view const needle)
{
if (needle.len > hay.len || pos > hay.len)
{
return hay.len;
}
return pos
+ sv_strnstrn((ptrdiff_t)(hay.len - pos), hay.s + pos,
(ptrdiff_t)needle.len, needle.s);
}
size_t
sv_rfind(str_view const h, size_t pos, str_view const n)
{
if (!h.len || n.len > h.len)
{
return h.len;
}
if (pos >= h.len)
{
pos = h.len - 1;
}
size_t const found
= sv_rstrnstrn((ptrdiff_t)pos + 1, h.s, (ptrdiff_t)n.len, n.s);
return found == pos + 1 ? h.len : found;
}
size_t
sv_find_first_of(str_view const hay, str_view const set)
{
if (!hay.s || !hay.len)
{
return 0;
}
if (!set.s || !set.len)
{
return hay.len;
}
return sv_strcspn(hay.len, hay.s, set.len, set.s);
}
size_t
sv_find_last_of(str_view const hay, str_view const set)
{
if (!hay.s || !hay.len)
{
return 0;
}
if (!set.s || !set.len)
{
return hay.len;
}
/* It may be tempting to go right to left but consider if that really
would be reliably faster across every possible string one encounters.
The last occurence of a set char could be anywhere in the string. */
size_t last_pos = hay.len;
for (size_t in = 0, prev = 0;
(in += sv_strspn(hay.len - in, hay.s + in, set.len, set.s)) != hay.len;
++in, prev = in)
{
if (in != prev)
{
last_pos = in - 1;
}
}
return last_pos;
}
size_t
sv_find_first_not_of(str_view const hay, str_view const set)
{
if (!hay.s || !hay.len)
{
return 0;
}
if (!set.s || !set.len)
{
return 0;
}
return sv_strspn(hay.len, hay.s, set.len, set.s);
}
size_t
sv_find_last_not_of(str_view const hay, str_view const set)
{
if (!hay.s || !hay.len)
{
return 0;
}
if (!set.s || !set.len)
{
return hay.len - 1;
}
size_t last_pos = hay.len;
for (size_t in = 0, prev = 0;
(in += sv_strspn(hay.len - in, hay.s + in, set.len, set.s)) != hay.len;
++in, prev = in)
{
if (in != prev)
{
last_pos = in;
}
}
return last_pos;
}
size_t
sv_npos(str_view const sv)
{
return sv.len;
}
/* ====================== Static Helpers ============================= */
static size_t
sv_after_find(str_view const hay, str_view const needle)
{
if (needle.len > hay.len)
{
return 0;
}
size_t delim_i = 0;
size_t i = 0;
for (; i < hay.len && needle.s[delim_i] == hay.s[i];
delim_i = (delim_i + 1) % needle.len, ++i)
{}
/* Also reset to the last mismatch found. If some of the delimeter matched
but then the string changed into a mismatch go back to get characters
that are partially in the delimeter. */
return i - delim_i;
}
static size_t
sv_before_rfind(str_view const hay, str_view const needle)
{
if (needle.len > hay.len || !needle.len || !hay.len)
{
return hay.len;
}
size_t delim_i = 0;
size_t i = 0;
for (; i < hay.len
&& needle.s[needle.len - delim_i - 1] == hay.s[hay.len - i - 1];
delim_i = (delim_i + 1) % needle.len, ++i)
{}
/* Ugly logic to account for the reverse nature of this modulo search.
the position needs to account for any part of the delim that may
have started to match but then mismatched. The 1 is because
this in an index being returned not a length. */
return i == hay.len ? hay.len : hay.len - i + delim_i - 1;
}
static inline size_t
sv_min(size_t const a, size_t const b)
{
return a < b ? a : b;
}
static inline ptrdiff_t
sv_signed_max(ptrdiff_t const a, ptrdiff_t const b)
{
return a > b ? a : b;
}
static inline sv_threeway_cmp
sv_char_cmp(char const a, char const b)
{
return (a > b) - (a < b);
}
/* ====================== Static Utilities =========================== */
/* This is section is modeled after the musl string.h library. However,
using str_view that may not be null terminated requires modifications. */
#define BITOP(a, b, op) \
((a)[(size_t)(b) / (8 * sizeof *(a))] op(size_t) 1 \
<< ((size_t)(b) % (8 * sizeof *(a))))
/* This is dangerous. Do not use this under normal circumstances.
This is an internal helper for the backwards two way string
searching algorithm. It expects that both arguments are
greater than or equal to n bytes in length similar to how
the forward version expects the same. However, the comparison
moves backward from the location provided for n bytes. */
static int
sv_rmemcmp(void const *const vl, void const *const vr, size_t n)
{
unsigned char const *l = vl;
unsigned char const *r = vr;
for (; n && *l == *r; n--, l--, r--)
{}
return n ? *l - *r : 0;
}
/* strcspn is based on musl C-standard library implementation
http://git.musl-libc.org/cgit/musl/tree/src/string/strcspn.c
A custom implemenatation is necessary because C standard library impls
have no concept of a string view and will continue searching beyond the
end of a view until null is found. This way, string searches are
efficient and only within the range specified. */
static size_t
sv_strcspn(size_t const str_sz, char const ARR_CONST_GEQ(str, str_sz),
size_t const set_sz, char const ARR_GEQ(set, set_sz))
{
if (!set_sz)
{
return str_sz;
}
char const *a = str;
size_t byteset[32 / sizeof(size_t)];
if (set_sz == 1)
{
for (size_t i = 0; i < str_sz && *a != *set; ++a, ++i)
{}
return a - str;
}
memset(byteset, 0, sizeof byteset);
for (size_t i = 0; i < set_sz && BITOP(byteset, *(unsigned char *)set, |=);
++set, ++i)
{}
for (size_t i = 0; i < str_sz && !BITOP(byteset, *(unsigned char *)a, &);
++a)
{}
return a - str;
}
/* strspn is based on musl C-standard library implementation
https://git.musl-libc.org/cgit/musl/tree/src/string/strspn.c
A custom implemenatation is necessary because C standard library impls
have no concept of a string view and will continue searching beyond the
end of a view until null is found. This way, string searches are
efficient and only within the range specified. */
static size_t
sv_strspn(size_t const str_sz, char const ARR_CONST_GEQ(str, str_sz),
size_t const set_sz, char const ARR_GEQ(set, set_sz))
{
char const *a = str;
size_t byteset[32 / sizeof(size_t)] = {0};
if (!set_sz)
{
return str_sz;
}
if (set_sz == 1)
{
for (size_t i = 0; i < str_sz && *a == *set; ++a, ++i)
{}
return a - str;
}
for (size_t i = 0; i < set_sz && BITOP(byteset, *(unsigned char *)set, |=);
++set, ++i)
{}
for (size_t i = 0; i < str_sz && BITOP(byteset, *(unsigned char *)a, &);
++a, ++i)
{}
return a - str;
}
/* Providing strnstrn rather than strstr at the lowest level works better
for string views where the string may not be null terminated. There needs
to always be the additional constraint that a search cannot exceed the
hay length. Returns 0 based index position at which needle begins in
hay if it can be found, otherwise the hay size is returned. */
static size_t
sv_strnstrn(ptrdiff_t const hay_sz, char const ARR_CONST_GEQ(hay, hay_sz),
ptrdiff_t const needle_sz,
char const ARR_CONST_GEQ(needle, needle_sz))
{
if (!hay_sz || !needle_sz || needle_sz > hay_sz)
{
return hay_sz;
}
if (1 == needle_sz)
{
return sv_strnchr(hay_sz, hay, *needle);
}
if (2 == needle_sz)
{
return sv_twobyte_strnstrn(hay_sz, (unsigned char *)hay, 2,
(unsigned char *)needle);
}
if (3 == needle_sz)
{
return sv_threebyte_strnstrn(hay_sz, (unsigned char *)hay, 3,
(unsigned char *)needle);
}
if (4 == needle_sz)
{
return sv_fourbyte_strnstrn(hay_sz, (unsigned char *)hay, 4,
(unsigned char *)needle);
}
return sv_tw_match(hay_sz, hay, needle_sz, needle);
}
/* For now reverse logic for backwards searches has been separated into
other functions. There is a possible formula to unit the reverse and
forward logic into one set of functions, but the code is ugly. See
the start of the reverse two-way algorithm for more. May unite if
a clean way exists. */
static size_t
sv_rstrnstrn(ptrdiff_t const hay_sz, char const ARR_CONST_GEQ(hay, hay_sz),
ptrdiff_t const needle_sz,
char const ARR_CONST_GEQ(needle, needle_sz))
{
if (!hay_sz || !needle_sz || needle_sz > hay_sz)
{
return hay_sz;
}
if (1 == needle_sz)
{
return sv_rstrnchr(hay_sz, hay, *needle);
}
if (2 == needle_sz)
{
return sv_rtwobyte_strnstrn(hay_sz, (unsigned char *)hay, 2,
(unsigned char *)needle);
}
if (3 == needle_sz)
{