-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Stringf.hpp
1654 lines (1486 loc) · 46.3 KB
/
Stringf.hpp
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 Kabuki Starship™ <kabukistarship.com>.
#pragma once
#ifndef SCRIPT2_STRINGF_CODE
#define SCRIPT2_STRINGF_CODE
#include "Stringf.h"
//
#include "Binary.hpp"
namespace _ {
/*
inline IUA ValueBase2(IUC value) {
// Example: 0b0000_0000_0000_0000_0111_0111_1001_1101
return 0;
} */
/* Gets the log_2 of the sizeof(T).
Naming Convension:
ISN = Integer Signed N-Bit = int and A=8-bit, B=16-bit, ...
O = 64-bits
N = 32-bits
M = 16-bits
L = 8-bits
SCRIPT Spec does not support 128-bit CPUs.
*/
template<typename T>
constexpr DTB CASizeCodef() {
switch (sizeof(T)) {
case 1: return 0; // A
case 2: return 1; // B
case 4: return 2; // C
case 8: return 3; // D
case 16: return 4; // E
case 32: return 5; // F
case 64: return 6; // G
case 128: return 7; // H
case 256: return 8; // I 2^8=256
case 512: return 9; // J
case 1024: return 10; // K
}
return 0;
}
// Gets the ASCII Size Width letter A-K
template<typename T>
constexpr CHA CSizeCodef() {
return 'A' + CHA(CASizeCodef<T>());
}
// Gets the ASCII Size Width letter A-K
inline CHA ASizeCodef(ISA sw) {
return 'A' + CHA(sw);
}
inline CHA ASizeCodef(ISB sw) {
return 'A' + CHA(sw);
}
inline CHA ASizeCodef(ISC sw) {
return 'A' + CHA(sw);
}
inline CHA ASizeCodef(ISD sw) {
return 'A' + CHA(sw);
}
//
template<typename T>
constexpr Sizef CSizef() {
return { -CASizeCodef<T>() }; // dez nutz.
}
template<typename IS>
const CHA* TSTAATypesPOD(IS type) {
auto strings = STAATypesPOD();
auto total = ATypePODCount;
type = (type < 0 || type > total) ? total : type;
return &strings[type << 2];
}
template<typename IS>
const CHA* TSTRATypesVector(IS value) {
auto strings = STRATypesVector();
auto value_max = 15;
value = (value < 0 || value > value_max) ? value_max : value;
return &strings[value << 2];
}
template<typename IS>
const CHA* TSTRATypesVectorClass(IS value) {
auto strings = STRATypesVectorClass();
auto value_max = 15;
value = (value < 0 || value > value_max) ? value_max : value;
return &strings[value << 2];
}
template<typename IS>
const CHA* TSTRATypesModifier(IS value) {
auto strings = STRATypesModifier();
auto value_max = 15;
value = (value < 0 || value > value_max) ? value_max : value;
return &strings[value << 2];
}
template<typename IS>
const CHA* TSTRATypesString(IS value) {
auto strings = STRATypesString();
auto value_max = 15;
value = (value < 0 || value > value_max) ? value_max : value;
return &strings[value << 2];
}
/* Gets one of the STRATypes. */
inline const CHA* STAATypePOD(DTW index) {
if (index < 0 || index >= Invalid) index = 32;
return STAATypesPOD() + (index << 2); // << 2 to * 4
}
/* Gets one f the STRATypes. */
inline const CHA* STAATypeVector(DTW index) {
if (index < 0 || index >= 4) index = 0;
return STRATypesVector() + (index << 2); // << 2 to * 4
}
/* Gets one f the STRATypes. */
inline const CHA* STAATypeVectorClasses(DTW index) {
if (index < 0 || index >= 16) index = 16;
return STRATypesVectorClass() + (index << 2); // << 2 to * 4
}
/* Gets one f the STRATypes. */
inline const CHA* STAATypeMap(DTW index) {
if (index < 0 || index >= 4) index = 0;
return STRAATypesMap() + (index << 2); // << 2 to * 4
}
/* Gets one f the STRATypes. */
inline const CHA* STAATypeModifier(DTW index) {
if (index < 0 || index >= 4) index = 0;
return STRATypesModifier() + (index << 2); // << 2 to * 4
}
inline ISN STRLength(IUA value) {
if (value < 10) return 1;
if (value < 100) return 2;
return 3;
}
inline ISN STRLength(ISA value) {
if (value < 0) return STRLength((IUA)-value) + 1;
return STRLength((IUA)value);
}
/* Compares the two Strings up to the given delimiter.
@param delimiter Delimiters in Script2 are equal to or less than.
@return 0 if the Strings are equal or a non-zero delta upon failure. */
template<typename CHT = CHR>
ISN TSTRCompare(const CHT* string, const CHT* other_String, CHT delimiter = 0) {
if (!string || !other_String) return 0;
ISN a, b, result;
if (!string) {
if (!other_String) return 0;
return ISN(*string);
}
if (!other_String) return 0 - *string;
a = *string;
b = *other_String;
if (!a) {
if (!b) return 0;
return b;
}
if (!b) {
if (!a) return 0;
return 0 - a;
}
// other_String SHOULD be a nil-terminated string without whitespace.
while (b) {
result = b - a;
if (result) {
return result;
}
if (a <= (ISC)delimiter) {
return result;
}
++string;
++other_String;
a = *string;
b = *other_String;
}
if (a > (ISC)delimiter) {
return b - a;
}
return 0;
}
/* Scrolls over to the next FPD quote mark.
@warning This function is only safe to use on ROM Strings with a nil-term
CHA. */
template<typename CHT = CHR>
inline const CHT* TSTREnd(const CHT* string) {
while (*string++);
return string - 1;
}
/* Scrolls over to the next FPD quote mark.
@warning This function is only safe to use on ROM Strings with a nil-term
CHA. */
template<typename CHT = CHR>
inline CHT* TSTREnd(CHT* string) {
return const_cast<CHT*>(TSTREnd<CHT>(TPtr<const CHT>(string)));
}
/* Finds the last instance of the given token character in the string.. */
template<typename CHT = CHR>
inline const CHT* TSTRFindLast(const CHT* string, CHA token) {
CHT* last_instance = nullptr;
CHT c = *string;
while (c) {
if (c == token) last_instance = string;
c = *(++string);
}
return last_instance;
}
/* Gets the length of the given CHA.
@todo Update function for UTF-8 and UTF-16.
@return Returns -1 if the text CHA is nil.*/
template<typename CHT = CHR, typename IS = ISR>
IS TSTRLength(const CHT* string) {
return (IS)(TSTREnd<CHT>(string) - string);
}
/* Gets the length of the given CHA.
@return Returns -1 if the text CHA is nil.
@warning This function is only safe to use on ROM Strings with a nil-term
CHA. */
template<typename CHT = CHR, typename IS = ISR>
inline IS TSTRLength(CHT* string) {
return TSTRLength<CHT>(TPtr<const CHT>(string));
}
/* Scans a item from the string.
@pre You must check if the string is nil before calling. */
inline const CHA* SScan(const CHA* string, CHA& item) {
item = *string++;
return string;
}
inline CHA* SScan(CHA* string, CHA& item) {
return const_cast<CHA*>(SScan(const_cast<const CHA*>(string), item));
}
inline const CHA* SScan(const CHA* string, CHB& item) {
CHC result;
string = SScan(string, result);
result = item;
}
inline CHA* SScan(CHA* string, CHB& item) {
return const_cast<CHA*>(SScan(const_cast<const CHA*>(string), item));
}
inline CHA* SScan(CHA* string, CHC& item) {
const CHA* const_string = const_cast<const CHA*>(string);
return const_cast<CHA*>(SScan(const_string, item));
}
#if USING_UTF16 == YES_0
inline const CHB* SScan(const CHB* string, CHA& item) {
item = CHA(*string++);
return string;
}
inline CHB* SScan(CHB* string, CHA& item) {
return const_cast<CHB*>(SScan(const_cast<const CHB*>(string), item));
}
inline const CHB* SScan(const CHB* string, CHB& c) {
c = *string++;
return string;
}
inline CHB* SScan(CHB* string, CHB& item) {
return const_cast<CHB*>(SScan(const_cast<const CHB*>(string), item));
}
inline CHB* SScan(CHB* string, CHC& item) {
return const_cast<CHB*>(SScan(const_cast<const CHB*>(string), item));
}
#endif
#if USING_UTF32 == YES_0
// CHA* SScan (CHB* string, CHB& item) and
// CHA* SScan (CHA* string, CHC& item) in cstring.h
inline const CHC* SScan(const CHC* string, CHA& result) {
result = CHA(*string++);
return string;
}
inline CHC* SScan(CHC* string, CHA& item) {
const CHC* const_string = const_cast<const CHC*>(string);
return const_cast<CHC*>(SScan(const_string, item));
}
inline const CHC* SScan(const CHC* string, CHB& result) {
result = CHB(*string++);
return string;
}
inline CHC* SScan(CHC* string, CHB& item) {
const CHC* const_string = const_cast<const CHC*>(string);
return const_cast<CHC*>(SScan(const_string, item));
}
inline const CHC* SScan(const CHC* string, CHC& result) {
result = *string++;
return string;
}
inline CHC* SScan(CHC* string, CHC& item) {
const CHC* const_string = const_cast<const CHC*>(string);
return const_cast<CHC*>(SScan(const_string, item));
}
#endif
template<typename CHT>
CHT* TScanChar(CHT* cursor, CHT& c) {}
/* Converts the given item to lowercase if it is uppercase. */
template<typename CHT>
inline CHT TToLower(CHT c) {
if (c >= 'A' && c <= 'Z') c -= 32;
return c;
}
inline CHA ToLower(CHA value) { return TToLower<CHA>(value); }
inline CHB ToLower(CHB value) { return TToLower<CHB>(value); }
inline CHC ToLower(CHC value) { return TToLower<CHC>(value); }
/* Checks if the given item is whitespace. */
template<typename CHT = CHR>
inline BOL TIsWhitespace(CHT item) {
return item <= ' ';
}
template<typename CHT>
ISN TSTRIsYesNo(const CHT* string) {
if (!string) return 0;
CHT c = TToLower<CHT>(*string++);
ISN result;
if (c == 'y')
result = 1;
else if (c == 'n')
result = -1;
else
return 0;
c = *string++;
if (TIsWhitespace<CHT>(c)) return result;
c = TToLower<CHT>(c);
if (c == 'o') return -(ISN)TIsWhitespace<CHT>(*string++);
if (TToLower<CHT>(*string++) != 'e') return 0;
if (TToLower<CHT>(*string++) != 's') return 0;
return (ISN)TIsWhitespace<CHT>(*string++);
}
/* Checks if the given CHA is a digit of a number_.
@return True if it is a digit. */
template<typename CHT = CHR>
BOL TIsDigit(CHT c) {
return (c >= '0') && (c <= '9');
}
/* Scans the given socket for an Signed Integer (IS).
@return Nil if there is no IU to scan.
@param socket The beginning of the socket.
@param item The IS to write the scanned IS. */
template<typename IS = ISW, typename IU = IUW, typename CHT = CHR>
const CHT* TScanSigned(const CHT* string, IS& item) {
if (!string) return nullptr;
IS sign;
const CHT* cursor = string;
CHT c = *cursor++;
if (c == '-') {
c = *string++;
sign = -1;
}
else {
sign = 1;
}
if (!TIsDigit<CHT>(c)) return nullptr;
// Find length:
c = *cursor++;
while (TIsDigit<CHT>(c)) c = *cursor++;
const CHT* stop = cursor; // Store stop to return.
cursor -= 2;
c = *cursor--;
IU value = IU(c) - '0';
IU pow_10_ui2 = 1;
while (cursor >= string) {
c = *cursor--;
pow_10_ui2 *= 10;
IU new_value = value + pow_10_ui2 * (((IU)c) - '0');
if (new_value < value) return nullptr;
value = new_value;
}
item = sign * value;
return stop;
}
/* Scans the given socket for an Signed Integer (IS).
@return Nil if there is no IU to scan.
@param socket The beginning of the socket.
@param item The IS to write the scanned IS. */
template<typename IS = ISW, typename IU = IUW, typename CHT = CHR>
CHT* TScanSigned(CHT* string, IS& item) {
const CHT* ptr = TPtr<const CHT>(string);
return const_cast<CHT*>(TScanSigned<IS, IU, CHT>(ptr, item));
}
/* Scans the given socket for an Signed Integer (IS).
@return Nil if there is no IU to scan.
@param socket The beginning of the socket.
@param item The IS to write the scanned IS. */
template<typename CHT = CHR>
const CHT* TSScan(const CHT* string, ISA& item) {
return TScanSigned<ISA, IUA, CHT>(string, item);
}
template<typename CHT = CHR>
CHT* TSScan(CHT* string, ISA& item) {
return TScanSigned<ISA, IUA, CHT>(string, item);
}
template<typename CHT = CHR>
const CHT* TSScan(const CHT* string, ISB& item) {
return TScanSigned<ISB, IUB, CHT>(string, item);
}
template<typename CHT = CHR>
CHT* TSScan(CHT* string, ISB& item) {
return TScanSigned<ISB, IUB, CHT>(string, item);
}
template<typename CHT = CHR>
const CHT* TSScan(const CHT* string, ISC& item) {
return TScanSigned<ISC, IUC, CHT>(string, item);
}
template<typename CHT = CHR>
CHT* TSScan(CHT* string, ISC& item) {
return TScanSigned<ISC, IUC, CHT>(string, item);
}
template<typename CHT = CHR>
const CHT* TSScan(const CHT* string, ISD& item) {
return TScanSigned<ISD, IUD, CHT>(string, item);
}
template<typename CHT = CHR>
CHT* TSScan(CHT* string, ISD& item) {
return TScanSigned<ISD, IUD, CHT>(string, item);
}
/* Scans the given socket for an unsigned integer (IU).
@return Nil if there is no IU to scan.
@param socket The beginning of the socket.
@param item The IU to write the scanned IU. */
template<typename IU, typename CHT = CHR>
const CHT* TScanUnsigned(const CHT* string, IU& item) {
if (!string) return nullptr;
const CHT* cursor = string;
CHT c = *cursor++;
if (!TIsDigit<CHT>(c)) return nullptr;
// Find length:
c = *cursor++;
while (TIsDigit<CHT>(c)) c = *cursor++;
const CHT* stop = cursor; // Store stop to return.
cursor -= 2;
c = *cursor--;
IU value = IU(c) - '0';
IU pow_10_ui2 = 1;
while (cursor >= string) {
c = *cursor--;
pow_10_ui2 *= 10;
IU new_value = value + pow_10_ui2 * (((IU)c) - '0');
if (new_value < value) return nullptr;
value = new_value;
}
item = value;
return stop;
}
/* Scans the given socket for an unsigned integer (IU).
@return Nil if there is no IU to scan.
@param socket The beginning of the socket.
@param item The IU to write the scanned IU. */
template<typename IU, typename CHT = CHR>
CHT* TScanUnsigned(CHT* string, IU& item) {
const CHT* ptr = TPtr<const CHT>(string);
return const_cast<CHT*>(TScanUnsigned<IU, CHT>(ptr, item));
}
/* Scans item from the string.
@return Nil upon failure. */
template<typename CHT = CHR>
const CHT* TSScan(const CHT* string, IUA& item) {
return TScanUnsigned<IUA, CHT>(string, item);
}
template<typename CHT = CHR>
CHT* TSScan(CHT* string, IUA& item) {
return TScanUnsigned<IUA, CHT>(string, item);
}
template<typename CHT = CHR>
const CHT* TSScan(const CHT* string, IUB& item) {
return TScanUnsigned<IUB, CHT>(string, item);
}
template<typename CHT = CHR>
CHT* TSScan(CHT* string, IUB& item) {
return TScanUnsigned<IUB, CHT>(string, item);
}
template<typename CHT = CHR>
const CHT* TSScan(const CHT* string, IUC& item) {
return TScanUnsigned<IUC, CHT>(string, item);
}
template<typename CHT = CHR>
CHT* TSScan(CHT* string, IUC& item) {
return TScanUnsigned<IUC, CHT>(string, item);
}
template<typename CHT = CHR>
const CHT* TSScan(const CHT* string, IUD& item) {
return TScanUnsigned<IUD, CHT>(string, item);
}
template<typename CHT = CHR>
CHT* TSScan(CHT* string, IUD& item) {
return TScanUnsigned<IUD, CHT>(string, item);
}
/* Prints a Unicode CHT to the given socket.
@return Nil upon failure or a pointer to the nil-term CHT upon success.
@param start The start of the socket.
@param count The element count.
@param item The string to print. */
template<typename CHT = CHR>
CHT* TSPrint(CHT* start, CHT* stop, CHA item) {
if (!start || start >= stop) return nullptr;
*start++ = item;
*start = 0;
return start;
}
/* Prints a Unicode CHT to the given socket.
@return Nil upon failure or a pointer to the nil-term CHT upon success.
@param start The beginning of the socket.
@param count The element count.
@param item The string to print. */
template<typename CHT = CHR>
CHT* TSPrint(CHT* start, ISW count, CHA item) {
return SPrint(start, start + count - 1, item);
}
/* Prints a Unicode CHT to the given socket.
@return Nil upon failure or a pointer to the nil-term CHT upon success.
@param start The beginning of the socket.
@param count The element count.
@param item The string to print. */
template<typename CHT = CHR>
CHT* TSPrint(CHT* start, ISW count, CHB item) {
return TSPrint<CHT>(start, start + count - 1, item);
}
/* Prints the given item to the string.
@return Nil upon failure or a pointer to the nil-term CHT upon success.
@param start Start of socket.
@param count Chars in the string boofer.
@param item Item to print. */
template<typename CHT = CHR>
CHT* TSPrint(CHT* start, ISW count, CHC item) {
return TSPrint<CHT>(start, start + count - 1, item);
}
inline CHA* SPrint(CHA* start, CHA* stop, CHA item) {
if (!PtrIsValid(stop) || start >= stop) return nullptr;
//if (!stop || start >= stop) return nullptr;
*start++ = item;
*start = 0;
return start;
}
inline CHA* SPrint(CHA* start, ISW size, CHA item) {
return SPrint(start, start + size - 1, item);
}
#if USING_UTF16 == YES_0
inline CHA* SPrint(CHA* string, ISW size, CHB item) {
return SPrint(string, string + size - 1, item);
}
inline CHB* SPrint(CHB* string, CHB* stop, CHB item) {
if (!string || string >= stop) return nullptr;
*string++ = item;
*string = 0;
return string;
}
inline CHB* SPrint(CHB* string, CHB* stop, CHA item) {
return SPrint(string, stop, CHB(item));
}
inline CHB* SPrint(CHB* string, ISW size, CHA item) {
return SPrint(string, string + size - 1, item);
}
inline CHB* SPrint(CHB* string, ISW size, CHB item) {
return SPrint(string, string + size - 1, item);
}
inline CHB* SPrint(CHB* string, ISW size, CHC item) {
return SPrint(string, string + size - 1, item);
}
#endif // #if USING_UTF16 == YES_0
#if USING_UTF32 == YES_0
inline CHA* SPrint(CHA* string, ISW size, CHC item) {
return SPrint(string, string + size - 1, item);
}
inline CHC* SPrint(CHC* string, CHC* stop, CHC item) {
if (!string || string >= stop) return nullptr;
*string++ = item;
*string = 0;
return string;
}
inline CHC* SPrint(CHC* string, ISW size, CHC item) {
return SPrint(string, string + size - 1, item);
}
inline CHC* SPrint(CHC* string, CHC* stop, CHA item) {
return SPrint(string, stop, CHC(item));
}
inline CHC* SPrint(CHC* string, ISW size, CHA item) {
return SPrint(string, size, CHC(item));
}
inline CHC* SPrint(CHC* string, ISW size, CHB item) {
return SPrint(string, size, CHC(item));
}
inline CHC* SPrint(CHC* string, CHC* stop, CHB item) {
return SPrint(string, stop, CHC(item));
}
#endif //< USING_UTF32 == YES_0
/* Prints a Unicode item to the given socket.
@return Nil upon failure or a pointer to the nil-term CHT upon success.
@param start The beginning of the socket.
@param stop The last CHT in the socket.
@param item The item to print. */
template<typename CHX, typename CHY>
inline CHX* TSPrintString(CHX* start, CHX* stop, const CHY* item) {
if (!start || start >= stop || !item) return nullptr;
CHL c = 0;
item = SScan(item, c);
while (c) {
start = SPrint(start, stop, c);
if (!start) return start;
item = SScan(item, c);
}
*start = 0;
return start;
}
template<typename CHT = CHR>
inline CHT* TSPrint(CHT* start, CHT* stop, const CHA* item) {
return TSPrintString<CHT, CHA>(start, stop, item);
}
template<typename CHT = CHR>
inline CHT* TSPrint(CHT* start, ISW boofer_size, const CHA* item) {
return TSPrintString<CHT>(start, start + boofer_size - 1, item);
}
#if USING_UTF16 == YES_0
/* Prints a Unicode item to the given socket.
@return Nil upon failure or a pointer to the nil-term CHT upon success.
@param string The beginning of the socket.
@param stop The last CHT in the socket.
@param item The item to print.
@warning This algorithm is designed to fail if the socket is not a valid socket
with one or more bytes in it, or if item is nil. */
template<typename CHT = CHR>
CHT* TSPrintString(CHT* string, CHT* stop, const CHB* item) {
return TSPrintString<CHT, CHB>(string, stop, item);
}
template<typename CHT = CHR>
inline CHT* TSPrint(CHT* start, CHT* stop, const CHB* item) {
return TSPrintString<CHT>(start, stop, item);
}
template<typename CHT = CHR>
inline CHT* TSPrint(CHT* start, ISW boofer_size, const CHB* item) {
return TSPrintString<CHT>(start, start + boofer_size - 1, item);
}
#endif
#if USING_UTF32 == YES_0
/* Prints a Unicode item to the given socket.
@return Nil upon failure or a pointer to the nil-term CHT upon success.
@param start The beginning of the socket.
@param stop The last CHT in the socket.
@param item The item to print.
@warning This algorithm is designed to fail if the socket is not a valid socket
with one or more bytes in it, or if item is nil. */
template<typename CHT = CHR>
CHT* TSPrintString(CHT* start, CHT* stop, const CHC* item) {
return TSPrintString<CHT, CHC>(start, stop, item);
}
template<typename CHT = CHR>
inline CHT* TSPrint(CHT* start, CHT* stop, const CHC* item) {
return TSPrintString<CHT>(start, stop, item);
}
template<typename CHT = CHR>
inline CHT* TSPrint(CHT* start, ISW boofer_size, const CHC* item) {
return TSPrintString<CHT>(start, start + boofer_size - 1, item);
}
#endif
/* Converts the given hex nibble to lowercase hex. */
inline CHA HexNibbleToLowerCase(IUA b) {
b = b & 0xf;
if (b > 9) return b + ('a' - 10);
return b + '0';
}
/* Converts a IUA a two-IUA hex representation. */
inline IUB HexByteToLowerCase(IUA b) {
IUB value = HexNibbleToLowerCase(b & 0xf);
value = value << 8;
value |= HexNibbleToLowerCase(b >> 4);
return value;
}
/* Converts the given hex nibble to uppercase hex. */
inline CHA HexNibbleToUpperCase(IUA nibble) {
nibble = nibble & 0xf;
if (nibble > 9) return nibble + ('A' - 10);
return nibble + '0';
}
/* Converts a IUA a two-IUA hex representation. */
inline IUB HexByteToUpperCase(IUA b) {
IUB value = (IUB)HexNibbleToUpperCase(b & 0xf);
value = value << 8;
IUB second_nibble = HexNibbleToUpperCase(b >> 4);
value |= second_nibble;
return value;
}
/* Converts a hex value to a byte. */
inline ISN HexToByte(CHA c) {
if (c < '0') {
return -1;
}
if (c >= 'a') {
if (c > 'f') return -1;
return c - ('a' - 10);
}
if (c >= 'A') {
if (c > 'F') return -1;
return c - ('A' - 10);
}
if (c > '9') return -1;
return c - '0';
}
/* Converts a single hex IUA a IUA.
@return Returns -1 if c is not a hex IUA. */
inline ISN HexToByte(IUB h) {
ISN lowerValue = HexToByte((CHA)(h >> 8));
if (lowerValue < 0) return -1;
ISN upper_value = HexToByte((CHA)h);
if (upper_value < 0) return -1;
return lowerValue | (upper_value << 4);
}
/* Skips the given CHT in a s if there are any.
@param cursor The first CHT in the boofer. */
template<typename CHT = CHR>
inline CHT* TSTRSkimodulear(CHT* cursor, CHT skip_char) {
return const_cast<const CHT*>(
TSTRSkimodulear<CHT>(TPtr<const CHT>(cursor), skip_char));
}
template<typename CHT>
void TPrint1(CHT* start, CHT token) {
*start++ = token;
*start++ = 0;
}
/* @todo Delete me
template<typename CHT>
void TPrint3(CHT* start, CHT token) {
*start++ = token;
*start++ = token;
*start++ = token;
*start++ = 0;
}*/
template<typename T, typename CHT = CHR>
CHT* TSPrintHex(CHT* start, CHT* stop, const void* origin, ISW bytes) {
CHT* end = start + (bytes * 2);
if (!start || bytes <= 0 || end < start) return nullptr;
const IUA* cursor = TPtr<const IUA>(origin);
while (bytes-- > 0) {
IUA byte = *cursor++;
*start++ = HexNibbleToUpperCase(byte >> 4);
*start++ = HexNibbleToUpperCase(byte & 0xf);
}
*start = 0;
return start;
}
/* Prints a hex value to the Console. */
template<typename CHT, typename IU>
CHT* TSPrintHex(CHT* start, CHT* stop, IU value) {
enum { cHexStringLengthSizeMax = sizeof(IU) * 2 + 3 };
if (!start || start + cHexStringLengthSizeMax >= stop) return nullptr;
*start++ = '0';
*start++ = 'x';
auto v = ToUnsigned(value);
for (ISC num_bits_shift = sizeof(IU) * 8 - 4; num_bits_shift >= 0;
num_bits_shift -= 4) {
*start++ = HexNibbleToUpperCase((IUA)(v >> num_bits_shift));
}
*start = 0;
return start;
}
/* Prints a hex value to a text socket. */
template<typename CHT = CHR>
inline CHT* TSPrintHex(CHT* start, CHT* stop, ISA value) {
return TSPrintHex<CHT, IUA>(start, stop, (IUA)value);
}
template<typename CHT = CHR>
inline CHT* TSPrintHex(CHT* start, CHT* stop, IUA value) {
return TSPrintHex<CHT, IUA>(start, stop, value);
}
template<typename CHT = CHR>
inline CHT* TSPrintHex(CHT* start, CHT* stop, ISB value) {
return TSPrintHex<CHT, IUB>(start, stop, (IUB)value);
}
template<typename CHT = CHR>
inline CHT* TSPrintHex(CHT* start, CHT* stop, IUB value) {
return TSPrintHex<CHT, IUB>(start, stop, value);
}
template<typename CHT = CHR>
inline CHT* TSPrintHex(CHT* start, CHT* stop, ISC value) {
return TSPrintHex<CHT, IUC>(start, stop, (IUC)value);
}
template<typename CHT = CHR>
inline CHT* TSPrintHex(CHT* start, CHT* stop, IUC value) {
return TSPrintHex<CHT, IUC>(start, stop, value);
}
template<typename CHT = CHR>
inline CHT* TSPrintHex(CHT* start, CHT* stop, ISD value) {
return TSPrintHex<CHT, IUD>(start, stop, (IUD)value);
}
template<typename CHT = CHR>
inline CHT* TSPrintHex(CHT* start, CHT* stop, IUD value) {
return TSPrintHex<CHT, IUD>(start, stop, value);
}
#if USING_FPC == YES_0
template<typename CHT = CHR>
inline CHT* TSPrintHex(CHT* start, CHT* stop, FPC value) {
return TSPrintHex<CHT, IUC>(start, stop, ToUnsigned(value));
}
#endif
#if USING_FPD == YES_0
template<typename CHT = CHR>
inline CHT* TSPrintHex(CHT* start, CHT* stop, FPD value) {
return TSPrintHex<CHT, IUD>(start, stop, ToUnsigned(value));
}
#endif
template<typename CHT = CHR>
inline CHT* TSPrintHex(CHT* start, CHT* stop, const void* ptr) {
return TSPrintHex<CHT, IUW>(start, stop, ToUnsigned(ptr));
}
/* Prints the given value to Binary. */
template<typename CHT = CHR, typename IU>
CHT* TPrintBinary(CHT* start, CHT* stop, IU value) {
if (start + sizeof(IU) * 8 >= stop) {
return nullptr;
}
for (ISC i = 0; i < sizeof(IU) * 8; ++i) {
*start++ = (CHT)('0' + (value >> (sizeof(IU) * 8 - 1)));
value = value << 1;
}
*start = 0;
return start;
}
/* Prints the given value to Binary. */
template<typename CHT = CHR>
inline CHT* TPrintBinary(CHT* start, CHT* stop, ISA value) {
return TPrintBinary<CHT, IUA>(start, stop, (IUA)value);
}
/* Prints the given value to Binary. */
template<typename CHT = CHR>
inline CHT* TPrintBinary(CHT* start, CHT* stop, IUA value) {
return TPrintBinary<CHT, IUA>(start, stop, value);
}
/* Prints the given value to Binary. */
template<typename CHT = CHR>
inline CHT* TPrintBinary(CHT* start, CHT* stop, ISB value) {
return TPrintBinary<CHT, IUB>(start, stop, (IUB)value);
}
/* Prints the given value to Binary. */
template<typename CHT = CHR>
inline CHT* TPrintBinary(CHT* start, CHT* stop, IUB value) {
return TPrintBinary<CHT, IUB>(start, stop, value);
}
/* Prints the given value to Binary. */
template<typename CHT = CHR>
inline CHT* TPrintBinary(CHT* start, CHT* stop, ISC value) {
return TPrintBinary<CHT, IUC>(start, stop, (IUC)value);
}
/* Prints the given value to Binary. */
template<typename CHT = CHR>
inline CHT* TPrintBinary(CHT* start, CHT* stop, IUC value) {
return TPrintBinary<CHT, IUC>(start, stop, value);
}
/* Prints the given value to Binary. */
template<typename CHT = CHR>
inline CHT* TPrintBinary(CHT* start, CHT* stop, ISD value) {
return TPrintBinary<CHT, IUD>(start, stop, (IUD)value);
}
/* Prints the given value to Binary. */
template<typename CHT = CHR>
inline CHT* TPrintBinary(CHT* start, CHT* stop, IUD value) {
return TPrintBinary<CHT, IUD>(start, stop, value);
}
/* Prints the given value to Binary. */
template<typename CHT = CHR>
CHT* TPrintBinary(CHT* start, CHT* stop, FPC value) {
IUC ui = *TPtr<IUC>(&value);
return TPrintBinary<CHT, IUC>(start, stop, ui);
}
/* Prints the given value to Binary. */
template<typename CHT = CHR>
CHT* TPrintBinary(CHT* start, CHT* stop, FPD value) {
IUD ui = *TPtr<IUD>(&value);
return TPrintBinary<CHT, IUD>(start, stop, ui);
}
/* Prints the given value to Binary. */
template<typename CHT = CHR>
CHT* TPrintBinary(CHT* start, CHT* stop, const void* item) {
IUW address = *TPtr<IUW>(&item);
return TPrintBinary<CHT, IUW>(start, stop, address);
}
/* Prints the given item aligned right the given column_count.
@return Nil if any of the pointers are nil or if column_count < 1, and a
pointer to the nil-term CHA upon success.
@param cursor The first CHT in the boofer.
@param stop The last CHT in the boofer.
@param item The item to printer.
@param column_count The token_ of columns to align right to. */
template<typename CHT = CHR>
CHT* TPrintRight(CHT* cursor, CHT* stop, const CHT* item,
ISC column_count = AConsoleWidth) {
if (!cursor || cursor + column_count > stop) {
return nullptr;
}
auto item_end = TSTREnd<CHT>(item);
CHT c; //< Temp variable.
if (item == item_end) return cursor;
ISW length = item_end - item;
// If the length is less than the column_count we need to print ".", "..",
// "..." or nothing and chop off some of the item.
ISW count = column_count - length;
if (count < 0) {
ISW dot_count = length + count;
if (dot_count <= 3) {
while (dot_count-- > 0) {
*cursor++ = '.';
}
*cursor = 0;
return cursor;
}