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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
/*!
  | Utilities for converting data from/to
  | strings.
  |
  */

crate::ix!();

pub trait ToUpper {
    fn to_upper(&self) -> Self;
}

pub trait ToLower {
    fn to_lower(&self) -> Self;
}

//-------------------------------------------[.cpp/bitcoin/src/util/strencodings.h]

/**
  | Used by SanitizeString()
  |
  */
pub enum SafeChars
{
    /**
      | The full set of allowed chars
      |
      */
    SAFE_CHARS_DEFAULT, 

    /**
      | BIP-0014 subset
      |
      */
    SAFE_CHARS_UA_COMMENT, 

    /**
      | Chars allowed in filenames
      |
      */
    SAFE_CHARS_FILENAME, 

    /**
      | Chars allowed in URIs (RFC 3986)
      |
      */
    SAFE_CHARS_URI, 
}

/**
  | LocaleIndependentAtoi is provided for backwards
  | compatibility reasons.
  |
  | New code should use ToIntegral or the ParseInt*
  | functions which provide parse error feedback.
  |
  | The goal of LocaleIndependentAtoi is to
  | replicate the exact defined behaviour of atoi
  | and atoi64 as they behave under the "C" locale.
  */
pub fn locale_independent_atoi<T>(str_: &str) -> T {

    todo!();
        /*
            const_assert(std::is_integral<T>::value);
        T result;
        // Emulate atoi(...) handling of white space and leading +/-.
        std::string s = TrimString(str);
        if (!s.empty() && s[0] == '+') {
            if (s.length() >= 2 && s[1] == '-') {
                return 0;
            }
            s = s.substr(1);
        }
        auto [_, error_condition] = std::from_chars(s.data(), s.data() + s.size(), result);
        if (error_condition != std::errc{}) {
            return 0;
        }
        return result;
        */
}

/**
  | Tests if the given character is a decimal
  | digit.
  | 
  | -----------
  | @param[in] c
  | 
  | character to test
  | 
  | -----------
  | @return
  | 
  | true if the argument is a decimal digit;
  | otherwise false.
  |
  */
pub fn is_digit(c: u8) -> bool {
    
    todo!();
        /*
            return c >= '0' && c <= '9';
        */
}

/**
  | Tests if the given character is a whitespace
  | character. The whitespace characters
  | are: 
  |
  | space, 
  | form-feed ('\f'), 
  | newline ('\n'), 
  | carriage return ('\r'), 
  | horizontal tab ('\t'), 
  | and vertical tab ('\v').
  | 
  | This function is locale independent.
  | Under the C locale this function gives
  | the same result as std::isspace.
  | 
  | -----------
  | @param[in] c
  | 
  | character to test
  | 
  | -----------
  | @return
  | 
  | true if the argument is a whitespace
  | character; otherwise false
  |
  */
#[inline] pub fn is_space(c: u8) -> bool {
    
    todo!();
        /*
            return c == ' ' || c == '\f' || c == '\n' || c == '\r' || c == '\t' || c == '\v';
        */
}

/**
  | Convert string to integral type T. Leading
  | whitespace, a leading +, or any trailing
  | character fail the parsing.
  | 
  | The required format expressed as regex
  | is `-?[0-9]+`. The minus sign is only
  | permitted for signed integer types.
  | 
  | -----------
  | @return
  | 
  | std::nullopt if the entire string could
  | not be parsed, or if the parsed value
  | is not in the range representable by
  | the type T.
  |
  */
pub fn to_integral<T>(str_: &str) -> Option<T> {

    todo!();
        /*
            const_assert(std::is_integral<T>::value);
        T result;
        const auto [first_nonmatching, error_condition] = std::from_chars(str.data(), str.data() + str.size(), result);
        if (first_nonmatching != str.data() + str.size() || error_condition != std::errc{}) {
            return std::nullopt;
        }
        return result;
        */
}

/**
  | Timing-attack-resistant comparison.
  | 
  | Takes time proportional to length of
  | first argument.
  |
  */
pub fn timing_resistant_equal<T>(a: &T, b: &T) -> bool {

    todo!();
        /*
            if (b.size() == 0) return a.size() == 0;
        size_t accumulator = a.size() ^ b.size();
        for (size_t i = 0; i < a.size(); i++)
            accumulator |= a[i] ^ b[i%b.size()];
        return accumulator == 0;
        */
}

