aboutsummaryrefslogtreecommitdiffstats
path: root/libsimple/mem.h
blob: a7cf03869d6ec02fc7b165a7a9c8d958131c21ce (plain) (blame)
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
/* See LICENSE file for copyright and license details. */


/**
 * Finds the first occurence of a byte value in an array of bytes,
 * the comparison is case-sensitive
 * 
 * @param   s  The array of bytes to search
 * @param   c  The byte value to search for
 * @param   n  The number of bytes in the byte array
 * @return     `s` with a minimal offset such that `*r == c`,
 *             where `r` is the returned pointer, `&s[n]` if
 *             no such offset exists within [s, &s[n])
 */
LIBSIMPLE_GCC_ONLY__(__attribute__((__pure__, __returns_nonnull__, __warn_unused_result__)))
void *libsimple_memscan(const void *, int, size_t);
#ifndef memscan
# define memscan libsimple_memscan
#endif


/**
 * Finds the first occurence of a byte value in an array of bytes,
 * the comparison is case-insensitive
 * 
 * @param   s  The array of bytes to search
 * @param   c  The byte value to search for
 * @param   n  The number of bytes in the byte array
 * @return     `s` with a minimal offset such that `tolower(*r) == tolower(c)`,
 *             where `r` is the returned pointer, `NULL` if
 *             no such offset exists within [s, &s[n])
 */
LIBSIMPLE_GCC_ONLY__(__attribute__((__pure__, __warn_unused_result__)))
void *libsimple_memcasechr(const void *, int, size_t);
#ifndef memcasechr
# define memcasechr libsimple_memcasechr
#endif


/**
 * Finds the first occurence of a byte value in an array of bytes,
 * the comparison is case-insensitive
 * 
 * @param   s  The array of bytes to search
 * @param   c  The byte value to search for
 * @param   n  The number of bytes in the byte array
 * @return     `s` with a minimal offset such that `tolower(*r) == tolower(c)`,
 *             where `r` is the returned pointer, `&s[n]` if
 *             no such offset exists within [s, &s[n])
 */
LIBSIMPLE_GCC_ONLY__(__attribute__((__pure__, __nonnull__, __returns_nonnull__, __warn_unused_result__)))
void *libsimple_memcasescan(const void *, int, size_t);
#ifndef memcasescan
# define memcasescan libsimple_memcasescan
#endif


/**
 * Finds the first occurence of a byte value in an array of bytes,
 * the comparison is case-sensitive
 * 
 * This function is optimised for instances where it is already
 * known that there is at least one occurence; if there is no
 * occurence of the specified byte value in the specified byte
 * array, its behaviour is undefined
 * 
 * @param   s  The array of bytes to search
 * @param   c  The byte value to search for
 * @return     `s` with a miminal offset such that `*r == c`,
 *             where `r` is the returned pointer
 */
LIBSIMPLE_GCC_ONLY__(__attribute__((__pure__, __nonnull__, __returns_nonnull__, __warn_unused_result__)))
void *libsimple_rawmemchr(const void *, int);
#ifndef rawmemchr
# define rawmemchr libsimple_rawmemchr
#endif


/**
 * Finds the first occurence of a byte value in an array of bytes,
 * the comparison is case-insensitive
 * 
 * This function is optimised for instances where it is already
 * known that there is at least one occurence; if there is no
 * occurence of the specified byte value in the specified byte
 * array, its behaviour is undefined
 * 
 * @param   s  The array of bytes to search
 * @param   c  The byte value to search for
 * @return     `s` with a miminal offset such that `tolower(*r) == tolower(c)`,
 *             where `r` is the returned pointer
 */
LIBSIMPLE_GCC_ONLY__(__attribute__((__pure__, __nonnull__, __returns_nonnull__, __warn_unused_result__)))
void *libsimple_rawmemcasechr(const void *, int);
#ifndef rawmemcasechr
# define rawmemcasechr libsimple_rawmemcasechr
#endif


