Skip to content

Commit 10f5393

Browse files
committed
clean up cformat a bit
1 parent 466e6fc commit 10f5393

File tree

1 file changed

+33
-35
lines changed

1 file changed

+33
-35
lines changed

vm/src/cformat.rs

Lines changed: 33 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,18 @@ bitflags! {
9292
}
9393
}
9494

95+
impl CConversionFlags {
96+
fn sign_string(&self) -> &'static str {
97+
if self.contains(CConversionFlags::SIGN_CHAR) {
98+
"+"
99+
} else if self.contains(CConversionFlags::BLANK_SIGN) {
100+
" "
101+
} else {
102+
""
103+
}
104+
}
105+
}
106+
95107
#[derive(Debug, PartialEq)]
96108
enum CFormatQuantity {
97109
Amount(usize),
@@ -198,46 +210,37 @@ impl CFormatSpec {
198210

199211
let sign_string = match num.sign() {
200212
Sign::Minus => "-",
201-
_ => {
202-
if self.flags.contains(CConversionFlags::SIGN_CHAR) {
203-
"+"
204-
} else if self.flags.contains(CConversionFlags::BLANK_SIGN) {
205-
" "
206-
} else {
207-
""
208-
}
209-
}
213+
_ => self.flags.sign_string(),
210214
};
211215

212-
let prefix = format!("{}{}", sign_string, prefix);
213-
214216
if self.flags.contains(CConversionFlags::ZERO_PAD) {
215217
let fill_char = if !self.flags.contains(CConversionFlags::LEFT_ADJUST) {
216218
'0'
217219
} else {
218220
' ' // '-' overrides the '0' conversion if both are given
219221
};
222+
let signed_prefix = format!("{}{}", sign_string, prefix);
220223
format!(
221224
"{}{}",
222-
prefix,
223-
self.fill_string(magnitude_string, fill_char, Some(prefix.chars().count()))
225+
signed_prefix,
226+
self.fill_string(
227+
magnitude_string,
228+
fill_char,
229+
Some(signed_prefix.chars().count())
230+
)
224231
)
225232
} else {
226-
self.fill_string(format!("{}{}", prefix, magnitude_string), ' ', None)
233+
self.fill_string(
234+
format!("{}{}{}", sign_string, prefix, magnitude_string),
235+
' ',
236+
None,
237+
)
227238
}
228239
}
229240

230241
pub(crate) fn format_float(&self, num: f64) -> Result<String, String> {
231-
let magnitude = num.abs();
232-
233242
let sign_string = if num.is_sign_positive() {
234-
if self.flags.contains(CConversionFlags::SIGN_CHAR) {
235-
"+"
236-
} else if self.flags.contains(CConversionFlags::BLANK_SIGN) {
237-
" "
238-
} else {
239-
""
240-
}
243+
self.flags.sign_string()
241244
} else {
242245
"-"
243246
};
@@ -248,6 +251,7 @@ impl CFormatSpec {
248251
Some(CFormatQuantity::Amount(p)) => p,
249252
_ => 6,
250253
};
254+
let magnitude = num.abs();
251255
format!("{:.*}", precision, magnitude)
252256
}
253257
CFormatType::Float(CFloatType::Exponent(_)) => {
@@ -308,10 +312,8 @@ impl CFormatSpec {
308312
}
309313
Ok(self.format_number(objint::get_value(&obj)))
310314
}
311-
CFormatType::Float(_) => if objtype::isinstance(&obj, &vm.ctx.types.float_type) {
312-
self.format_float(objfloat::get_value(&obj))
313-
} else if objtype::isinstance(&obj, &vm.ctx.types.int_type) {
314-
self.format_float(objint::get_value(&obj).to_f64().unwrap())
315+
CFormatType::Float(_) => if let Some(value) = objfloat::try_float(&obj, vm)? {
316+
self.format_float(value)
315317
} else {
316318
let required_type_string = "an floating point or integer";
317319
return Err(vm.new_type_error(format!(
@@ -326,16 +328,12 @@ impl CFormatSpec {
326328
let ch = {
327329
if objtype::isinstance(&obj, &vm.ctx.types.int_type) {
328330
// BigInt truncation is fine in this case because only the unicode range is relevant
329-
match objint::get_value(&obj)
331+
objint::get_value(&obj)
330332
.to_u32()
331333
.and_then(std::char::from_u32)
332-
{
333-
Some(value) => Ok(value),
334-
None => {
335-
Err(vm
336-
.new_overflow_error("%c arg not in range(0x110000)".to_owned()))
337-
}
338-
}
334+
.ok_or_else(|| {
335+
vm.new_overflow_error("%c arg not in range(0x110000)".to_owned())
336+
})
339337
} else if objtype::isinstance(&obj, &vm.ctx.types.str_type) {
340338
let s = objstr::borrow_value(&obj);
341339
let num_chars = s.chars().count();

0 commit comments

Comments
 (0)