Skip to content

Commit

Permalink
extract out locales for a later change
Browse files Browse the repository at this point in the history
  • Loading branch information
conradludgate committed Oct 17, 2022
1 parent e5abeb9 commit 9bb312c
Showing 1 changed file with 77 additions and 53 deletions.
130 changes: 77 additions & 53 deletions src/format/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,63 @@ const TOO_SHORT: ParseError = ParseError(ParseErrorKind::TooShort);
const TOO_LONG: ParseError = ParseError(ParseErrorKind::TooLong);
const BAD_FORMAT: ParseError = ParseError(ParseErrorKind::BadFormat);

#[cfg(any(feature = "alloc", feature = "std", test))]
struct Locales {
short_months: &'static [&'static str],
long_months: &'static [&'static str],
short_weekdays: &'static [&'static str],
long_weekdays: &'static [&'static str],
am_pm: &'static [&'static str],
}

#[cfg(any(feature = "alloc", feature = "std", test))]
impl Locales {
fn new(_locale: Option<Locale>) -> Self {
#[cfg(feature = "unstable-locales")]
{
let locale = _locale.unwrap_or(Locale::POSIX);
Self {
short_months: locales::short_months(locale),
long_months: locales::long_months(locale),
short_weekdays: locales::short_weekdays(locale),
long_weekdays: locales::long_weekdays(locale),
am_pm: locales::am_pm(locale),
}
}
#[cfg(not(feature = "unstable-locales"))]
Self {
short_months: &[
"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
],
long_months: &[
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December",
],
short_weekdays: &["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"],
long_weekdays: &[
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday",
],
am_pm: &["AM", "PM"],
}
}
}

/// Formats single formatting item
#[cfg(any(feature = "alloc", feature = "std", test))]
#[cfg_attr(docsrs, doc(cfg(any(feature = "alloc", feature = "std"))))]
Expand All @@ -463,45 +520,9 @@ fn format_inner<'a>(
time: Option<&NaiveTime>,
off: Option<&(String, FixedOffset)>,
item: &Item<'a>,
_locale: Option<Locale>,
locale: Option<Locale>,
) -> fmt::Result {
#[cfg(feature = "unstable-locales")]
let (short_months, long_months, short_weekdays, long_weekdays, am_pm, am_pm_lowercase) = {
let locale = _locale.unwrap_or(Locale::POSIX);
let am_pm = locales::am_pm(locale);
(
locales::short_months(locale),
locales::long_months(locale),
locales::short_weekdays(locale),
locales::long_weekdays(locale),
am_pm,
&[am_pm[0].to_lowercase(), am_pm[1].to_lowercase()],
)
};
#[cfg(not(feature = "unstable-locales"))]
let (short_months, long_months, short_weekdays, long_weekdays, am_pm, am_pm_lowercase) = {
(
&["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"],
&[
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December",
],
&["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"],
&["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"],
&["AM", "PM"],
&["am", "pm"],
)
};
let locale = Locales::new(locale);

use num_integer::{div_floor, mod_floor};

Expand Down Expand Up @@ -581,35 +602,38 @@ fn format_inner<'a>(
let ret =
match *spec {
ShortMonthName => date.map(|d| {
result.push_str(short_months[d.month0() as usize]);
result.push_str(locale.short_months[d.month0() as usize]);
Ok(())
}),
LongMonthName => date.map(|d| {
result.push_str(long_months[d.month0() as usize]);
result.push_str(locale.long_months[d.month0() as usize]);
Ok(())
}),
ShortWeekdayName => date.map(|d| {
result
.push_str(short_weekdays[d.weekday().num_days_from_sunday() as usize]);
result.push_str(
locale.short_weekdays[d.weekday().num_days_from_sunday() as usize],
);
Ok(())
}),
LongWeekdayName => date.map(|d| {
result.push_str(long_weekdays[d.weekday().num_days_from_sunday() as usize]);
result.push_str(
locale.long_weekdays[d.weekday().num_days_from_sunday() as usize],
);
Ok(())
}),
LowerAmPm => time.map(|t| {
#[cfg_attr(feature = "cargo-clippy", allow(clippy::useless_asref))]
{
result.push_str(if t.hour12().0 {
am_pm_lowercase[1].as_ref()
} else {
am_pm_lowercase[0].as_ref()
});
let ampm = if t.hour12().0 { locale.am_pm[1] } else { locale.am_pm[0] };
for char in ampm.chars() {
result.extend(char.to_lowercase())
}
Ok(())
}),
UpperAmPm => time.map(|t| {
result.push_str(if t.hour12().0 { am_pm[1] } else { am_pm[0] });
result.push_str(if t.hour12().0 {
locale.am_pm[1]
} else {
locale.am_pm[0]
});
Ok(())
}),
Nanosecond => time.map(|t| {
Expand Down Expand Up @@ -680,9 +704,9 @@ fn format_inner<'a>(
write!(
result,
"{}, {:02} {} {:04} {:02}:{:02}:{:02} ",
short_weekdays[d.weekday().num_days_from_sunday() as usize],
locale.short_weekdays[d.weekday().num_days_from_sunday() as usize],
d.day(),
short_months[d.month0() as usize],
locale.short_months[d.month0() as usize],
d.year(),
t.hour(),
t.minute(),
Expand Down

0 comments on commit 9bb312c

Please sign in to comment.