/**
 * Finds the last occurence of a byte value in an array of bytes,
 * the comparison is case-sensitive
 * 
 * @param   s  The array of bytes to search
 * @param   c  The byte value to search for
 * @param   n  The number of bytes in the byte array
 * @return     `s` with a maximal offset such that `*r == c`,
 *             where `r` is the returned pointer `NULL` if no
 *             such offset exists within [s, &s[n])
 */
LIBSIMPLE_GCC_ONLY__(__attribute__((__pure__, __warn_unused_result__)))
void *libsimple_memrchr(const void *, int, size_t);
#ifndef memrchr
# define memrchr libsimple_memrchr
#endif


/**
 * Finds the last occurence of a byte value in an array of bytes,
 * the comparison is case-insensitive
 * 
 * @param   s  The array of bytes to search
 * @param   c  The byte value to search for
 * @param   n  The number of bytes in the byte array
 * @return     `s` with a maximal offset such that `tolower(*r) == tolower(c)`,
 *             where `r` is the returned pointer `NULL` if no
 *             such offset exists within [s, &s[n])
 */
LIBSIMPLE_GCC_ONLY__(__attribute__((__pure__, __warn_unused_result__)))
void *libsimple_memrcasechr(const void *, int, size_t);
#ifndef memrcasechr
# define memrcasechr libsimple_memrcasechr
#endif


/**
 * Finds the last occurence of a byte value in an array of bytes,
 * the comparison is case-sensitive
 * 
 * This function is optimised for instances where it is already
 * known that there is at least one occurence; if there is no
 * occurence of the specified byte value in the specified byte
 * array, its behaviour is undefined
 * 
 * @param   s  The array of bytes to search
 * @param   c  The byte value to search for
 * @param   n  The number of bytes in the byte array
 * @return     `s` with a maximal offset such that `*r == c`,
 *             where `r` is the returned pointer
 */
LIBSIMPLE_GCC_ONLY__(__attribute__((__pure__, __nonnull__, __returns_nonnull__, __warn_unused_result__)))
void *libsimple_rawmemrchr(const void *, int, size_t);
#ifndef rawmemrchr
# define rawmemrchr libsimple_rawmemrchr
#endif


/**
 * Finds the last occurence of a byte value in an array of bytes,
 * the comparison is case-insensitive
 * 
 * This function is optimised for instances where it is already
 * known that there is at least one occurence; if there is no
 * occurence of the specified byte value in the specified byte
 * array, its behaviour is undefined
 * 
 * @param   s  The array of bytes to search
 * @param   c  The byte value to search for
 * @param   n  The number of bytes in the byte array
 * @return     `s` with a maximal offset such that `tolower(*r) == tolower(c)`,
 *             where `r` is the returned pointer
 */
LIBSIMPLE_GCC_ONLY__(__attribute__((__pure__, __nonnull__, __returns_nonnull__, __warn_unused_result__)))
void *libsimple_rawmemrcasechr(const void *, int, size_t);
#ifndef rawmemrcasechr
# define rawmemrcasechr libsimple_rawmemrcasechr
#endif


/**
 * Finds the first occurence of a byte value in an array of bytes,
 * other than specified byte value, the comparison is case-sensitive
 * 
 * @param   s  The array of bytes to search
 * @param   c  The byte value to search for
 * @param   n  The number of bytes in the byte array
 * @return     `s` with a minimal offset such that `*r != c`,
 *             where `r` is the returned pointer, `NULL` if
 *             no such offset exists within [s, &s[n])
 */
LIBSIMPLE_GCC_ONLY__(__attribute__((__pure__, __warn_unused_result__)))
void *libsimple_memchr_inv(const void *, int, size_t);
#ifndef memchr_inv
# define memchr_inv libsimple_memchr_inv
#endif


/**
 * Finds the first occurence of a byte value in an array of bytes,
 * other than specified byte value, the comparison is case-sensitive
 * 
 * @param   s  The array of bytes to search
 * @param   c  The byte value to search for
 * @param   n  The number of bytes in the byte array
 * @return     `s` with a minimal offset such that `*r != c`,
 *             where `r` is the returned pointer, `&s[n]` if
 *             no such offset exists within [s, &s[n])
 */
