Skip to content

Commit 25e6a46

Browse files
killerswannikomatsakis
authored andcommitted
---
yaml --- r: 7741 b: refs/heads/snap-stage3 c: d4b287e h: refs/heads/master i: 7739: 0ca6a36 v: v3
1 parent e236886 commit 25e6a46

File tree

3 files changed

+159
-8
lines changed

3 files changed

+159
-8
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
refs/heads/master: 2898dcc5d97da9427ac367542382b6239d9c0bbf
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
4-
refs/heads/snap-stage3: 0493a7c87d7769a3b3b819a7b38cd242f95dfad2
4+
refs/heads/snap-stage3: d4b287e8525758896e78b39eca6f6e1c0f32cd98
55
refs/heads/try: 2898dcc5d97da9427ac367542382b6239d9c0bbf
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105

branches/snap-stage3/src/libcore/str.rs

Lines changed: 125 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ export eq, lteq, hash, is_empty, is_not_empty, is_whitespace, byte_len,
1414
char_at, bytes, is_ascii, shift_byte, pop_byte,
1515
unsafe_from_byte, unsafe_from_bytes, from_char, char_range_at,
1616
from_cstr, sbuf, as_buf, push_byte, utf8_char_width, safe_slice,
17-
contains, iter_chars, loop_chars, loop_chars_sub,
18-
escape;
17+
contains, iter_chars, chars_iter, bytes_iter,
18+
loop_chars, loop_chars_sub, escape, any, all, map, windowed;
1919

