diff --git a/src/currency/cash.rs b/src/currency/cash.rs index b7bcd276..9b1dabaf 100644 --- a/src/currency/cash.rs +++ b/src/currency/cash.rs @@ -1,4 +1,4 @@ -use std::fmt::{self, Write}; +use std::fmt; use std::ops::{Add, AddAssign, Div, Mul, Neg, Sub, SubAssign}; use std::str::FromStr; @@ -175,24 +175,35 @@ impl fmt::Display for Cash { } fn format_currency(currency: &str, mut amount: &str) -> String { - let mut buffer = String::new(); + let mut buffer = String::with_capacity(amount.len() + 1); - if currency == "USD" { + let prefix = match currency { + "EUR" => Some('€'), + "GBP" => Some('£'), + "USD" => Some('$'), + _ => None, + }; + + if let Some(prefix) = prefix { if amount.starts_with('-') || amount.starts_with('+') { - write!(&mut buffer, "{}", &amount[..1]).unwrap(); + buffer.push_str(&amount[..1]); amount = &amount[1..]; } - - write!(&mut buffer, "$").unwrap(); + buffer.push(prefix); } - write!(&mut buffer, "{}", amount).unwrap(); + buffer.push_str(amount); - match currency { - "USD" => (), - "RUB" => write!(&mut buffer, "₽").unwrap(), - _ => write!(&mut buffer, " {}", currency).unwrap(), - }; + if prefix.is_none() { + match currency { + "HKD" => buffer.push_str(" HK$"), + "RUB" => buffer.push('₽'), + _ => { + buffer.push(' '); + buffer.push_str(currency); + }, + }; + } buffer } diff --git a/testdata b/testdata index f95d8f6b..c347bb87 160000 --- a/testdata +++ b/testdata @@ -1 +1 @@ -Subproject commit f95d8f6bba723d5173850516487a606bd044736f +Subproject commit c347bb8725023d6095f17ff6369c975ef3d278e3