Skip to content

Commit a6288ac

Browse files
committed
Cargo format.
1 parent bbe4844 commit a6288ac

File tree

3 files changed

+195
-80
lines changed

3 files changed

+195
-80
lines changed

vm/src/format.rs

+152-56
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ impl FormatAlign {
1818
'>' => Some(FormatAlign::Right),
1919
'=' => Some(FormatAlign::AfterSign),
2020
'^' => Some(FormatAlign::Center),
21-
_ => None,
21+
_ => None,
2222
}
2323
}
2424
}
@@ -75,8 +75,7 @@ fn get_num_digits(text: &str) -> usize {
7575
text.len()
7676
}
7777

78-
fn parse_align(text: &str) -> (Option<FormatAlign>, &str){
79-
78+
fn parse_align(text: &str) -> (Option<FormatAlign>, &str) {
8079
let mut chars = text.chars();
8180
let maybe_align = chars.next().and_then(FormatAlign::from_char);
8281
if maybe_align.is_some() {
@@ -105,21 +104,24 @@ fn parse_fill_and_align(text: &str) -> (Option<char>, Option<FormatAlign>, &str)
105104
}
106105

107106
fn parse_number(text: &str) -> (Option<usize>, &str) {
108-
let num_digits: usize= get_num_digits(text);
107+
let num_digits: usize = get_num_digits(text);
109108
if num_digits == 0 {
110-
return (None, text)
109+
return (None, text);
111110
}
112111
// This should never fail
113-
(Some(text[..num_digits].parse::<usize>().unwrap()), &text[num_digits..])
112+
(
113+
Some(text[..num_digits].parse::<usize>().unwrap()),
114+
&text[num_digits..],
115+
)
114116
}
115117

116-
fn parse_sign(text: &str) -> (Option<FormatSign>, &str){
118+
fn parse_sign(text: &str) -> (Option<FormatSign>, &str) {
117119
let mut chars = text.chars();
118120
match chars.next() {
119121
Some('-') => (Some(FormatSign::Minus), chars.as_str()),
120122
Some('+') => (Some(FormatSign::Plus), chars.as_str()),
121123
Some(' ') => (Some(FormatSign::MinusOrSpace), chars.as_str()),
122-
_ => (None, text)
124+
_ => (None, text),
123125
}
124126
}
125127

@@ -149,7 +151,7 @@ fn parse_precision(text: &str) -> (Option<usize>, &str) {
149151
} else {
150152
(None, text)
151153
}
152-
},
154+
}
153155
_ => (None, text),
154156
}
155157
}
@@ -211,10 +213,16 @@ impl FormatSpec {
211213
}
212214

213215
fn compute_fill_string(fill_char: char, fill_chars_needed: i32) -> String {
214-
(0..fill_chars_needed).map(|_| fill_char).collect::<String>()
216+
(0..fill_chars_needed)
217+
.map(|_| fill_char)
218+
.collect::<String>()
215219
}
216220