LIBSIMPLE_GCC_ONLY__(__attribute__((__pure__, __returns_nonnull__, __warn_unused_result__)))
void *libsimple_memscan_inv(const void *, int, size_t);
#ifndef memscan_inv
# define memscan_inv libsimple_memscan_inv
#endif


/**
 * Finds the first occurence of a byte value in an array of bytes,
 * other than specified byte value, the comparison is case-insensitive
 * 
 * @param   s  The array of bytes to search
 * @param   c  The byte value to search for
 * @param   n  The number of bytes in the byte array
 * @return     `s` with a minimal offset such that `tolower(*r) != tolower(c)`,
 *             where `r` is the returned pointer, `NULL` if
 *             no such offset exists within [s, &s[n])
 */
LIBSIMPLE_GCC_ONLY__(__attribute__((__pure__, __warn_unused_result__)))
void *libsimple_memcasechr_inv(const void *, int, size_t);
#ifndef memcasechr_inv
# define memcasechr_inv libsimple_memcasechr_inv
#endif


/**
 * Finds the first occurence of a byte value in an array of bytes,
 * other than specified byte value, the comparison is case-insensitive
 * 
 * @param   s  The array of bytes to search
 * @param   c  The byte value to search for
 * @param   n  The number of bytes in the byte array
 * @return     `s` with a minimal offset such that `tolower(*r) != tolower(c)`,
 *             where `r` is the returned pointer, `&s[n]` if
 *             no such offset exists within [s, &s[n])
 */
LIBSIMPLE_GCC_ONLY__(__attribute__((__pure__, __nonnull__, __returns_nonnull__, __warn_unused_result__)))
void *libsimple_memcasescan_inv(const void *, int, size_t);
#ifndef memcasescan_inv
# define memcasescan_inv libsimple_memcasescan_inv
#endif


/**
 * Finds the first occurence of a byte value in an array of bytes,
 * other than specified byte value, the comparison is case-sensitive
 * 
 * This function is optimised for instances where it is already
 * known that there is at least one occurence; if there is no
 * occurence of any byte value than the specified byte value in
 * the specified byte array, its behaviour is undefined
 * 
 * @param   s  The array of bytes to search
 * @param   c  The byte value to search for
 * @return     `s` with a miminal offset such that `*r != c`,
 *             where `r` is the returned pointer
 */
LIBSIMPLE_GCC_ONLY__(__attribute__((__pure__, __nonnull__, __returns_nonnull__, __warn_unused_result__)))
void *libsimple_rawmemchr_inv(const void *, int);
#ifndef rawmemchr_inv
# define rawmemchr_inv libsimple_rawmemchr_inv
#endif


/**
 * Finds the first occurence of a byte value in an array of bytes,
 * other than specified byte value, the comparison is case-insensitive
 * 
 * This function is optimised for instances where it is already
 * known that there is at least one occurence; if there is no
 * occurence of any byte value than the specified byte value in
 * the specified byte array, its behaviour is undefined
 * 
 * @param   s  The array of bytes to search
 * @param   c  The byte value to search for
 * @return     `s` with a miminal offset such that `tolower(*r) != tolower(c)`,
 *             where `r` is the returned pointer
 */
LIBSIMPLE_GCC_ONLY__(__attribute__((__pure__, __nonnull__, __returns_nonnull__, __warn_unused_result__)))
void *libsimple_rawmemcasechr_inv(const void *, int);
#ifndef rawmemcasechr_inv
# define rawmemcasechr_inv libsimple_rawmemcasechr_inv
#endif


/**
 * Finds the last occurence of a byte value in an array of bytes,
 * other than specified byte value, the comparison is case-sensitive
 * 
 * @param   s  The array of bytes to search
 * @param   c  The byte value to search for
 * @param   n  The number of bytes in the byte array
 * @return     `s` with a maximal offset such that `*r != c`,
 *             where `r` is the returned pointer `NULL` if no
 *             such offset exists within [s, &s[n])
 */
