Skip to content

Commit 1e938e3

Browse files
committed
---
yaml --- r: 708 b: refs/heads/master c: 2880ecd h: refs/heads/master v: v3
1 parent 017242b commit 1e938e3

17 files changed

+853
-709
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
---
2-
refs/heads/master: 738fe078a43c3f184d0e147f89042474a61fa779
2+
refs/heads/master: 2880ecd73ff7443ad72eb7af3c85e673024fc7fd

trunk/src/lib/_int.rs

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,27 @@ fn nonpositive(int x) -> bool { ret x <= 0; }
1919
fn nonnegative(int x) -> bool { ret x >= 0; }
2020

2121
iter range(mutable int lo, int hi) -> int {
22-
while (lo < hi) {
23-
put lo;
24-
lo += 1;
25-
}
22+
while (lo < hi) {
23+
put lo;
24+
lo += 1;
25+
}
2626
}
2727

2828
fn to_str(mutable int n, uint radix) -> str
2929
{
30-
check (0u < radix && radix <= 16u);
31-
if (n < 0) {
32-
ret "-" + _uint.to_str((-n) as uint, radix);
33-
} else {
34-
ret _uint.to_str(n as uint, radix);
35-
}
30+
check (0u < radix && radix <= 16u);
31+
if (n < 0) {
32+
ret "-" + _uint.to_str((-n) as uint, radix);
33+
} else {
34+
ret _uint.to_str(n as uint, radix);
35+
}
3636
}
37+
38+
// Local Variables:
39+
// mode: rust;
40+
// fill-column: 78;
41+
// indent-tabs-mode: nil
42+
// c-basic-offset: 4
43+
// buffer-file-coding-system: utf-8-unix
44+
// compile-command: "make -k -C .. 2>&1 | sed -e 's/\\/x\\//x:\\//g'";
45+
// End:

trunk/src/lib/_str.rs

Lines changed: 136 additions & 135 deletions
Original file line numberDiff line numberDiff line change
@@ -3,58 +3,58 @@ import rustrt.sbuf;
33
import std._vec.rustrt.vbuf;
44

55
native "rust" mod rustrt {
6-
type sbuf;
7-
fn str_buf(str s) -> sbuf;
8-
fn str_byte_len(str s) -> uint;
9-
fn str_alloc(uint n_bytes) -> str;
10-
fn str_from_vec(vec[u8] b) -> str;
11-
fn refcount[T](str s) -> uint;
6+
type sbuf;
7+
fn str_buf(str s) -> sbuf;
8+
fn str_byte_len(str s) -> uint;
9+
fn str_alloc(uint n_bytes) -> str;
10+
fn str_from_vec(vec[u8] b) -> str;
11+
fn refcount[T](str s) -> uint;
1212
}
1313