2020
#[abi = "cdecl"]
2121
native mod rustrt {
@@ -346,7 +346,6 @@ Function: iter_chars
346346
347347
Iterate over the characters in a string
348348
*/
349-
350349
fn iter_chars(s: str, it: fn(char)) {
351350
let pos = 0u, len = byte_len(s);
352351
while (pos < len) {
@@ -356,6 +355,34 @@ fn iter_chars(s: str, it: fn(char)) {
356355
}
357356
}
358357

358+
/*
359+
Function: chars_iter
360+
361+
Iterate over the characters in a string
362+
363+
FIXME: A synonym to iter_chars
364+
*/
365+
fn chars_iter(ss: str, it: fn&(char)) {
366+
iter_chars(ss, it)
367+
}
368+
369+
/*
370+
Function: bytes_iter
371+
372+
Iterate over the bytes in a string
373+
374+
FIXME: Should it really include the last byte '\0'?
375+
*/
376+
fn bytes_iter(ss: str, it: fn&(u8)) {
377+
let pos = 0u;
378+
let len = byte_len(ss);
379+
380+
while (pos < len) {
381+
it(ss[pos]);
382+
pos += 1u;
383+
}
384+
}
385+
359386
/*
360387
Function: loop_chars
361388
@@ -1116,14 +1143,25 @@ fn escape(s: str) -> str {
11161143
/*
11171144
Function: all
11181145
1119-
Return true if a predicate matches all characters
1146+
Return true if a predicate matches all characters or
1147+
if the string contains no characters
11201148
1121-
If the string contains no characters
1149+
// FIXME: a synonym to loop_chars
11221150
*/
11231151
fn all(ss: str, ff: fn&(char) -> bool) -> bool {
11241152
str::loop_chars(ss, ff)
11251153
}
11261154

1155+
/*
1156+
Function: any
1157+
1158+
Return true if a predicate matches any character
1159+
(and false if it matches none or there are no characters)
1160+
*/
1161+
fn any(ss: str, pred: fn&(char) -> bool) -> bool {
1162+
!all(ss, {|cc| !pred(cc)})
1163+
}
1164+
11271165
/*
11281166
Function: map
11291167
@@ -1139,6 +1177,26 @@ fn map(ss: str, ff: fn&(char) -> char) -> str {
11391177
ret result;
11401178
}
11411179

1180+
/*
1181+
Function: windowed
1182+
1183+
Create a vector of substrings of size `nn`
1184+
*/
1185+
fn windowed(nn: uint, ss: str) -> [str] {
1186+
let ww = [];
1187+
let len = str::char_len(ss);
1188+
1189+
assert 1u <= nn;
1190+
1191+
let ii = 0u;
1192+
while ii+nn <= len {
1193+
let w = char_slice( ss, ii, ii+nn );
1194+
vec::push(ww,w);
1195+
ii += 1u;
1196+
}
1197+
1198+
ret ww;
1199+
}
11421200

11431201
#[cfg(test)]
11441202
mod tests {
@@ -1590,6 +1648,39 @@ mod tests {
15901648
}
15911649
i += 1;
15921650
}
1651+
1652+
iter_chars("") {|ch| fail; } // should not fail
1653+
}
1654+
1655+
#[test]
1656+
fn test_chars_iter() {
1657+
let i = 0;
1658+
chars_iter("x\u03c0y") {|ch|
1659+
alt i {
1660+
0 { assert ch == 'x'; }
1661+
1 { assert ch == '\u03c0'; }
1662+
2 { assert ch == 'y'; }
1663+
}
1664+
i += 1;
1665+
}
1666+
1667+
chars_iter("") {|_ch| fail; } // should not fail
1668+
}
1669+
1670+
#[test]
1671+
fn test_bytes_iter() {
1672+
let i = 0;
1673+
1674+
bytes_iter("xyz") {|bb|
1675+
alt i {
1676+
0 { assert bb == 'x' as u8; }
1677+
1 { assert bb == 'y' as u8; }
1678+
2 { assert bb == 'z' as u8; }
1679+
}
1680+
i += 1;
1681+
}
1682+
1683+
bytes_iter("") {|bb| assert bb == 0u8; }
15931684
}
15941685

15951686
#[test]
@@ -1601,17 +1692,44 @@ mod tests {
16011692
}
16021693

16031694
#[test]
1604-
fn test_map () {
1695+
fn test_map() {
16051696
assert "" == map("", char::to_upper);
16061697
assert "YMCA" == map("ymca", char::to_upper);
16071698
}
16081699

16091700
#[test]
1610-
fn test_all () {
1701+
fn test_all() {
16111702
assert true == all("", char::is_uppercase);
16121703
assert false == all("ymca", char::is_uppercase);
16131704
assert true == all("YMCA", char::is_uppercase);
16141705
assert false == all("yMCA", char::is_uppercase);
16151706
assert false == all("YMCy", char::is_uppercase);
16161707
}
1708+
1709+
#[test]
1710+
fn test_any() {
1711+
assert false == any("", char::is_uppercase);
1712+
assert false == any("ymca", char::is_uppercase);
1713+
assert true == any("YMCA", char::is_uppercase);
1714+
assert true == any("yMCA", char::is_uppercase);
1715+
assert true == any("YMCy", char::is_uppercase);
1716+
}
1717+
1718+
#[test]
1719+
fn test_windowed() {
1720+
let data = "ประเทศไทย中";
1721+
1722+
assert ["ประ", "ระเ", "ะเท", "เทศ", "ทศไ", "ศไท", "ไทย", "ทย中"]
1723+
== windowed(3u, data);
1724+
1725+
assert [data] == windowed(10u, data);
1726+
1727+
assert [] == windowed(6u, "abcd");
1728+
}
1729+
1730+
#[test]
1731+
#[should_fail]
1732+
fn test_windowed_() {
1733+
let _x = windowed(0u, "abcd");
1734+
}
16171735
}

branches/snap-stage3/src/libcore/vec.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -829,6 +829,23 @@ fn permute<T: copy>(v: [const T], put: fn([T])) {
829829
}
830830
}
831831

832+
fn windowed <TT: copy> (nn: uint, xx: [TT]) -> [[TT]] {
833+
let ww = [];
834+
835+
assert 1u <= nn;
836+
837+
vec::iteri (xx, {|ii, _x|
838+
let len = vec::len(xx);
839+
840+
if ii+nn <= len {
841+
let w = vec::slice ( xx, ii, ii+nn );
842+
vec::push (ww, w);
843+
}
844+
});
845+
846+
ret ww;
847+
}
848+
832849
/*
833850
Function: to_ptr
834851
@@ -1497,6 +1514,22 @@ mod tests {
14971514
assert concat([[1], [2,3]]) == [1, 2, 3];
14981515
}
14991516

1517+
#[test]
1518+
fn test_windowed () {
1519+
assert [[1u,2u,3u],[2u,3u,4u],[3u,4u,5u],[4u,5u,6u]]
1520+
== windowed (3u, [1u,2u,3u,4u,5u,6u]);
1521+
1522+
assert [[1u,2u,3u,4u],[2u,3u,4u,5u],[3u,4u,5u,6u]]
1523+
== windowed (4u, [1u,2u,3u,4u,5u,6u]);
1524+
1525+
assert [] == windowed (7u, [1u,2u,3u,4u,5u,6u]);
1526+
}
1527+
1528+
#[test]
1529+
#[should_fail]
1530+
fn test_windowed_() {
1531+
let _x = windowed (0u, [1u,2u,3u,4u,5u,6u]);
1532+
}
15001533
}
15011534

15021535
// Local Variables:

0 commit comments

Comments
 (0)