LIBSIMPLE_GCC_ONLY__(__attribute__((__pure__, __warn_unused_result__)))
void *libsimple_memrchr_inv(const void *, int, size_t);
#ifndef memrchr_inv
# define memrchr_inv libsimple_memrchr_inv
#endif


/**
 * Finds the last occurence of a byte value in an array of bytes,
 * other than specified byte value, the comparison is case-insensitive
 * 
 * @param   s  The array of bytes to search
 * @param   c  The byte value to search for
 * @param   n  The number of bytes in the byte array
 * @return     `s` with a maximal offset such that `tolower(*r) != tolower(c)`,
 *             where `r` is the returned pointer `NULL` if no
 *             such offset exists within [s, &s[n])
 */
LIBSIMPLE_GCC_ONLY__(__attribute__((__pure__, __warn_unused_result__)))
void *libsimple_memrcasechr_inv(const void *, int, size_t);
#ifndef memrcasechr_inv
# define memrcasechr_inv libsimple_memrcasechr_inv
#endif


/**
 * Finds the last occurence of a byte value in an array of bytes,
 * other than specified byte value, the comparison is case-sensitive
 * 
 * This function is optimised for instances where it is already
 * known that there is at least one occurence; if there is no
 * occurence of any byte value than the specified byte value in
 * the specified byte array, its behaviour is undefined
 * 
 * @param   s  The array of bytes to search
 * @param   c  The byte value to search for
 * @param   n  The number of bytes in the byte array
 * @return     `s` with a maximal offset such that `*r != c`,
 *             where `r` is the returned pointer
 */
LIBSIMPLE_GCC_ONLY__(__attribute__((__pure__, __nonnull__, __returns_nonnull__, __warn_unused_result__)))
void *libsimple_rawmemrchr_inv(const void *, int, size_t);
#ifndef rawmemrchr_inv
# define rawmemrchr_inv libsimple_rawmemrchr_inv
#endif


/**
 * Finds the last occurence of a byte value in an array of bytes,
 * other than specified byte value, the comparison is case-insensitive
 * 
 * This function is optimised for instances where it is already
 * known that there is at least one occurence; if there is no
 * occurence of any byte value than the specified byte value in
 * the specified byte array, its behaviour is undefined
 * 
 * @param   s  The array of bytes to search
 * @param   c  The byte value to search for
 * @param   n  The number of bytes in the byte array
 * @return     `s` with a maximal offset such that `tolower(*r) != tolower(c)`,
 *             where `r` is the returned pointer
 */
LIBSIMPLE_GCC_ONLY__(__attribute__((__pure__, __nonnull__, __returns_nonnull__, __warn_unused_result__)))
void *libsimple_rawmemrcasechr_inv(const void *, int, size_t);
#ifndef rawmemrcasechr_inv
# define rawmemrcasechr_inv libsimple_rawmemrcasechr_inv
#endif


/**
 * Finds the first substring in an array of bytes, the comparison is case-sensitive
 * 
 * @param   haystack   The array of bytes to search
 * @param   nhaystack  The length of `haystack`
 * @param   needle     The substring to search for
 * @param   nneedle    The length of `needle`
 * @return             `haystack` with a minimal offset such that,
 *                     `!memcmp(r, needle, nneedle)` where `r` is the
 *                     returned pointer, `NULL` if no such offset exists
 */
LIBSIMPLE_GCC_ONLY__(__attribute__((__pure__, __warn_unused_result__)))
void *libsimple_memmem(const void *, size_t, const void *, size_t);
#ifndef memmem
# define memmem libsimple_memmem
#endif


