@@ -678,98 +678,6 @@ pub fn pad_end(self : String, total_width : Int, padding_char : Char) -> String
678
678
}
679
679
}
680
680
681
- ///|
682
- /// Returns the character at the given index from the end of the string.
683
- ///
684
- /// # Examples
685
- ///
686
- /// ```moonbit
687
- /// let s = "Hello🤣🤣🤣"
688
- /// inspect!(s.rev_get(0), content="🤣")
689
- /// inspect!(s.rev_get(4), content="l")
690
- /// ```
691
- ///
692
- /// # Panics
693
- ///
694
- /// @alert unsafe "Panics if the index is out of bounds."
695
- pub fn String ::rev_get (self : String , index : Int ) -> Char {
696
- guard index >= 0 else { abort ("index out of bounds" ) }
697
- for utf16_offset = self .length () - 1 , char_count = 0
698
- utf16_offset >= 0 && char_count < index
699
- utf16_offset = utf16_offset - 1 , char_count = char_count + 1 {
700
- let c1 = self .unsafe_charcode_at (utf16_offset )
701
- if is_trailing_surrogate (c1 ) && utf16_offset - 1 >= 0 {
702
- let c2 = self .unsafe_charcode_at (utf16_offset - 1 )
703
- if is_leading_surrogate (c2 ) {
704
- continue utf16_offset - 2 , char_count + 1
705
- } else {
706
- abort ("invalid surrogate pair" )
707
- }
708
- }
709
- } else {
710
- guard char_count == index && utf16_offset >= 0 else {
711
- abort ("index out of bounds" )
712
- }
713
- let c1 = self .unsafe_charcode_at (utf16_offset )
714
- if is_trailing_surrogate (c1 ) && utf16_offset - 1 >= 0 {
715
- let c2 = self .unsafe_charcode_at (utf16_offset - 1 )
716
- if is_leading_surrogate (c2 ) {
717
- code_point_of_surrogate_pair (c2 , c1 )
718
- } else {
719
- abort ("invalid surrogate pair" )
720
- }
721
- } else {
722
- Char ::from_int (c1 )
723
- }
724
- }
725
- }
726
-
727
- ///|
728
- /// Test if the length of the string is equal to the given length.
729
- ///
730
- /// This has O(n) complexity where n is the length in the parameter.
731
- pub fn String ::length_eq (self : String , len : Int ) -> Bool {
732
- let codeunit_len = self .length ()
733
- for index = 0 , count = 0
734
- index < codeunit_len && count < len
735
- index = index + 1 , count = count + 1 {
736
- let c1 = self .unsafe_charcode_at (index )
737
- if is_leading_surrogate (c1 ) && index + 1 < codeunit_len {
738
- let c2 = self .unsafe_charcode_at (index + 1 )
739
- if is_trailing_surrogate (c2 ) {
740
- continue index + 2 , count + 1
741
- } else {
742
- abort ("invalid surrogate pair" )
743
- }
744
- }
745
- } else {
746
- count == len && index == codeunit_len
747
- }
748
- }
749
-
750
- ///|
751
- /// Test if the length of the string is greater than or equal to the given length.
752
- ///
753
- /// This has O(n) complexity where n is the length in the parameter.
754
- pub fn String ::length_ge (self : String , len : Int ) -> Bool {
755
- let codeunit_len = self .length ()
756
- for index = 0 , count = 0
757
- index < codeunit_len && count < len
758
- index = index + 1 , count = count + 1 {
759
- let c1 = self .unsafe_charcode_at (index )
760
- if is_leading_surrogate (c1 ) && index + 1 < codeunit_len {
761
- let c2 = self .unsafe_charcode_at (index + 1 )
762
- if is_trailing_surrogate (c2 ) {
763
- continue index + 2 , count + 1
764
- } else {
765
- abort ("invalid surrogate pair" )
766
- }
767
- }
768
- } else {
769
- count >= len
770
- }
771
- }
772
-
773
681
///|
774
682
/// Returns the index of the n-th (zero-indexed) character within the range [start, end).
775
683
fn String ::offset_of_nth_char_forward (
@@ -854,3 +762,66 @@ pub fn String::offset_of_nth_char(
854
762
self .offset_of_nth_char_backward (- i , start_offset ~, end_offset ~)
855
763
}
856
764
}
765
+
766
+ ///|
767
+ /// Returns the Unicode character at the given index.
768
+ pub fn String ::char_at (self : String , index : Int ) -> Char {
769
+ guard index >= 0 && index < self .length () else {
770
+ abort ("index out of bounds" )
771
+ }
772
+ self .unsafe_char_at (index )
773
+ }
774
+
775
+ ///|
776
+ /// Test if the length of the string is equal to the given length.
777
+ ///
778
+ /// This has O(n) complexity where n is the length in the parameter.
779
+ pub fn String ::char_length_eq (
780
+ self : String ,
781
+ len : Int ,
782
+ start_offset ~ : Int = 0 ,
783
+ end_offset ~ : Int = self .length ()
784
+ ) -> Bool {
785
+ for index = start_offset , count = 0
786
+ index < end_offset && count < len
787
+ index = index + 1 , count = count + 1 {
788
+ let c1 = self .unsafe_charcode_at (index )
789
+ if is_leading_surrogate (c1 ) && index + 1 < end_offset {
790
+ let c2 = self .unsafe_charcode_at (index + 1 )
791
+ if is_trailing_surrogate (c2 ) {
792
+ continue index + 2 , count + 1
793
+ } else {
794
+ abort ("invalid surrogate pair" )
795
+ }
796
+ }
797
+ } else {
798
+ count == len && index == end_offset
799
+ }
800
+ }
801
+
802
+ ///|
803
+ /// Test if the length of the string is greater than or equal to the given length.
804
+ ///
805
+ /// This has O(n) complexity where n is the length in the parameter.
806
+ pub fn String ::char_length_ge (
807
+ self : String ,
808
+ len : Int ,
809
+ start_offset ~ : Int = 0 ,
810
+ end_offset ~ : Int = self .length ()
811
+ ) -> Bool {
812
+ for index = start_offset , count = 0
813
+ index < end_offset && count < len
814
+ index = index + 1 , count = count + 1 {
815
+ let c1 = self .unsafe_charcode_at (index )
816
+ if is_leading_surrogate (c1 ) && index + 1 < end_offset {
817
+ let c2 = self .unsafe_charcode_at (index + 1 )
818
+ if is_trailing_surrogate (c2 ) {
819
+ continue index + 2 , count + 1
820
+ } else {
821
+ abort ("invalid surrogate pair" )
822
+ }
823
+ }
824
+ } else {
825
+ count >= len
826
+ }
827
+ }
0 commit comments