1414
fn eq(&str a, &str b) -> bool {
15-
let uint i = byte_len(a);
16-
if (byte_len(b) != i) {
17-
ret false;
18-
}
19-
while (i > 0u) {
20-
i -= 1u;
21-
auto cha = a.(i);
22-
auto chb = b.(i);
23-
if (cha != chb) {
24-
ret false;
15+
let uint i = byte_len(a);
16+
if (byte_len(b) != i) {
17+
ret false;
18+
}
19+
while (i > 0u) {
20+
i -= 1u;
21+
auto cha = a.(i);
22+
auto chb = b.(i);
23+
if (cha != chb) {
24+
ret false;
25+
}
2526
}
26-
}
27-
ret true;
27+
ret true;
2828
}
2929

3030
fn hash(&str s) -> uint {
31-
// djb hash.
32-
// FIXME: replace with murmur.
33-
let uint u = 5381u;
34-
for (u8 c in s) {
35-
u *= 33u;
36-
u += (c as uint);
37-
}
38-
ret u;
31+
// djb hash.
32+
// FIXME: replace with murmur.
33+
let uint u = 5381u;
34+
for (u8 c in s) {
35+
u *= 33u;
36+
u += (c as uint);
37+
}
38+
ret u;
3939
}
4040

4141
fn is_utf8(vec[u8] v) -> bool {
42-
fail; // FIXME
42+
fail; // FIXME
4343
}
4444

4545
fn is_ascii(str s) -> bool {
46-
let uint i = byte_len(s);
47-
while (i > 0u) {
48-
i -= 1u;
49-
if ((s.(i) & 0x80u8) != 0u8) {
50-
ret false;
46+
let uint i = byte_len(s);
47+
while (i > 0u) {
48+
i -= 1u;
49+
if ((s.(i) & 0x80u8) != 0u8) {
50+
ret false;
51+
}
5152
}
52-
}
53-
ret true;
53+
ret true;
5454
}
5555

5656
fn alloc(uint n_bytes) -> str {
57-
ret rustrt.str_alloc(n_bytes);
57+
ret rustrt.str_alloc(n_bytes);
5858
}
5959

6060
// Returns the number of bytes (a.k.a. UTF-8 code units) in s.
@@ -63,159 +63,160 @@ fn alloc(uint n_bytes) -> str {
6363
// http://icu-project.org/apiref/icu4c/classBreakIterator.html for a
6464
// way to implement those.
6565
fn byte_len(str s) -> uint {
66-
ret rustrt.str_byte_len(s);
66+
ret rustrt.str_byte_len(s);
6767
}
6868

6969
fn buf(str s) -> sbuf {
70-
ret rustrt.str_buf(s);
70+
ret rustrt.str_buf(s);
7171
}
7272

7373
fn bytes(str s) -> vec[u8] {
74-
/* FIXME (issue #58):
75-
* Should be...
76-
*
77-
* fn ith(str s, uint i) -> u8 {
78-
* ret s.(i);
79-
* }
80-
* ret _vec.init_fn[u8](bind ith(s, _), byte_len(s));
81-
*
82-
* but we do not correctly decrement refcount of s when
83-
* the binding dies, so we have to do this manually.
84-
*/
85-
let uint n = _str.byte_len(s);
86-
let vec[u8] v = _vec.alloc[u8](n);
87-
let uint i = 0u;
88-
while (i < n) {
89-
v += vec(s.(i));
90-
i += 1u;
91-
}
92-
ret v;
74+
/* FIXME (issue #58):
75+
* Should be...
76+
*
77+
* fn ith(str s, uint i) -> u8 {
78+
* ret s.(i);
79+
* }
80+
* ret _vec.init_fn[u8](bind ith(s, _), byte_len(s));
81+
*
82+
* but we do not correctly decrement refcount of s when
83+
* the binding dies, so we have to do this manually.
84+
*/
85+
let uint n = _str.byte_len(s);
86+
let vec[u8] v = _vec.alloc[u8](n);
87+
let uint i = 0u;
88+
while (i < n) {
89+
v += vec(s.(i));
90+
i += 1u;
91+
}
92+
ret v;
9393
}
9494

9595
fn from_bytes(vec[u8] v) : is_utf8(v) -> str {
96-
ret rustrt.str_from_vec(v);
96+
ret rustrt.str_from_vec(v);
9797
}
9898

9999
fn refcount(str s) -> uint {
100-
// -1 because calling this function incremented the refcount.
101-
ret rustrt.refcount[u8](s) - 1u;
100+
// -1 because calling this function incremented the refcount.
101+
ret rustrt.refcount[u8](s) - 1u;
102102
}
103103

104104

105105
// Standard bits from the world of string libraries.
106106

107107
fn index(str s, u8 c) -> int {
108-
let int i = 0;
109-
for (u8 k in s) {
110-
if (k == c) {
111-
ret i;
108+
let int i = 0;
109+
for (u8 k in s) {
110+
if (k == c) {
111+
ret i;
112+
}
113+
i += 1;
112114
}
113-
i += 1;
114-
}
115-
ret -1;
115+
ret -1;
116116
}
117117

118118
fn rindex(str s, u8 c) -> int {
119-
let int n = _str.byte_len(s) as int;
120-
while (n >= 0) {
121-
if (s.(n) == c) {
122-
ret n;
119+
let int n = _str.byte_len(s) as int;
120+
while (n >= 0) {
121+
if (s.(n) == c) {
122+
ret n;
123+
}
124+
n -= 1;
123125
}
124-
n -= 1;
125-
}
126-
ret n;
126+
ret n;
127127
}
128128

129129
fn find(str haystack, str needle) -> int {
130130

131-
let int haystack_len = byte_len(haystack) as int;
132-
let int needle_len = byte_len(needle) as int;
131+
let int haystack_len = byte_len(haystack) as int;
132+
let int needle_len = byte_len(needle) as int;
133133

134-
if (needle_len == 0) {
135-
ret 0;
136-
}
134+
if (needle_len == 0) {
135+
ret 0;
136+
}
137137

138-
fn match_at(&str haystack,
139-
&str needle,
140-
int i) -> bool {
141-
let int j = i;
142-
for (u8 c in needle) {
143-
if (haystack.(j) != c) {
144-
ret false;
145-
}
146-
j += 1;
138+
fn match_at(&str haystack,
139+
&str needle,
140+
int i) -> bool {
141+
let int j = i;
142+
for (u8 c in needle) {
143+
if (haystack.(j) != c) {
144+
ret false;
145+
}
146+
j += 1;
147+
}
148+
ret true;
147149
}
148-
ret true;
149-
}
150150

151-
let int i = 0;
152-
while (i <= haystack_len - needle_len) {
153-
if (match_at(haystack, needle, i)) {
154-
ret i;
151+
let int i = 0;
152+
while (i <= haystack_len - needle_len) {
153+
if (match_at(haystack, needle, i)) {
154+
ret i;
155+
}
156+
i += 1;
155157
}
156-
i += 1;
157-
}
158-
ret -1;
158+
ret -1;
159159
}
160160

161161
fn substr(str s, uint begin, uint len) -> str {
162-
let str accum = "";
163-
let uint i = begin;
164-
while (i < begin+len) {
165-
accum += s.(i);
166-
i += 1u;
167-
}
168-
ret accum;
162+
let str accum = "";
163+
let uint i = begin;
164+
while (i < begin+len) {
165+
accum += s.(i);
166+
i += 1u;
167+
}
168+
ret accum;
169169
}
170170

171171
fn split(str s, u8 sep) -> vec[str] {
172-
let vec[str] v = vec();
173-
let str accum = "";
174-
let bool ends_with_sep = false;
175-
for (u8 c in s) {
176-
if (c == sep) {
177-
v += accum;
178-
accum = "";
179-
ends_with_sep = true;
180-
} else {
181-
accum += c;
182-
ends_with_sep = false;
172+
let vec[str] v = vec();
173+
let str accum = "";
174+
let bool ends_with_sep = false;
175+
for (u8 c in s) {
176+
if (c == sep) {
177+
v += accum;
178+
accum = "";
179+
ends_with_sep = true;
180+
} else {
181+
accum += c;
182+
ends_with_sep = false;
183+
}
184+
}
185+
if (_str.byte_len(accum) != 0u ||
186+
ends_with_sep) {
187+
v += accum;
183188
}
184-
}
185-
if (_str.byte_len(accum) != 0u ||
186-
ends_with_sep) {
187-
v += accum;
188-
}
189-
ret v;
189+
ret v;
190190
}
191191

192192
fn concat(vec[str] v) -> str {
193-
let str s = "";
194-
for (str ss in v) {
195-
s += ss;
196-
}
197-
ret s;
193+
let str s = "";
194+
for (str ss in v) {
195+
s += ss;
196+
}
197+
ret s;
198198
}
199199

200200
fn connect(vec[str] v, str sep) -> str {
201-
let str s = "";
202-
let bool first = true;
203-
for (str ss in v) {
204-
if (first) {
205-
first = false;
206-
} else {
207-
s += sep;
201+
let str s = "";
202+
let bool first = true;
203+
for (str ss in v) {
204+
if (first) {
205+
first = false;
206+
} else {
207+
s += sep;
208+
}
209+
s += ss;
208210
}
209-
s += ss;
210-
}
211-
ret s;
211+
ret s;
212212
}
213213

214214

215215
// Local Variables:
216216
// mode: rust;
217217
// fill-column: 78;
218218
// indent-tabs-mode: nil
219+
// c-basic-offset: 4
219220
// buffer-file-coding-system: utf-8-unix
220221
// compile-command: "make -k -C .. 2>&1 | sed -e 's/\\/x\\//x:\\//g'";
221222
// End:

0 commit comments

Comments
 (0)