/**
 * Finds the first substring in an array of bytes, the comparison is case-insensitive
 * 
 * @param   haystack   The array of bytes to search
 * @param   nhaystack  The length of `haystack`
 * @param   needle     The substring to search for
 * @param   nneedle    The length of `needle`
 * @return             `haystack` with a minimal offset such that,
 *                     `!memcasecmp(r, needle, nneedle)` where `r` is the
 *                     returned pointer, `NULL` if no such offset exists
 */
LIBSIMPLE_GCC_ONLY__(__attribute__((__pure__, __warn_unused_result__)))
void *libsimple_memcasemem(const void *, size_t, const void *, size_t);
#ifndef memcasemem
# define memcasemem libsimple_memcasemem
#endif


/**
 * Finds the last substring in an array of bytes, the comparison is case-sensitive
 * 
 * @param   haystack   The array of bytes to search
 * @param   nhaystack  The length of `haystack`
 * @param   needle     The substring to search for
 * @param   nneedle    The length of `needle`
 * @return             `haystack` with a maximal offset such that,
 *                     `!memcmp(r, needle, nneedle)` where `r` is the
 *                     returned pointer, `NULL` if no such offset exists
 */
LIBSIMPLE_GCC_ONLY__(__attribute__((__pure__, __warn_unused_result__)))
void *libsimple_memrmem(const void *, size_t, const void *, size_t);
#ifndef memrmem
# define memrmem libsimple_memrmem
#endif


/**
 * Finds the last substring in an array of bytes, the comparison is case-insensitive
 * 
 * @param   haystack   The array of bytes to search
 * @param   nhaystack  The length of `haystack`
 * @param   needle     The substring to search for
 * @param   nneedle    The length of `needle`
 * @return             `haystack` with a maximal offset such that,
 *                     `!memcasecmp(r, needle, nneedle)` where `r` is the
 *                     returned pointer, `NULL` if no such offset exists
 */
LIBSIMPLE_GCC_ONLY__(__attribute__((__pure__, __warn_unused_result__)))
void *libsimple_memrcasemem(const void *, size_t, const void *, size_t);
#ifndef memrcasemem
# define memrcasemem libsimple_memrcasemem
#endif


/**
 * Checks the beginning of an array of bytes, the comparison is case-sensitive
 * 
 * @param   s  The array of bytes to check
 * @param   n  The length of `s`
 * @param   t  The desired beginning of `s`
 * @param   m  The length of `t`
 * @return     1 if `s` begins with `t`, 0 otherwise
 */
LIBSIMPLE_GCC_ONLY__(__attribute__((__pure__, __warn_unused_result__)))
int libsimple_memstarts(const void *, size_t, const void *, size_t);
#ifndef memstarts
# define memstarts libsimple_memstarts
#endif


/**
 * Checks the beginning of an array of bytes, the comparison is case-insensitive
 * 
 * @param   s  The array of bytes to check
 * @param   n  The length of `s`
 * @param   t  The desired beginning of `s`
 * @param   m  The length of `t`
 * @return     1 if `s` begins with `t`, 0 otherwise
 */
LIBSIMPLE_GCC_ONLY__(__attribute__((__pure__, __warn_unused_result__)))
int libsimple_memcasestarts(const void *, size_t, const void *, size_t);
#ifndef memcasestarts
# define memcasestarts libsimple_memcasestarts
#endif


/**
 * Checks the end of an array of bytes, the comparison is case-sensitive
 * 
 * @param   s  The array of bytes to check
 * @param   n  The length of `s`
 * @param   t  The desired ending of `s`
 * @param   m  The length of `t`
 * @return     1 if `s` ends with `t`, 0 otherwise
 */
LIBSIMPLE_GCC_ONLY__(__attribute__((__pure__, __warn_unused_result__)))
int libsimple_memends(const void *, size_t, const void *, size_t);
#ifndef memends
# define memends libsimple_memends
#endif


/**
 * Checks the end of an array of bytes, the comparison is case-insensitive
 * 
 * @param   s  The array of bytes to check
 * @param   n  The length of `s`
 * @param   t  The desired ending of `s`
 * @param   m  The length of `t`
 * @return     1 if `s` ends with `t`, 0 otherwise
 */
