Skip to content

Commit

Permalink
Fixes formatted float string size.
Browse files Browse the repository at this point in the history
  • Loading branch information
guillaumeblanc committed Jun 20, 2024
1 parent c84baf0 commit cddd75b
Showing 1 changed file with 6 additions and 16 deletions.
22 changes: 6 additions & 16 deletions samples/framework/internal/imgui_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -95,22 +95,12 @@ const float kPanelTitleMarginY = 1.f;
const float kCircleRadius = 32.f;

bool FormatFloat(float _value, char* _string, const char* _string_end) {
// Requires 8 fixed characters count + digits of precision + \0.
static const int precision = 2;
if (!_string || _string_end - _string < 8 + precision + 1) {
return false;
}
std::snprintf(_string, _string_end - _string, "%.2g\n", _value);

// Removes unnecessary '0' digits in the exponent.
char* exponent = strchr(_string, 'e');
char* last_zero = strrchr(_string, '0');
if (exponent && last_zero > exponent) {
memmove(exponent + 2, // After exponent and exponent's sign.
last_zero + 1, // From the first character after the exponent.
strlen(last_zero + 1) + 1); // The remaining characters count.
}
return true;
char buffer[16];
std::snprintf(buffer, sizeof(buffer), "%.03f", _value);
buffer[5] = '\0'; // Ensures as maximum of n characters.
const intptr_t buff_size = _string_end - _string;
const int ret = std::snprintf(_string, buff_size, "%s\n", buffer);
return ret > 0 && ret < buff_size;
}

ImGuiImpl::ImGuiImpl()
Expand Down

0 comments on commit cddd75b

Please sign in to comment.