/**
  | Convert from one power-of-2 number
  | base to another.
  |
  */
pub fn convert_bits<O, I, const frombits: i32, const tobits: i32, const pad: bool>(
        outfn: &O,
        it:    I,
        end:   I) -> bool {

    todo!();
        /*
            size_t acc = 0;
        size_t bits = 0;
        constexpr size_t maxv = (1 << tobits) - 1;
        constexpr size_t max_acc = (1 << (frombits + tobits - 1)) - 1;
        while (it != end) {
            acc = ((acc << frombits) | *it) & max_acc;
            bits += frombits;
            while (bits >= tobits) {
                bits -= tobits;
                outfn((acc >> bits) & maxv);
            }
            ++it;
        }
        if (pad) {
            if (bits) outfn((acc << (tobits - bits)) & maxv);
        } else if (bits >= frombits || ((acc << (tobits - bits)) & maxv)) {
            return false;
        }
        return true;
        */
}

impl ToLower for u8 {

    /**
      | Converts the given character to its
      | lowercase equivalent.
      | 
      | This function is locale independent.
      | It only converts uppercase characters
      | in the standard 7-bit ASCII range.
      | 
      | This is a feature, not a limitation.
      | 
      | -----------
      | @param[in] c
      | 
      | the character to convert to lowercase.
      | 
      | -----------
      | @return
      | 
      | the lowercase equivalent of c; or the
      | argument if no conversion is possible.
      |
      */
    fn to_lower(&self) -> u8 {

        todo!();
        /*
                return (c >= 'A' && c <= 'Z' ? (c - 'A') + 'a' : c);
            */
    }
}

/**
  | Converts the given character to its
  | uppercase equivalent.
  | 
  | This function is locale independent.
  | It only converts lowercase characters
  | in the standard 7-bit ASCII range.
  | 
  | This is a feature, not a limitation.
  | 
  | -----------
  | @param[in] c
  | 
  | the character to convert to uppercase.
  | 
  | -----------
  | @return
  | 
  | the uppercase equivalent of c; or the
  | argument if no conversion is possible.
  |
  */
impl ToUpper for u8 {

    fn to_upper(&self) -> u8 {
        
        todo!();
            /*
                return (c >= 'a' && c <= 'z' ? (c - 'a') + 'A' : c);
            */
    }
}

//-------------------------------------------[.cpp/bitcoin/src/util/strencodings.cpp]

pub const CHARS_ALPHA_NUM: &'static str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";

lazy_static!{
    static ref SAFE_CHARS: Box<[String]> = 
        Box::new([
            CHARS_ALPHA_NUM.to_owned() + " .,;-_/:?@()",            // SAFE_CHARS_DEFAULT
            CHARS_ALPHA_NUM.to_owned() + " .,;-_?@",                // SAFE_CHARS_UA_COMMENT
            CHARS_ALPHA_NUM.to_owned() + ".-_",                     // SAFE_CHARS_FILENAME
            CHARS_ALPHA_NUM.to_owned() + "!*'();:@&=+$,/?#[]-_.~%", // SAFE_CHARS_URI
        ]);
}

/**
  | Remove unsafe chars. Safe chars chosen
  | to allow simple messages/URLs/email
  | addresses, but avoid anything even
  | possibly remotely dangerous like &
  | or >
  | 
  | -----------
  | @param[in] str
  | 
  | The string to sanitize
  | ----------
  | @param[in] rule
  | 
  | The set of safe chars to choose (default:
  | least restrictive)
  | 
  | -----------
  | @return
  | 
  | A new string without unsafe chars
  |
  */
pub fn sanitize_string(
        str_: &str,
        rule: Option<i32>) -> String {

    let rule: i32 = rule.unwrap_or(SafeChars::SAFE_CHARS_DEFAULT as i32);
    
    todo!();
        /*
            std::string strResult;
        for (std::string::size_type i = 0; i < str.size(); i++)
        {
            if (SAFE_CHARS[rule].find(str[i]) != std::string::npos)
                strResult.push_back(str[i]);
        }
        return strResult;
        */
}

pub const util_hexdigit: [i8; 256] = [ 
    -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
    -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
    -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
    0,1,2,3,4,5,6,7,8,9,-1,-1,-1,-1,-1,-1,
    -1,0xa,0xb,0xc,0xd,0xe,0xf,-1,-1,-1,-1,-1,-1,-1,-1,-1,
    -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
    -1,0xa,0xb,0xc,0xd,0xe,0xf,-1,-1,-1,-1,-1,-1,-1,-1,-1,
    -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
    -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
    -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
    -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
    -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
    -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
    -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
    -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
    -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, 
];