LIBSIMPLE_GCC_ONLY__(__attribute__((__pure__, __warn_unused_result__)))
int libsimple_memcaseends(const void *, size_t, const void *, size_t);
#ifndef memcaseends
# define memcaseends libsimple_memcaseends
#endif


/**
 * Checks two arrays of bytes for equality, the comparison is case-insensitive
 * 
 * @param   a  One of the arrays of bytes
 * @param   b  The other arrays of bytes
 * @param   n  The lengths of the arrays
 * @return     1 if the arrays are equal, 0 otherwise
 */
LIBSIMPLE_GCC_ONLY__(__attribute__((__pure__, __warn_unused_result__)))
int libsimple_memcasecmp(const void *, const void *, size_t);
#ifndef memcasecmp
# define memcasecmp libsimple_memcasecmp
#endif


/**
 * Checks two arrays of bytes for equality, the comparison is case-sensitive
 * 
 * @param   a  One of the arrays of bytes
 * @param   b  The other arrays of bytes
 * @param   n  The lengths of the arrays
 * @return     1 if the arrays are equal, 0 otherwise
 */
LIBSIMPLE_GCC_ONLY__(__attribute__((__pure__, __warn_unused_result__)))
inline int
libsimple_memeq(const void *a__, const void *b__, size_t n__)
{
	return !memcmp(a__, b__, n__);
}
#ifndef memeq
# define memeq libsimple_memeq
#endif


/**
 * Checks two arrays of bytes for equality, the comparison is case-insensitive
 * 
 * @param   a  One of the arrays of bytes
 * @param   b  The other arrays of bytes
 * @param   n  The lengths of the arrays
 * @return     1 if the arrays are equal, 0 otherwise
 */
LIBSIMPLE_GCC_ONLY__(__attribute__((__pure__, __warn_unused_result__)))
inline int
libsimple_memcaseeq(const void *a__, const void *b__, size_t n__)
{
	return !memcasecmp(a__, b__, n__);
}
#ifndef memcaseeq
# define memcaseeq libsimple_memcaseeq
#endif


/**
 * Copies an array of bytes into another
 * 
 * @param   d  The array the bytes should be copied into
 * @param   s  The array of bytes that should be copied
 * @param   n  The number of bytes to copy
 * @return     `&d[n]`
 */
inline void *
libsimple_mempcpy(void *restrict d__, const void *restrict s__, size_t n__)
{
	return &((char *)memcpy(d__, s__, n__))[n__];
}
#ifndef mempcpy
# define mempcpy libsimple_mempcpy
#endif


/**
 * Moves bytes within an array of bytes
 * 
 * @param   d  The location in the array the bytes should be moved to
 * @param   s  The bytes that should be moved
 * @param   n  The number of bytes to move
 * @return     `&d[n]`
 */
inline void *
libsimple_mempmove(void *d__, const void *s__, size_t n__)
{
	return &((char *)memmove(d__, s__, n__))[n__];
}
#ifndef mempmove
# define mempmove libsimple_mempmove
#endif


/**
 * Fill an array of bytes with a specified byte
 * 
 * @param   s  The array of bytes to fill
 * @param   c  The byte to fill the array with
 * @param   n  The number of bytes to write to `s`
 * @return     `&s[n]`
 */
inline void *
libsimple_mempset(void *s__, int c__, size_t n__)
{
	return &((char *)memset(s__, c__, n__))[n__];
}
#ifndef mempset
# define mempset libsimple_mempset
#endif


/**
 * Copy an array of bytes, but stop after a specific byte
 * 
 * This function is optimised for instances where it is already
 * known that there is at least one occurence; if there is no
 * occurence of the specified byte value in the specified byte
 * array, its behaviour is undefined
 * 
 * @param   d  The location the byte array shall be copied to
 * @param   s  The byte array to copy
 * @param   c  The byte that stops the copying
 * @return     `&rawmemchr(d, c)[1]` (after copying)
 */
