@@ -6,11 +6,11 @@ String manipulation.
6
6
7
7
export eq, lteq, hash, is_empty, is_not_empty, is_whitespace, byte_len,
8
8
byte_len_range, index,
9
- rindex, find, starts_with, ends_with, substr, slice, split, concat ,
10
- connect, to_upper, replace, char_slice, trim_left, trim_right, trim ,
11
- unshift_char, shift_char, pop_char, push_char, is_utf8, from_chars ,
12
- to_chars, char_len, char_len_range, char_at, bytes, is_ascii ,
13
- shift_byte, pop_byte,
9
+ rindex, find, starts_with, ends_with, substr, slice, split, split_str ,
10
+ concat , connect, to_upper, replace, char_slice, trim_left, trim_right,
11
+ trim , unshift_char, shift_char, pop_char, push_char, is_utf8,
12
+ from_chars , to_chars, char_len, char_len_range, char_at, bytes,
13
+ is_ascii , shift_byte, pop_byte,
14
14
unsafe_from_byte, unsafe_from_bytes, from_char, char_range_at,
15
15
str_from_cstr, sbuf, as_buf, push_byte, utf8_char_width, safe_slice,
16
16
contains, iter_chars, loop_chars, loop_chars_sub,
@@ -744,6 +744,42 @@ fn split(s: str, sep: u8) -> [str] {
744
744
ret v;
745
745
}
746
746
747
+ /*
748
+ Function: split_str
749
+
750
+ Splits a string at each occurrence of the given separator string. Empty
751
+ leading fields are suppressed, and empty trailing fields are preserved.
752
+
753
+ Returns:
754
+
755
+ A vector containing all the strings between each occurrence of the separator.
756
+ */
757
+ fn split_str ( s : str , sep : str ) -> [ str ] {
758
+ assert byte_len ( sep) > 0 u;
759
+ let v: [ str ] = [ ] , accum = "" , sep_match = 0 u, leading = true ;
760
+ for c: u8 in s {
761
+ // Did we match the entire separator?
762
+ if sep_match == byte_len ( sep) {
763
+ if !leading { v += [ accum] ; }
764
+ accum = "" ;
765
+ sep_match = 0 u;
766
+ }
767
+
768
+ if c == sep[ sep_match] {
769
+ sep_match += 1 u;
770
+ } else {
771
+ sep_match = 0 u;
772
+ accum += unsafe_from_byte ( c) ;
773
+ leading = false ;
774
+ }
775
+ }
776
+
777
+ if byte_len ( accum) > 0 u { v += [ accum] ; }
778
+ if sep_match == byte_len ( sep) { v += [ "" ] ; }
779
+
780
+ ret v;
781
+ }
782
+
747
783
/*
748
784
Function: concat
749
785
0 commit comments