217-
fn add_magnitude_separators_for_char(magnitude_string: String, interval: usize, separator: char) -> String {
221+
fn add_magnitude_separators_for_char(
222+
magnitude_string: String,
223+
interval: usize,
224+
separator: char,
225+
) -> String {
218226
let mut result = String::new();
219227
let mut remaining: usize = magnitude_string.len() % interval;
220228
if remaining == 0 {
@@ -245,13 +253,21 @@ impl FormatSpec {
245253

246254
fn add_magnitude_separators(&self, magnitude_string: String) -> String {
247255
match self.grouping_option {
248-
Some(FormatGrouping::Comma) => FormatSpec::add_magnitude_separators_for_char(magnitude_string, self.get_separator_interval(), ','),
249-
Some(FormatGrouping::Underscore) => FormatSpec::add_magnitude_separators_for_char(magnitude_string, self.get_separator_interval(), '_'),
250-
None => magnitude_string
256+
Some(FormatGrouping::Comma) => FormatSpec::add_magnitude_separators_for_char(
257+
magnitude_string,
258+
self.get_separator_interval(),
259+
',',
260+
),
261+
Some(FormatGrouping::Underscore) => FormatSpec::add_magnitude_separators_for_char(
262+
magnitude_string,
263+
self.get_separator_interval(),
264+
'_',
265+
),
266+
None => magnitude_string,
251267
}
252268
}
253269

254-
pub fn format_int(&self, num: &BigInt) -> Result<String, &'static str> {
270+
pub fn format_int(&self, num: &BigInt) -> Result<String, &'static str> {
255271
let fill_char = self.fill.unwrap_or(' ');
256272
let magnitude = num.abs();
257273
let prefix = if self.alternate_form {
@@ -262,7 +278,9 @@ impl FormatSpec {
262278
Some(FormatType::HexUpper) => "0x",
263279
_ => "",
264280
}
265-
} else { "" };
281+
} else {
282+
""
283+
};
266284
let raw_magnitude_string_result: Result<String, &'static str> = match self.format_type {
267285
Some(FormatType::Binary) => Ok(magnitude.to_str_radix(2)),
268286
Some(FormatType::Decimal) => Ok(magnitude.to_str_radix(10)),
@@ -276,18 +294,34 @@ impl FormatSpec {
276294
Some(FormatType::Number) => Ok(magnitude.to_str_radix(10)),
277295
Some(FormatType::String) => Err("Unknown format code 's' for object of type 'int'"),
278296
Some(FormatType::Character) => Err("Unknown format code 'c' for object of type 'int'"),
279-
Some(FormatType::GeneralFormatUpper) => Err("Unknown format code 'G' for object of type 'int'"),
280-
Some(FormatType::GeneralFormatLower) => Err("Unknown format code 'g' for object of type 'int'"),
281-
Some(FormatType::ExponentUpper) => Err("Unknown format code 'E' for object of type 'int'"),
282-
Some(FormatType::ExponentLower) => Err("Unknown format code 'e' for object of type 'int'"),
283-
Some(FormatType::FixedPointUpper) => Err("Unknown format code 'F' for object of type 'int'"),
284-
Some(FormatType::FixedPointLower) => Err("Unknown format code 'f' for object of type 'int'"),
297+
Some(FormatType::GeneralFormatUpper) => {
298+
Err("Unknown format code 'G' for object of type 'int'")
299+
}
300+
Some(FormatType::GeneralFormatLower) => {
301+
Err("Unknown format code 'g' for object of type 'int'")
302+
}
303+
Some(FormatType::ExponentUpper) => {
304+
Err("Unknown format code 'E' for object of type 'int'")
305+
}
306+
Some(FormatType::ExponentLower) => {
307+
Err("Unknown format code 'e' for object of type 'int'")
308+
}
309+
Some(FormatType::FixedPointUpper) => {
310+
Err("Unknown format code 'F' for object of type 'int'")
311+
}
312+
Some(FormatType::FixedPointLower) => {
313+
Err("Unknown format code 'f' for object of type 'int'")
314+
}
285315
None => Ok(magnitude.to_str_radix(10)),
286316
};
287317
if !raw_magnitude_string_result.is_ok() {
288318
return raw_magnitude_string_result;
289319
}
290-
let magnitude_string = format!("{}{}", prefix, self.add_magnitude_separators(raw_magnitude_string_result.unwrap()));
320+
let magnitude_string = format!(
321+
"{}{}",
322+
prefix,
323+
self.add_magnitude_separators(raw_magnitude_string_result.unwrap())
324+
);
291325
let align = self.align.unwrap_or(FormatAlign::Right);
292326

293327
// Use the byte length as the string length since we're in ascii
@@ -300,21 +334,43 @@ impl FormatSpec {
300334
FormatSign::Plus => "+",
301335
FormatSign::Minus => "",
302336
FormatSign::MinusOrSpace => " ",
303-
}
337+
},
304338
};
305339

306-
let fill_chars_needed: i32 = self.width.map_or(0, |w| cmp::max(0, (w as i32) - (num_chars as i32)- (sign_str.len() as i32)));
340+
let fill_chars_needed: i32 = self.width.map_or(0, |w| {
341+
cmp::max(0, (w as i32) - (num_chars as i32) - (sign_str.len() as i32))
342+
});
307343
Ok(match align {
308-
FormatAlign::Left => format!("{}{}{}", sign_str, magnitude_string, FormatSpec::compute_fill_string(fill_char, fill_chars_needed)),
309-
FormatAlign::Right => format!("{}{}{}", FormatSpec::compute_fill_string(fill_char, fill_chars_needed), sign_str, magnitude_string),
310-
FormatAlign::AfterSign => format!("{}{}{}", sign_str, FormatSpec::compute_fill_string(fill_char, fill_chars_needed), magnitude_string),
344+
FormatAlign::Left => format!(
345+
"{}{}{}",
346+
sign_str,
347+
magnitude_string,
348+
FormatSpec::compute_fill_string(fill_char, fill_chars_needed)
349+
),
350+
FormatAlign::Right => format!(
351+
"{}{}{}",
352+
FormatSpec::compute_fill_string(fill_char, fill_chars_needed),
353+
sign_str,
354+
magnitude_string
355+
),
356+
FormatAlign::AfterSign => format!(
357+
"{}{}{}",
358+
sign_str,
359+
FormatSpec::compute_fill_string(fill_char, fill_chars_needed),
360+
magnitude_string
361+
),
311362
FormatAlign::Center => {
312363
let left_fill_chars_needed = fill_chars_needed / 2;
313364
let right_fill_chars_needed = fill_chars_needed - left_fill_chars_needed;
314-
let left_fill_string = FormatSpec::compute_fill_string(fill_char, left_fill_chars_needed);
315-
let right_fill_string = FormatSpec::compute_fill_string(fill_char, right_fill_chars_needed);
316-
format!("{}{}{}{}", left_fill_string, sign_str, magnitude_string, right_fill_string)
317-
},
365+
let left_fill_string =
366+
FormatSpec::compute_fill_string(fill_char, left_fill_chars_needed);
367+
let right_fill_string =
368+
FormatSpec::compute_fill_string(fill_char, right_fill_chars_needed);
369+
format!(
370+
"{}{}{}{}",
371+
left_fill_string, sign_str, magnitude_string, right_fill_string
372+
)
373+
}
318374
})
319375
}
320376
}
@@ -345,14 +401,14 @@ impl FormatPart {
345401
pub fn is_auto(&self) -> bool {
346402
match self {
347403
FormatPart::AutoSpec(_) => true,
348-
_ => false
404+
_ => false,
349405
}
350406
}
351407

352408
pub fn is_index(&self) -> bool {
353409
match self {
354410
FormatPart::IndexSpec(_, _) => true,
355-
_ => false
411+
_ => false,
356412
}
357413
}
358414
}
@@ -387,14 +443,14 @@ impl FormatString {
387443
Ok((next_char, remaining)) => {
388444
result_string.push(next_char);
389445
cur_text = remaining;
390-
},
446+
}
391447
Err(err) => {
392448
if result_string.len() > 0 {
393449
return Ok((FormatPart::Literal(result_string.to_string()), cur_text));
394450
} else {
395451
return Err(err);
396452
}
397-
},
453+
}
398454
}
399455
}
400456
Ok((FormatPart::Literal(result_string), ""))
@@ -413,7 +469,7 @@ impl FormatString {
413469

414470
if arg_part.len() == 0 {
415471
return Ok(FormatPart::AutoSpec(format_spec));
416-
}
472+
}
417473