inline void *
libsimple_rawmemccpy(void *restrict d___, const void *restrict s___, int c___)
{
	char c__ = (char)c___, *restrict d__ = d___;
	const char *restrict s__ = s___;
	for (; (*d__++ = *s__) != c__; s__++);
	return d__;
}
#ifndef rawmemccpy
# define rawmemccpy libsimple_rawmemccpy
#endif


/**
 * Move a string, but stop after a specific character
 * 
 * @param   d  The location the byte array shall be copied to
 * @param   s  The byte array to copy
 * @param   c  The byte that stops the copying
 * @param   n  The length of `s`
 * @return     `&rawmemchr(d, c)[1]` (after copying) if `c` can
 *             be found within the first `n` bytes of `s` (before
 *             copying), `NULL` otherwise
 */
void *libsimple_memcmove(void *, const void *, int, size_t);
#ifndef memcmove
# define memcmove libsimple_memcmove
#endif


/**
 * Move a string, but stop after a specific character
 * 
 * This function is optimised for instances where it is already
 * known that there is at least one occurence; if there is no
 * occurence of the specified byte value in the specified byte
 * array, its behaviour is undefined
 * 
 * @param   d  The location the byte array shall be copied to
 * @param   s  The byte array to copy
 * @param   c  The byte that stops the copying
 * @return     `&rawmemchr(d, c)[1]` (after copying)
 */
inline void *
libsimple_rawmemcmove(void *d___, const void *s___, int c___)
{
	char *d__ = d___, *p__, c__ = (char)c___;
	const char *s__ = s___;
	size_t n__;
	if (d__ <= s__) {
		for (; (*d__++ = *s__) != c__; s__++);
		return d__;
	} else {
		for (p__ = *(char **)(void *)&s__; *p__++ != c__;);
		n__ = (size_t)(p__ - s__);
		p__ = &d__[n__];
		while (n__) {
			n__--;
			d__[n__] = s__[n__];
		}
		return p__;
	}
}
#ifndef rawmemcmove
# define rawmemcmove libsimple_rawmemcmove
#endif


/**
 * Replace all instances of a byte in an array of byte with another byte
 * 
 * @param   s    The byte array
 * @param   old  The value of the bytes to replace
 * @param   new  The value to replace the bytes with
 * @param   n    The length of `s`
 * @return       `(void *)&((char *)s)[n]`
 */
inline void *
libsimple_memreplace(void *s___, int old___, int new___, size_t n__)
{
	char old__ = (char)old___, new__ = (char)new___, *s__ = s___;
	char *ret__ = &s__[n__];
	while (n__)
		if (s__[--n__] == old__)
			s__[n__] = new__;
	return ret__;
}
#ifndef memreplace
# define memreplace libsimple_memreplace
#endif


/**
 * Copy an array of bytes but convert to lower case
 * 
 * `d` and `s` may overlap; the function has an
 * optimisation for when `d == s`
 * 
 * `d` will be `s` but in lower case
 * 
 * @param   d  The location the array shall be copied to
 * @param   s  The byte array to copy
 * @param   n  The number of bytes to copy or covert
 * @return     `&d[n]`
 */
void *libsimple_memptolower(void *, const void *, size_t);
#ifndef memptolower
# define memptolower libsimple_memptolower
#endif


/**
 * Copy an array of bytes but convert to upper case
 * 
 * `d` and `s` may overlap; the function has an
 * optimisation for when `d == s`
 * 
 * `d` will be `s` but in upper case
 * 
 * @param   d  The location the array shall be copied to
 * @param   s  The byte array to copy
 * @param   n  The number of bytes to copy or covert
 * @return     `&d[n]`
 */
void *libsimple_memptoupper(void *, const void *, size_t);
#ifndef memptoupper
# define memptoupper libsimple_memptoupper
#endif


