@@ -590,6 +590,183 @@ TEST(StringRefTest, getAsUnsignedIntegerBadStrings) {
590
590
}
591
591
}
592
592
593
+ struct ConsumeUnsignedPair {
594
+ const char *Str;
595
+ uint64_t Expected;
596
+ const char *Leftover;
597
+ } ConsumeUnsigned[] = {
598
+ {" 0" , 0 , " " },
599
+ {" 255" , 255 , " " },
600
+ {" 256" , 256 , " " },
601
+ {" 65535" , 65535 , " " },
602
+ {" 65536" , 65536 , " " },
603
+ {" 4294967295" , 4294967295ULL , " " },
604
+ {" 4294967296" , 4294967296ULL , " " },
605
+ {" 255A376" , 255 , " A376" },
606
+ {" 18446744073709551615" , 18446744073709551615ULL , " " },
607
+ {" 18446744073709551615ABC" , 18446744073709551615ULL , " ABC" },
608
+ {" 042" , 34 , " " },
609
+ {" 0x42" , 66 , " " },
610
+ {" 0x42-0x34" , 66 , " -0x34" },
611
+ {" 0b101010" , 42 , " " },
612
+ {" 0429F" , 042 , " 9F" }, // Auto-sensed octal radix, invalid digit
613
+ {" 0x42G12" , 0x42 , " G12" }, // Auto-sensed hex radix, invalid digit
614
+ {" 0b10101020101" , 42 , " 20101" }}; // Auto-sensed binary radix, invalid digit.
615
+
616
+ struct ConsumeSignedPair {
617
+ const char *Str;
618
+ int64_t Expected;
619
+ const char *Leftover;
620
+ } ConsumeSigned[] = {
621
+ {" 0" , 0 , " " },
622
+ {" -0" , 0 , " " },
623
+ {" 0-1" , 0 , " -1" },
624
+ {" -0-1" , 0 , " -1" },
625
+ {" 127" , 127 , " " },
626
+ {" 128" , 128 , " " },
627
+ {" 127-1" , 127 , " -1" },
628
+ {" 128-1" , 128 , " -1" },
629
+ {" -128" , -128 , " " },
630
+ {" -129" , -129 , " " },
631
+ {" -128-1" , -128 , " -1" },
632
+ {" -129-1" , -129 , " -1" },
633
+ {" 32767" , 32767 , " " },
634
+ {" 32768" , 32768 , " " },
635
+ {" 32767-1" , 32767 , " -1" },
636
+ {" 32768-1" , 32768 , " -1" },
637
+ {" -32768" , -32768 , " " },
638
+ {" -32769" , -32769 , " " },
639
+ {" -32768-1" , -32768 , " -1" },
640
+ {" -32769-1" , -32769 , " -1" },
641
+ {" 2147483647" , 2147483647LL , " " },
642
+ {" 2147483648" , 2147483648LL , " " },
643
+ {" 2147483647-1" , 2147483647LL , " -1" },
644
+ {" 2147483648-1" , 2147483648LL , " -1" },
645
+ {" -2147483648" , -2147483648LL , " " },
646
+ {" -2147483649" , -2147483649LL , " " },
647
+ {" -2147483648-1" , -2147483648LL , " -1" },
648
+ {" -2147483649-1" , -2147483649LL , " -1" },
649
+ {" -9223372036854775808" , -(9223372036854775807LL ) - 1 , " " },
650
+ {" -9223372036854775808-1" , -(9223372036854775807LL ) - 1 , " -1" },
651
+ {" 042" , 34 , " " },
652
+ {" 042-1" , 34 , " -1" },
653
+ {" 0x42" , 66 , " " },
654
+ {" 0x42-1" , 66 , " -1" },
655
+ {" 0b101010" , 42 , " " },
656
+ {" 0b101010-1" , 42 , " -1" },
657
+ {" -042" , -34 , " " },
658
+ {" -042-1" , -34 , " -1" },
659
+ {" -0x42" , -66 , " " },
660
+ {" -0x42-1" , -66 , " -1" },
661
+ {" -0b101010" , -42 , " " },
662
+ {" -0b101010-1" , -42 , " -1" }};
663
+
664
+ TEST (StringRefTest, consumeIntegerUnsigned) {
665
+ uint8_t U8;
666
+ uint16_t U16;
667
+ uint32_t U32;
668
+ uint64_t U64;
669
+
670
+ for (size_t i = 0 ; i < array_lengthof (ConsumeUnsigned); ++i) {
671
+ StringRef Str = ConsumeUnsigned[i].Str ;
672
+ bool U8Success = Str.consumeInteger (0 , U8);
673
+ if (static_cast <uint8_t >(ConsumeUnsigned[i].Expected ) ==
674
+ ConsumeUnsigned[i].Expected ) {
675
+ ASSERT_FALSE (U8Success);
676
+ EXPECT_EQ (U8, ConsumeUnsigned[i].Expected );
677
+ EXPECT_EQ (Str, ConsumeUnsigned[i].Leftover );
678
+ } else {
679
+ ASSERT_TRUE (U8Success);
680
+ }
681
+
682
+ Str = ConsumeUnsigned[i].Str ;
683
+ bool U16Success = Str.consumeInteger (0 , U16);
684
+ if (static_cast <uint16_t >(ConsumeUnsigned[i].Expected ) ==
685
+ ConsumeUnsigned[i].Expected ) {
686
+ ASSERT_FALSE (U16Success);
687
+ EXPECT_EQ (U16, ConsumeUnsigned[i].Expected );
688
+ EXPECT_EQ (Str, ConsumeUnsigned[i].Leftover );
689
+ } else {
690
+ ASSERT_TRUE (U16Success);
691
+ }
692
+
693
+ Str = ConsumeUnsigned[i].Str ;
694
+ bool U32Success = Str.consumeInteger (0 , U32);
695
+ if (static_cast <uint32_t >(ConsumeUnsigned[i].Expected ) ==
696
+ ConsumeUnsigned[i].Expected ) {
697
+ ASSERT_FALSE (U32Success);
698
+ EXPECT_EQ (U32, ConsumeUnsigned[i].Expected );
699
+ EXPECT_EQ (Str, ConsumeUnsigned[i].Leftover );
700
+ } else {
701
+ ASSERT_TRUE (U32Success);
702
+ }
703
+
704
+ Str = ConsumeUnsigned[i].Str ;
705
+ bool U64Success = Str.consumeInteger (0 , U64);
706
+ if (static_cast <uint64_t >(ConsumeUnsigned[i].Expected ) ==
707
+ ConsumeUnsigned[i].Expected ) {
708
+ ASSERT_FALSE (U64Success);
709
+ EXPECT_EQ (U64, ConsumeUnsigned[i].Expected );
710
+ EXPECT_EQ (Str, ConsumeUnsigned[i].Leftover );
711
+ } else {
712
+ ASSERT_TRUE (U64Success);
713
+ }
714
+ }
715
+ }
716
+
717
+ TEST (StringRefTest, consumeIntegerSigned) {
718
+ int8_t S8;
719
+ int16_t S16;
720
+ int32_t S32;
721
+ int64_t S64;
722
+
723
+ for (size_t i = 0 ; i < array_lengthof (ConsumeSigned); ++i) {
724
+ StringRef Str = ConsumeSigned[i].Str ;
725
+ bool S8Success = Str.consumeInteger (0 , S8);
726
+ if (static_cast <int8_t >(ConsumeSigned[i].Expected ) ==
727
+ ConsumeSigned[i].Expected ) {
728
+ ASSERT_FALSE (S8Success);
729
+ EXPECT_EQ (S8, ConsumeSigned[i].Expected );
730
+ EXPECT_EQ (Str, ConsumeSigned[i].Leftover );
731
+ } else {
732
+ ASSERT_TRUE (S8Success);
733
+ }
734
+
735
+ Str = ConsumeSigned[i].Str ;
736
+ bool S16Success = Str.consumeInteger (0 , S16);
737
+ if (static_cast <int16_t >(ConsumeSigned[i].Expected ) ==
738
+ ConsumeSigned[i].Expected ) {
739
+ ASSERT_FALSE (S16Success);
740
+ EXPECT_EQ (S16, ConsumeSigned[i].Expected );
741
+ EXPECT_EQ (Str, ConsumeSigned[i].Leftover );
742
+ } else {
743
+ ASSERT_TRUE (S16Success);
744
+ }
745
+
746
+ Str = ConsumeSigned[i].Str ;
747
+ bool S32Success = Str.consumeInteger (0 , S32);
748
+ if (static_cast <int32_t >(ConsumeSigned[i].Expected ) ==
749
+ ConsumeSigned[i].Expected ) {
750
+ ASSERT_FALSE (S32Success);
751
+ EXPECT_EQ (S32, ConsumeSigned[i].Expected );
752
+ EXPECT_EQ (Str, ConsumeSigned[i].Leftover );
753
+ } else {
754
+ ASSERT_TRUE (S32Success);
755
+ }
756
+
757
+ Str = ConsumeSigned[i].Str ;
758
+ bool S64Success = Str.consumeInteger (0 , S64);
759
+ if (static_cast <int64_t >(ConsumeSigned[i].Expected ) ==
760
+ ConsumeSigned[i].Expected ) {
761
+ ASSERT_FALSE (S64Success);
762
+ EXPECT_EQ (S64, ConsumeSigned[i].Expected );
763
+ EXPECT_EQ (Str, ConsumeSigned[i].Leftover );
764
+ } else {
765
+ ASSERT_TRUE (S64Success);
766
+ }
767
+ }
768
+ }
769
+
593
770
static const char *join_input[] = { " a" , " b" , " c" };
594
771
static const char join_result1[] = " a" ;
595
772
static const char join_result2[] = " a:b:c" ;
0 commit comments