@@ -224,8 +224,8 @@ Section: Adding to and removing from a string
224
224
pub fn pop_char ( s : & mut ~str ) -> char {
225
225
let end = len ( * s) ;
226
226
assert end > 0 u;
227
- let { ch, prev } = char_range_at_reverse ( * s, end) ;
228
- unsafe { raw:: set_len ( s, prev ) ; }
227
+ let CharRange { ch, next } = char_range_at_reverse ( * s, end) ;
228
+ unsafe { raw:: set_len ( s, next ) ; }
229
229
return ch;
230
230
}
231
231
@@ -237,7 +237,7 @@ pub fn pop_char(s: &mut ~str) -> char {
237
237
* If the string does not contain any characters
238
238
*/
239
239
pub fn shift_char ( s : & mut ~str ) -> char {
240
- let { ch, next} = char_range_at ( * s, 0 u) ;
240
+ let CharRange { ch, next} = char_range_at ( * s, 0 u) ;
241
241
* s = unsafe { raw:: slice_bytes ( * s, next, len ( * s) ) } ;
242
242
return ch;
243
243
}
@@ -253,7 +253,7 @@ pub fn shift_char(s: &mut ~str) -> char {
253
253
*/
254
254
#[ inline]
255
255
pub fn view_shift_char ( s : & a/str ) -> ( char , & a/str ) {
256
- let { ch, next} = char_range_at ( s, 0 u) ;
256
+ let CharRange { ch, next} = char_range_at ( s, 0 u) ;
257
257
let next_s = unsafe { raw:: view_bytes ( s, next, len ( s) ) } ;
258
258
return ( ch, next_s) ;
259
259
}
@@ -296,7 +296,7 @@ pub pure fn trim_right_chars(s: &str, chars_to_trim: &[char]) -> ~str {
296
296
match rfind ( s, |c| !chars_to_trim. contains ( & c) ) {
297
297
None => ~"",
298
298
Some ( last) => {
299
- let { next, _ } = char_range_at ( s, last) ;
299
+ let next = char_range_at ( s, last) . next ;
300
300
unsafe { raw:: slice_bytes ( s, 0 u, next) }
301
301
}
302
302
}
@@ -328,7 +328,7 @@ pub pure fn trim_right(s: &str) -> ~str {
328
328
match rfind ( s, |c| !char:: is_whitespace ( c) ) {
329
329
None => ~"",
330
330
Some ( last) => {
331
- let { next, _ } = char_range_at ( s, last) ;
331
+ let next = char_range_at ( s, last) . next ;
332
332
unsafe { raw:: slice_bytes ( s, 0 u, next) }
333
333
}
334
334
}
@@ -365,7 +365,7 @@ pub pure fn chars(s: &str) -> ~[char] {
365
365
let mut buf = ~[ ] , i = 0 ;
366
366
let len = len ( s) ;
367
367
while i < len {
368
- let { ch, next} = char_range_at ( s, i) ;
368
+ let CharRange { ch, next} = char_range_at ( s, i) ;
369
369
unsafe { buf. push ( ch) ; }
370
370
i = next;
371
371
}
@@ -475,7 +475,7 @@ pure fn split_inner(s: &str, sepfn: fn(cc: char) -> bool, count: uint,
475
475
let l = len ( s) ;
476
476
let mut result = ~[ ] , i = 0 u, start = 0 u, done = 0 u;
477
477
while i < l && done < count {
478
- let { ch, next} = char_range_at ( s, i) ;
478
+ let CharRange { ch, next} = char_range_at ( s, i) ;
479
479
if sepfn ( ch) {
480
480
if allow_empty || start < i unsafe {
481
481
result. push ( unsafe { raw:: slice_bytes ( s, start, i) } ) ;
@@ -866,7 +866,7 @@ pub pure fn each_chari(s: &str, it: fn(uint, char) -> bool) {
866
866
let mut pos = 0 u, ch_pos = 0 u;
867
867
let len = len ( s) ;
868
868
while pos < len {
869
- let { ch, next} = char_range_at ( s, pos) ;
869
+ let CharRange { ch, next} = char_range_at ( s, pos) ;
870
870
pos = next;
871
871
if !it ( ch_pos, ch) { break ; }
872
872
ch_pos += 1 u;
@@ -878,7 +878,7 @@ pub pure fn chars_each(s: &str, it: fn(char) -> bool) {
878
878
let mut pos = 0 u;
879
879
let len = len ( s) ;
880
880
while ( pos < len) {
881
- let { ch, next} = char_range_at ( s, pos) ;
881
+ let CharRange { ch, next} = char_range_at ( s, pos) ;
882
882
pos = next;
883
883
if !it ( ch) { return ; }
884
884
}
@@ -1144,7 +1144,7 @@ pub pure fn find_between(s: &str, start: uint, end: uint, f: fn(char) -> bool)
1144
1144
assert is_char_boundary ( s, start) ;
1145
1145
let mut i = start;
1146
1146
while i < end {
1147
- let { ch, next} = char_range_at ( s, i) ;
1147
+ let CharRange { ch, next} = char_range_at ( s, i) ;
1148
1148
if f ( ch) { return Some ( i) ; }
1149
1149
i = next;
1150
1150
}
@@ -1224,7 +1224,7 @@ pub pure fn rfind_between(s: &str, start: uint, end: uint,
1224
1224
assert is_char_boundary ( s, start) ;
1225
1225
let mut i = start;
1226
1226
while i > end {
1227
- let { ch, prev} = char_range_at_reverse ( s, i) ;
1227
+ let CharRange { ch, next : prev} = char_range_at_reverse ( s, i) ;
1228
1228
if f ( ch) { return Some ( prev) ; }
1229
1229
i = prev;
1230
1230
}
@@ -1538,7 +1538,7 @@ pub pure fn count_chars(s: &str, start: uint, end: uint) -> uint {
1538
1538
assert is_char_boundary ( s, end) ;
1539
1539
let mut i = start, len = 0 u;
1540
1540
while i < end {
1541
- let { next, _ } = char_range_at ( s, i) ;
1541
+ let next = char_range_at ( s, i) . next ;
1542
1542
len += 1 u;
1543
1543
i = next;
1544
1544
}
@@ -1552,7 +1552,7 @@ pub pure fn count_bytes(s: &b/str, start: uint, n: uint) -> uint {
1552
1552
let l = len ( s) ;
1553
1553
while cnt > 0 u {
1554
1554
assert end < l;
1555
- let { next, _ } = char_range_at ( s, end) ;
1555
+ let next = char_range_at ( s, end) . next ;
1556
1556
cnt -= 1 u;
1557
1557
end = next;
1558
1558
}
@@ -1595,7 +1595,7 @@ pub pure fn is_char_boundary(s: &str, index: uint) -> bool {
1595
1595
* let s = "中华Việt Nam";
1596
1596
* let i = 0u;
1597
1597
* while i < str::len(s) {
1598
- * let {ch, next} = str::char_range_at(s, i);
1598
+ * let CharRange {ch, next} = str::char_range_at(s, i);
1599
1599
* std::io::println(fmt!("%u: %c",i,ch));
1600
1600
* i = next;
1601
1601
* }
@@ -1631,11 +1631,11 @@ pub pure fn is_char_boundary(s: &str, index: uint) -> bool {
1631
1631
* If `i` is greater than or equal to the length of the string.
1632
1632
* If `i` is not the index of the beginning of a valid UTF-8 character.
1633
1633
*/
1634
- pub pure fn char_range_at ( s : & str , i : uint ) -> { ch : char , next : uint } {
1634
+ pub pure fn char_range_at ( s : & str , i : uint ) -> CharRange {
1635
1635
let b0 = s[ i] ;
1636
1636
let w = utf8_char_width ( b0) ;
1637
1637
assert ( w != 0 u) ;
1638
- if w == 1 u { return { ch: b0 as char , next: i + 1 u} ; }
1638
+ if w == 1 u { return CharRange { ch : b0 as char , next : i + 1 u} ; }
1639
1639
let mut val = 0 u;
1640
1640
let end = i + w;
1641
1641
let mut i = i + 1 u;
@@ -1650,21 +1650,26 @@ pub pure fn char_range_at(s: &str, i: uint) -> {ch: char, next: uint} {
1650
1650
// the first to clip off the marker bits at the left of the byte, and then
1651
1651
// a second (as uint) to get it to the right position.
1652
1652
val += ( ( b0 << ( ( w + 1 u) as u8 ) ) as uint ) << ( ( w - 1 u) * 6 u - w - 1 u) ;
1653
- return { ch: val as char , next: i} ;
1653
+ return CharRange { ch : val as char , next : i} ;
1654
1654
}
1655
1655
1656
1656
/// Pluck a character out of a string
1657
1657
pub pure fn char_at ( s : & str , i : uint ) -> char {
1658
1658
return char_range_at ( s, i) . ch ;
1659
1659
}
1660
1660
1661
+ pub struct CharRange {
1662
+ ch : char ,
1663
+ next : uint
1664
+ }
1665
+
1661
1666
/**
1662
1667
* Given a byte position and a str, return the previous char and its position
1663
1668
*
1664
1669
* This function can be used to iterate over a unicode string in reverse.
1665
1670
*/
1666
1671
pure fn char_range_at_reverse ( ss : & str , start : uint )
1667
- -> { ch : char , prev : uint } {
1672
+ -> CharRange {
1668
1673
1669
1674
let mut prev = start;
1670
1675
@@ -1677,7 +1682,7 @@ pure fn char_range_at_reverse(ss: &str, start: uint)
1677
1682
prev -= 1 u;
1678
1683
1679
1684
let ch = char_at ( ss, prev) ;
1680
- return { ch: ch, prev : prev} ;
1685
+ return CharRange { ch : ch, next : prev} ;
1681
1686
}
1682
1687
1683
1688
/**
@@ -1707,7 +1712,7 @@ pub pure fn all_between(s: &str, start: uint, end: uint,
1707
1712
assert is_char_boundary ( s, start) ;
1708
1713
let mut i = start;
1709
1714
while i < end {
1710
- let { ch, next} = char_range_at ( s, i) ;
1715
+ let CharRange { ch, next} = char_range_at ( s, i) ;
1711
1716
if !it ( ch) { return false ; }
1712
1717
i = next;
1713
1718
}
0 commit comments