418474
if let Ok(index) = arg_part.parse::<usize>() {
419475
Ok(FormatPart::IndexSpec(index, format_spec))
@@ -422,7 +478,6 @@ impl FormatString {
422478
}
423479
}
424480

425-
426481
fn parse_spec(text: &str) -> Result<(FormatPart, &str), FormatParseError> {
427482
let mut chars = text.chars();
428483
if chars.next() != Some('{') {
@@ -467,11 +522,26 @@ mod tests {
467522

468523
#[test]
469524
fn test_fill_and_align() {
470-
assert_eq!(parse_fill_and_align(" <"), (Some(' '), Some(FormatAlign::Left), ""));
471-
assert_eq!(parse_fill_and_align(" <22"), (Some(' '), Some(FormatAlign::Left), "22"));
472-
assert_eq!(parse_fill_and_align("<22"), (None, Some(FormatAlign::Left), "22"));
473-
assert_eq!(parse_fill_and_align(" ^^"), (Some(' '), Some(FormatAlign::Center), "^"));
474-
assert_eq!(parse_fill_and_align("==="), (Some('='), Some(FormatAlign::AfterSign), "="));
525+
assert_eq!(
526+
parse_fill_and_align(" <"),
527+
(Some(' '), Some(FormatAlign::Left), "")
528+
);
529+
assert_eq!(
530+
parse_fill_and_align(" <22"),
531+
(Some(' '), Some(FormatAlign::Left), "22")
532+
);
533+
assert_eq!(
534+
parse_fill_and_align("<22"),
535+
(None, Some(FormatAlign::Left), "22")
536+
);
537+
assert_eq!(
538+
parse_fill_and_align(" ^^"),
539+
(Some(' '), Some(FormatAlign::Center), "^")
540+
);
541+
assert_eq!(
542+
parse_fill_and_align("==="),
543+
(Some('='), Some(FormatAlign::AfterSign), "=")
544+
);
475545
}
476546

477547
#[test]
@@ -521,42 +591,68 @@ mod tests {
521591

522592
#[test]
523593
fn test_format_int() {
524-
assert_eq!(parse_format_spec("d").format_int(&BigInt::from_bytes_be(Sign::Plus, b"\x10")), Ok("16".to_string()));
525-
assert_eq!(parse_format_spec("x").format_int(&BigInt::from_bytes_be(Sign::Plus, b"\x10")), Ok("10".to_string()));
526-
assert_eq!(parse_format_spec("b").format_int(&BigInt::from_bytes_be(Sign::Plus, b"\x10")), Ok("10000".to_string()));
527-
assert_eq!(parse_format_spec("o").format_int(&BigInt::from_bytes_be(Sign::Plus, b"\x10")), Ok("20".to_string()));
528-
assert_eq!(parse_format_spec("+d").format_int(&BigInt::from_bytes_be(Sign::Plus, b"\x10")), Ok("+16".to_string()));
529-
assert_eq!(parse_format_spec("^ 5d").format_int(&BigInt::from_bytes_be(Sign::Minus, b"\x10")), Ok(" -16 ".to_string()));
530-
assert_eq!(parse_format_spec("0>+#10x").format_int(&BigInt::from_bytes_be(Sign::Plus, b"\x10")), Ok("00000+0x10".to_string()));
594+
assert_eq!(
595+
parse_format_spec("d").format_int(&BigInt::from_bytes_be(Sign::Plus, b"\x10")),
596+
Ok("16".to_string())
597+
);
598+
assert_eq!(
599+
parse_format_spec("x").format_int(&BigInt::from_bytes_be(Sign::Plus, b"\x10")),
600+
Ok("10".to_string())
601+
);
602+
assert_eq!(
603+
parse_format_spec("b").format_int(&BigInt::from_bytes_be(Sign::Plus, b"\x10")),
604+
Ok("10000".to_string())
605+
);
606+
assert_eq!(
607+
parse_format_spec("o").format_int(&BigInt::from_bytes_be(Sign::Plus, b"\x10")),
608+
Ok("20".to_string())
609+
);
610+
assert_eq!(
611+
parse_format_spec("+d").format_int(&BigInt::from_bytes_be(Sign::Plus, b"\x10")),
612+
Ok("+16".to_string())
613+
);
614+
assert_eq!(
615+
parse_format_spec("^ 5d").format_int(&BigInt::from_bytes_be(Sign::Minus, b"\x10")),
616+
Ok(" -16 ".to_string())
617+
);
618+
assert_eq!(
619+
parse_format_spec("0>+#10x").format_int(&BigInt::from_bytes_be(Sign::Plus, b"\x10")),
620+
Ok("00000+0x10".to_string())
621+
);
531622
}
532623

533624
#[test]
534625
fn test_format_parse() {
535626
let expected = Ok(FormatString {
536-
format_parts: vec!(
627+
format_parts: vec![
537628
FormatPart::Literal("abcd".to_string()),
538629
FormatPart::IndexSpec(1, String::new()),
539630
FormatPart::Literal(":".to_string()),
540-
FormatPart::KeywordSpec("key".to_string(), String::new()))
631+
FormatPart::KeywordSpec("key".to_string(), String::new()),
632+
],
541633
});
542634

543635
assert_eq!(FormatString::from_str("abcd{1}:{key}"), expected);
544636
}
545637

546638
#[test]
547639
fn test_format_parse_fail() {
548-
assert_eq!(FormatString::from_str("{s"), Err(FormatParseError::UnmatchedBracket));
640+
assert_eq!(
641+
FormatString::from_str("{s"),
642+
Err(FormatParseError::UnmatchedBracket)
643+
);
549644
}
550645

551646
#[test]
552647
fn test_format_parse_escape() {
553648
let expected = Ok(FormatString {
554-
format_parts: vec!(
649+
format_parts: vec![
555650
FormatPart::Literal("{".to_string()),
556651
FormatPart::KeywordSpec("key".to_string(), String::new()),
557-
FormatPart::Literal("}ddfe".to_string()))
652+
FormatPart::Literal("}ddfe".to_string()),
653+
],
558654
});
559655

560656
assert_eq!(FormatString::from_str("{{{key}}}ddfe"), expected);
561657
}
562-
}
658+
}

vm/src/obj/objint.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1+
use super::super::format::FormatSpec;
12
use super::super::pyobject::{
23
FromPyObjectRef, PyContext, PyFuncArgs, PyObject, PyObjectKind, PyObjectRef, PyResult,
34
TypeProtocol,
45
};
5-
use super::super::format::{
6-
FormatSpec,
7-
};
86
use super::super::vm::VirtualMachine;
97
use super::objfloat;
108
use super::objstr;
@@ -227,7 +225,10 @@ fn int_format(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
227225
arg_check!(
228226
vm,
229227
args,
230-
required = [(i, Some(vm.ctx.int_type())), (format_spec, Some(vm.ctx.str_type()))]
228+
required = [
229+
(i, Some(vm.ctx.int_type())),
230+
(format_spec, Some(vm.ctx.str_type()))
231+
]
231232
);
232233
let string_value = objstr::get_value(format_spec);
233234
let format_spec = FormatSpec::parse(&string_value);

0 commit comments

Comments
 (0)