forked from weston-embedded/uC-LIB
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib_str.c
4031 lines (3702 loc) · 196 KB
/
lib_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
/*
*********************************************************************************************************
* uC/LIB
* Custom Library Modules
*
* Copyright 2004-2020 Silicon Laboratories Inc. www.silabs.com
*
* SPDX-License-Identifier: APACHE-2.0
*
* This software is subject to an open source license and is distributed by
* Silicon Laboratories Inc. pursuant to the terms of the Apache License,
* Version 2.0 available at www.apache.org/licenses/LICENSE-2.0.
*
*********************************************************************************************************
*/
/*
*********************************************************************************************************
*
* ASCII STRING MANAGEMENT
*
* Filename : lib_str.c
* Version : V1.39.00
*********************************************************************************************************
* Note(s) : (1) NO compiler-supplied standard library functions are used in library or product software.
*
* (a) ALL standard library functions are implemented in the custom library modules :
*
* (1) \<Custom Library Directory>\lib_*.*
*
* (2) \<Custom Library Directory>\Ports\<cpu>\<compiler>\lib*_a.*
*
* where
* <Custom Library Directory> directory path for custom library software
* <cpu> directory name for specific processor (CPU)
* <compiler> directory name for specific compiler
*
* (b) Product-specific library functions are implemented in individual products.
*
*********************************************************************************************************
* Notice(s) : (1) The Institute of Electrical and Electronics Engineers and The Open Group, have given
* us permission to reprint portions of their documentation. Portions of this text are
* reprinted and reproduced in electronic form from the IEEE Std 1003.1, 2004 Edition,
* Standard for Information Technology -- Portable Operating System Interface (POSIX),
* The Open Group Base Specifications Issue 6, Copyright (C) 2001-2004 by the Institute
* of Electrical and Electronics Engineers, Inc and The Open Group. In the event of any
* discrepancy between these versions and the original IEEE and The Open Group Standard,
* the original IEEE and The Open Group Standard is the referee document. The original
* Standard can be obtained online at http://www.opengroup.org/unix/online.html.
*********************************************************************************************************
*/
/*
*********************************************************************************************************
* INCLUDE FILES
*********************************************************************************************************
*/
#define MICRIUM_SOURCE
#define LIB_STR_MODULE
#include <lib_str.h>
/*
*********************************************************************************************************
* LOCAL DEFINES
*********************************************************************************************************
*/
/*
*********************************************************************************************************
* LOCAL CONSTANTS
*********************************************************************************************************
*/
/*
*********************************************************************************************************
* LOCAL DATA TYPES
*********************************************************************************************************
*/
/*
*********************************************************************************************************
* LOCAL TABLES
*********************************************************************************************************
*/
static const CPU_INT32U Str_MultOvfThTbl_Int32U[] = {
(CPU_INT32U) DEF_INT_32U_MAX_VAL, /* Invalid base 0. */
(CPU_INT32U)(DEF_INT_32U_MAX_VAL / 1u), /* Invalid base 1. */
(CPU_INT32U)(DEF_INT_32U_MAX_VAL / 2u), /* 32-bit mult ovf th for base 2. */
(CPU_INT32U)(DEF_INT_32U_MAX_VAL / 3u), /* 32-bit mult ovf th for base 3. */
(CPU_INT32U)(DEF_INT_32U_MAX_VAL / 4u), /* 32-bit mult ovf th for base 4. */
(CPU_INT32U)(DEF_INT_32U_MAX_VAL / 5u), /* 32-bit mult ovf th for base 5. */
(CPU_INT32U)(DEF_INT_32U_MAX_VAL / 6u), /* 32-bit mult ovf th for base 6. */
(CPU_INT32U)(DEF_INT_32U_MAX_VAL / 7u), /* 32-bit mult ovf th for base 7. */
(CPU_INT32U)(DEF_INT_32U_MAX_VAL / 8u), /* 32-bit mult ovf th for base 8. */
(CPU_INT32U)(DEF_INT_32U_MAX_VAL / 9u), /* 32-bit mult ovf th for base 9. */
(CPU_INT32U)(DEF_INT_32U_MAX_VAL / 10u), /* 32-bit mult ovf th for base 10. */
(CPU_INT32U)(DEF_INT_32U_MAX_VAL / 11u), /* 32-bit mult ovf th for base 11. */
(CPU_INT32U)(DEF_INT_32U_MAX_VAL / 12u), /* 32-bit mult ovf th for base 12. */
(CPU_INT32U)(DEF_INT_32U_MAX_VAL / 13u), /* 32-bit mult ovf th for base 13. */
(CPU_INT32U)(DEF_INT_32U_MAX_VAL / 14u), /* 32-bit mult ovf th for base 14. */
(CPU_INT32U)(DEF_INT_32U_MAX_VAL / 15u), /* 32-bit mult ovf th for base 15. */
(CPU_INT32U)(DEF_INT_32U_MAX_VAL / 16u), /* 32-bit mult ovf th for base 16. */
(CPU_INT32U)(DEF_INT_32U_MAX_VAL / 17u), /* 32-bit mult ovf th for base 17. */
(CPU_INT32U)(DEF_INT_32U_MAX_VAL / 18u), /* 32-bit mult ovf th for base 18. */
(CPU_INT32U)(DEF_INT_32U_MAX_VAL / 19u), /* 32-bit mult ovf th for base 19. */
(CPU_INT32U)(DEF_INT_32U_MAX_VAL / 20u), /* 32-bit mult ovf th for base 20. */
(CPU_INT32U)(DEF_INT_32U_MAX_VAL / 21u), /* 32-bit mult ovf th for base 21. */
(CPU_INT32U)(DEF_INT_32U_MAX_VAL / 22u), /* 32-bit mult ovf th for base 22. */
(CPU_INT32U)(DEF_INT_32U_MAX_VAL / 23u), /* 32-bit mult ovf th for base 23. */
(CPU_INT32U)(DEF_INT_32U_MAX_VAL / 24u), /* 32-bit mult ovf th for base 24. */
(CPU_INT32U)(DEF_INT_32U_MAX_VAL / 25u), /* 32-bit mult ovf th for base 25. */
(CPU_INT32U)(DEF_INT_32U_MAX_VAL / 26u), /* 32-bit mult ovf th for base 26. */
(CPU_INT32U)(DEF_INT_32U_MAX_VAL / 27u), /* 32-bit mult ovf th for base 27. */
(CPU_INT32U)(DEF_INT_32U_MAX_VAL / 28u), /* 32-bit mult ovf th for base 28. */
(CPU_INT32U)(DEF_INT_32U_MAX_VAL / 29u), /* 32-bit mult ovf th for base 29. */
(CPU_INT32U)(DEF_INT_32U_MAX_VAL / 30u), /* 32-bit mult ovf th for base 30. */
(CPU_INT32U)(DEF_INT_32U_MAX_VAL / 31u), /* 32-bit mult ovf th for base 31. */
(CPU_INT32U)(DEF_INT_32U_MAX_VAL / 32u), /* 32-bit mult ovf th for base 32. */
(CPU_INT32U)(DEF_INT_32U_MAX_VAL / 33u), /* 32-bit mult ovf th for base 33. */
(CPU_INT32U)(DEF_INT_32U_MAX_VAL / 34u), /* 32-bit mult ovf th for base 34. */
(CPU_INT32U)(DEF_INT_32U_MAX_VAL / 35u), /* 32-bit mult ovf th for base 35. */
(CPU_INT32U)(DEF_INT_32U_MAX_VAL / 36u) /* 32-bit mult ovf th for base 36. */
};
/*
*********************************************************************************************************
* LOCAL GLOBAL VARIABLES
*********************************************************************************************************
*/
/*
*********************************************************************************************************
* LOCAL FUNCTION PROTOTYPES
*********************************************************************************************************
*/
static CPU_CHAR *Str_FmtNbr_Int32 ( CPU_INT32U nbr,
CPU_INT08U nbr_dig,
CPU_INT08U nbr_base,
CPU_BOOLEAN nbr_neg,
CPU_CHAR lead_char,
CPU_BOOLEAN lower_case,
CPU_BOOLEAN nul,
CPU_CHAR *pstr);
static CPU_INT32U Str_ParseNbr_Int32(const CPU_CHAR *pstr,
CPU_CHAR **pstr_next,
CPU_INT08U nbr_base,
CPU_BOOLEAN nbr_signed,
CPU_BOOLEAN *pnbr_neg);
/*
*********************************************************************************************************
* LOCAL CONFIGURATION ERRORS
*********************************************************************************************************
*/
/*
*********************************************************************************************************
* Str_Len()
*
* Description : Calculate length of a string.
*
* Argument(s) : pstr Pointer to string (see Note #1).
*
* Return(s) : Length of string; number of characters in string before terminating NULL character
* (see Note #2b1).
*
* Caller(s) : Application.
*
* Note(s) : (1) String buffer NOT modified.
*
* (2) (a) IEEE Std 1003.1, 2004 Edition, Section 'strlen() : DESCRIPTION' states that :
*
* (1) "The strlen() function shall compute the number of bytes in the string to
* which 's' ('pstr') points," ...
* (2) "not including the terminating null byte."
*
* (b) IEEE Std 1003.1, 2004 Edition, Section 'strlen() : RETURN VALUE' states that :
*
* (1) "The strlen() function shall return the length of 's' ('pstr');" ...
* (2) "no return value shall be reserved to indicate an error."
*
* (3) String length calculation terminates when :
*
* (a) String pointer points to NULL.
* (1) String buffer overlaps with NULL address.
* (2) String length calculated for string up to but NOT beyond or including
* the NULL address.
*
* (b) Terminating NULL character found.
* (1) String length calculated for string up to but NOT including
* the NULL character (see Note #2a2).
*********************************************************************************************************
*/
CPU_SIZE_T Str_Len (const CPU_CHAR *pstr)
{
CPU_SIZE_T len;
len = Str_Len_N(pstr,
DEF_INT_CPU_U_MAX_VAL);
return (len);
}
/*
*********************************************************************************************************
* Str_Len_N()
*
* Description : Calculate length of a string, up to a maximum number of characters.
*
* Argument(s) : pstr Pointer to string (see Note #1).
*
* len_max Maximum number of characters to search (see Note #3c).
*
* Return(s) : Length of string; number of characters in string before terminating NULL character,
* if terminating NULL character found (see Note #2b1).
*
* Requested maximum number of characters to search,
* if terminating NULL character NOT found (see Note #3c).
*
* Caller(s) : Application.
*
* Note(s) : (1) String buffer NOT modified.
*
* (2) (a) IEEE Std 1003.1, 2004 Edition, Section 'strlen() : DESCRIPTION' states that :
*
* (1) "The strlen() function shall compute the number of bytes in the string to
* which 's' ('pstr') points," ...
* (2) "not including the terminating null byte."
*
* (b) IEEE Std 1003.1, 2004 Edition, Section 'strlen() : RETURN VALUE' states that :
*
* (1) "The strlen() function shall return the length of 's' ('pstr');" ...
* (2) "no return value shall be reserved to indicate an error."
*
* (3) String length calculation terminates when :
*
* (a) String pointer points to NULL.
* (1) String buffer overlaps with NULL address.
* (2) String length calculated for string up to but NOT beyond or including
* the NULL address.
*
* (b) Terminating NULL character found.
* (1) String length calculated for string up to but NOT including
* the NULL character (see Note #2a2).
*
* (c) 'len_max' number of characters searched.
* (1) 'len_max' number of characters does NOT include the terminating NULL character.
*********************************************************************************************************
*/
CPU_SIZE_T Str_Len_N (const CPU_CHAR *pstr,
CPU_SIZE_T len_max)
{
const CPU_CHAR *pstr_len;
CPU_SIZE_T len;
pstr_len = pstr;
len = 0u;
while (( pstr_len != (const CPU_CHAR *) 0 ) && /* Calc str len until NULL ptr (see Note #3a) ... */
(*pstr_len != ( CPU_CHAR )'\0') && /* ... or NULL char found (see Note #3b) ... */
( len < ( CPU_SIZE_T)len_max)) { /* ... or max nbr chars srch'd (see Note #3c). */
pstr_len++;
len++;
}
return (len); /* Rtn str len (see Note #3b1). */
}
/*
*********************************************************************************************************
* Str_Copy()
*
* Description : Copy source string to destination string buffer.
*
* Argument(s) : pstr_dest Pointer to destination string buffer to receive source string copy (see Note #1a).
*
* pstr_src Pointer to source string to copy into destination string buffer (see Note #1b).
*
* Return(s) : Pointer to destination string, if NO error(s) [see Note #2b1].
*
* Pointer to NULL, otherwise (see Note #2b2A).
*
* Caller(s) : Application.
*
* Note(s) : (1) (a) Destination buffer size NOT validated; buffer overruns MUST be prevented by caller.
*
* (1) Destination buffer size MUST be large enough to accommodate the entire source
* string size including the terminating NULL character.
*
* (b) Source buffer NOT modified.
*
* (2) (a) IEEE Std 1003.1, 2004 Edition, Section 'strcpy() : DESCRIPTION' states that :
*
* (1) "The strcpy() function shall copy the string pointed to by 's2' ('pstr_src')
* ... into the array pointed to by 's1' ('pstr_dest')" ...
* (2) "(including the terminating null byte)."
*
* (b) IEEE Std 1003.1, 2004 Edition, Section 'strcpy() : RETURN VALUE' states that :
*
* (1) "The strcpy() function shall return 's1' ('pstr_dest');" ...
* (2) "no return value is reserved to indicate an error."
* (A) #### This requirement is intentionally NOT implemented in order to return
* NULL for any error(s).
*
* (c) IEEE Std 1003.1, 2004 Edition, Section 'strcpy() : DESCRIPTION' states that "if
* copying takes place between objects that overlap, the behavior is undefined".
*
* (3) String copy terminates when :
*
* (a) Destination/Source string pointer(s) are passed NULL pointers.
* (1) No string copy performed; NULL pointer returned.
*
* (b) Destination/Source string pointer(s) point to NULL.
* (1) String buffer(s) overlap with NULL address; NULL pointer returned.
*
* (c) Source string's terminating NULL character found.
* (1) Entire source string copied into destination string buffer (see Note #2a).
*********************************************************************************************************
*/
CPU_CHAR *Str_Copy ( CPU_CHAR *pstr_dest,
const CPU_CHAR *pstr_src)
{
CPU_CHAR *pstr_rtn;
pstr_rtn = Str_Copy_N(pstr_dest,
pstr_src,
DEF_INT_CPU_U_MAX_VAL);
return (pstr_rtn);
}
/*
*********************************************************************************************************
* Str_Copy_N()
*
* Description : Copy source string to destination string buffer, up to a maximum number of characters.
*
* Argument(s) : pstr_dest Pointer to destination string buffer to receive source string copy (see Note #1a).
*
* pstr_src Pointer to source string to copy into destination string buffer (see Note #1b).
*
* len_max Maximum number of characters to copy (see Notes #2a2 & #3d).
*
* Return(s) : Pointer to destination string, if NO error(s) [see Note #2b1].
*
* Pointer to NULL, otherwise (see Note #2b2A).
*
* Caller(s) : Application.
*
* Note(s) : (1) (a) Destination buffer size NOT validated; buffer overruns MUST be prevented by caller.
*
* (1) Destination buffer size MUST be large enough to accommodate the entire source
* string size including the terminating NULL character.
*
* (b) Source string buffer NOT modified.
*
* (2) (a) (1) IEEE Std 1003.1, 2004 Edition, Section 'strncpy() : DESCRIPTION' states that :
*
* (A) "The strncpy() function shall copy ... the array pointed to by 's2'
* ('pstr_src') to the array pointed to by 's1' ('pstr_dest')"; ...
* (B) but "not more than 'n' ('len_max') bytes" ...
* (C) & "(bytes that follow a null byte are not copied)".
*
* (2) (A) IEEE Std 1003.1, 2004 Edition, Section 'strncpy() : DESCRIPTION' adds that
* "if the array pointed to by 's2' ('pstr_src') is a string that is shorter
* than 'n' ('len_max') bytes, null bytes shall be appended to the copy in
* the array pointed to by 's1' ('pstr_dest'), until 'n' ('len_max') bytes
* in all are written."
*
* (1) #### Since Str_Copy() limits the maximum number of characters to copy
* via Str_Copy_N() by the CPU's maximum number of addressable characters,
* this requirement is intentionally NOT implemented to avoid appending
* a potentially large number of unnecessary terminating NULL characters.
*
* (B) IEEE Std 1003.1, 2004 Edition, Section 'strncpy() : APPLICATION USAGE' also
* states that "if there is no null byte in the first 'n' ('len_max') bytes of
* the array pointed to by 's2' ('pstr_src'), the result is not null-terminated".
*
* (b) IEEE Std 1003.1, 2004 Edition, Section 'strncpy() : RETURN VALUE' states that :
*
* (1) "The strncpy() function shall return 's1' ('pstr_dest');" ...
* (2) "no return value is reserved to indicate an error."
* (A) #### This requirement is intentionally ignored in order to return NULL
* for any error(s).
*
* (c) IEEE Std 1003.1, 2004 Edition, Section 'strncpy() : DESCRIPTION' states that "if
* copying takes place between objects that overlap, the behavior is undefined".
*
* (3) String copy terminates when :
*
* (a) Destination/Source string pointer(s) are passed NULL pointers.
* (1) No string copy performed; NULL pointer returned.
*
* (b) Destination/Source string pointer(s) point to NULL.
* (1) String buffer(s) overlap with NULL address; NULL pointer returned.
*
* (c) Source string's terminating NULL character found.
* (1) Entire source string copied into destination string buffer (see Note #2a1A).
*
* (d) 'len_max' number of characters copied.
* (1) 'len_max' number of characters MAY include the terminating NULL character
* (see Note #2a1C).
* (2) Null copies allowed (i.e. zero-length copies).
* (A) No string copy performed; destination string returned (see Note #2b1).
*********************************************************************************************************
*/
CPU_CHAR *Str_Copy_N ( CPU_CHAR *pstr_dest,
const CPU_CHAR *pstr_src,
CPU_SIZE_T len_max)
{
CPU_CHAR *pstr_copy_dest;
const CPU_CHAR *pstr_copy_src;
CPU_SIZE_T len_copy;
/* Rtn NULL if str ptr(s) NULL (see Note #3a1). */
if (pstr_dest == (CPU_CHAR *)0) {
return ((CPU_CHAR *)0);
}
if (pstr_src == (const CPU_CHAR *)0) {
return ((CPU_CHAR *)0);
}
pstr_copy_dest = pstr_dest;
pstr_copy_src = pstr_src;
len_copy = 0u;
while (( pstr_copy_dest != ( CPU_CHAR *) 0 ) && /* Copy str until NULL ptr(s) [see Note #3b] ... */
( pstr_copy_src != (const CPU_CHAR *) 0 ) &&
(*pstr_copy_src != ( CPU_CHAR )'\0') && /* ... or NULL char found (see Note #3c); ... */
( len_copy < ( CPU_SIZE_T)len_max)) { /* ... or max nbr chars copied (see Note #3d). */
*pstr_copy_dest = *pstr_copy_src;
pstr_copy_dest++;
pstr_copy_src++;
len_copy++;
}
/* Rtn NULL if NULL ptr(s) found (see Note #3b1). */
if ((pstr_copy_dest == ( CPU_CHAR *)0) ||
(pstr_copy_src == (const CPU_CHAR *)0)) {
return ((CPU_CHAR *)0);
}
if (len_copy < len_max) { /* If copy str len < max buf len (see Note #2a2A), ... */
*pstr_copy_dest = (CPU_CHAR)'\0'; /* ... copy NULL char (see Note #3c1). */
}
return (pstr_dest); /* Rtn ptr to dest str (see Note #2b1). */
}
/*
*********************************************************************************************************
* Str_Cat()
*
* Description : Append concatenation string to destination string.
*
* Argument(s) : pstr_dest Pointer to destination string to append concatenation string (see Note #1a).
*
* pstr_cat Pointer to concatenation string to append to destination string (see Note #1b).
*
* Return(s) : Pointer to destination string, if NO error(s) [see Note #2b1].
*
* Pointer to NULL, otherwise (see Note #2b2A).
*
* Caller(s) : Application.
*
* Note(s) : (1) (a) Destination buffer size NOT validated; buffer overruns MUST be prevented by caller.
*
* (1) Destination buffer size MUST be large enough to accommodate the entire
* concatenated string size including the terminating NULL character.
*
* (b) Concatenation string buffer NOT modified.
*
* (2) (a) IEEE Std 1003.1, 2004 Edition, Section 'strcat() : DESCRIPTION' states that :
*
* (1) "The strcat() function shall append a copy of the string pointed to by 's2'
* ('pstr_cat') ... to the end of the string pointed to by 's1' ('pstr_dest')."
*
* (2) (A) "The initial byte of 's2' ('pstr_cat') overwrites the null byte at the
* end of 's1' ('pstr_dest')."
* (B) A "terminating null byte" is appended at the end of the concatenated
* destination strings.
*
* (b) IEEE Std 1003.1, 2004 Edition, Section 'strcat() : RETURN VALUE' states that :
*
* (1) "The strcat() function shall return 's1' ('pstr_dest');" ...
* (2) "no return value shall be reserved to indicate an error."
* (A) #### This requirement is intentionally NOT implemented in order to return
* NULL for any error(s).
*
* (c) IEEE Std 1003.1, 2004 Edition, Section 'strcat() : DESCRIPTION' states that "if
* copying takes place between objects that overlap, the behavior is undefined."
*
* (3) String concatenation terminates when :
*
* (a) Destination/Concatenation string pointer(s) are passed NULL pointers.
* (1) No string concatenation performed; NULL pointer returned.
*
* (b) Destination/Concatenation string pointer(s) point to NULL.
* (1) String buffer(s) overlap with NULL address; NULL pointer returned.
*
* (c) Concatenation string's terminating NULL character found.
* (1) Entire concatenation string appended to destination string (see Note #2a1).
*********************************************************************************************************
*/
CPU_CHAR *Str_Cat ( CPU_CHAR *pstr_dest,
const CPU_CHAR *pstr_cat)
{
CPU_CHAR *pstr_rtn;
pstr_rtn = Str_Cat_N(pstr_dest,
pstr_cat,
DEF_INT_CPU_U_MAX_VAL);
return (pstr_rtn);
}
/*
*********************************************************************************************************
* Str_Cat_N()
*
* Description : Append concatenation string to destination string, up to a maximum number of characters.
*
* Argument(s) : pstr_dest Pointer to destination string to append concatenation string (see Note #1a).
*
* pstr_cat Pointer to concatenation string to append to destination string (see Note #1b).
*
* len_max Maximum number of characters to concatenate (see Notes #2a1B & #3d).
*
* Return(s) : Pointer to destination string, if NO error(s) [see Note #2b1].
*
* Pointer to NULL, otherwise (see Note #2b2A).
*
* Caller(s) : Application.
*
* Note(s) : (1) (a) Destination buffer size NOT validated; buffer overruns MUST be prevented by caller.
*
* (1) Destination buffer size MUST be large enough to accommodate the entire
* concatenated string size including the terminating NULL character.
*
* (b) Concatenation string buffer NOT modified.
*
* (2) (a) IEEE Std 1003.1, 2004 Edition, Section 'strncat() : DESCRIPTION' states that :
*
* (1) (A) "The strncat() function shall append ... the array pointed to by 's2'
* ('pstr_cat') to the end of the string pointed to by 's1' ('pstr_dest')" ...
* (B) but "not more than 'n' ('len_max') bytes".
*
* (2) (A) "The initial byte of 's2' ('pstr_cat') overwrites the null byte at the
* end of 's1' ('pstr_dest')."
* (B) "(a null byte and bytes that follow it are not appended)."
* (C) "A terminating null byte is always appended to the result."
*
* (b) IEEE Std 1003.1, 2004 Edition, Section 'strncat() : RETURN VALUE' states that :
*
* (1) "The strncat() function shall return 's1' ('pstr_dest');" ...
* (2) "no return value shall be reserved to indicate an error."
* (A) #### This requirement is intentionally NOT implemented in order to return
* NULL for any error(s).
*
* (c) IEEE Std 1003.1, 2004 Edition, Section 'strncat() : DESCRIPTION' states that "if
* copying takes place between objects that overlap, the behavior is undefined."
*
* (3) String concatenation terminates when :
*
* (a) Destination/Concatenation string pointer(s) are passed NULL pointers.
* (1) No string concatenation performed; NULL pointer returned.
*
* (b) Destination/Concatenation string pointer(s) point to NULL.
* (1) String buffer(s) overlap with NULL address; NULL pointer returned.
*
* (c) Concatenation string's terminating NULL character found.
* (1) Entire concatenation string appended to destination string (see Note #2a1A).
*
* (d) 'len_max' number of characters concatenated.
*
* (1) 'len_max' number of characters does NOT include the terminating NULL character
* (see Note #2a2).
*
* (2) Null concatenations allowed (i.e. zero-length concatenations).
* (A) No string concatenation performed; destination string returned
* (see Note #2b1).
*********************************************************************************************************
*/
CPU_CHAR *Str_Cat_N ( CPU_CHAR *pstr_dest,
const CPU_CHAR *pstr_cat,
CPU_SIZE_T len_max)
{
CPU_CHAR *pstr_cat_dest;
const CPU_CHAR *pstr_cat_src;
CPU_SIZE_T len_cat;
/* Rtn NULL if str ptr(s) NULL (see Note #3a1). */
if (pstr_dest == (CPU_CHAR *)0) {
return ((CPU_CHAR *)0);
}
if (pstr_cat == (const CPU_CHAR *)0) {
return ((CPU_CHAR *)0);
}
if (len_max < 1) { /* Rtn dest str if cat len = 0 (see Note #3d2A). */
return ((CPU_CHAR *)pstr_dest);
}
pstr_cat_dest = pstr_dest;
while (( pstr_cat_dest != (CPU_CHAR *) 0 ) && /* Adv to end of cur dest str until NULL ptr ... */
(*pstr_cat_dest != (CPU_CHAR )'\0')) { /* ... or NULL char found.. */
pstr_cat_dest++;
}
if (pstr_cat_dest == (CPU_CHAR *)0) { /* Rtn NULL if NULL ptr found (see Note #3b1). */
return ((CPU_CHAR *)0);
}
pstr_cat_src = pstr_cat;
len_cat = 0u;
while (( pstr_cat_dest != ( CPU_CHAR *) 0 ) && /* Cat str until NULL ptr(s) [see Note #3b] ... */
( pstr_cat_src != (const CPU_CHAR *) 0 ) &&
(*pstr_cat_src != ( CPU_CHAR )'\0') && /* ... or NULL char found (see Note #3c); ... */
( len_cat < ( CPU_SIZE_T)len_max)) { /* ... or max nbr chars cat'd (see Note #3d). */
*pstr_cat_dest = *pstr_cat_src;
pstr_cat_dest++;
pstr_cat_src++;
len_cat++;
}
/* Rtn NULL if NULL ptr(s) found (see Note #3b1). */
if ((pstr_cat_dest == ( CPU_CHAR *)0) ||
(pstr_cat_src == (const CPU_CHAR *)0)) {
return ((CPU_CHAR *)0);
}
*pstr_cat_dest = (CPU_CHAR)'\0'; /* Append NULL char (see Note #2a2C). */
return (pstr_dest); /* Rtn ptr to dest str (see Note #2b1). */
}
/*
*********************************************************************************************************
* Str_Cmp()
*
* Description : Determine if two strings are identical.
*
* Argument(s) : p1_str Pointer to first string (see Note #1).
*
* p2_str Pointer to second string (see Note #1).
*
* Return(s) : 0, if strings are identical (see Notes #3a1A, #3a2A, & #3b).
*
* Negative value, if 'p1_str' is less than 'p2_str' (see Notes #3a1B1, #3a2B1, & #3c).
*
* Positive value, if 'p1_str' is greater than 'p2_str' (see Notes #3a1B2, #3a2B2, & #3c).
*
* See also Note #2b.
*
* Caller(s) : Application.
*
* Note(s) : (1) String buffers NOT modified.
*
* (2) (a) IEEE Std 1003.1, 2004 Edition, Section 'strcmp() : DESCRIPTION' states that "the
* strcmp() function shall compare the string pointed to by 's1' ('p1_str') to the
* string pointed to by 's2' ('p2_str)".
*
* (b) (1) IEEE Std 1003.1, 2004 Edition, Section 'strcmp() : RETURN VALUE' states that
* "upon successful completion, strcmp() shall return an integer greater than,
* equal to, or less than 0".
*
* (2) IEEE Std 1003.1, 2004 Edition, Section 'strcmp() : DESCRIPTION' adds that "the
* sign of a non-zero return value shall be determined by the sign of the difference
* between the values of the first pair of bytes ... that differ in the strings
* being compared".
*
* (3) String comparison terminates when :
*
* (a) (1) (A) BOTH string pointer(s) are passed NULL pointers.
* (1) NULL strings identical; 0 returned.
*
* (B) (1) 'p1_str' passed a NULL pointer.
* (a) Return negative value of character pointed to by 'p2_str'.
*
* (2) 'p2_str' passed a NULL pointer.
* (a) Return positive value of character pointed to by 'p1_str'.
*
* (2) (A) BOTH strings point to NULL.
* (1) Strings overlap with NULL address.
* (2) Strings identical up to but NOT beyond or including the NULL address;
* 0 returned.
*
* (B) (1) 'p1_str_cmp_next' points to NULL.
* (a) 'p1_str' overlaps with NULL address.
* (b) Strings compared up to but NOT beyond or including the NULL address.
* (c) Return negative value of character pointed to by 'p2_str_cmp_next'.
*
* (2) 'p2_str_cmp_next' points to NULL.
* (a) 'p2_str' overlaps with NULL address.
* (b) Strings compared up to but NOT beyond or including the NULL address.
* (c) Return positive value of character pointed to by 'p1_str_cmp_next'.
*
* (b) Terminating NULL character found in both strings.
* (1) Strings identical; 0 returned.
* (2) Only one NULL character test required in conditional since previous condition
* tested character equality.
*
* (c) Non-matching characters found.
* (1) Return signed-integer difference of the character pointed to by 'p2_str'
* from the character pointed to by 'p1_str'.
*
* (4) Since 16-bit signed arithmetic is performed to calculate a non-identical comparison
* return value, 'CPU_CHAR' native data type size MUST be 8-bit.
*********************************************************************************************************
*/
CPU_INT16S Str_Cmp (const CPU_CHAR *p1_str,
const CPU_CHAR *p2_str)
{
CPU_INT16S cmp_val;
cmp_val = Str_Cmp_N(p1_str,
p2_str,
DEF_INT_CPU_U_MAX_VAL);
return (cmp_val);
}
/*
*********************************************************************************************************
* Str_Cmp_N()
*
* Description : Determine if two strings are identical for up to a maximum number of characters.
*
* Argument(s) : p1_str Pointer to first string (see Note #1).
*
* p2_str Pointer to second string (see Note #1).
*
* len_max Maximum number of characters to compare (see Note #3d).
*
* Return(s) : 0, if strings are identical (see Notes #3a1A, #3a2A, #3b, & #3d).
*
* Negative value, if 'p1_str' is less than 'p2_str' (see Notes #3a1B1, #3a2B1, & #3c).
*
* Positive value, if 'p1_str' is greater than 'p2_str' (see Notes #3a1B2, #3a2B2, & #3c).
*
* See also Note #2b.
*
* Caller(s) : Application.
*
* Note(s) : (1) String buffers NOT modified.
*
* (2) (a) IEEE Std 1003.1, 2004 Edition, Section 'strncmp() : DESCRIPTION' states that :
*
* (1) "The strncmp() function shall compare ... the array pointed to by 's1' ('p1_str')
* to the array pointed to by 's2' ('p2_str)" ...
* (2) but "not more than 'n' ('len_max') bytes" of either array.
*
* (b) (1) IEEE Std 1003.1, 2004 Edition, Section 'strncmp() : RETURN VALUE' states that
* "upon successful completion, strncmp() shall return an integer greater than,
* equal to, or less than 0".
*
* (2) IEEE Std 1003.1, 2004 Edition, Section 'strncmp() : DESCRIPTION' adds that
* "the sign of a non-zero return value is determined by the sign of the difference
* between the values of the first pair of bytes ... that differ in the strings
* being compared".
*
* (3) String comparison terminates when :
*
* (a) (1) (A) BOTH string pointer(s) are passed NULL pointers.
* (1) NULL strings identical; 0 returned.
*
* (B) (1) 'p1_str' passed a NULL pointer.
* (a) Return negative value of character pointed to by 'p2_str'.
*
* (2) 'p2_str' passed a NULL pointer.
* (a) Return positive value of character pointed to by 'p1_str'.
*
* (2) (A) BOTH strings point to NULL.
* (1) Strings overlap with NULL address.
* (2) Strings identical up to but NOT beyond or including the NULL address;
* 0 returned.
*
* (B) (1) 'p1_str_cmp_next' points to NULL.
* (a) 'p1_str' overlaps with NULL address.
* (b) Strings compared up to but NOT beyond or including the NULL address.
* (c) Return negative value of character pointed to by 'p2_str_cmp_next'.
*
* (2) 'p2_str_cmp_next' points to NULL.
* (a) 'p2_str' overlaps with NULL address.
* (b) Strings compared up to but NOT beyond or including the NULL address.
* (c) Return positive value of character pointed to by 'p1_str_cmp_next'.
*
* (b) Terminating NULL character found in both strings.
* (1) Strings identical; 0 returned.
* (2) Only one NULL character test required in conditional since previous condition
* tested character equality.
*
* (c) Non-matching characters found.
* (1) Return signed-integer difference of the character pointed to by 'p2_str'
* from the character pointed to by 'p1_str'.
*
* (d) (1) 'len_max' passed a zero length.
* (A) Zero-length strings identical; 0 returned.
*
* (2) First 'len_max' number of characters identical.
* (A) Strings identical; 0 returned.
*
* See also Note #2a2.
*
* (4) Since 16-bit signed arithmetic is performed to calculate a non-identical comparison
* return value, 'CPU_CHAR' native data type size MUST be 8-bit.
*********************************************************************************************************
*/
CPU_INT16S Str_Cmp_N (const CPU_CHAR *p1_str,
const CPU_CHAR *p2_str,
CPU_SIZE_T len_max)
{
const CPU_CHAR *p1_str_cmp;
const CPU_CHAR *p2_str_cmp;
const CPU_CHAR *p1_str_cmp_next;
const CPU_CHAR *p2_str_cmp_next;
CPU_INT16S cmp_val;
CPU_SIZE_T cmp_len;
if (len_max < 1) { /* If cmp len = 0, rtn 0 (see Note #3d1A). */
return (0);
}
if (p1_str == (const CPU_CHAR *)0) {
if (p2_str == (const CPU_CHAR *)0) {
return (0); /* If BOTH str ptrs NULL, rtn 0 (see Note #3a1A). */
}
cmp_val = (CPU_INT16S)((CPU_INT16S)0 - (CPU_INT16S)(*p2_str));
return (cmp_val); /* If p1_str NULL, rtn neg p2_str val (see Note #3a1B1).*/
}
if (p2_str == (const CPU_CHAR *)0) {
cmp_val = (CPU_INT16S)(*p1_str);
return (cmp_val); /* If p2_str NULL, rtn pos p1_str val (see Note #3a1B2).*/
}
p1_str_cmp = p1_str;
p2_str_cmp = p2_str;
p1_str_cmp_next = p1_str_cmp;
p2_str_cmp_next = p2_str_cmp;
p1_str_cmp_next++;
p2_str_cmp_next++;
cmp_len = 0u;
while ((*p1_str_cmp == *p2_str_cmp) && /* Cmp strs until non-matching chars (see Note #3c) ... */
(*p1_str_cmp != ( CPU_CHAR )'\0') && /* ... or NULL chars (see Note #3b) ... */
( p1_str_cmp_next != (const CPU_CHAR *) 0 ) && /* ... or NULL ptr(s) found (see Note #3a2). */
( p2_str_cmp_next != (const CPU_CHAR *) 0 ) &&
( cmp_len < ( CPU_SIZE_T)len_max)) { /* ... or max nbr chars cmp'd (see Note #3d2). */
p1_str_cmp++;
p2_str_cmp++;
p1_str_cmp_next++;
p2_str_cmp_next++;
cmp_len++;
}
if (cmp_len == len_max) { /* If strs identical for max len nbr of chars, ... */
return (0); /* ... rtn 0 (see Note #3d2A). */
}
if (*p1_str_cmp != *p2_str_cmp) { /* If strs NOT identical, ... */
/* ... calc & rtn char diff (see Note #3c1). */
cmp_val = (CPU_INT16S)((CPU_INT16S)(*p1_str_cmp) - (CPU_INT16S)(*p2_str_cmp));
} else if (*p1_str_cmp == (CPU_CHAR)'\0') { /* If NULL char(s) found, ... */
cmp_val = (CPU_INT16S)0; /* ... strs identical; rtn 0 (see Note #3b). */
} else {
if (p1_str_cmp_next == (const CPU_CHAR *)0) {
if (p2_str_cmp_next == (const CPU_CHAR *)0) { /* If BOTH next str ptrs NULL, ... */
cmp_val = (CPU_INT16S)0; /* ... rtn 0 (see Note #3a2A). */
} else { /* If p1_str_cmp_next NULL, ... */
/* ... rtn neg p2_str_cmp_next val (see Note #3a2B1). */
cmp_val = (CPU_INT16S)((CPU_INT16S)0 - (CPU_INT16S)(*p2_str_cmp_next));
}
} else { /* If p2_str_cmp_next NULL, ... */
cmp_val = (CPU_INT16S)(*p1_str_cmp_next); /* ... rtn pos p1_str_cmp_next val (see Note #3a2B2). */
}
}
return (cmp_val);
}
/*
*********************************************************************************************************
* Str_CmpIgnoreCase()
*
* Description : Determine if two strings are identical, ignoring case.
*
* Argument(s) : p1_str Pointer to first string (see Note #1).
*
* p2_str Pointer to second string (see Note #1).
*
* Return(s) : 0, if strings are identical (see Notes #3a1A, #3a2A, & #3b).
*
* Negative value, if 'p1_str' is less than 'p2_str' (see Notes #3a1B1, #3a2B1, & #3c).
*
* Positive value, if 'p1_str' is greater than 'p2_str' (see Notes #3a1B2, #3a2B2, & #3c).
*
* See also Note #2b.
*
* Caller(s) : Application.
*
* Note(s) : (1) String buffers NOT modified.
*
* (2) (a) IEEE Std 1003.1, 2004 Edition, Section 'strcasecmp() : DESCRIPTION' states that :
*
* (1) (A) "The strcasecmp() function shall compare ... the string pointed to by 's1'
* ('p1_str') to the string pointed to by 's2' ('p2_str')" ...
* (B) "ignoring differences in case".
*
* (2) "strcasecmp() ... shall behave as if the strings had been converted to lowercase
* and then a byte comparison performed."
*
* (b) (1) IEEE Std 1003.1, 2004 Edition, Section 'strcasecmp() : RETURN VALUE' states that
* "upon successful completion, strcasecmp() shall return an integer greater than,
* equal to, or less than 0".
*
* (2) IEEE Std 1003.1, 2004 Edition, Section 'strcmp() : DESCRIPTION' adds that "the
* sign of a non-zero return value shall be determined by the sign of the difference
* between the values of the first pair of bytes ... that differ in the strings
* being compared".
*
* (3) String comparison terminates when :
*
* (a) (1) (A) BOTH string pointer(s) are passed NULL pointers.
* (1) NULL strings identical; 0 returned.
*
* (B) (1) 'p1_str' passed a NULL pointer.
* (a) Return negative value of character pointed to by 'p2_str', converted
* to lower case (see Note #2a2).
*
* (2) 'p2_str' passed a NULL pointer.
* (a) Return positive value of character pointed to by 'p1_str', converted
* to lower case (see Note #2a2).
*
* (2) (A) BOTH strings point to NULL.
* (1) Strings overlap with NULL address.
* (2) Strings identical up to but NOT beyond or including the NULL address;
* 0 returned.
*
* (B) (1) 'p1_str_cmp_next' points to NULL.
* (a) 'p1_str' overlaps with NULL address.
* (b) Strings compared up to but NOT beyond or including the NULL address.
* (c) Return negative value of character pointed to by 'p2_str_cmp_next',
* converted to lower case (see Note #2a2).
*
* (2) 'p2_str_cmp_next' points to NULL.
* (a) 'p2_str' overlaps with NULL address.
* (b) Strings compared up to but NOT beyond or including the NULL address.
* (c) Return positive value of character pointed to by 'p1_str_cmp_next',
* converted to lower case (see Note #2a2).
*
* (b) Terminating NULL character found in both strings.
* (1) Strings identical; 0 returned.
* (2) Only one NULL character test required in conditional since previous condition
* tested character equality.
*
* (c) Non-matching characters found.
* (1) Return signed-integer difference of the character pointed to by 'p2_str',