Advertisement
ulfben

std::format_to_n (limit format output)

Nov 30th, 2022 (edited)
836
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.25 KB | None | 0 0
  1. void to_string(float val, std::string &out, size_t maxLength = 5) {
  2.     out.clear();
  3.     std::format_to_n(std::back_inserter(out), maxLength, "{:.1f}"sv, val);
  4. }
  5. void to_string(unsigned int val, std::string &out) {
  6.     out.clear();
  7.     std::format_to_n(std::back_inserter(out), 3, "{}"sv, val);
  8. }
  9.  
  10. void Profiler::Profile(void) {
  11.   _endTime = GetExactTime();
  12.   textBox.clear();
  13.   textBox.append(header);
  14.   std::string ave, min, max, num; //hoisted out of loop for re-se.
  15.   for (const auto &sample : samples) {
  16.     assert_if_invalid(sample);
  17.     const auto sampleTime = sample.accumulator - sample.childrensTime;
  18.     const auto percentTime = (sampleTime / (_endTime - _startTime)) * 100.0f;
  19.     StoreProfileInHistory(sample.name, percentTime);
  20.     const auto [aveTime, minTime, maxTime] = GetProfileFromHistory(sample.name);
  21.     to_string(aveTime, ave);
  22.     to_string(minTime, min);
  23.     to_string(maxTime, max); 
  24.     to_string(sample.count, num);              
  25.     textBox += std::format("{:5} : {:5} : {:5} : {:5} : {}\n"sv,
  26.                             ave,   min,    max,   num, indent_n(sample.parentCount, sample.name));    
  27.   }
  28.   samples.clear();
  29.   _startTime = GetExactTime();
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement