@@ -27,15 +27,15 @@ type UserInfo = {
27
27
28
28
pub type Query = ~[ ( ~str , ~str ) ] ;
29
29
30
- pub fn Url ( scheme : ~str , user : Option < UserInfo > , host : ~str ,
30
+ pub pure fn Url ( scheme : ~str , user : Option < UserInfo > , host : ~str ,
31
31
port : Option < ~str > , path : ~str , query : Query ,
32
32
fragment : Option < ~str > ) -> Url {
33
33
Url { scheme : move scheme, user : move user, host : move host,
34
34
port : move port, path : move path, query : move query,
35
35
fragment : move fragment }
36
36
}
37
37
38
- fn UserInfo ( user : ~str , pass : Option < ~str > ) -> UserInfo {
38
+ pure fn UserInfo ( user : ~str , pass : Option < ~str > ) -> UserInfo {
39
39
{ user: move user, pass: move pass}
40
40
}
41
41
@@ -84,8 +84,9 @@ fn encode_inner(s: &str, full_url: bool) -> ~str {
84
84
*
85
85
* This function is compliant with RFC 3986.
86
86
*/
87
- pub fn encode ( s : & str ) -> ~str {
88
- encode_inner ( s, true )
87
+ pub pure fn encode ( s : & str ) -> ~str {
88
+ // unsafe only because encode_inner does (string) IO
89
+ unsafe { encode_inner ( s, true ) }
89
90
}
90
91
91
92
/**
@@ -95,8 +96,9 @@ pub fn encode(s: &str) -> ~str {
95
96
* This function is compliant with RFC 3986.
96
97
*/
97
98
98
- pub fn encode_component ( s : & str ) -> ~str {
99
- encode_inner ( s, false )
99
+ pub pure fn encode_component ( s : & str ) -> ~str {
100
+ // unsafe only because encode_inner does (string) IO
101
+ unsafe { encode_inner ( s, false ) }
100
102
}
101
103
102
104
fn decode_inner ( s : & str , full_url : bool ) -> ~str {
@@ -142,15 +144,17 @@ fn decode_inner(s: &str, full_url: bool) -> ~str {
142
144
*
143
145
* This will only decode escape sequences generated by encode_uri.
144
146
*/
145
- pub fn decode ( s : & str ) -> ~str {
146
- decode_inner ( s, true )
147
+ pub pure fn decode ( s : & str ) -> ~str {
148
+ // unsafe only because decode_inner does (string) IO
149
+ unsafe { decode_inner ( s, true ) }
147
150
}
148
151
149
152
/**
150
153
* Decode a string encoded with percent encoding.
151
154
*/
152
- pub fn decode_component ( s : & str ) -> ~str {
153
- decode_inner ( s, false )
155
+ pub pure fn decode_component ( s : & str ) -> ~str {
156
+ // unsafe only because decode_inner does (string) IO
157
+ unsafe { decode_inner ( s, false ) }
154
158
}
155
159
156
160
fn encode_plus ( s : & str ) -> ~str {
@@ -264,19 +268,21 @@ pub fn decode_form_urlencoded(s: ~[u8]) ->
264
268
}
265
269
266
270
267
- fn split_char_first ( s : & str , c : char ) -> ( ~str , ~str ) {
271
+ pure fn split_char_first ( s : & str , c : char ) -> ( ~str , ~str ) {
268
272
let len = str:: len ( s) ;
269
273
let mut index = len;
270
274
let mut mat = 0 ;
271
- do io:: with_str_reader ( s) |rdr| {
272
- let mut ch : char ;
273
- while !rdr. eof ( ) {
274
- ch = rdr. read_byte ( ) as char ;
275
- if ch == c {
276
- // found a match, adjust markers
277
- index = rdr. tell ( ) -1 ;
278
- mat = 1 ;
279
- break ;
275
+ unsafe {
276
+ do io:: with_str_reader ( s) |rdr| {
277
+ let mut ch : char ;
278
+ while !rdr. eof ( ) {
279
+ ch = rdr. read_byte ( ) as char ;
280
+ if ch == c {
281
+ // found a match, adjust markers
282
+ index = rdr. tell ( ) -1 ;
283
+ mat = 1 ;
284
+ break ;
285
+ }
280
286
}
281
287
}
282
288
}
@@ -288,7 +294,7 @@ fn split_char_first(s: &str, c: char) -> (~str, ~str) {
288
294
}
289
295
}
290
296
291
- fn userinfo_from_str ( uinfo : & str ) -> UserInfo {
297
+ pure fn userinfo_from_str ( uinfo : & str ) -> UserInfo {
292
298
let ( user, p) = split_char_first ( uinfo, ':' ) ;
293
299
let pass = if str:: len ( p) == 0 {
294
300
option:: None
@@ -315,12 +321,12 @@ impl UserInfo : Eq {
315
321
pure fn ne ( other : & UserInfo ) -> bool { !self . eq ( other) }
316
322
}
317
323
318
- fn query_from_str ( rawquery : & str ) -> Query {
324
+ pure fn query_from_str ( rawquery : & str ) -> Query {
319
325
let mut query: Query = ~[ ] ;
320
326
if str:: len ( rawquery) != 0 {
321
327
for str:: split_char( rawquery, '&' ) . each |p| {
322
328
let ( k, v) = split_char_first ( * p, '=' ) ;
323
- query. push ( ( decode_component ( k) , decode_component ( v) ) ) ;
329
+ unsafe { query. push ( ( decode_component ( k) , decode_component ( v) ) ) ; }
324
330
} ;
325
331
}
326
332
return query;
@@ -340,7 +346,7 @@ pub pure fn query_to_str(query: Query) -> ~str {
340
346
}
341
347
342
348
// returns the scheme and the rest of the url, or a parsing error
343
- pub fn get_scheme(rawurl: &str) -> result::Result<(~str, ~str), @~str> {
349
+ pub pure fn get_scheme(rawurl: &str) -> result::Result<(~str, ~str), @~str> {
344
350
for str::each_chari(rawurl) |i,c| {
345
351
match c {
346
352
'A' .. 'Z' | 'a' .. 'z' => loop,
@@ -387,7 +393,7 @@ impl Input : Eq {
387
393
}
388
394
389
395
// returns userinfo, host, port, and unparsed part, or an error
390
- fn get_authority ( rawurl : & str ) ->
396
+ pure fn get_authority ( rawurl : & str ) ->
391
397
result:: Result < ( Option < UserInfo > , ~str , Option < ~str > , ~str ) , @~str > {
392
398
if !str:: starts_with ( rawurl, ~"//") {
393
399
// there is no authority.
@@ -517,7 +523,7 @@ fn get_authority(rawurl: &str) ->
517
523
518
524
let end = end; // make end immutable so it can be captured
519
525
520
- let host_is_end_plus_one: & fn ( ) -> bool = || {
526
+ let host_is_end_plus_one: & pure fn( ) -> bool = || {
521
527
end+1 == len
522
528
&& ![ '?' , '#' , '/' ] . contains ( & ( rawurl[ end] as char ) )
523
529
} ;
@@ -556,7 +562,7 @@ fn get_authority(rawurl: &str) ->
556
562
557
563
558
564
// returns the path and unparsed part of url, or an error
559
- fn get_path ( rawurl : & str , authority : bool ) ->
565
+ pure fn get_path ( rawurl : & str , authority : bool ) ->
560
566
result:: Result < ( ~str , ~str ) , @~str > {
561
567
let len = str:: len ( rawurl) ;
562
568
let mut end = len;
@@ -587,7 +593,7 @@ fn get_path(rawurl: &str, authority : bool) ->
587
593
}
588
594
589
595
// returns the parsed query and the fragment, if present
590
- fn get_query_fragment ( rawurl : & str ) ->
596
+ pure fn get_query_fragment ( rawurl : & str ) ->
591
597
result:: Result < ( Query , Option < ~str > ) , @~str > {
592
598
if !str:: starts_with ( rawurl, ~"?") {
593
599
if str:: starts_with ( rawurl, ~"#") {
@@ -619,42 +625,42 @@ fn get_query_fragment(rawurl: &str) ->
619
625
*
620
626
*/
621
627
622
- pub fn from_str(rawurl: &str) -> result::Result<Url, ~str> {
628
+ pub pure fn from_str(rawurl: &str) -> result::Result<Url, ~str> {
623
629
// scheme
624
630
let mut schm = get_scheme(rawurl);
625
631
if result::is_err(&schm) {
626
632
return result::Err(copy *result::get_err(&schm));
627
633
}
628
- let (scheme, rest) = result::unwrap( schm);
634
+ let (scheme, rest) = schm.get( );
629
635
630
636
// authority
631
637
let mut auth = get_authority(rest);
632
638
if result::is_err(&auth) {
633
639
return result::Err(copy *result::get_err(&auth));
634
640
}
635
- let (userinfo, host, port, rest) = result::unwrap( auth);
641
+ let (userinfo, host, port, rest) = auth.get( );
636
642
637
643
// path
638
644
let has_authority = if host == ~" " { false } else { true } ;
639
645
let mut pth = get_path ( rest, has_authority) ;
640
646
if result:: is_err( & pth) {
641
647
return result:: Err ( copy * result:: get_err ( & pth) ) ;
642
648
}
643
- let ( path, rest) = result :: unwrap ( pth) ;
649
+ let ( path, rest) = pth. get ( ) ;
644
650
645
651
// query and fragment
646
652
let mut qry = get_query_fragment ( rest) ;
647
653
if result:: is_err ( & qry) {
648
654
return result:: Err ( copy * result:: get_err ( & qry) ) ;
649
655
}
650
- let ( query, fragment) = result :: unwrap ( qry) ;
656
+ let ( query, fragment) = qry. get ( ) ;
651
657
652
658
return result:: Ok ( Url ( scheme, userinfo, host,
653
659
port, path, query, fragment) ) ;
654
660
}
655
661
656
662
impl Url : FromStr {
657
- static fn from_str( s : & str ) -> Option < Url > {
663
+ static pure fn from_str( s: & str ) -> Option < Url > {
658
664
match from_str ( s ) {
659
665
Ok ( move url) => Some ( url) ,
660
666
Err ( _) => None
0 commit comments