mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-10-26 11:23:08 +01:00
The name of a node or "(no name)" was written out in twenty places and four local functions, a lower-case copy of a text in four files, the test for a number, the file name of a template made comparable and the heading of a direction in three and two, the green main button of a panel in five and the key=value parameters of a self-test command in five. Each of them is one helper now: name_of, tall_button_height and main_button with the panel helpers, numeric with format, normalized with the include helpers, heading_of with the plan geometry and selftest_values with the tools; lower gives way to ToLower.
35 lines
916 B
C++
35 lines
916 B
C++
/*
|
|
This Source Code Form is subject to the
|
|
terms of the Mozilla Public License, v.
|
|
2.0. If a copy of the MPL was not
|
|
distributed with this file, You can
|
|
obtain one at
|
|
http://mozilla.org/MPL/2.0/.
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
#include <cstdio>
|
|
#include <cstdlib>
|
|
#include <string>
|
|
|
|
// printf-style formatting into a string, for the messages and the data lines of the editor
|
|
template <typename... Args>
|
|
std::string format(char const *Format, Args... Arguments)
|
|
{
|
|
auto const length{std::snprintf(nullptr, 0, Format, Arguments...)};
|
|
if (length <= 0)
|
|
return {};
|
|
std::string text(static_cast<std::size_t>(length) + 1, '\0');
|
|
std::snprintf(text.data(), text.size(), Format, Arguments...);
|
|
text.resize(static_cast<std::size_t>(length));
|
|
return text;
|
|
}
|
|
|
|
inline bool numeric(std::string const &Text, double &Value)
|
|
{
|
|
char *end{nullptr};
|
|
Value = std::strtod(Text.c_str(), &end);
|
|
return false == Text.empty() && *end == '\0';
|
|
}
|