pub fn hex_digit(c: u8) -> i8 {
    
    todo!();
        /*
            return p_util_hexdigit[(unsigned char)c];
        */
}

/**
  | Returns true if each character in str
  | is a hex character, and has an even number
  | of hex digits.
  |
  */
pub fn is_hex(str_: &str) -> bool {
    
    todo!();
        /*
            for(std::string::const_iterator it(str.begin()); it != str.end(); ++it)
        {
            if (HexDigit(*it) < 0)
                return false;
        }
        return (str.size() > 0) && (str.size()%2 == 0);
        */
}

/**
  | Return true if the string is a hex number,
  | optionally prefixed with "0x"
  |
  */
pub fn is_hex_number(str_: &str) -> bool {
    
    todo!();
        /*
            size_t starting_location = 0;
        if (str.size() > 2 && *str.begin() == '0' && *(str.begin()+1) == 'x') {
            starting_location = 2;
        }
        for (const char c : str.substr(starting_location)) {
            if (HexDigit(c) < 0) return false;
        }
        // Return false for empty string or "0x".
        return (str.size() > starting_location);
        */
}

pub fn parse_hex(psz: &str) -> Vec<u8> {
    
    todo!();
        /*
            // convert hex dump to vector
        std::vector<unsigned char> vch;
        while (true)
        {
            while (IsSpace(*psz))
                psz++;
            signed char c = HexDigit(*psz++);
            if (c == (signed char)-1)
                break;
            unsigned char n = (c << 4);
            c = HexDigit(*psz++);
            if (c == (signed char)-1)
                break;
            n |= c;
            vch.push_back(n);
        }
        return vch;
        */
}

pub fn encode_base64_bytes(input: &[u8]) -> String {
    
    todo!();
        /*
            static const char *pbase64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

        std::string str;
        str.reserve(((input.size() + 2) / 3) * 4);
        ConvertBits<8, 6, true>([&](int v) { str += pbase64[v]; }, input.begin(), input.end());
        while (str.size() % 4) str += '=';
        return str;
        */
}

pub fn encode_base64(str_: &String) -> String {
    
    todo!();
        /*
            return EncodeBase64(MakeUCharSpan(str));
        */
}

pub fn decode_base64_bytes(
        p:          *const u8,
        pf_invalid: Option<*mut bool>) -> Vec<u8> {
    
    todo!();
        /*
            static const int decode64_table[256] =
        {
            -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
            -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
            -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1,
            -1, -1, -1, -1, -1,  0,  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, -1, -1, -1, -1, -1, -1, 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, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
            -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
            -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
            -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
            -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
            -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
            -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
        };

        const char* e = p;
        std::vector<uint8_t> val;
        val.reserve(strlen(p));
        while (*p != 0) {
            int x = decode64_table[(unsigned char)*p];
            if (x == -1) break;
            val.push_back(x);
            ++p;
        }

        std::vector<unsigned char> ret;
        ret.reserve((val.size() * 3) / 4);
        bool valid = ConvertBits<6, 8, false>([&](unsigned char c) { ret.push_back(c); }, val.begin(), val.end());

        const char* q = p;
        while (valid && *p != 0) {
            if (*p != '=') {
                valid = false;
                break;
            }
            ++p;
        }
        valid = valid && (p - e) % 4 == 0 && p - q < 4;
        if (pf_invalid) *pf_invalid = !valid;

        return ret;
        */
}

pub fn decode_base64(
        str_:       &str,
        pf_invalid: Option<&mut bool>) -> String {
    
    todo!();
        /*
            if (!ValidAsCString(str)) {
            if (pf_invalid) {
                *pf_invalid = true;
            }
            return {};
        }
        std::vector<unsigned char> vchRet = DecodeBase64(str.c_str(), pf_invalid);
        return std::string((const char*)vchRet.data(), vchRet.size());
        */
}

/**
  | Base32 encode.
  | 
  | If `pad` is true, then the output will
  | be padded with '=' so that its length
  | is a multiple of 8.
  |
  */
