6
6
// option. This file may not be copied, modified, or distributed
7
7
// except according to those terms.
8
8
9
-
10
9
use std:: u32;
11
10
use std:: char;
12
11
use std:: str;
13
12
14
-
15
13
// Bootstring parameters for Punycode
16
14
static BASE : u32 = 36 ;
17
15
static T_MIN : u32 = 1 ;
@@ -46,7 +44,7 @@ pub fn decode(input: &str) -> Option<~[char]> {
46
44
let ( mut output, input) = match input. rfind ( DELIMITER ) {
47
45
None => ( ~[ ] , input) ,
48
46
Some ( position) => (
49
- input. slice_to ( position) . chars ( ) . to_owned_vec ( ) ,
47
+ input. slice_to ( position) . chars ( ) . collect ( ) ,
50
48
if position > 0 { input. slice_from ( position + 1 ) } else { input }
51
49
)
52
50
} ;
@@ -71,7 +69,7 @@ pub fn decode(input: &str) -> Option<~[char]> {
71
69
byte @ 0x61 .. 0x7A => byte - 0x61 , // a..z
72
70
_ => return None
73
71
} as u32 ;
74
- if digit > ( u32:: max_value - i) / weight {
72
+ if digit > ( u32:: MAX - i) / weight {
75
73
return None // Overflow
76
74
}
77
75
i += digit * weight;
@@ -81,7 +79,7 @@ pub fn decode(input: &str) -> Option<~[char]> {
81
79
if digit < t {
82
80
break
83
81
}
84
- if weight > u32:: max_value / ( BASE - t) {
82
+ if weight > u32:: MAX / ( BASE - t) {
85
83
return None // Overflow
86
84
}
87
85
weight *= BASE - t;
@@ -93,7 +91,7 @@ pub fn decode(input: &str) -> Option<~[char]> {
93
91
}
94
92
let length = output. len ( ) as u32 ;
95
93
bias = adapt ( i - previous_i, length + 1 , previous_i == 0 ) ;
96
- if i / ( length + 1 ) > u32:: max_value - code_point {
94
+ if i / ( length + 1 ) > u32:: MAX - code_point {
97
95
return None // Overflow
98
96
}
99
97
// i was supposed to wrap around from length+1 to 0,
@@ -118,7 +116,7 @@ pub fn encode(input: &[char]) -> Option<~str> {
118
116
// Handle "basic" (ASCII) code points. They are encoded as-is.
119
117
let output_bytes = input. iter ( ) . filter_map ( |& c|
120
118
if c. is_ascii ( ) { Some ( c as u8 ) } else { None }
121
- ) . to_owned_vec ( ) ;
119
+ ) . collect ( ) ;
122
120
let mut output = unsafe { str:: raw:: from_utf8_owned ( output_bytes) } ;
123
121
let basic_length = output. len ( ) as u32 ;
124
122
if basic_length > 0 {
@@ -134,7 +132,7 @@ pub fn encode(input: &[char]) -> Option<~str> {
134
132
// Find the next larger one.
135
133
let min_code_point = input. iter ( ) . map ( |& c| c as u32 )
136
134
. filter ( |& c| c >= code_point) . min ( ) . unwrap ( ) ;
137
- if min_code_point - code_point > ( u32:: max_value - delta) / ( processed + 1 ) {
135
+ if min_code_point - code_point > ( u32:: MAX - delta) / ( processed + 1 ) {
138
136
return None // Overflow
139
137
}
140
138
// Increase delta to advance the decoder’s <code_point,i> state to <min_code_point,0>
@@ -192,7 +190,7 @@ fn value_to_digit(value: u32, output: &mut ~str) {
192
190
mod tests {
193
191
use super :: { decode, encode} ;
194
192
use std:: str:: from_chars;
195
- use extra :: json:: { from_str, List , Object , String } ;
193
+ use serialize :: json:: { from_str, List , Object , String } ;
196
194
197
195
fn one_test ( description : & str , decoded : & str , encoded : & str ) {
198
196
match decode ( encoded) {
@@ -203,9 +201,11 @@ mod tests {
203
201
format!( "Incorrect decoding of {:?}:\n {:?}\n != {:?}\n {}" ,
204
202
encoded, result. as_slice( ) , decoded, description) )
205
203
}
206
- }
204
+ }
205
+
206
+ let dec_chars: ~[ char ] = decoded. chars ( ) . collect ( ) ;
207
207
208
- match encode ( decoded . chars ( ) . to_owned_vec ( ) ) {
208
+ match encode ( dec_chars ) {
209
209
None => fail ! ( "Encoding {:?} failed." , decoded) ,
210
210
Some ( result) => {
211
211
assert ! ( result. as_slice( ) == encoded,
0 commit comments