@@ -3,58 +3,58 @@ import rustrt.sbuf;
3
3
import std. _vec . rustrt . vbuf ;
4
4
5
5
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 ;
12
12
}
13
13
14
14
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 > 0 u) {
20
- i -= 1 u;
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 > 0 u) {
20
+ i -= 1 u;
21
+ auto cha = a. ( i) ;
22
+ auto chb = b. ( i) ;
23
+ if ( cha != chb) {
24
+ ret false ;
25
+ }
25
26
}
26
- }
27
- ret true;
27
+ ret true;
28
28
}
29
29
30
30
fn hash ( & str s) -> uint {
31
- // djb hash.
32
- // FIXME: replace with murmur.
33
- let uint u = 5381 u;
34
- for ( u8 c in s) {
35
- u *= 33 u;
36
- u += ( c as uint ) ;
37
- }
38
- ret u;
31
+ // djb hash.
32
+ // FIXME: replace with murmur.
33
+ let uint u = 5381 u;
34
+ for ( u8 c in s) {
35
+ u *= 33 u;
36
+ u += ( c as uint ) ;
37
+ }
38
+ ret u;
39
39
}
40
40
41
41
fn is_utf8 ( vec[ u8] v ) -> bool {
42
- fail; // FIXME
42
+ fail; // FIXME
43
43
}
44
44
45
45
fn is_ascii ( str s) -> bool {
46
- let uint i = byte_len ( s) ;
47
- while ( i > 0 u) {
48
- i -= 1 u;
49
- if ( ( s. ( i) & 0x80u8 ) != 0u8 ) {
50
- ret false ;
46
+ let uint i = byte_len ( s) ;
47
+ while ( i > 0 u) {
48
+ i -= 1 u;
49
+ if ( ( s. ( i) & 0x80u8 ) != 0u8 ) {
50
+ ret false ;
51
+ }
51
52
}
52
- }
53
- ret true;
53
+ ret true;
54
54
}
55
55
56
56
fn alloc ( uint n_bytes ) -> str {
57
- ret rustrt. str_alloc ( n_bytes) ;
57
+ ret rustrt. str_alloc ( n_bytes) ;
58
58
}
59
59
60
60
// Returns the number of bytes (a.k.a. UTF-8 code units) in s.
@@ -63,159 +63,160 @@ fn alloc(uint n_bytes) -> str {
63
63
// http://icu-project.org/apiref/icu4c/classBreakIterator.html for a
64
64
// way to implement those.
65
65
fn byte_len ( str s) -> uint {
66
- ret rustrt. str_byte_len ( s) ;
66
+ ret rustrt. str_byte_len ( s) ;
67
67
}
68
68
69
69
fn buf ( str s) -> sbuf {
70
- ret rustrt. str_buf ( s) ;
70
+ ret rustrt. str_buf ( s) ;
71
71
}
72
72
73
73
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 = 0 u;
88
- while ( i < n) {
89
- v += vec ( s. ( i) ) ;
90
- i += 1 u;
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 = 0 u;
88
+ while ( i < n) {
89
+ v += vec ( s. ( i) ) ;
90
+ i += 1 u;
91
+ }
92
+ ret v;
93
93
}
94
94
95
95
fn from_bytes ( vec[ u8] v ) : is_utf8( v ) -> str {
96
- ret rustrt. str_from_vec ( v) ;
96
+ ret rustrt. str_from_vec ( v) ;
97
97
}
98
98
99
99
fn refcount ( str s) -> uint {
100
- // -1 because calling this function incremented the refcount.
101
- ret rustrt. refcount [ u8] ( s) - 1 u;
100
+ // -1 because calling this function incremented the refcount.
101
+ ret rustrt. refcount [ u8] ( s) - 1 u;
102
102
}
103
103
104
104
105
105
// Standard bits from the world of string libraries.
106
106
107
107
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 ;
112
114
}
113
- i += 1 ;
114
- }
115
- ret -1 ;
115
+ ret -1 ;
116
116
}
117
117
118
118
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 ;
123
125
}
124
- n -= 1 ;
125
- }
126
- ret n;
126
+ ret n;
127
127
}
128
128
129
129
fn find ( str haystack , str needle ) -> int {
130
130
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 ;
133
133
134
- if ( needle_len == 0 ) {
135
- ret 0 ;
136
- }
134
+ if ( needle_len == 0 ) {
135
+ ret 0 ;
136
+ }
137
137
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;
147
149
}
148
- ret true;
149
- }
150
150
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 ;
155
157
}
156
- i += 1 ;
157
- }
158
- ret -1 ;
158
+ ret -1 ;
159
159
}
160
160
161
161
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 += 1 u;
167
- }
168
- ret accum;
162
+ let str accum = "" ;
163
+ let uint i = begin;
164
+ while ( i < begin+len) {
165
+ accum += s. ( i) ;
166
+ i += 1 u;
167
+ }
168
+ ret accum;
169
169
}
170
170
171
171
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) != 0 u ||
186
+ ends_with_sep) {
187
+ v += accum;
183
188
}
184
- }
185
- if ( _str. byte_len ( accum) != 0 u ||
186
- ends_with_sep) {
187
- v += accum;
188
- }
189
- ret v;
189
+ ret v;
190
190
}
191
191
192
192
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;
198
198
}
199
199
200
200
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;
208
210
}
209
- s += ss;
210
- }
211
- ret s;
211
+ ret s;
212
212
}
213
213
214
214
215
215
// Local Variables:
216
216
// mode: rust;
217
217
// fill-column: 78;
218
218
// indent-tabs-mode: nil
219
+ // c-basic-offset: 4
219
220
// buffer-file-coding-system: utf-8-unix
220
221
// compile-command: "make -k -C .. 2>&1 | sed -e 's/\\/x\\//x:\\//g'";
221
222
// End:
0 commit comments