pub fn encode_base32_bytes(
        input: &[u8],
        pad:   Option<bool>) -> String {
    let pad: bool = pad.unwrap_or(true);
    
    todo!();
        /*
            static const char *pbase32 = "abcdefghijklmnopqrstuvwxyz234567";

        std::string str;
        str.reserve(((input.size() + 4) / 5) * 8);
        ConvertBits<8, 5, true>([&](int v) { str += pbase32[v]; }, input.begin(), input.end());
        if (pad) {
            while (str.size() % 8) {
                str += '=';
            }
        }
        return str;
        */
}

/**
  | Base32 encode.
  | 
  | If `pad` is true, then the output will
  | be padded with '=' so that its length
  | is a multiple of 8.
  |
  */
pub fn encode_base32(
        str_: &[u8],
        pad:  Option<bool>) -> String {
    let pad: bool = pad.unwrap_or(true);
    
    todo!();
        /*
            return EncodeBase32(MakeUCharSpan(str), pad);
        */
}

pub fn decode_base32_bytes(
        p:          *const u8,
        pf_invalid: Option<*mut bool>) -> Vec<u8> {
    
    todo!();
        /*
            static const int decode32_table[256] =
        {
            -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
            -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
            -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 26, 27, 28, 29, 30, 31, -1, -1, -1, -1,
            -1, -1, -1, -1, -1,  0,  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, -1, -1, -1, -1, -1, -1,  0,  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, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
            -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
            -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
            -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
            -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
            -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
            -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
        };

        const char* e = p;
        std::vector<uint8_t> val;
        val.reserve(strlen(p));
        while (*p != 0) {
            int x = decode32_table[(unsigned char)*p];
            if (x == -1) break;
            val.push_back(x);
            ++p;
        }

        std::vector<unsigned char> ret;
        ret.reserve((val.size() * 5) / 8);
        bool valid = ConvertBits<5, 8, false>([&](unsigned char c) { ret.push_back(c); }, val.begin(), val.end());

        const char* q = p;
        while (valid && *p != 0) {
            if (*p != '=') {
                valid = false;
                break;
            }
            ++p;
        }
        valid = valid && (p - e) % 8 == 0 && p - q < 8;
        if (pf_invalid) *pf_invalid = !valid;

        return ret;
        */
}

pub fn decode_base32(
        str_:       &str,
        pf_invalid: Option<*mut bool>) -> String {
    
    todo!();
        /*
            if (!ValidAsCString(str)) {
            if (pf_invalid) {
                *pf_invalid = true;
            }
            return {};
        }
        std::vector<unsigned char> vchRet = DecodeBase32(str.c_str(), pf_invalid);
        return std::string((const char*)vchRet.data(), vchRet.size());
        */
}

pub fn parse_integral<T>(
        str_: &String,
        out:  *mut T) -> bool {

    todo!();
        /*
            const_assert(std::is_integral<T>::value);
        // Replicate the exact behavior of strtol/strtoll/strtoul/strtoull when
        // handling leading +/- for backwards compatibility.
        if (str.length() >= 2 && str[0] == '+' && str[1] == '-') {
            return false;
        }
        const std::optional<T> opt_int = ToIntegral<T>((!str.empty() && str[0] == '+') ? str.substr(1) : str);
        if (!opt_int) {
            return false;
        }
        if (out != nullptr) {
            *out = *opt_int;
        }
        return true;
        */
}

/**
  | Convert string to signed 32-bit integer
  | with strict parse error feedback.
  | 
  | 
  | -----------
  | @return
  | 
  | true if the entire string could be parsed
  | as valid integer, false if not the entire
  | string could be parsed or when overflow
  | or underflow occurred.
  |
  */
pub fn parse_int32(
        str_: &String,
        out:  *mut i32) -> bool {
    
    todo!();
        /*
            return ParseIntegral<int32_t>(str, out);
        */
}

/**
  | Convert string to signed 64-bit integer
  | with strict parse error feedback.
  | 
  | -----------
  | @return
  | 
  | true if the entire string could be parsed
  | as valid integer, false if not the entire
  | string could be parsed or when overflow
  | or underflow occurred.
  |
  */
pub fn parse_int64(
        str_: &String,
        out:  *mut i64) -> bool {
    
    todo!();
        /*
            return ParseIntegral<int64_t>(str, out);
        */
}

/**
  | Convert decimal string to unsigned
  | 8-bit integer with strict parse error
  | feedback.
  | 
  | -----------
  | @return
  | 
  | true if the entire string could be parsed
  | as valid integer, false if not the entire
  | string could be parsed or when overflow
  | or underflow occurred.
  |
  */
