Skip to content

Commit 2407373

Browse files
committed
core: Make #fmt pure
1 parent 01e2471 commit 2407373

File tree

3 files changed

+39
-18
lines changed

3 files changed

+39
-18
lines changed

src/libcore/extfmt.rs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -277,20 +277,20 @@ mod rt {
277277
278278
type conv = {flags: u32, width: count, precision: count, ty: ty};
279279
280-
fn conv_int(cv: conv, i: int) -> ~str {
280+
pure fn conv_int(cv: conv, i: int) -> ~str {
281281
let radix = 10u;
282282
let prec = get_int_precision(cv);
283283
let mut s : ~str = int_to_str_prec(i, radix, prec);
284284
if 0 <= i {
285285
if have_flag(cv.flags, flag_sign_always) {
286-
str::unshift_char(s, '+');
286+
unchecked { str::unshift_char(s, '+') };
287287
} else if have_flag(cv.flags, flag_space_for_sign) {
288-
str::unshift_char(s, ' ');
288+
unchecked { str::unshift_char(s, ' ') };
289289
}
290290
}
291-
ret pad(cv, s, pad_signed);
291+
ret unchecked { pad(cv, s, pad_signed) };
292292
}
293-
fn conv_uint(cv: conv, u: uint) -> ~str {
293+
pure fn conv_uint(cv: conv, u: uint) -> ~str {
294294
let prec = get_int_precision(cv);
295295
let mut rs =
296296
alt cv.ty {
@@ -300,17 +300,17 @@ mod rt {
300300
ty_bits { uint_to_str_prec(u, 2u, prec) }
301301
ty_octal { uint_to_str_prec(u, 8u, prec) }
302302
};
303-
ret pad(cv, rs, pad_unsigned);
303+
ret unchecked { pad(cv, rs, pad_unsigned) };
304304
}
305-
fn conv_bool(cv: conv, b: bool) -> ~str {
305+
pure fn conv_bool(cv: conv, b: bool) -> ~str {
306306
let s = if b { ~"true" } else { ~"false" };
307307
// run the boolean conversion through the string conversion logic,
308308
// giving it the same rules for precision, etc.
309309
ret conv_str(cv, s);
310310
}
311-
fn conv_char(cv: conv, c: char) -> ~str {
311+
pure fn conv_char(cv: conv, c: char) -> ~str {
312312
let mut s = str::from_char(c);
313-
ret pad(cv, s, pad_nozero);
313+
ret unchecked { pad(cv, s, pad_nozero) };
314314
}
315315
pure fn conv_str(cv: conv, s: &str) -> ~str {
316316
// For strings, precision is the maximum characters
@@ -325,29 +325,29 @@ mod rt {
325325
};
326326
ret unchecked { pad(cv, unpadded, pad_nozero) };
327327
}
328-
fn conv_float(cv: conv, f: float) -> ~str {
328+
pure fn conv_float(cv: conv, f: float) -> ~str {
329329
let (to_str, digits) = alt cv.precision {
330330
count_is(c) { (float::to_str_exact, c as uint) }
331331
count_implied { (float::to_str, 6u) }
332332
};
333-
let mut s = to_str(f, digits);
333+
let mut s = unchecked { to_str(f, digits) };
334334
if 0.0 <= f {
335335
if have_flag(cv.flags, flag_sign_always) {
336336
s = ~"+" + s;
337337
} else if have_flag(cv.flags, flag_space_for_sign) {
338338
s = ~" " + s;
339339
}
340340
}
341-
ret pad(cv, s, pad_float);
341+
ret unchecked { pad(cv, s, pad_float) };
342342
}
343-
fn conv_poly<T>(cv: conv, v: T) -> ~str {
343+
pure fn conv_poly<T>(cv: conv, v: T) -> ~str {
344344
let s = sys::log_str(v);
345345
ret conv_str(cv, s);
346346
}
347347
348348
// Convert an int to string with minimum number of digits. If precision is
349349
// 0 and num is 0 then the result is the empty string.
350-
fn int_to_str_prec(num: int, radix: uint, prec: uint) -> ~str {
350+
pure fn int_to_str_prec(num: int, radix: uint, prec: uint) -> ~str {
351351
ret if num < 0 {
352352
~"-" + uint_to_str_prec(-num as uint, radix, prec)
353353
} else { uint_to_str_prec(num as uint, radix, prec) };
@@ -356,7 +356,7 @@ mod rt {
356356
// Convert a uint to string with a minimum number of digits. If precision
357357
// is 0 and num is 0 then the result is the empty string. Could move this
358358
// to uint: but it doesn't seem all that useful.
359-
fn uint_to_str_prec(num: uint, radix: uint, prec: uint) -> ~str {
359+
pure fn uint_to_str_prec(num: uint, radix: uint, prec: uint) -> ~str {
360360
ret if prec == 0u && num == 0u {
361361
~""
362362
} else {
@@ -369,7 +369,7 @@ mod rt {
369369
} else { s }
370370
};
371371
}
372-
fn get_int_precision(cv: conv) -> uint {
372+
pure fn get_int_precision(cv: conv) -> uint {
373373
ret alt cv.precision {
374374
count_is(c) { c as uint }
375375
count_implied { 1u }

src/libcore/uint-template.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ fn from_str_radix(buf: ~str, radix: u64) -> option<u64> {
159159
*
160160
* Fails if `radix` < 2 or `radix` > 16
161161
*/
162-
fn to_str(num: T, radix: uint) -> ~str {
162+
pure fn to_str(num: T, radix: uint) -> ~str {
163163
do to_str_bytes(false, num, radix) |slice| {
164164
do vec::as_buf(slice) |p, len| {
165165
unsafe { str::unsafe::from_buf_len(p, len) }
@@ -168,7 +168,7 @@ fn to_str(num: T, radix: uint) -> ~str {
168168
}
169169

170170
/// Low-level helper routine for string conversion.
171-
fn to_str_bytes<U>(neg: bool, num: T, radix: uint,
171+
pure fn to_str_bytes<U>(neg: bool, num: T, radix: uint,
172172
f: fn(v: &[u8]) -> U) -> U {
173173

174174
#[inline(always)]

src/test/run-pass/pure-fmt.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Testing that calling #fmt (via #debug) doesn't complain about impure borrows
2+
3+
pure fn foo() {
4+
let a = {
5+
b: @"hi",
6+
c: 0,
7+
d: 1,
8+
e: 'a',
9+
f: 0.0,
10+
g: true
11+
};
12+
#debug("test %?", a.b);
13+
#debug("test %u", a.c);
14+
#debug("test %i", a.d);
15+
#debug("test %c", a.e);
16+
#debug("test %f", a.f);
17+
#debug("test %b", a.g);
18+
}
19+
20+
fn main() {
21+
}

0 commit comments

Comments
 (0)