8000 int locale format skeleton · RustPython/RustPython@f1df833 · GitHub
[go: up one dir, main page]

Skip to content

Commit f1df833

Browse files
committed
int locale format skeleton
1 parent 3cb6428 commit f1df833

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

common/src/format.rs

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ use num_bigint::{BigInt, Sign};
55
use num_traits::{cast::ToPrimitive, Signed};
66
use std::{cmp, str::FromStr};
77

8+
pub struct Locale;
9+
pub type LocaleFunc = fn() -> Locale;
10+
811
trait FormatParse {
912
fn parse(text: &str) -> (Option<Self>, &str)
1013
where
@@ -312,6 +315,7 @@ impl FormatSpec {
312315
inter: i32,
313316
sep: char,
314317
disp_digit_cnt: i32,
318+
locale_info: Option<LocaleFunc>, // will be used to calculate numbers here
315319
) -> String {
316320
// Don't add separators to the floating decimal point of numbers
317321
let mut parts = magnitude_str.splitn(2, '.');
@@ -391,7 +395,12 @@ impl FormatSpec {
391395
}
392396
}
393397

394-
fn add_magnitude_separators(&self, magnitude_str: String, prefix: &str) -> String {
398+
fn add_magnitude_separators(
399+
&self,
400+
magnitude_str: String,
401+
prefix: &str,
402+
locale_info: Option<LocaleFunc>,
403+
) -> String {
395404
match &self.grouping_option {
396405
Some(fg) => {
397406
let sep = match fg {
@@ -407,6 +416,7 @@ impl FormatSpec {
407416
inter,
408417
sep,
409418
disp_digit_cnt,
419+
locale_info,
410420
)
411421
}
412422
None => magnitude_str,
@@ -487,7 +497,7 @@ impl FormatSpec {
487497
FormatSign::MinusOrSpace => " ",
488498
}
489499
};
490-
let magnitude_str = self.add_magnitude_separators(raw_magnitude_str?, sign_str);
500+
let magnitude_str = self.add_magnitude_separators(raw_magnitude_str?, sign_str, None);
491501
self.format_sign_and_align(
492502
unsafe { &BorrowedStr::from_ascii_unchecked(magnitude_str.as_bytes()) },
493503
sign_str,
@@ -503,7 +513,11 @@ impl FormatSpec {
503513
}
504514
}
505515

506-
pub fn format_int(&self, num: &BigInt) -> Result<String, FormatSpecError> {
516+
pub fn format_int(
517+
&self,
518+
num: &BigInt,
519+
locale_info: Option<LocaleFunc>,
520+
) -> Result<String, FormatSpecError> {
507521
self.validate_format(FormatType::Decimal)?;
508522
let magnitude = num.abs();
509523
let prefix = if self.alternate_form {
@@ -559,7 +573,8 @@ impl FormatSpec {
559573
},
560574
};
561575
let sign_prefix = format!("{sign_str}{prefix}");
562-
let magnitude_str = self.add_magnitude_separators(raw_magnitude_str, &sign_prefix);
576+
let magnitude_str =
577+
self.add_magnitude_separators(raw_magnitude_str, &sign_prefix, locale_info);
563578
self.format_sign_and_align(
564579
&BorrowedStr::from_bytes(magnitude_str.as_bytes()),
565580
&sign_prefix,

vm/src/builtins/int.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -568,8 +568,9 @@ impl PyInt {
568568

569569
#[pymethod(magic)]
570570
fn format(&self, spec: PyStrRef, vm: &VirtualMachine) -> PyResult<String> {
571+
let locale_func = None; // TODO: place proper get_locale_info wrapper
571572
FormatSpec::parse(spec.as_str())
572-
.and_then(|format_spec| format_spec.format_int(&self.value))
573+
.and_then(|format_spec| format_spec.format_int(&self.value, locale_func))
573574
.map_err(|err| err.into_pyexception(vm))
574575
}
575576

0 commit comments

Comments
 (0)
0