2011-06-15 11:19:50 -07:00
|
|
|
|
|
|
|
|
2011-04-13 20:51:24 -04:00
|
|
|
/* 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.
|
|
|
|
*/
|
2011-05-12 17:24:54 +02:00
|
|
|
import option::none;
|
|
|
|
import option::some;
|
2011-04-11 19:52:36 -04:00
|
|
|
|
2011-06-15 11:19:50 -07:00
|
|
|
|
2011-04-13 20:51:24 -04:00
|
|
|
/*
|
2011-05-17 00:58:52 -04:00
|
|
|
* We have a 'ct' (compile-time) module that parses format strings into a
|
2011-04-13 20:51:24 -04:00
|
|
|
* sequence of conversions. From those conversions AST fragments are built
|
2011-05-17 00:58:52 -04:00
|
|
|
* that call into properly-typed functions in the 'rt' (run-time) module.
|
|
|
|
* Each of those run-time conversion functions accepts another conversion
|
2011-04-13 20:51:24 -04:00
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2011-04-13 18:59:40 -04:00
|
|
|
// Functions used by the fmt extension at compile time
|
2011-05-12 19:51:22 -04:00
|
|
|
mod ct {
|
2011-06-15 11:19:50 -07:00
|
|
|
tag signedness { signed; unsigned; }
|
|
|
|
tag caseness { case_upper; case_lower; }
|
2011-04-13 18:59:40 -04:00
|
|
|
tag ty {
|
|
|
|
ty_bool;
|
|
|
|
ty_str;
|
|
|
|
ty_char;
|
|
|
|
ty_int(signedness);
|
|
|
|
ty_bits;
|
|
|
|
ty_hex(caseness);
|
2011-04-26 20:13:23 -04:00
|
|
|
ty_octal;
|
2011-04-13 18:59:40 -04:00
|
|
|
// FIXME: More types
|
2011-04-10 14:09:47 -04:00
|
|
|
|
2011-06-15 11:19:50 -07:00
|
|
|
}
|
2011-04-13 18:59:40 -04:00
|
|
|
tag flag {
|
|
|
|
flag_left_justify;
|
|
|
|
flag_left_zero_pad;
|
2011-04-17 19:50:58 -04:00
|
|
|
flag_space_for_sign;
|
2011-04-17 18:19:26 -04:00
|
|
|
flag_sign_always;
|
2011-04-13 18:59:40 -04:00
|
|
|
flag_alternate;
|
|
|
|
}
|
|
|
|
tag count {
|
|
|
|
count_is(int);
|
|
|
|
count_is_param(int);
|
|
|
|
count_is_next_param;
|
|
|
|
count_implied;
|
|
|
|
}
|
2011-04-10 14:09:47 -04:00
|
|
|
|
2011-04-13 18:59:40 -04:00
|
|
|
// A formatted conversion from an expression to a string
|
2011-06-15 11:19:50 -07:00
|
|
|
type conv =
|
|
|
|
rec(option::t[int] param,
|
|
|
|
vec[flag] flags,
|
|
|
|
count width,
|
|
|
|
count precision,
|
|
|
|
ty ty);
|
2011-04-10 14:09:47 -04:00
|
|
|
|
2011-04-11 19:52:36 -04:00
|
|
|
|
2011-06-15 11:19:50 -07:00
|
|
|
// A fragment of the output sequence
|
|
|
|
tag piece { piece_string(str); piece_conv(conv); }
|
|
|
|
type error_fn = fn(str) -> ! ;
|
2011-06-04 18:23:06 -04:00
|
|
|
|
|
|
|
fn parse_fmt_string(str s, error_fn error) -> vec[piece] {
|
2011-05-16 18:21:22 -07:00
|
|
|
let vec[piece] pieces = [];
|
2011-05-17 20:41:41 +02:00
|
|
|
auto lim = str::byte_len(s);
|
2011-04-11 20:22:27 -04:00
|
|
|
auto buf = "";
|
2011-06-10 12:02:57 +02:00
|
|
|
fn flush_buf(str buf, &mutable vec[piece] pieces) -> str {
|
2011-05-17 20:41:41 +02:00
|
|
|
if (str::byte_len(buf) > 0u) {
|
2011-04-11 20:22:27 -04:00
|
|
|
auto piece = piece_string(buf);
|
2011-05-16 18:21:22 -07:00
|
|
|
pieces += [piece];
|
2011-04-11 20:22:27 -04:00
|
|
|
}
|
|
|
|
ret "";
|
2011-04-11 19:52:36 -04:00
|
|
|
}
|
2011-04-11 20:22:27 -04:00
|
|
|
auto i = 0u;
|
|
|
|
while (i < lim) {
|
2011-05-17 20:41:41 +02:00
|
|
|
auto curr = str::substr(s, i, 1u);
|
|
|
|
if (str::eq(curr, "%")) {
|
2011-04-11 19:52:36 -04:00
|
|
|
i += 1u;
|
2011-04-11 20:22:27 -04:00
|
|
|
if (i >= lim) {
|
2011-06-04 18:23:06 -04:00
|
|
|
error("unterminated conversion at end of string");
|
2011-04-11 20:22:27 -04:00
|
|
|
}
|
2011-05-17 20:41:41 +02:00
|
|
|
auto curr2 = str::substr(s, i, 1u);
|
|
|
|
if (str::eq(curr2, "%")) {
|
2011-04-11 20:22:27 -04:00
|
|
|
i += 1u;
|
|
|
|
} else {
|
|
|
|
buf = flush_buf(buf, pieces);
|
2011-06-04 18:23:06 -04:00
|
|
|
auto res = parse_conversion(s, i, lim, error);
|
2011-05-16 18:21:22 -07:00
|
|
|
pieces += [res._0];
|
2011-04-11 20:22:27 -04:00
|
|
|
i = res._1;
|
|
|
|
}
|
2011-06-15 11:19:50 -07:00
|
|
|
} else { buf += curr; i += 1u; }
|
2011-04-11 19:52:36 -04:00
|
|
|
}
|
2011-04-11 20:22:27 -04:00
|
|
|
buf = flush_buf(buf, pieces);
|
|
|
|
ret pieces;
|
2011-04-11 19:52:36 -04:00
|
|
|
}
|
2011-05-12 17:24:54 +02:00
|
|
|
fn peek_num(str s, uint i, uint lim) -> option::t[tup(uint, uint)] {
|
2011-06-15 11:19:50 -07:00
|
|
|
if (i >= lim) { ret none[tup(uint, uint)]; }
|
2011-04-11 20:22:27 -04:00
|
|
|
auto c = s.(i);
|
|
|
|
if (!('0' as u8 <= c && c <= '9' as u8)) {
|
2011-05-12 17:24:54 +02:00
|
|
|
ret option::none[tup(uint, uint)];
|
2011-04-11 19:52:36 -04:00
|
|
|
}
|
2011-06-15 11:19:50 -07:00
|
|
|
auto n = c - ('0' as u8) as uint;
|
2011-05-20 19:09:14 -04:00
|
|
|
ret alt (peek_num(s, i + 1u, lim)) {
|
2011-06-15 11:19:50 -07:00
|
|
|
case (none) { some[tup(uint, uint)](tup(n, i + 1u)) }
|
|
|
|
case (some(?next)) {
|
|
|
|
auto m = next._0;
|
|
|
|
auto j = next._1;
|
|
|
|
some[tup(uint, uint)](tup(n * 10u + m, j))
|
|
|
|
}
|
|
|
|
};
|
2011-04-11 20:22:27 -04:00
|
|
|
}
|
2011-06-15 11:19:50 -07:00
|
|
|
fn parse_conversion(str s, uint i, uint lim, error_fn error) ->
|
|
|
|
tup(piece, uint) {
|
2011-04-11 20:22:27 -04:00
|
|
|
auto parm = parse_parameter(s, i, lim);
|
|
|
|
auto flags = parse_flags(s, parm._1, lim);
|
|
|
|
auto width = parse_count(s, flags._1, lim);
|
|
|
|
auto prec = parse_precision(s, width._1, lim);
|
2011-06-04 18:23:06 -04:00
|
|
|
auto ty = parse_type(s, prec._1, lim, error);
|
2011-06-15 11:19:50 -07:00
|
|
|
ret tup(piece_conv(rec(param=parm._0,
|
|
|
|
flags=flags._0,
|
|
|
|
width=width._0,
|
|
|
|
precision=prec._0,
|
|
|
|
ty=ty._0)), ty._1);
|
2011-04-11 19:52:36 -04:00
|
|
|
}
|
2011-05-12 17:24:54 +02:00
|
|
|
fn parse_parameter(str s, uint i, uint lim) -> tup(option::t[int], uint) {
|
2011-06-15 11:19:50 -07:00
|
|
|
if (i >= lim) { ret tup(none[int], i); }
|
2011-04-11 20:22:27 -04:00
|
|
|
auto num = peek_num(s, i, lim);
|
2011-05-20 19:09:14 -04:00
|
|
|
ret alt (num) {
|
2011-06-15 11:19:50 -07:00
|
|
|
case (none) { tup(none[int], i) }
|
|
|
|
case (some(?t)) {
|
|
|
|
auto n = t._0;
|
|
|
|
auto j = t._1;
|
|
|
|
if (j < lim && s.(j) == '$' as u8) {
|
|
|
|
tup(some[int](n as int), j + 1u)
|
|
|
|
} else { tup(none[int], i) }
|
2011-04-11 20:22:27 -04:00
|
|
|
}
|
2011-06-15 11:19:50 -07:00
|
|
|
};
|
2011-04-11 19:52:36 -04:00
|
|
|
}
|
2011-04-11 20:22:27 -04:00
|
|
|
fn parse_flags(str s, uint i, uint lim) -> tup(vec[flag], uint) {
|
2011-05-16 18:21:22 -07:00
|
|
|
let vec[flag] noflags = [];
|
2011-06-15 11:19:50 -07:00
|
|
|
if (i >= lim) { ret tup(noflags, i); }
|
2011-04-11 20:22:27 -04:00
|
|
|
fn more_(flag f, str s, uint i, uint lim) -> tup(vec[flag], uint) {
|
|
|
|
auto next = parse_flags(s, i + 1u, lim);
|
|
|
|
auto rest = next._0;
|
|
|
|
auto j = next._1;
|
2011-05-16 18:21:22 -07:00
|
|
|
let vec[flag] curr = [f];
|
2011-04-11 20:22:27 -04:00
|
|
|
ret tup(curr + rest, j);
|
|
|
|
}
|
|
|
|
auto more = bind more_(_, s, i, lim);
|
|
|
|
auto f = s.(i);
|
2011-06-15 11:19:50 -07:00
|
|
|
ret if (f == '-' as u8) {
|
|
|
|
more(flag_left_justify)
|
|
|
|
} else if (f == '0' as u8) {
|
|
|
|
more(flag_left_zero_pad)
|
|
|
|
} else if (f == ' ' as u8) {
|
|
|
|
more(flag_space_for_sign)
|
|
|
|
} else if (f == '+' as u8) {
|
|
|
|
more(flag_sign_always)
|
|
|
|
} else if (f == '#' as u8) {
|
|
|
|
more(flag_alternate)
|
|
|
|
} else { tup(noflags, i) };
|
2011-04-11 19:52:36 -04:00
|
|
|
}
|
2011-04-11 20:22:27 -04:00
|
|
|
fn parse_count(str s, uint i, uint lim) -> tup(count, uint) {
|
2011-05-20 19:09:14 -04:00
|
|
|
ret if (i >= lim) {
|
2011-06-15 11:19:50 -07:00
|
|
|
tup(count_implied, i)
|
|
|
|
} else if (s.(i) == '*' as u8) {
|
|
|
|
auto param = parse_parameter(s, i + 1u, lim);
|
|
|
|
auto j = param._1;
|
|
|
|
alt (param._0) {
|
|
|
|
case (none) { tup(count_is_next_param, j) }
|
|
|
|
case (some(?n)) { tup(count_is_param(n), j) }
|
2011-04-11 20:22:27 -04:00
|
|
|
}
|
2011-06-15 11:19:50 -07:00
|
|
|
} else {
|
|
|
|
auto num = peek_num(s, i, lim);
|
|
|
|
alt (num) {
|
|
|
|
case (none) { tup(count_implied, i) }
|
|
|
|
case (some(?num)) { tup(count_is(num._0 as int), num._1) }
|
2011-04-11 20:22:27 -04:00
|
|
|
}
|
2011-06-15 11:19:50 -07:00
|
|
|
};
|
2011-04-11 19:52:36 -04:00
|
|
|
}
|
2011-04-11 20:22:27 -04:00
|
|
|
fn parse_precision(str s, uint i, uint lim) -> tup(count, uint) {
|
2011-05-20 19:09:14 -04:00
|
|
|
ret if (i >= lim) {
|
2011-06-15 11:19:50 -07:00
|
|
|
tup(count_implied, i)
|
|
|
|
} else if (s.(i) == '.' as u8) {
|
|
|
|
auto 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._0) {
|
|
|
|
case (count_implied) { tup(count_is(0), count._1) }
|
|
|
|
case (_) { count }
|
2011-04-17 13:10:02 -04:00
|
|
|
}
|
2011-06-15 11:19:50 -07:00
|
|
|
} else { tup(count_implied, i) };
|
2011-04-11 19:52:36 -04:00
|
|
|
}
|
2011-06-04 18:23:06 -04:00
|
|
|
fn parse_type(str s, uint i, uint lim, error_fn error) -> tup(ty, uint) {
|
2011-06-15 11:19:50 -07:00
|
|
|
if (i >= lim) { error("missing type in conversion"); }
|
2011-05-17 20:41:41 +02:00
|
|
|
auto tstr = str::substr(s, i, 1u);
|
2011-06-15 11:19:50 -07:00
|
|
|
auto 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")) {
|
|
|
|
|
|
|
|
// TODO: Do we really want two signed types here?
|
|
|
|
// How important is it to be printf compatible?
|
|
|
|
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 { error("unknown type in conversion: " + tstr) };
|
2011-04-11 20:22:27 -04:00
|
|
|
ret tup(t, i + 1u);
|
|
|
|
}
|
2011-04-11 19:52:36 -04:00
|
|
|
}
|
2011-04-11 20:21:28 -04:00
|
|
|
|
2011-06-15 11:19:50 -07:00
|
|
|
|
2011-04-13 20:51:24 -04:00
|
|
|
// 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.
|
2011-05-12 19:51:22 -04:00
|
|
|
mod rt {
|
|
|
|
tag flag {
|
|
|
|
flag_left_justify;
|
|
|
|
flag_left_zero_pad;
|
|
|
|
flag_space_for_sign;
|
|
|
|
flag_sign_always;
|
|
|
|
flag_alternate;
|
2011-06-15 11:19:50 -07:00
|
|
|
|
2011-05-12 19:51:22 -04:00
|
|
|
// FIXME: This is a hack to avoid creating 0-length vec exprs,
|
|
|
|
// which have some difficulty typechecking currently. See
|
|
|
|
// comments in front::extfmt::make_flags
|
|
|
|
flag_none;
|
|
|
|
}
|
2011-06-15 11:19:50 -07:00
|
|
|
tag count { count_is(int); count_implied; }
|
|
|
|
tag ty { ty_default; ty_bits; ty_hex_upper; ty_hex_lower; ty_octal; }
|
2011-05-12 19:51:22 -04:00
|
|
|
|
|
|
|
// FIXME: May not want to use a vector here for flags;
|
|
|
|
// instead just use a bool per flag
|
2011-06-15 11:19:50 -07:00
|
|
|
type conv = rec(vec[flag] flags, count width, count precision, ty ty);
|
2011-05-12 19:51:22 -04:00
|
|
|
|
|
|
|
fn conv_int(&conv cv, int i) -> str {
|
|
|
|
auto radix = 10u;
|
|
|
|
auto prec = get_int_precision(cv);
|
|
|
|
auto s = int_to_str_prec(i, radix, prec);
|
|
|
|
if (0 <= i) {
|
|
|
|
if (have_flag(cv.flags, flag_sign_always)) {
|
|
|
|
s = "+" + s;
|
|
|
|
} else if (have_flag(cv.flags, flag_space_for_sign)) {
|
|
|
|
s = " " + s;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ret pad(cv, s, pad_signed);
|
|
|
|
}
|
|
|
|
fn conv_uint(&conv cv, uint u) -> str {
|
|
|
|
auto prec = get_int_precision(cv);
|
2011-06-15 11:19:50 -07:00
|
|
|
auto res =
|
|
|
|
alt (cv.ty) {
|
|
|
|
case (ty_default) { uint_to_str_prec(u, 10u, prec) }
|
|
|
|
case (ty_hex_lower) { uint_to_str_prec(u, 16u, prec) }
|
|
|
|
case (ty_hex_upper) {
|
|
|
|
str::to_upper(uint_to_str_prec(u, 16u, prec))
|
|
|
|
}
|
|
|
|
case (ty_bits) { uint_to_str_prec(u, 2u, prec) }
|
|
|
|
case (ty_octal) { uint_to_str_prec(u, 8u, prec) }
|
|
|
|
};
|
2011-05-12 19:51:22 -04:00
|
|
|
ret pad(cv, res, pad_unsigned);
|
|
|
|
}
|
|
|
|
fn conv_bool(&conv cv, bool b) -> str {
|
2011-06-15 11:19:50 -07:00
|
|
|
auto s = if (b) { "true" } else { "false" };
|
2011-05-12 19:51:22 -04:00
|
|
|
// run the boolean conversion through the string conversion logic,
|
|
|
|
// giving it the same rules for precision, etc.
|
2011-06-15 11:19:50 -07:00
|
|
|
|
2011-05-12 19:51:22 -04:00
|
|
|
ret conv_str(cv, s);
|
|
|
|
}
|
|
|
|
fn conv_char(&conv cv, char c) -> str {
|
2011-05-17 20:41:41 +02:00
|
|
|
ret pad(cv, str::from_char(c), pad_nozero);
|
2011-05-12 19:51:22 -04:00
|
|
|
}
|
|
|
|
fn conv_str(&conv cv, str s) -> str {
|
2011-06-15 11:19:50 -07:00
|
|
|
auto unpadded =
|
|
|
|
alt (cv.precision) {
|
|
|
|
case (count_implied) { s }
|
|
|
|
case (count_is(?max)) {
|
|
|
|
|
|
|
|
// For strings, precision is the maximum characters
|
|
|
|
// displayed
|
|
|
|
if (max as uint < str::char_len(s)) {
|
|
|
|
|
|
|
|
// FIXME: substr works on bytes, not chars!
|
|
|
|
str::substr(s, 0u, max as uint)
|
|
|
|
} else { s }
|
2011-05-12 19:51:22 -04:00
|
|
|
}
|
2011-06-15 11:19:50 -07:00
|
|
|
};
|
2011-05-12 19:51:22 -04:00
|
|
|
ret pad(cv, unpadded, pad_nozero);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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(int num, uint radix, uint prec) -> str {
|
2011-05-20 19:09:14 -04:00
|
|
|
ret if (num < 0) {
|
2011-06-15 11:19:50 -07:00
|
|
|
"-" + uint_to_str_prec(-num as uint, radix, prec)
|
|
|
|
} else { uint_to_str_prec(num as uint, radix, prec) };
|
2011-05-12 19:51:22 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
2011-05-17 20:41:41 +02:00
|
|
|
// to uint: but it doesn't seem all that useful.
|
2011-05-12 19:51:22 -04:00
|
|
|
fn uint_to_str_prec(uint num, uint radix, uint prec) -> str {
|
2011-05-20 19:09:14 -04:00
|
|
|
ret if (prec == 0u && num == 0u) {
|
2011-06-15 11:19:50 -07:00
|
|
|
""
|
2011-05-20 19:09:14 -04:00
|
|
|
} else {
|
2011-06-15 11:19:50 -07:00
|
|
|
auto s = uint::to_str(num, radix);
|
|
|
|
auto len = str::char_len(s);
|
|
|
|
if (len < prec) {
|
|
|
|
auto diff = prec - len;
|
|
|
|
auto pad = str_init_elt('0', diff);
|
|
|
|
pad + s
|
|
|
|
} else { s }
|
|
|
|
};
|
2011-05-12 19:51:22 -04:00
|
|
|
}
|
|
|
|
fn get_int_precision(&conv cv) -> uint {
|
2011-05-20 19:09:14 -04:00
|
|
|
ret alt (cv.precision) {
|
2011-06-15 11:19:50 -07:00
|
|
|
case (count_is(?c)) { c as uint }
|
|
|
|
case (count_implied) { 1u }
|
|
|
|
};
|
2011-05-12 19:51:22 -04:00
|
|
|
}
|
|
|
|
|
2011-05-17 20:41:41 +02:00
|
|
|
// FIXME: This might be useful in str: but needs to be utf8 safe first
|
2011-05-12 19:51:22 -04:00
|
|
|
fn str_init_elt(char c, uint n_elts) -> str {
|
2011-05-17 20:41:41 +02:00
|
|
|
auto svec = vec::init_elt[u8](c as u8, n_elts);
|
2011-05-12 19:51:22 -04:00
|
|
|
// FIXME: Using unsafe_from_bytes because rustboot
|
|
|
|
// can't figure out the is_utf8 predicate on from_bytes?
|
|
|
|
|
2011-06-15 11:19:50 -07:00
|
|
|
ret str::unsafe_from_bytes(svec);
|
2011-05-12 19:51:22 -04:00
|
|
|
}
|
2011-06-15 11:19:50 -07:00
|
|
|
tag pad_mode { pad_signed; pad_unsigned; pad_nozero; }
|
2011-05-12 19:51:22 -04:00
|
|
|
fn pad(&conv cv, str s, pad_mode mode) -> str {
|
|
|
|
auto uwidth;
|
|
|
|
alt (cv.width) {
|
2011-06-15 11:19:50 -07:00
|
|
|
case (count_implied) { ret s; }
|
2011-05-12 19:51:22 -04:00
|
|
|
case (count_is(?width)) {
|
|
|
|
// FIXME: Maybe width should be uint
|
2011-06-15 11:19:50 -07:00
|
|
|
|
2011-05-12 19:51:22 -04:00
|
|
|
uwidth = width as uint;
|
|
|
|
}
|
|
|
|
}
|
2011-05-17 20:41:41 +02:00
|
|
|
auto strlen = str::char_len(s);
|
2011-06-15 11:19:50 -07:00
|
|
|
if (uwidth <= strlen) { ret s; }
|
2011-05-12 19:51:22 -04:00
|
|
|
auto padchar = ' ';
|
|
|
|
auto diff = uwidth - strlen;
|
|
|
|
if (have_flag(cv.flags, flag_left_justify)) {
|
|
|
|
auto padstr = str_init_elt(padchar, diff);
|
|
|
|
ret s + padstr;
|
|
|
|
}
|
|
|
|
auto might_zero_pad = false;
|
|
|
|
auto signed = false;
|
|
|
|
alt (mode) {
|
|
|
|
case (pad_nozero) {
|
|
|
|
// fallthrough
|
2011-06-15 11:19:50 -07:00
|
|
|
|
2011-05-12 19:51:22 -04:00
|
|
|
}
|
2011-06-15 11:19:50 -07:00
|
|
|
case (pad_signed) { might_zero_pad = true; signed = true; }
|
|
|
|
case (pad_unsigned) { might_zero_pad = true; }
|
2011-05-12 19:51:22 -04:00
|
|
|
}
|
|
|
|
fn have_precision(&conv cv) -> bool {
|
2011-05-20 19:09:14 -04:00
|
|
|
ret alt (cv.precision) {
|
2011-06-15 11:19:50 -07:00
|
|
|
case (count_implied) { false }
|
|
|
|
case (_) { true }
|
|
|
|
};
|
2011-05-12 19:51:22 -04:00
|
|
|
}
|
|
|
|
auto zero_padding = false;
|
2011-06-15 11:19:50 -07:00
|
|
|
if (might_zero_pad && have_flag(cv.flags, flag_left_zero_pad) &&
|
|
|
|
!have_precision(cv)) {
|
2011-05-12 19:51:22 -04:00
|
|
|
padchar = '0';
|
|
|
|
zero_padding = true;
|
|
|
|
}
|
|
|
|
auto padstr = str_init_elt(padchar, diff);
|
|
|
|
// 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.
|
|
|
|
|
2011-06-15 11:19:50 -07:00
|
|
|
if (signed && zero_padding && str::byte_len(s) > 0u) {
|
2011-05-12 19:51:22 -04:00
|
|
|
auto head = s.(0);
|
2011-06-15 11:19:50 -07:00
|
|
|
if (head == '+' as u8 || head == '-' as u8 || head == ' ' as u8) {
|
2011-05-17 20:41:41 +02:00
|
|
|
auto headstr = str::unsafe_from_bytes([head]);
|
|
|
|
auto bytelen = str::byte_len(s);
|
|
|
|
auto numpart = str::substr(s, 1u, bytelen - 1u);
|
2011-05-12 19:51:22 -04:00
|
|
|
ret headstr + padstr + numpart;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ret padstr + s;
|
|
|
|
}
|
|
|
|
fn have_flag(vec[flag] flags, flag f) -> bool {
|
2011-06-15 11:19:50 -07:00
|
|
|
for (flag candidate in flags) { if (candidate == f) { ret true; } }
|
2011-05-12 19:51:22 -04:00
|
|
|
ret false;
|
|
|
|
}
|
|
|
|
}
|
2011-04-11 20:22:05 -04:00
|
|
|
// Local Variables:
|
|
|
|
// mode: rust;
|
|
|
|
// fill-column: 78;
|
|
|
|
// indent-tabs-mode: nil
|
|
|
|
// c-basic-offset: 4
|
|
|
|
// buffer-file-coding-system: utf-8-unix
|
2011-06-15 12:01:19 -07:00
|
|
|
// compile-command: "make -k -C $RBUILD 2>&1 | sed -e 's/\\/x\\//x:\\//g'";
|
2011-04-11 20:22:05 -04:00
|
|
|
// End:
|