pub fn parse_uint8(
        str_: &str,
        out:  *mut u8) -> bool {
    
    todo!();
        /*
            return ParseIntegral<uint8_t>(str, out);
        */
}

/**
  | Convert decimal string to unsigned
  | 16-bit integer with strict parse error
  | feedback.
  | 
  | -----------
  | @return
  | 
  | true if the entire string could be parsed
  | as valid integer, false if the entire
  | string could not be parsed or if overflow
  | or underflow occurred.
  |
  */
pub fn parse_uint16(
        str_: &str,
        out:  *mut u16) -> bool {
    
    todo!();
        /*
            return ParseIntegral<uint16_t>(str, out);
        */
}

/**
  | Convert decimal string to unsigned
  | 32-bit integer with strict parse error
  | feedback.
  | 
  | -----------
  | @return
  | 
  | true if the entire string could be parsed
  | as valid integer, false if not the entire
  | string could be parsed or when overflow
  | or underflow occurred.
  |
  */
pub fn parse_uint32(
        str_: &str,
        out:  *mut u32) -> bool {
    
    todo!();
        /*
            return ParseIntegral<uint32_t>(str, out);
        */
}

/**
  | Convert decimal string to unsigned
  | 64-bit integer with strict parse error
  | feedback.
  | 
  | 
  | -----------
  | @return
  | 
  | true if the entire string could be parsed
  | as valid integer, false if not the entire
  | string could be parsed or when overflow
  | or underflow occurred.
  |
  */
pub fn parse_uint64(
        str_: &str,
        out:  *mut u64) -> bool {
    
    todo!();
        /*
            return ParseIntegral<uint64_t>(str, out);
        */
}

/** 
 | Upper bound for mantissa.
 |
 | 10^18-1 is the largest arbitrary decimal that
 | will fit in a signed 64-bit integer.
 |
 | Larger integers cannot consist of arbitrary
 | combinations of 0-9:
 |
 |   999999999999999999  1^18-1
 |  9223372036854775807  (1<<63)-1  (max int64_t)
 |  9999999999999999999  1^19-1     (would overflow)
 */
pub const UPPER_BOUND: i64 = 1000000000000000000 - 1;

/**
  | Helper function for ParseFixedPoint
  |
  */
#[inline] pub fn process_mantissa_digit(
        ch:              u8,
        mantissa:        &mut i64,
        mantissa_tzeros: &mut i32) -> bool {
    
    todo!();
        /*
            if(ch == '0')
            ++mantissa_tzeros;
        else {
            for (int i=0; i<=mantissa_tzeros; ++i) {
                if (mantissa > (UPPER_BOUND / 10LL))
                    return false; /* overflow */
                mantissa *= 10;
            }
            mantissa += ch - '0';
            mantissa_tzeros = 0;
        }
        return true;
        */
}

/**
  | Parse number as fixed point according
  | to JSON number syntax.
  | 
  | See https://json.org/number.gif
  | 
  | -----------
  | @note
  | 
  | The result must be in the range (-10^18,10^18),
  | otherwise an overflow error will trigger.
  | 
  | -----------
  | @return
  | 
  | true on success, false on error.
  |
  */
