Skip to content

Commit

Permalink
refactor: template - minor optimization
Browse files Browse the repository at this point in the history
optimize allocations for context map
  • Loading branch information
jqnatividad committed Nov 8, 2024
1 parent 930b3f2 commit 7c7b551
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/cmd/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ pub fn run(argv: &[&str]) -> CliResult<()> {
sanitized_headers.push(QSV_ROWNO.to_owned());
csv::StringRecord::from(sanitized_headers)
};
let headers_len = headers.len();

// Set up output handling
let output_to_dir = args.arg_outdir.is_some();
Expand Down Expand Up @@ -266,7 +267,7 @@ pub fn run(argv: &[&str]) -> CliResult<()> {
.map(|record| {
let curr_record = record;

let mut context = simd_json::owned::Object::default();
let mut context = simd_json::owned::Object::with_capacity(headers_len);
let mut row_number = 0_u64;

if no_headers {
Expand Down Expand Up @@ -364,6 +365,8 @@ fn substr(value: &str, start: u32, end: Option<u32>) -> String {
/// Formats a float number string with the specified decimal precision.
/// Returns --customfilter-error (default: <FILTER_ERROR>) if input cannot be parsed as float.
fn format_float(value: &str, precision: u32) -> String {
// Prevent excessive precision
let precision = precision.min(16);
value.parse::<f64>().map_or_else(
|_| FILTER_ERROR.get().unwrap().clone(),
|num| format!("{:.1$}", num, precision as usize),
Expand Down Expand Up @@ -412,5 +415,8 @@ fn round_num(value: &str, places: u32) -> String {
/// Returns true for "true", "1", or "yes" (case insensitive).
/// Returns false for all other values.
fn str_to_bool(value: &str) -> bool {
matches!(value.to_ascii_lowercase().as_str(), "true" | "1" | "yes")
matches!(
value.to_ascii_lowercase().as_str(),
"true" | "1" | "yes" | "t" | "y"
)
}

0 comments on commit 7c7b551

Please sign in to comment.