2012-03-06 21:09:32 -06:00
|
|
|
#[doc(hidden)];
|
|
|
|
|
2011-12-13 18:25:51 -06:00
|
|
|
/*
|
|
|
|
Syntax Extension: fmt
|
|
|
|
|
|
|
|
Format a string
|
|
|
|
|
|
|
|
The 'fmt' extension is modeled on the posix printf system.
|
|
|
|
|
|
|
|
A posix conversion ostensibly looks like this
|
|
|
|
|
|
|
|
> %[parameter][flags][width][.precision][length]type
|
|
|
|
|
|
|
|
Given the different numeric type bestiary we have, we omit the 'length'
|
|
|
|
parameter and support slightly different conversions for 'type'
|
|
|
|
|
|
|
|
> %[parameter][flags][width][.precision]type
|
|
|
|
|
|
|
|
we also only support translating-to-rust a tiny subset of the possible
|
|
|
|
combinations at the moment.
|
|
|
|
|
|
|
|
Example:
|
|
|
|
|
2011-12-22 16:42:52 -06:00
|
|
|
#debug("hello, %s!", "world");
|
2011-12-13 18:25:51 -06:00
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
import option::{some, none};
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We have a 'ct' (compile-time) module that parses format strings into a
|
|
|
|
* sequence of conversions. From those conversions AST fragments are built
|
|
|
|
* that call into properly-typed functions in the 'rt' (run-time) module.
|
|
|
|
* Each of those run-time conversion functions accepts another conversion
|
|
|
|
* description that specifies how to format its output.
|
|
|
|
*
|
|
|
|
* The building of the AST is currently done in a module inside the compiler,
|
|
|
|
* but should migrate over here as the plugin interface is defined.
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Functions used by the fmt extension at compile time
|
|
|
|
mod ct {
|
2012-01-19 19:55:34 -06:00
|
|
|
enum signedness { signed, unsigned, }
|
|
|
|
enum caseness { case_upper, case_lower, }
|
2012-01-19 17:56:54 -06:00
|
|
|
enum ty {
|
2012-01-19 19:55:34 -06:00
|
|
|
ty_bool,
|
|
|
|
ty_str,
|
|
|
|
ty_char,
|
|
|
|
ty_int(signedness),
|
|
|
|
ty_bits,
|
|
|
|
ty_hex(caseness),
|
|
|
|
ty_octal,
|
|
|
|
ty_float,
|
|
|
|
ty_poly,
|
2011-12-13 18:25:51 -06:00
|
|
|
}
|
2012-01-19 17:56:54 -06:00
|
|
|
enum flag {
|
2012-01-19 19:55:34 -06:00
|
|
|
flag_left_justify,
|
|
|
|
flag_left_zero_pad,
|
|
|
|
flag_space_for_sign,
|
|
|
|
flag_sign_always,
|
|
|
|
flag_alternate,
|
2011-12-13 18:25:51 -06:00
|
|
|
}
|
2012-01-19 17:56:54 -06:00
|
|
|
enum count {
|
2012-01-19 19:55:34 -06:00
|
|
|
count_is(int),
|
|
|
|
count_is_param(int),
|
|
|
|
count_is_next_param,
|
|
|
|
count_implied,
|
2011-12-13 18:25:51 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// A formatted conversion from an expression to a string
|
|
|
|
type conv =
|
2012-02-01 18:49:36 -06:00
|
|
|
{param: option<int>,
|
2011-12-13 18:25:51 -06:00
|
|
|
flags: [flag],
|
|
|
|
width: count,
|
|
|
|
precision: count,
|
|
|
|
ty: ty};
|
|
|
|
|
|
|
|
|
|
|
|
// A fragment of the output sequence
|
2012-01-19 19:55:34 -06:00
|
|
|
enum piece { piece_string(str), piece_conv(conv), }
|
2011-12-13 18:25:51 -06:00
|
|
|
type error_fn = fn@(str) -> ! ;
|
|
|
|
|
2012-02-07 01:06:21 -06:00
|
|
|
fn parse_fmt_string(s: str, error: error_fn) -> [piece] unsafe {
|
2012-03-06 22:48:40 -06:00
|
|
|
let mut pieces: [piece] = [];
|
2012-02-23 03:44:04 -06:00
|
|
|
let lim = str::len(s);
|
2012-03-06 22:48:40 -06:00
|
|
|
let mut buf = "";
|
2011-12-13 18:25:51 -06:00
|
|
|
fn flush_buf(buf: str, &pieces: [piece]) -> str {
|
2012-02-23 03:44:04 -06:00
|
|
|
if str::len(buf) > 0u {
|
2011-12-13 18:25:51 -06:00
|
|
|
let piece = piece_string(buf);
|
|
|
|
pieces += [piece];
|
|
|
|
}
|
|
|
|
ret "";
|
|
|
|
}
|
2012-03-06 22:48:40 -06:00
|
|
|
let mut i = 0u;
|
2011-12-13 18:25:51 -06:00
|
|
|
while i < lim {
|
2012-06-07 13:35:58 -05:00
|
|
|
let size = str::utf8_char_width(s[i]);
|
|
|
|
let curr = str::slice(s, i, i+size);
|
2011-12-13 18:25:51 -06:00
|
|
|
if str::eq(curr, "%") {
|
|
|
|
i += 1u;
|
|
|
|
if i >= lim {
|
|
|
|
error("unterminated conversion at end of string");
|
|
|
|
}
|
2012-02-23 02:45:25 -06:00
|
|
|
let curr2 = str::slice(s, i, i+1u);
|
2011-12-13 18:25:51 -06:00
|
|
|
if str::eq(curr2, "%") {
|
2012-01-20 10:12:03 -06:00
|
|
|
buf += curr2;
|
2011-12-13 18:25:51 -06:00
|
|
|
i += 1u;
|
|
|
|
} else {
|
|
|
|
buf = flush_buf(buf, pieces);
|
|
|
|
let rs = parse_conversion(s, i, lim, error);
|
|
|
|
pieces += [rs.piece];
|
|
|
|
i = rs.next;
|
|
|
|
}
|
2012-06-07 13:35:58 -05:00
|
|
|
} else { buf += curr; i += size; }
|
2011-12-13 18:25:51 -06:00
|
|
|
}
|
2012-05-24 15:35:57 -05:00
|
|
|
flush_buf(buf, pieces);
|
2011-12-13 18:25:51 -06:00
|
|
|
ret pieces;
|
|
|
|
}
|
|
|
|
fn peek_num(s: str, i: uint, lim: uint) ->
|
2012-02-01 18:49:36 -06:00
|
|
|
option<{num: uint, next: uint}> {
|
2011-12-13 18:25:51 -06:00
|
|
|
if i >= lim { ret none; }
|
|
|
|
let c = s[i];
|
|
|
|
if !('0' as u8 <= c && c <= '9' as u8) { ret option::none; }
|
2012-02-09 04:50:54 -06:00
|
|
|
let n = (c - ('0' as u8)) as uint;
|
2011-12-13 18:25:51 -06:00
|
|
|
ret alt peek_num(s, i + 1u, lim) {
|
2012-01-19 00:37:22 -06:00
|
|
|
none { some({num: n, next: i + 1u}) }
|
2011-12-13 18:25:51 -06:00
|
|
|
some(next) {
|
|
|
|
let m = next.num;
|
|
|
|
let j = next.next;
|
|
|
|
some({num: n * 10u + m, next: j})
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
fn parse_conversion(s: str, i: uint, lim: uint, error: error_fn) ->
|
|
|
|
{piece: piece, next: uint} {
|
|
|
|
let parm = parse_parameter(s, i, lim);
|
|
|
|
let flags = parse_flags(s, parm.next, lim);
|
|
|
|
let width = parse_count(s, flags.next, lim);
|
|
|
|
let prec = parse_precision(s, width.next, lim);
|
|
|
|
let ty = parse_type(s, prec.next, lim, error);
|
|
|
|
ret {piece:
|
|
|
|
piece_conv({param: parm.param,
|
|
|
|
flags: flags.flags,
|
|
|
|
width: width.count,
|
|
|
|
precision: prec.count,
|
|
|
|
ty: ty.ty}),
|
|
|
|
next: ty.next};
|
|
|
|
}
|
|
|
|
fn parse_parameter(s: str, i: uint, lim: uint) ->
|
2012-02-01 18:49:36 -06:00
|
|
|
{param: option<int>, next: uint} {
|
2011-12-13 18:25:51 -06:00
|
|
|
if i >= lim { ret {param: none, next: i}; }
|
|
|
|
let num = peek_num(s, i, lim);
|
|
|
|
ret alt num {
|
2012-01-19 00:37:22 -06:00
|
|
|
none { {param: none, next: i} }
|
2011-12-13 18:25:51 -06:00
|
|
|
some(t) {
|
|
|
|
let n = t.num;
|
|
|
|
let j = t.next;
|
|
|
|
if j < lim && s[j] == '$' as u8 {
|
|
|
|
{param: some(n as int), next: j + 1u}
|
|
|
|
} else { {param: none, next: i} }
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
fn parse_flags(s: str, i: uint, lim: uint) ->
|
|
|
|
{flags: [flag], next: uint} {
|
|
|
|
let noflags: [flag] = [];
|
|
|
|
if i >= lim { ret {flags: noflags, next: i}; }
|
|
|
|
|
|
|
|
fn more_(f: flag, s: str, i: uint, lim: uint) ->
|
2012-02-05 19:30:47 -06:00
|
|
|
{flags: [flag], next: uint} {
|
2011-12-13 18:25:51 -06:00
|
|
|
let next = parse_flags(s, i + 1u, lim);
|
|
|
|
let rest = next.flags;
|
|
|
|
let j = next.next;
|
|
|
|
let curr: [flag] = [f];
|
2012-02-05 19:30:47 -06:00
|
|
|
ret {flags: curr + rest, next: j};
|
2011-12-13 18:25:51 -06:00
|
|
|
}
|
2012-06-19 21:34:01 -05:00
|
|
|
let more = {|x|more_(x, s, i, lim)};
|
2011-12-13 18:25:51 -06:00
|
|
|
let f = s[i];
|
|
|
|
ret if f == '-' as u8 {
|
2012-02-05 19:30:47 -06:00
|
|
|
more(flag_left_justify)
|
2011-12-13 18:25:51 -06:00
|
|
|
} else if f == '0' as u8 {
|
2012-02-05 19:30:47 -06:00
|
|
|
more(flag_left_zero_pad)
|
2011-12-13 18:25:51 -06:00
|
|
|
} else if f == ' ' as u8 {
|
2012-02-05 19:30:47 -06:00
|
|
|
more(flag_space_for_sign)
|
2011-12-13 18:25:51 -06:00
|
|
|
} else if f == '+' as u8 {
|
2012-02-05 19:30:47 -06:00
|
|
|
more(flag_sign_always)
|
2011-12-13 18:25:51 -06:00
|
|
|
} else if f == '#' as u8 {
|
2012-02-05 19:30:47 -06:00
|
|
|
more(flag_alternate)
|
2011-12-13 18:25:51 -06:00
|
|
|
} else { {flags: noflags, next: i} };
|
|
|
|
}
|
|
|
|
fn parse_count(s: str, i: uint, lim: uint) -> {count: count, next: uint} {
|
|
|
|
ret if i >= lim {
|
|
|
|
{count: count_implied, next: i}
|
|
|
|
} else if s[i] == '*' as u8 {
|
|
|
|
let param = parse_parameter(s, i + 1u, lim);
|
|
|
|
let j = param.next;
|
|
|
|
alt param.param {
|
2012-01-19 00:37:22 -06:00
|
|
|
none { {count: count_is_next_param, next: j} }
|
2011-12-13 18:25:51 -06:00
|
|
|
some(n) { {count: count_is_param(n), next: j} }
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
let num = peek_num(s, i, lim);
|
|
|
|
alt num {
|
2012-01-19 00:37:22 -06:00
|
|
|
none { {count: count_implied, next: i} }
|
2011-12-13 18:25:51 -06:00
|
|
|
some(num) {
|
|
|
|
{count: count_is(num.num as int), next: num.next}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
fn parse_precision(s: str, i: uint, lim: uint) ->
|
|
|
|
{count: count, next: uint} {
|
|
|
|
ret if i >= lim {
|
|
|
|
{count: count_implied, next: i}
|
|
|
|
} else if s[i] == '.' as u8 {
|
|
|
|
let count = parse_count(s, i + 1u, lim);
|
|
|
|
|
|
|
|
|
|
|
|
// If there were no digits specified, i.e. the precision
|
|
|
|
// was ".", then the precision is 0
|
|
|
|
alt count.count {
|
2012-01-19 00:37:22 -06:00
|
|
|
count_implied { {count: count_is(0), next: count.next} }
|
2011-12-13 18:25:51 -06:00
|
|
|
_ { count }
|
|
|
|
}
|
|
|
|
} else { {count: count_implied, next: i} };
|
|
|
|
}
|
|
|
|
fn parse_type(s: str, i: uint, lim: uint, error: error_fn) ->
|
2012-02-07 01:06:21 -06:00
|
|
|
{ty: ty, next: uint} unsafe {
|
2011-12-13 18:25:51 -06:00
|
|
|
if i >= lim { error("missing type in conversion"); }
|
2012-02-23 02:45:25 -06:00
|
|
|
let tstr = str::slice(s, i, i+1u);
|
2011-12-13 18:25:51 -06:00
|
|
|
// TODO: Do we really want two signed types here?
|
|
|
|
// How important is it to be printf compatible?
|
|
|
|
let t =
|
|
|
|
if str::eq(tstr, "b") {
|
|
|
|
ty_bool
|
|
|
|
} else if str::eq(tstr, "s") {
|
|
|
|
ty_str
|
|
|
|
} else if str::eq(tstr, "c") {
|
|
|
|
ty_char
|
|
|
|
} else if str::eq(tstr, "d") || str::eq(tstr, "i") {
|
|
|
|
ty_int(signed)
|
|
|
|
} else if str::eq(tstr, "u") {
|
|
|
|
ty_int(unsigned)
|
|
|
|
} else if str::eq(tstr, "x") {
|
|
|
|
ty_hex(case_lower)
|
|
|
|
} else if str::eq(tstr, "X") {
|
|
|
|
ty_hex(case_upper)
|
|
|
|
} else if str::eq(tstr, "t") {
|
|
|
|
ty_bits
|
|
|
|
} else if str::eq(tstr, "o") {
|
|
|
|
ty_octal
|
|
|
|
} else if str::eq(tstr, "f") {
|
|
|
|
ty_float
|
2012-01-11 11:09:46 -06:00
|
|
|
} else if str::eq(tstr, "?") {
|
|
|
|
ty_poly
|
2011-12-13 18:25:51 -06:00
|
|
|
} else { error("unknown type in conversion: " + tstr) };
|
|
|
|
ret {ty: t, next: i + 1u};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Functions used by the fmt extension at runtime. For now there are a lot of
|
|
|
|
// decisions made a runtime. If it proves worthwhile then some of these
|
|
|
|
// conditions can be evaluated at compile-time. For now though it's cleaner to
|
|
|
|
// implement it this way, I think.
|
|
|
|
mod rt {
|
2012-01-19 17:56:54 -06:00
|
|
|
enum flag {
|
2012-01-19 19:55:34 -06:00
|
|
|
flag_left_justify,
|
|
|
|
flag_left_zero_pad,
|
|
|
|
flag_space_for_sign,
|
|
|
|
flag_sign_always,
|
|
|
|
flag_alternate,
|
2011-12-13 18:25:51 -06:00
|
|
|
}
|
2012-01-19 19:55:34 -06:00
|
|
|
enum count { count_is(int), count_implied, }
|
|
|
|
enum ty { ty_default, ty_bits, ty_hex_upper, ty_hex_lower, ty_octal, }
|
2011-12-13 18:25:51 -06:00
|
|
|
|
2012-06-21 18:44:10 -05:00
|
|
|
// FIXME (#1993): May not want to use a vector here for flags; instead
|
|
|
|
// just use a bool per flag.
|
2011-12-13 18:25:51 -06:00
|
|
|
type conv = {flags: [flag], width: count, precision: count, ty: ty};
|
|
|
|
|
|
|
|
fn conv_int(cv: conv, i: int) -> str {
|
|
|
|
let radix = 10u;
|
|
|
|
let prec = get_int_precision(cv);
|
2012-03-15 19:45:38 -05:00
|
|
|
let mut s : str = int_to_str_prec(i, radix, prec);
|
2011-12-13 18:25:51 -06:00
|
|
|
if 0 <= i {
|
|
|
|
if have_flag(cv.flags, flag_sign_always) {
|
2012-03-15 19:45:38 -05:00
|
|
|
str::unshift_char(s, '+');
|
2011-12-13 18:25:51 -06:00
|
|
|
} else if have_flag(cv.flags, flag_space_for_sign) {
|
2012-03-15 19:45:38 -05:00
|
|
|
str::unshift_char(s, ' ');
|
2011-12-13 18:25:51 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
ret pad(cv, s, pad_signed);
|
|
|
|
}
|
|
|
|
fn conv_uint(cv: conv, u: uint) -> str {
|
|
|
|
let prec = get_int_precision(cv);
|
2012-03-15 19:45:38 -05:00
|
|
|
let mut rs =
|
2011-12-13 18:25:51 -06:00
|
|
|
alt cv.ty {
|
2012-01-19 00:37:22 -06:00
|
|
|
ty_default { uint_to_str_prec(u, 10u, prec) }
|
|
|
|
ty_hex_lower { uint_to_str_prec(u, 16u, prec) }
|
|
|
|
ty_hex_upper { str::to_upper(uint_to_str_prec(u, 16u, prec)) }
|
|
|
|
ty_bits { uint_to_str_prec(u, 2u, prec) }
|
|
|
|
ty_octal { uint_to_str_prec(u, 8u, prec) }
|
2011-12-13 18:25:51 -06:00
|
|
|
};
|
|
|
|
ret pad(cv, rs, pad_unsigned);
|
|
|
|
}
|
|
|
|
fn conv_bool(cv: conv, b: bool) -> str {
|
|
|
|
let s = if b { "true" } else { "false" };
|
|
|
|
// run the boolean conversion through the string conversion logic,
|
|
|
|
// giving it the same rules for precision, etc.
|
|
|
|
|
|
|
|
ret conv_str(cv, s);
|
|
|
|
}
|
|
|
|
fn conv_char(cv: conv, c: char) -> str {
|
2012-03-15 19:45:38 -05:00
|
|
|
let mut s = str::from_char(c);
|
|
|
|
ret pad(cv, s, pad_nozero);
|
2011-12-13 18:25:51 -06:00
|
|
|
}
|
2012-02-07 01:06:21 -06:00
|
|
|
fn conv_str(cv: conv, s: str) -> str unsafe {
|
2011-12-13 18:25:51 -06:00
|
|
|
// For strings, precision is the maximum characters
|
|
|
|
// displayed
|
2012-03-15 19:45:38 -05:00
|
|
|
let mut unpadded = alt cv.precision {
|
2012-02-23 09:59:30 -06:00
|
|
|
count_implied { s }
|
|
|
|
count_is(max) {
|
|
|
|
if max as uint < str::char_len(s) {
|
|
|
|
str::substr(s, 0u, max as uint)
|
|
|
|
} else { s }
|
|
|
|
}
|
|
|
|
};
|
2011-12-13 18:25:51 -06:00
|
|
|
ret pad(cv, unpadded, pad_nozero);
|
|
|
|
}
|
|
|
|
fn conv_float(cv: conv, f: float) -> str {
|
|
|
|
let (to_str, digits) = alt cv.precision {
|
|
|
|
count_is(c) { (float::to_str_exact, c as uint) }
|
2012-01-19 00:37:22 -06:00
|
|
|
count_implied { (float::to_str, 6u) }
|
2011-12-13 18:25:51 -06:00
|
|
|
};
|
2012-03-06 22:48:40 -06:00
|
|
|
let mut s = to_str(f, digits);
|
2011-12-13 18:25:51 -06:00
|
|
|
if 0.0 <= f {
|
|
|
|
if have_flag(cv.flags, flag_sign_always) {
|
|
|
|
s = "+" + s;
|
|
|
|
} else if have_flag(cv.flags, flag_space_for_sign) {
|
|
|
|
s = " " + s;
|
|
|
|
}
|
|
|
|
}
|
2012-06-01 18:10:22 -05:00
|
|
|
ret pad(cv, s, pad_float);
|
2011-12-13 18:25:51 -06:00
|
|
|
}
|
2012-01-11 11:09:46 -06:00
|
|
|
fn conv_poly<T>(cv: conv, v: T) -> str {
|
|
|
|
let s = sys::log_str(v);
|
|
|
|
ret conv_str(cv, s);
|
|
|
|
}
|
2011-12-13 18:25:51 -06:00
|
|
|
|
|
|
|
// Convert an int to string with minimum number of digits. If precision is
|
|
|
|
// 0 and num is 0 then the result is the empty string.
|
|
|
|
fn int_to_str_prec(num: int, radix: uint, prec: uint) -> str {
|
|
|
|
ret if num < 0 {
|
|
|
|
"-" + uint_to_str_prec(-num as uint, radix, prec)
|
|
|
|
} else { uint_to_str_prec(num as uint, radix, prec) };
|
|
|
|
}
|
|
|
|
|
|
|
|
// Convert a uint to string with a minimum number of digits. If precision
|
|
|
|
// is 0 and num is 0 then the result is the empty string. Could move this
|
|
|
|
// to uint: but it doesn't seem all that useful.
|
|
|
|
fn uint_to_str_prec(num: uint, radix: uint, prec: uint) -> str {
|
|
|
|
ret if prec == 0u && num == 0u {
|
|
|
|
""
|
|
|
|
} else {
|
|
|
|
let s = uint::to_str(num, radix);
|
2012-02-23 09:59:30 -06:00
|
|
|
let len = str::char_len(s);
|
2011-12-13 18:25:51 -06:00
|
|
|
if len < prec {
|
|
|
|
let diff = prec - len;
|
2012-03-16 17:29:24 -05:00
|
|
|
let pad = str::from_chars(vec::from_elem(diff, '0'));
|
2011-12-13 18:25:51 -06:00
|
|
|
pad + s
|
|
|
|
} else { s }
|
|
|
|
};
|
|
|
|
}
|
|
|
|
fn get_int_precision(cv: conv) -> uint {
|
|
|
|
ret alt cv.precision {
|
|
|
|
count_is(c) { c as uint }
|
2012-01-19 00:37:22 -06:00
|
|
|
count_implied { 1u }
|
2011-12-13 18:25:51 -06:00
|
|
|
};
|
|
|
|
}
|
2012-06-01 18:10:22 -05:00
|
|
|
enum pad_mode { pad_signed, pad_unsigned, pad_nozero, pad_float }
|
2012-03-15 19:45:38 -05:00
|
|
|
fn pad(cv: conv, &s: str, mode: pad_mode) -> str unsafe {
|
|
|
|
let uwidth : uint = alt cv.width {
|
2012-01-19 00:37:22 -06:00
|
|
|
count_implied { ret s; }
|
2011-12-13 18:25:51 -06:00
|
|
|
count_is(width) {
|
2012-03-15 19:45:38 -05:00
|
|
|
// FIXME: width should probably be uint (see Issue #1996)
|
|
|
|
width as uint
|
2011-12-13 18:25:51 -06:00
|
|
|
}
|
2012-03-06 22:48:40 -06:00
|
|
|
};
|
2012-02-23 09:59:30 -06:00
|
|
|
let strlen = str::char_len(s);
|
2011-12-13 18:25:51 -06:00
|
|
|
if uwidth <= strlen { ret s; }
|
2012-03-06 22:48:40 -06:00
|
|
|
let mut padchar = ' ';
|
2011-12-13 18:25:51 -06:00
|
|
|
let diff = uwidth - strlen;
|
|
|
|
if have_flag(cv.flags, flag_left_justify) {
|
2012-03-16 17:29:24 -05:00
|
|
|
let padstr = str::from_chars(vec::from_elem(diff, padchar));
|
2011-12-13 18:25:51 -06:00
|
|
|
ret s + padstr;
|
|
|
|
}
|
2012-03-06 22:48:40 -06:00
|
|
|
let {might_zero_pad, signed} = alt mode {
|
|
|
|
pad_nozero { {might_zero_pad:false, signed:false} }
|
|
|
|
pad_signed { {might_zero_pad:true, signed:true } }
|
2012-06-01 18:10:22 -05:00
|
|
|
pad_float { {might_zero_pad:true, signed:true } }
|
2012-03-06 22:48:40 -06:00
|
|
|
pad_unsigned { {might_zero_pad:true, signed:false} }
|
|
|
|
};
|
2011-12-13 18:25:51 -06:00
|
|
|
fn have_precision(cv: conv) -> bool {
|
2012-01-19 00:37:22 -06:00
|
|
|
ret alt cv.precision { count_implied { false } _ { true } };
|
2011-12-13 18:25:51 -06:00
|
|
|
}
|
2012-03-06 22:48:40 -06:00
|
|
|
let zero_padding = {
|
|
|
|
if might_zero_pad && have_flag(cv.flags, flag_left_zero_pad) &&
|
2012-06-01 18:10:22 -05:00
|
|
|
(!have_precision(cv) || mode == pad_float) {
|
2012-03-06 22:48:40 -06:00
|
|
|
padchar = '0';
|
|
|
|
true
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
};
|
2012-03-16 17:29:24 -05:00
|
|
|
let padstr = str::from_chars(vec::from_elem(diff, padchar));
|
2011-12-13 18:25:51 -06:00
|
|
|
// This is completely heinous. If we have a signed value then
|
|
|
|
// potentially rip apart the intermediate result and insert some
|
|
|
|
// zeros. It may make sense to convert zero padding to a precision
|
|
|
|
// instead.
|
|
|
|
|
2012-02-23 03:44:04 -06:00
|
|
|
if signed && zero_padding && str::len(s) > 0u {
|
2012-03-15 19:45:38 -05:00
|
|
|
let head = str::shift_char(s);
|
|
|
|
if head == '+' || head == '-' || head == ' ' {
|
2012-03-16 17:29:24 -05:00
|
|
|
let headstr = str::from_chars(vec::from_elem(1u, head));
|
2012-03-15 19:45:38 -05:00
|
|
|
ret headstr + padstr + s;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
str::unshift_char(s, head);
|
2011-12-13 18:25:51 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
ret padstr + s;
|
|
|
|
}
|
|
|
|
fn have_flag(flags: [flag], f: flag) -> bool {
|
2012-03-27 08:14:12 -05:00
|
|
|
for vec::each(flags) {|candidate| if candidate == f { ret true; } }
|
2011-12-13 18:25:51 -06:00
|
|
|
ret false;
|
|
|
|
}
|
|
|
|
}
|
2012-01-20 10:12:03 -06:00
|
|
|
|
2011-12-13 18:25:51 -06:00
|
|
|
// Local Variables:
|
|
|
|
// mode: rust;
|
|
|
|
// fill-column: 78;
|
|
|
|
// indent-tabs-mode: nil
|
|
|
|
// c-basic-offset: 4
|
|
|
|
// buffer-file-coding-system: utf-8-unix
|
|
|
|
// End:
|