From 12e9769b4b24db3510e429fb430e10031de4e449 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Wed, 16 Jun 2021 21:40:17 +0530 Subject: [PATCH] Dont resize scratch unneccessarily --- src/calibre/srv/fast_css_transform.cpp | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/calibre/srv/fast_css_transform.cpp b/src/calibre/srv/fast_css_transform.cpp index 493f32d350..e3a45ea5d5 100644 --- a/src/calibre/srv/fast_css_transform.cpp +++ b/src/calibre/srv/fast_css_transform.cpp @@ -432,6 +432,11 @@ class Token { for (size_t i = 0; i < text.size(); i++) text[i] = src[i]; } + void set_ascii_text(const char *txt, int sz) { + text.resize(sz); + for (int i = 0; i < sz; i++) text[i] = txt[i]; + } + bool convert_absolute_font_size(std::string &scratch) { if (!unit_at || !text_as_ascii_lowercase(scratch)) return false; frozen::string unit(scratch.data() + unit_at, scratch.size() - unit_at); @@ -440,11 +445,10 @@ class Token { double val = parse_css_number(scratch, unit_at).as_double(); double new_val = convert_font_size(val, lit->second); if (val == new_val) return false; - scratch.reserve(128); scratch.clear(); scratch.resize(128); - int num = std::snprintf(&scratch[0], scratch.capacity(), "%grem", new_val); + char txt[128]; + int num = std::snprintf(txt, sizeof(txt), "%grem", new_val); if (num <= 0) throw std::runtime_error("Failed to format font size"); - scratch.resize(num); - set_text(scratch); + set_ascii_text(txt, num); return true; }