forked from HelenOS/helenos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstr.c
1548 lines (1342 loc) · 37.2 KB
/
str.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2001-2004 Jakub Jermar
* Copyright (c) 2005 Martin Decky
* Copyright (c) 2008 Jiri Svoboda
* Copyright (c) 2011 Martin Sucha
* Copyright (c) 2011 Oleg Romanenko
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* - The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/** @addtogroup libc
* @{
*/
/**
* @file
* @brief String functions.
*
* Strings and characters use the Universal Character Set (UCS). The standard
* strings, called just strings are encoded in UTF-8. Wide strings (encoded
* in UTF-32) are supported to a limited degree. A single character is
* represented as char32_t.@n
*
* Overview of the terminology:@n
*
* Term Meaning
* -------------------- ----------------------------------------------------
* byte 8 bits stored in uint8_t (unsigned 8 bit integer)
*
* character UTF-32 encoded Unicode character, stored in char32_t
* (unsigned 32 bit integer), code points 0 .. 1114111
* are valid
*
* ASCII character 7 bit encoded ASCII character, stored in char
* (usually signed 8 bit integer), code points 0 .. 127
* are valid
*
* string UTF-8 encoded NULL-terminated Unicode string, char *
*
* wide string UTF-32 encoded NULL-terminated Unicode string,
* char32_t *
*
* [wide] string size number of BYTES in a [wide] string (excluding
* the NULL-terminator), size_t
*
* [wide] string length number of CHARACTERS in a [wide] string (excluding
* the NULL-terminator), size_t
*
* [wide] string width number of display cells on a monospace display taken
* by a [wide] string, size_t
*
*
* Overview of string metrics:@n
*
* Metric Abbrev. Type Meaning
* ------ ------ ------ -------------------------------------------------
* size n size_t number of BYTES in a string (excluding the
* NULL-terminator)
*
* length l size_t number of CHARACTERS in a string (excluding the
* null terminator)
*
* width w size_t number of display cells on a monospace display
* taken by a string
*
*
* Function naming prefixes:@n
*
* chr_ operate on characters
* ascii_ operate on ASCII characters
* str_ operate on strings
* wstr_ operate on wide strings
*
* [w]str_[n|l|w] operate on a prefix limited by size, length
* or width
*
*
* A specific character inside a [wide] string can be referred to by:@n
*
* pointer (char *, char32_t *)
* byte offset (size_t)
* character index (size_t)
*
*/
#include <str.h>
#include <assert.h>
#include <ctype.h>
#include <errno.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#include <align.h>
#include <mem.h>
/** Byte mask consisting of lowest @n bits (out of 8) */
#define LO_MASK_8(n) ((uint8_t) ((1 << (n)) - 1))
/** Byte mask consisting of lowest @n bits (out of 32) */
#define LO_MASK_32(n) ((uint32_t) ((1 << (n)) - 1))
/** Byte mask consisting of highest @n bits (out of 8) */
#define HI_MASK_8(n) (~LO_MASK_8(8 - (n)))
/** Number of data bits in a UTF-8 continuation byte */
#define CONT_BITS 6
/** Decode a single character from a string.
*
* Decode a single character from a string of size @a size. Decoding starts
* at @a offset and this offset is moved to the beginning of the next
* character. In case of decoding error, offset generally advances at least
* by one. However, offset is never moved beyond size.
*
* @param str String (not necessarily NULL-terminated).
* @param offset Byte offset in string where to start decoding.
* @param size Size of the string (in bytes).
*
* @return Value of decoded character, U_SPECIAL on decoding error or
* NULL if attempt to decode beyond @a size.
*
*/
char32_t str_decode(const char *str, size_t *offset, size_t size)
{
if (*offset + 1 > size)
return 0;
/* First byte read from string */
uint8_t b0 = (uint8_t) str[(*offset)++];
/* Determine code length */
unsigned int b0_bits; /* Data bits in first byte */
unsigned int cbytes; /* Number of continuation bytes */
if ((b0 & 0x80) == 0) {
/* 0xxxxxxx (Plain ASCII) */
b0_bits = 7;
cbytes = 0;
} else if ((b0 & 0xe0) == 0xc0) {
/* 110xxxxx 10xxxxxx */
b0_bits = 5;
cbytes = 1;
} else if ((b0 & 0xf0) == 0xe0) {
/* 1110xxxx 10xxxxxx 10xxxxxx */
b0_bits = 4;
cbytes = 2;
} else if ((b0 & 0xf8) == 0xf0) {
/* 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx */
b0_bits = 3;
cbytes = 3;
} else {
/* 10xxxxxx -- unexpected continuation byte */
return U_SPECIAL;
}
if (*offset + cbytes > size)
return U_SPECIAL;
char32_t ch = b0 & LO_MASK_8(b0_bits);
/* Decode continuation bytes */
while (cbytes > 0) {
uint8_t b = (uint8_t) str[(*offset)++];
/* Must be 10xxxxxx */
if ((b & 0xc0) != 0x80)
return U_SPECIAL;
/* Shift data bits to ch */
ch = (ch << CONT_BITS) | (char32_t) (b & LO_MASK_8(CONT_BITS));
cbytes--;
}
return ch;
}
/** Decode a single character from a string to the left.
*
* Decode a single character from a string of size @a size. Decoding starts
* at @a offset and this offset is moved to the beginning of the previous
* character. In case of decoding error, offset generally decreases at least
* by one. However, offset is never moved before 0.
*
* @param str String (not necessarily NULL-terminated).
* @param offset Byte offset in string where to start decoding.
* @param size Size of the string (in bytes).
*
* @return Value of decoded character, U_SPECIAL on decoding error or
* NULL if attempt to decode beyond @a start of str.
*
*/
char32_t str_decode_reverse(const char *str, size_t *offset, size_t size)
{
if (*offset == 0)
return 0;
size_t processed = 0;
/* Continue while continuation bytes found */
while (*offset > 0 && processed < 4) {
uint8_t b = (uint8_t) str[--(*offset)];
if (processed == 0 && (b & 0x80) == 0) {
/* 0xxxxxxx (Plain ASCII) */
return b & 0x7f;
} else if ((b & 0xe0) == 0xc0 || (b & 0xf0) == 0xe0 ||
(b & 0xf8) == 0xf0) {
/* Start byte */
size_t start_offset = *offset;
return str_decode(str, &start_offset, size);
} else if ((b & 0xc0) != 0x80) {
/* Not a continuation byte */
return U_SPECIAL;
}
processed++;
}
/* Too many continuation bytes */
return U_SPECIAL;
}
/** Encode a single character to string representation.
*
* Encode a single character to string representation (i.e. UTF-8) and store
* it into a buffer at @a offset. Encoding starts at @a offset and this offset
* is moved to the position where the next character can be written to.
*
* @param ch Input character.
* @param str Output buffer.
* @param offset Byte offset where to start writing.
* @param size Size of the output buffer (in bytes).
*
* @return EOK if the character was encoded successfully, EOVERFLOW if there
* was not enough space in the output buffer or EINVAL if the character
* code was invalid.
*/
errno_t chr_encode(const char32_t ch, char *str, size_t *offset, size_t size)
{
if (*offset >= size)
return EOVERFLOW;
if (!chr_check(ch))
return EINVAL;
/*
* Unsigned version of ch (bit operations should only be done
* on unsigned types).
*/
uint32_t cc = (uint32_t) ch;
/* Determine how many continuation bytes are needed */
unsigned int b0_bits; /* Data bits in first byte */
unsigned int cbytes; /* Number of continuation bytes */
if ((cc & ~LO_MASK_32(7)) == 0) {
b0_bits = 7;
cbytes = 0;
} else if ((cc & ~LO_MASK_32(11)) == 0) {
b0_bits = 5;
cbytes = 1;
} else if ((cc & ~LO_MASK_32(16)) == 0) {
b0_bits = 4;
cbytes = 2;
} else if ((cc & ~LO_MASK_32(21)) == 0) {
b0_bits = 3;
cbytes = 3;
} else {
/* Codes longer than 21 bits are not supported */
return EINVAL;
}
/* Check for available space in buffer */
if (*offset + cbytes >= size)
return EOVERFLOW;
/* Encode continuation bytes */
unsigned int i;
for (i = cbytes; i > 0; i--) {
str[*offset + i] = 0x80 | (cc & LO_MASK_32(CONT_BITS));
cc = cc >> CONT_BITS;
}
/* Encode first byte */
str[*offset] = (cc & LO_MASK_32(b0_bits)) | HI_MASK_8(8 - b0_bits - 1);
/* Advance offset */
*offset += cbytes + 1;
return EOK;
}
/** Get size of string.
*
* Get the number of bytes which are used by the string @a str (excluding the
* NULL-terminator).
*
* @param str String to consider.
*
* @return Number of bytes used by the string
*
*/
size_t str_size(const char *str)
{
size_t size = 0;
while (*str++ != 0)
size++;
return size;
}
/** Get size of wide string.
*
* Get the number of bytes which are used by the wide string @a str (excluding the
* NULL-terminator).
*
* @param str Wide string to consider.
*
* @return Number of bytes used by the wide string
*
*/
size_t wstr_size(const char32_t *str)
{
return (wstr_length(str) * sizeof(char32_t));
}
/** Get size of string with length limit.
*
* Get the number of bytes which are used by up to @a max_len first
* characters in the string @a str. If @a max_len is greater than
* the length of @a str, the entire string is measured (excluding the
* NULL-terminator).
*
* @param str String to consider.
* @param max_len Maximum number of characters to measure.
*
* @return Number of bytes used by the characters.
*
*/
size_t str_lsize(const char *str, size_t max_len)
{
size_t len = 0;
size_t offset = 0;
while (len < max_len) {
if (str_decode(str, &offset, STR_NO_LIMIT) == 0)
break;
len++;
}
return offset;
}
/** Get size of string with size limit.
*
* Get the number of bytes which are used by the string @a str
* (excluding the NULL-terminator), but no more than @max_size bytes.
*
* @param str String to consider.
* @param max_size Maximum number of bytes to measure.
*
* @return Number of bytes used by the string
*
*/
size_t str_nsize(const char *str, size_t max_size)
{
size_t size = 0;
while ((*str++ != 0) && (size < max_size))
size++;
return size;
}
/** Get size of wide string with size limit.
*
* Get the number of bytes which are used by the wide string @a str
* (excluding the NULL-terminator), but no more than @max_size bytes.
*
* @param str Wide string to consider.
* @param max_size Maximum number of bytes to measure.
*
* @return Number of bytes used by the wide string
*
*/
size_t wstr_nsize(const char32_t *str, size_t max_size)
{
return (wstr_nlength(str, max_size) * sizeof(char32_t));
}
/** Get size of wide string with length limit.
*
* Get the number of bytes which are used by up to @a max_len first
* wide characters in the wide string @a str. If @a max_len is greater than
* the length of @a str, the entire wide string is measured (excluding the
* NULL-terminator).
*
* @param str Wide string to consider.
* @param max_len Maximum number of wide characters to measure.
*
* @return Number of bytes used by the wide characters.
*
*/
size_t wstr_lsize(const char32_t *str, size_t max_len)
{
return (wstr_nlength(str, max_len * sizeof(char32_t)) * sizeof(char32_t));
}
/** Get number of characters in a string.
*
* @param str NULL-terminated string.
*
* @return Number of characters in string.
*
*/
size_t str_length(const char *str)
{
size_t len = 0;
size_t offset = 0;
while (str_decode(str, &offset, STR_NO_LIMIT) != 0)
len++;
return len;
}
/** Get number of characters in a wide string.
*
* @param str NULL-terminated wide string.
*
* @return Number of characters in @a str.
*
*/
size_t wstr_length(const char32_t *wstr)
{
size_t len = 0;
while (*wstr++ != 0)
len++;
return len;
}
/** Get number of characters in a string with size limit.
*
* @param str NULL-terminated string.
* @param size Maximum number of bytes to consider.
*
* @return Number of characters in string.
*
*/
size_t str_nlength(const char *str, size_t size)
{
size_t len = 0;
size_t offset = 0;
while (str_decode(str, &offset, size) != 0)
len++;
return len;
}
/** Get number of characters in a string with size limit.
*
* @param str NULL-terminated string.
* @param size Maximum number of bytes to consider.
*
* @return Number of characters in string.
*
*/
size_t wstr_nlength(const char32_t *str, size_t size)
{
size_t len = 0;
size_t limit = ALIGN_DOWN(size, sizeof(char32_t));
size_t offset = 0;
while ((offset < limit) && (*str++ != 0)) {
len++;
offset += sizeof(char32_t);
}
return len;
}
/** Get character display width on a character cell display.
*
* @param ch Character
* @return Width of character in cells.
*/
size_t chr_width(char32_t ch)
{
return 1;
}
/** Get string display width on a character cell display.
*
* @param str String
* @return Width of string in cells.
*/
size_t str_width(const char *str)
{
size_t width = 0;
size_t offset = 0;
char32_t ch;
while ((ch = str_decode(str, &offset, STR_NO_LIMIT)) != 0)
width += chr_width(ch);
return width;
}
/** Check whether character is plain ASCII.
*
* @return True if character is plain ASCII.
*
*/
bool ascii_check(char32_t ch)
{
if (ch <= 127)
return true;
return false;
}
/** Check whether character is valid
*
* @return True if character is a valid Unicode code point.
*
*/
bool chr_check(char32_t ch)
{
if (ch <= 1114111)
return true;
return false;
}
/** Compare two NULL terminated strings.
*
* Do a char-by-char comparison of two NULL-terminated strings.
* The strings are considered equal iff their length is equal
* and both strings consist of the same sequence of characters.
*
* A string S1 is less than another string S2 if it has a character with
* lower value at the first character position where the strings differ.
* If the strings differ in length, the shorter one is treated as if
* padded by characters with a value of zero.
*
* @param s1 First string to compare.
* @param s2 Second string to compare.
*
* @return 0 if the strings are equal, -1 if the first is less than the second,
* 1 if the second is less than the first.
*
*/
int str_cmp(const char *s1, const char *s2)
{
char32_t c1 = 0;
char32_t c2 = 0;
size_t off1 = 0;
size_t off2 = 0;
while (true) {
c1 = str_decode(s1, &off1, STR_NO_LIMIT);
c2 = str_decode(s2, &off2, STR_NO_LIMIT);
if (c1 < c2)
return -1;
if (c1 > c2)
return 1;
if (c1 == 0 || c2 == 0)
break;
}
return 0;
}
/** Compare two NULL terminated strings with length limit.
*
* Do a char-by-char comparison of two NULL-terminated strings.
* The strings are considered equal iff
* min(str_length(s1), max_len) == min(str_length(s2), max_len)
* and both strings consist of the same sequence of characters,
* up to max_len characters.
*
* A string S1 is less than another string S2 if it has a character with
* lower value at the first character position where the strings differ.
* If the strings differ in length, the shorter one is treated as if
* padded by characters with a value of zero. Only the first max_len
* characters are considered.
*
* @param s1 First string to compare.
* @param s2 Second string to compare.
* @param max_len Maximum number of characters to consider.
*
* @return 0 if the strings are equal, -1 if the first is less than the second,
* 1 if the second is less than the first.
*
*/
int str_lcmp(const char *s1, const char *s2, size_t max_len)
{
char32_t c1 = 0;
char32_t c2 = 0;
size_t off1 = 0;
size_t off2 = 0;
size_t len = 0;
while (true) {
if (len >= max_len)
break;
c1 = str_decode(s1, &off1, STR_NO_LIMIT);
c2 = str_decode(s2, &off2, STR_NO_LIMIT);
if (c1 < c2)
return -1;
if (c1 > c2)
return 1;
if (c1 == 0 || c2 == 0)
break;
++len;
}
return 0;
}
/** Compare two NULL terminated strings in case-insensitive manner.
*
* Do a char-by-char comparison of two NULL-terminated strings.
* The strings are considered equal iff their length is equal
* and both strings consist of the same sequence of characters
* when converted to lower case.
*
* A string S1 is less than another string S2 if it has a character with
* lower value at the first character position where the strings differ.
* If the strings differ in length, the shorter one is treated as if
* padded by characters with a value of zero.
*
* @param s1 First string to compare.
* @param s2 Second string to compare.
*
* @return 0 if the strings are equal, -1 if the first is less than the second,
* 1 if the second is less than the first.
*
*/
int str_casecmp(const char *s1, const char *s2)
{
char32_t c1 = 0;
char32_t c2 = 0;
size_t off1 = 0;
size_t off2 = 0;
while (true) {
c1 = tolower(str_decode(s1, &off1, STR_NO_LIMIT));
c2 = tolower(str_decode(s2, &off2, STR_NO_LIMIT));
if (c1 < c2)
return -1;
if (c1 > c2)
return 1;
if (c1 == 0 || c2 == 0)
break;
}
return 0;
}
/** Compare two NULL terminated strings with length limit in case-insensitive
* manner.
*
* Do a char-by-char comparison of two NULL-terminated strings.
* The strings are considered equal iff
* min(str_length(s1), max_len) == min(str_length(s2), max_len)
* and both strings consist of the same sequence of characters,
* up to max_len characters.
*
* A string S1 is less than another string S2 if it has a character with
* lower value at the first character position where the strings differ.
* If the strings differ in length, the shorter one is treated as if
* padded by characters with a value of zero. Only the first max_len
* characters are considered.
*
* @param s1 First string to compare.
* @param s2 Second string to compare.
* @param max_len Maximum number of characters to consider.
*
* @return 0 if the strings are equal, -1 if the first is less than the second,
* 1 if the second is less than the first.
*
*/
int str_lcasecmp(const char *s1, const char *s2, size_t max_len)
{
char32_t c1 = 0;
char32_t c2 = 0;
size_t off1 = 0;
size_t off2 = 0;
size_t len = 0;
while (true) {
if (len >= max_len)
break;
c1 = tolower(str_decode(s1, &off1, STR_NO_LIMIT));
c2 = tolower(str_decode(s2, &off2, STR_NO_LIMIT));
if (c1 < c2)
return -1;
if (c1 > c2)
return 1;
if (c1 == 0 || c2 == 0)
break;
++len;
}
return 0;
}
/** Test whether p is a prefix of s.
*
* Do a char-by-char comparison of two NULL-terminated strings
* and determine if p is a prefix of s.
*
* @param s The string in which to look
* @param p The string to check if it is a prefix of s
*
* @return true iff p is prefix of s else false
*
*/
bool str_test_prefix(const char *s, const char *p)
{
char32_t c1 = 0;
char32_t c2 = 0;
size_t off1 = 0;
size_t off2 = 0;
while (true) {
c1 = str_decode(s, &off1, STR_NO_LIMIT);
c2 = str_decode(p, &off2, STR_NO_LIMIT);
if (c2 == 0)
return true;
if (c1 != c2)
return false;
if (c1 == 0)
break;
}
return false;
}
/** Get a string suffix.
*
* Return a string suffix defined by the prefix length.
*
* @param s The string to get the suffix from.
* @param prefix_length Number of prefix characters to ignore.
*
* @return String suffix.
*
*/
const char *str_suffix(const char *s, size_t prefix_length)
{
size_t off = 0;
size_t i = 0;
while (true) {
str_decode(s, &off, STR_NO_LIMIT);
i++;
if (i >= prefix_length)
break;
}
return s + off;
}
/** Copy string.
*
* Copy source string @a src to destination buffer @a dest.
* No more than @a size bytes are written. If the size of the output buffer
* is at least one byte, the output string will always be well-formed, i.e.
* null-terminated and containing only complete characters.
*
* @param dest Destination buffer.
* @param count Size of the destination buffer (must be > 0).
* @param src Source string.
*
*/
void str_cpy(char *dest, size_t size, const char *src)
{
/* There must be space for a null terminator in the buffer. */
assert(size > 0);
assert(src != NULL);
size_t src_off = 0;
size_t dest_off = 0;
char32_t ch;
while ((ch = str_decode(src, &src_off, STR_NO_LIMIT)) != 0) {
if (chr_encode(ch, dest, &dest_off, size - 1) != EOK)
break;
}
dest[dest_off] = '\0';
}
/** Copy size-limited substring.
*
* Copy prefix of string @a src of max. size @a size to destination buffer
* @a dest. No more than @a size bytes are written. The output string will
* always be well-formed, i.e. null-terminated and containing only complete
* characters.
*
* No more than @a n bytes are read from the input string, so it does not
* have to be null-terminated.
*
* @param dest Destination buffer.
* @param count Size of the destination buffer (must be > 0).
* @param src Source string.
* @param n Maximum number of bytes to read from @a src.
*
*/
void str_ncpy(char *dest, size_t size, const char *src, size_t n)
{
/* There must be space for a null terminator in the buffer. */
assert(size > 0);
size_t src_off = 0;
size_t dest_off = 0;
char32_t ch;
while ((ch = str_decode(src, &src_off, n)) != 0) {
if (chr_encode(ch, dest, &dest_off, size - 1) != EOK)
break;
}
dest[dest_off] = '\0';
}
/** Append one string to another.
*
* Append source string @a src to string in destination buffer @a dest.
* Size of the destination buffer is @a dest. If the size of the output buffer
* is at least one byte, the output string will always be well-formed, i.e.
* null-terminated and containing only complete characters.
*
* @param dest Destination buffer.
* @param count Size of the destination buffer.
* @param src Source string.
*/
void str_append(char *dest, size_t size, const char *src)
{
size_t dstr_size;
dstr_size = str_size(dest);
if (dstr_size >= size)
return;
str_cpy(dest + dstr_size, size - dstr_size, src);
}
/** Convert space-padded ASCII to string.
*
* Common legacy text encoding in hardware is 7-bit ASCII fitted into
* a fixed-width byte buffer (bit 7 always zero), right-padded with spaces
* (ASCII 0x20). Convert space-padded ascii to string representation.
*
* If the text does not fit into the destination buffer, the function converts
* as many characters as possible and returns EOVERFLOW.
*
* If the text contains non-ASCII bytes (with bit 7 set), the whole string is
* converted anyway and invalid characters are replaced with question marks
* (U_SPECIAL) and the function returns EIO.
*
* Regardless of return value upon return @a dest will always be well-formed.
*
* @param dest Destination buffer
* @param size Size of destination buffer
* @param src Space-padded ASCII.
* @param n Size of the source buffer in bytes.
*
* @return EOK on success, EOVERFLOW if the text does not fit
* destination buffer, EIO if the text contains
* non-ASCII bytes.
*/
errno_t spascii_to_str(char *dest, size_t size, const uint8_t *src, size_t n)
{
size_t sidx;
size_t didx;
size_t dlast;
uint8_t byte;
errno_t rc;
errno_t result;
/* There must be space for a null terminator in the buffer. */
assert(size > 0);
result = EOK;
didx = 0;
dlast = 0;
for (sidx = 0; sidx < n; ++sidx) {
byte = src[sidx];
if (!ascii_check(byte)) {
byte = U_SPECIAL;
result = EIO;
}
rc = chr_encode(byte, dest, &didx, size - 1);
if (rc != EOK) {
assert(rc == EOVERFLOW);
dest[didx] = '\0';
return rc;
}
/* Remember dest index after last non-empty character */
if (byte != 0x20)
dlast = didx;
}
/* Terminate string after last non-empty character */
dest[dlast] = '\0';
return result;
}
/** Convert wide string to string.
*
* Convert wide string @a src to string. The output is written to the buffer
* specified by @a dest and @a size. @a size must be non-zero and the string
* written will always be well-formed.
*
* @param dest Destination buffer.
* @param size Size of the destination buffer.
* @param src Source wide string.
*/
void wstr_to_str(char *dest, size_t size, const char32_t *src)
{
char32_t ch;
size_t src_idx;
size_t dest_off;
/* There must be space for a null terminator in the buffer. */
assert(size > 0);
src_idx = 0;
dest_off = 0;
while ((ch = src[src_idx++]) != 0) {
if (chr_encode(ch, dest, &dest_off, size - 1) != EOK)
break;
}
dest[dest_off] = '\0';
}