/**
 * Copy an array of bytes but convert to lower case
 * 
 * `d` and `s` may overlap; the function has an
 * optimisation for when `d == s`
 * 
 * `d` will be `s` but in lower case
 * 
 * @param   d  The location the array shall be copied to
 * @param   s  The byte array to copy
 * @param   n  The number of bytes to copy or covert
 * @return     `d`
 */
inline void *
libsimple_memtolower(void *d__, const void *s__, size_t n__)
{
	libsimple_memptolower(d__, s__, n__);
	return d__;
}
#ifndef memtolower
# define memtolower libsimple_memtolower
#endif


/**
 * Copy an array of bytes but convert to upper case
 * 
 * `d` and `s` may overlap; the function has an
 * optimisation for when `d == s`
 * 
 * `d` will be `s` but in upper case
 * 
 * @param   d  The location the array shall be copied to
 * @param   s  The byte array to copy
 * @param   n  The number of bytes to copy or covert
 * @return     `d`
 */
inline void *
libsimple_memtoupper(void *d__, const void *s__, size_t n__)
{
	libsimple_memptoupper(d__, s__, n__);
	return d__;
}
#ifndef memtoupper
# define memtoupper libsimple_memtoupper
#endif


/**
 * Compares the beginning of two memory segments, the comparison is case-sensitive
 * 
 * @param   a  One of the arrays
 * @param   n  The length of `a`
 * @param   b  The other array
 * @param   m  The length of `b`
 * @return     The number of bytes `a` and `b` have
 *             in common in their beginnings
 */
LIBSIMPLE_GCC_ONLY__(__attribute__((__pure__, __warn_unused_result__)))
size_t libsimple_memeqlen(const void *, size_t, const void *, size_t);
#ifndef memeqlen
# define memeqlen libsimple_memeqlen
#endif


/**
 * Compares the beginning of two memory segments, the comparison is case-insensitive
 * 
 * @param   a  One of the arrays
 * @param   n  The length of `a`
 * @param   b  The other array
 * @param   m  The length of `b`
 * @return     The number of bytes `a` and `b` have
 *             in common in their beginnings
 */
LIBSIMPLE_GCC_ONLY__(__attribute__((__pure__, __warn_unused_result__)))
size_t libsimple_memcaseeqlen(const void *, size_t, const void *, size_t);
#ifndef memcaseeqlen
# define memcaseeqlen libsimple_memcaseeqlen
#endif


/**
 * Compares the end of two memory segments, the comparison is case-sensitive
 * 
 * @param   a  One of the arrays
 * @param   n  The length of `a`
 * @param   b  The other array
 * @param   m  The length of `b`
 * @return     The number of bytes `a` and `b` have
 *             in common in their ends
 */
LIBSIMPLE_GCC_ONLY__(__attribute__((__pure__, __warn_unused_result__)))
size_t libsimple_memreqlen(const void *, size_t, const void *, size_t);
#ifndef memreqlen
# define memreqlen libsimple_memreqlen
#endif


/**
 * Compares the end of two memory segments, the comparison is case-insensitive
 * 
 * @param   a  One of the arrays
 * @param   n  The length of `a`
 * @param   b  The other array
 * @param   m  The length of `b`
 * @return     The number of bytes `a` and `b` have
 *             in common in their ends
 */
LIBSIMPLE_GCC_ONLY__(__attribute__((__pure__, __warn_unused_result__)))
size_t libsimple_memrcaseeqlen(const void *, size_t, const void *, size_t);
#ifndef memrcaseeqlen
# define memrcaseeqlen libsimple_memrcaseeqlen
#endif


/**
 * Check whether a string is encoded in UTF-8
 * 
 * @param   string              The string
 * @param   n                   The length of the string
 * @param   allow_modified_nul  Whether Modified UTF-8 is allowed, which
 *                              allows a two-byte encoding for NUL
 * @return                      1 if good, 0 on encoding error
 */
LIBSIMPLE_GCC_ONLY__(__attribute__((__pure__, __warn_unused_result__)))
int libsimple_memisutf8(const char *, size_t, int);
#ifndef memisutf8
# define memisutf8 libsimple_memisutf8
#endif