pub fn parse_fixed_point(
        val:        &str,
        decimals:   i32,
        amount_out: *mut i64) -> bool {
    
    todo!();
        /*
            int64_t mantissa = 0;
        int64_t exponent = 0;
        int mantissa_tzeros = 0;
        bool mantissa_sign = false;
        bool exponent_sign = false;
        int ptr = 0;
        int end = val.size();
        int point_ofs = 0;

        if (ptr < end && val[ptr] == '-') {
            mantissa_sign = true;
            ++ptr;
        }
        if (ptr < end)
        {
            if (val[ptr] == '0') {
                /* pass single 0 */
                ++ptr;
            } else if (val[ptr] >= '1' && val[ptr] <= '9') {
                while (ptr < end && IsDigit(val[ptr])) {
                    if (!ProcessMantissaDigit(val[ptr], mantissa, mantissa_tzeros))
                        return false; /* overflow */
                    ++ptr;
                }
            } else return false; /* missing expected digit */
        } else return false; /* empty string or loose '-' */
        if (ptr < end && val[ptr] == '.')
        {
            ++ptr;
            if (ptr < end && IsDigit(val[ptr]))
            {
                while (ptr < end && IsDigit(val[ptr])) {
                    if (!ProcessMantissaDigit(val[ptr], mantissa, mantissa_tzeros))
                        return false; /* overflow */
                    ++ptr;
                    ++point_ofs;
                }
            } else return false; /* missing expected digit */
        }
        if (ptr < end && (val[ptr] == 'e' || val[ptr] == 'E'))
        {
            ++ptr;
            if (ptr < end && val[ptr] == '+')
                ++ptr;
            else if (ptr < end && val[ptr] == '-') {
                exponent_sign = true;
                ++ptr;
            }
            if (ptr < end && IsDigit(val[ptr])) {
                while (ptr < end && IsDigit(val[ptr])) {
                    if (exponent > (UPPER_BOUND / 10LL))
                        return false; /* overflow */
                    exponent = exponent * 10 + val[ptr] - '0';
                    ++ptr;
                }
            } else return false; /* missing expected digit */
        }
        if (ptr != end)
            return false; /* trailing garbage */

        /* finalize exponent */
        if (exponent_sign)
            exponent = -exponent;
        exponent = exponent - point_ofs + mantissa_tzeros;

        /* finalize mantissa */
        if (mantissa_sign)
            mantissa = -mantissa;

        /* convert to one 64-bit fixed-point value */
        exponent += decimals;
        if (exponent < 0)
            return false; /* cannot represent values smaller than 10^-decimals */
        if (exponent >= 18)
            return false; /* cannot represent values larger than or equal to 10^(18-decimals) */

        for (int i=0; i < exponent; ++i) {
            if (mantissa > (UPPER_BOUND / 10LL) || mantissa < -(UPPER_BOUND / 10LL))
                return false; /* overflow */
            mantissa *= 10;
        }
        if (mantissa > UPPER_BOUND || mantissa < -UPPER_BOUND)
            return false; /* overflow */

        if (amount_out)
            *amount_out = mantissa;

        return true;
        */
}

impl ToLower for String {

    /**
      | Returns the lowercase equivalent of
      | the given string.
      | 
      | This function is locale independent.
      | It only converts uppercase characters
      | in the standard 7-bit ASCII range.
      | 
      | This is a feature, not a limitation.
      | 
      | -----------
      | @param[in] str
      | 
      | the string to convert to lowercase.
      | 
      | -----------
      | @return
      | 
      | lowercased equivalent of str
      |
      */
    fn to_lower(&self) -> String {

        todo!();
            /*
                std::string r;
            for (auto ch : str) r += ToLower((unsigned char)ch);
            return r;
            */
    }
}

/**
  | Returns the uppercase equivalent of
  | the given string.
  | 
  | This function is locale independent.
  | It only converts lowercase characters
  | in the standard 7-bit ASCII range.
  | 
  | This is a feature, not a limitation.
  | 
  | -----------
  | @param[in] str
  | 
  | the string to convert to uppercase.
  | 
  | -----------
  | @return
  | 
  | UPPERCASED EQUIVALENT OF str
  |
  */
impl ToUpper for String {
    fn to_upper(&self) -> String {
        
        todo!();
            /*
                std::string r;
            for (auto ch : str) r += ToUpper((unsigned char)ch);
            return r;
            */
    }
}

/**
  | Capitalizes the first character of
  | the given string.
  | 
  | This function is locale independent.
  | It only converts lowercase characters
  | in the standard 7-bit ASCII range.
  | 
  | This is a feature, not a limitation.
  | 
  | -----------
  | @param[in] str
  | 
  | the string to capitalize.
  | 
  | -----------
  | @return
  | 
  | string with the first letter capitalized.
  |
  */
pub fn capitalize(str_: &str) -> String {
    
    todo!();
        /*
            if (str.empty()) return str;
        str[0] = ToUpper(str.front());
        return str;
        */
}

/**
  | Convert a span of bytes to a lower-case
  | hexadecimal string.
  |
  */
pub fn hex_str(s: &[u8]) -> String {
    
    todo!();
        /*
            std::string rv(s.size() * 2, '\0');
        static constexpr char hexmap[16] = { '0', '1', '2', '3', '4', '5', '6', '7',
                                             '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
        auto it = rv.begin();
        for (uint8_t v : s) {
            *it++ = hexmap[v >> 4];
            *it++ = hexmap[v & 15];
        }
        assert(it == rv.end());
        return rv;
        */
}