Skip to content

Commit 24caaf9

Browse files
committed
Rewrote format.
- Using a vector of elements instead of passing through va_args. - Add more localization strings.
1 parent 94409d3 commit 24caaf9

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+871
-230
lines changed

include/fn/basicArithm.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ namespace steppable::__internals::arithmetic
164164
* @param[in] steps The number of steps to perform the power operation.
165165
* @return The result of the power operation as a string.
166166
*/
167-
std::string power(std::string _number, const std::string& raiseTo, int steps = 2);
167+
std::string power(const std::string& _number, const std::string& raiseTo, int steps = 2);
168168

169169
/**
170170
* @brief Subtracts one string representation of a number from another string representation of a number.
@@ -187,7 +187,7 @@ namespace steppable::__internals::arithmetic
187187
*
188188
* @return The result of the root operation.
189189
*/
190-
std::string root(const std::string& _number, const std::string& base, size_t decimals = 8);
190+
std::string root(const std::string& _number, const std::string& base, size_t decimals = 8, int steps = 0);
191191

192192
/**
193193
* @brief Converts a root operation into a surd.
@@ -556,7 +556,7 @@ namespace steppable::__internals::arithmetic
556556
catch (std::exception& e)
557557
{
558558
output::error("loop", "Exception occurred in predicate."s);
559-
output::error("loop", "Exception message: %s"s, e.what());
559+
output::error("loop", "Exception message: {0}"s, { e.what() });
560560
}
561561
current = add(current, "1", 0);
562562
result = compare(current, times, 0);

include/format.hpp

Lines changed: 1 addition & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939

4040
#pragma once
4141

42-
#include <cstdarg>
4342
#include <string>
4443
#include <vector>
4544

@@ -49,72 +48,5 @@
4948
*/
5049
namespace steppable::__internals::format
5150
{
52-
// https://stackoverflow.com/a/49812356/14868780
53-
/**
54-
* @brief Formats a string using a variable number of arguments.
55-
*
56-
* This function takes a format string and a variable number of arguments,
57-
* and returns a formatted string. It uses the same format specifiers as
58-
* the standard library's `printf` function.
59-
*
60-
* @tparam CharT The character type of the string.
61-
* @param[in] sFormat The format string.
62-
* @param[in] ... The variable arguments.
63-
* @return The formatted string.
64-
*/
65-
template<typename CharT>
66-
std::basic_string<CharT> vFormat(const std::basic_string<CharT> sFormat, ...)
67-
{
68-
const CharT* const zcFormat = sFormat.c_str();
69-
70-
// Initialize a variable argument array
71-
va_list vaArgs; // NOLINT(cppcoreguidelines-init-variables)
72-
va_start(vaArgs, sFormat);
73-
74-
// Reliably acquire the size from a copy of the variable argument array
75-
// and a functionally reliable call to mock the formatting
76-
va_list vaCopy; // NOLINT(cppcoreguidelines-init-variables)
77-
va_copy(vaCopy, vaArgs);
78-
const int iLen = std::vsnprintf(nullptr, 0, zcFormat, vaCopy);
79-
va_end(vaCopy);
80-
81-
// Return a formatted string without risking memory mismanagement
82-
// and without assuming any compiler or platform specific behavior
83-
std::vector<CharT> formatted(iLen + 1);
84-
(void)std::vsnprintf(formatted.data(), formatted.size(), zcFormat, vaArgs);
85-
va_end(vaArgs);
86-
return std::string(formatted.data(), formatted.size());
87-
}
88-
89-
/**
90-
* @brief Formats a string using a format specifier and variable arguments.
91-
*
92-
* This function takes a format specifier and variable arguments, similar to the printf function,
93-
* and returns a formatted string. The format specifier is a string that may contain placeholders
94-
* for the variable arguments. The function uses the vsnprintf function to perform the formatting.
95-
*
96-
* @tparam CharT The character type of the string.
97-
* @param[in] sFormat The format specifier string.
98-
* @param[in] ... The variable arguments.
99-
* @return The formatted string.
100-
*/
101-
template<typename CharT>
102-
std::basic_string<CharT> vFormat(const CharT* sFormat, ...)
103-
{
104-
const CharT* const zcFormat = sFormat;
105-
106-
va_list vaArgs; // NOLINT(cppcoreguidelines-init-variables)
107-
va_start(vaArgs, sFormat);
108-
109-
va_list vaCopy; // NOLINT(cppcoreguidelines-init-variables)
110-
va_copy(vaCopy, vaArgs);
111-
const int iLen = std::vsnprintf(nullptr, 0, zcFormat, vaCopy); // NOLINT(clang-diagnostic-format-nonliteral)
112-
va_end(vaCopy);
113-
114-
std::vector<CharT> formatted(iLen + 1);
115-
static_cast<void>(std::vsnprintf(
116-
formatted.data(), formatted.size(), zcFormat, vaArgs)); // NOLINT(clang-diagnostic-format-nonliteral)
117-
va_end(vaArgs);
118-
return std::string(formatted.data(), formatted.size());
119-
}
51+
std::string format(const std::string& format, const std::vector<std::string>& args);
12052
} // namespace steppable::__internals::format

include/getString.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,5 @@ namespace steppable::localization
3535
*
3636
* @return The string in its localized form.
3737
*/
38-
std::string getString(const std::string& origin, const std::string& key);
38+
std::string $(const std::string& origin, const std::string& key);
3939
} // namespace steppable::localization

include/output.hpp

Lines changed: 17 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -72,48 +72,22 @@ namespace steppable::output
7272
* It is intended to tell the user that something is going wrong and cannot be handled.
7373
*
7474
* @tparam T The character type of the message.
75-
* @tparam Args The types of the additional arguments.
7675
* @param[in] name The name of the error.
7776
* @param[in] msg The error message.
7877
* @param[in] args Additional arguments for formatting the message.
7978
*/
80-
template<typename T, typename... Args>
81-
void error(const std::string& name, std::basic_string<T> msg, Args... args)
79+
template<typename T>
80+
void error(const std::string& name, std::basic_string<T> msg, const std::vector<std::string>& args = {})
8281
{
83-
auto formattedMsg = format::vFormat(std::string(msg), args...);
84-
std::cerr << colors::red << formats::bold << LARGE_DOT << " Error: " << reset << colors::red;
82+
auto formattedMsg = format::format(msg, args);
83+
std::cerr << colors::red << formats::bold << LARGE_DOT << name << " - ERROR: " << reset << colors::red;
8584
std::cerr << formattedMsg << reset << '\n';
8685

8786
// Write to the log file
8887
auto logger = logging::Logger(name, "steppable.log");
8988
logger.error(formattedMsg);
9089
}
9190

92-
/**
93-
* @brief Prints an error message.
94-
*
95-
* This function prints an error message with the given name and message.
96-
* Additional arguments can be provided to format the message using the
97-
* `std::basic_string<T>` format syntax.
98-
*
99-
* The message is printed in red color, and is also written to the log file.
100-
* It is intended to tell the user that something is going wrong and cannot be handled.
101-
*
102-
* @tparam CharT The character type of the message.
103-
* @param[in] name The name of the error.
104-
* @param[in] msg The error message.
105-
*/
106-
template<typename CharT>
107-
void error(const std::string& name, std::basic_string<CharT> msg)
108-
{
109-
std::cerr << colors::red << formats::bold << LARGE_DOT << " Error: " << reset << colors::red;
110-
std::cerr << msg << reset << '\n';
111-
112-
// Write to the log file
113-
auto logger = logging::Logger(name, "steppable.log");
114-
logger.error(msg);
115-
}
116-
11791
/**
11892
* @brief Prints a warning message.
11993
*
@@ -125,15 +99,16 @@ namespace steppable::output
12599
* It is intended to tell the user that something is going wrong but can be handled.
126100
*
127101
* @tparam T The character type of the message.
128-
* @tparam Args The types of the additional arguments.
129102
* @param[in] msg The error message.
130103
* @param[in] args Additional arguments for formatting the message.
131104
*/
132-
template<typename T, typename... Args>
133-
[[maybe_unused]] void warning(T msg, Args... args)
105+
template<typename T>
106+
[[maybe_unused]] void warning(const std::basic_string<T>& name,
107+
const std::basic_string<T>& msg,
108+
const std::vector<std::string>& args = {})
134109
{
135-
std::cout << colors::yellow << formats::bold << LARGE_DOT << " Warning: " << reset << colors::yellow;
136-
std::cout << format::vFormat(msg, args...) << reset << '\n';
110+
std::cout << colors::yellow << formats::bold << LARGE_DOT << name << " - WARNING: " << reset << colors::yellow;
111+
std::cout << format::format(msg, args) << reset << '\n';
137112
}
138113

139114
/**
@@ -147,14 +122,16 @@ namespace steppable::output
147122
* It is intended to tell the user that something is going right, or to provide some information.
148123
*
149124
* @tparam T The character type of the message.
150-
* @tparam Args The types of the additional arguments.
151125
* @param[in] msg The info message.
152126
* @param[in] args Additional arguments for formatting the message.
153127
*/
154-
template<typename T, typename... Args>
155-
void info(T msg, Args... args)
128+
template<typename T>
129+
void info(const std::basic_string<T>& name,
130+
const std::basic_string<T>& msg,
131+
const std::vector<std::string>& args = {})
156132
{
157-
std::cout << colors::brightGreen << formats::bold << LARGE_DOT << " Info: " << reset << colors::brightGreen;
158-
std::cout << format::vFormat(msg, args...) << reset << '\n';
133+
std::cout << colors::brightGreen << formats::bold << LARGE_DOT << name << " INFO: " << reset
134+
<< colors::brightGreen;
135+
std::cout << format::format(msg, args) << reset << '\n';
159136
}
160137
} // namespace steppable::output

include/testing.hpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,14 @@ using namespace std::literals;
7373

7474
/// @brief This macro ends the main function and prints the number of errors encountered.
7575
// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
76-
#define TEST_END() \
77-
if (errors) \
78-
error("TEST_END", "Not all tests passed. There are %i errors."s, errors); \
79-
else \
80-
info("All tests passed."); \
81-
std::cout << colors::brightBlue << std::setw(80) << std::setfill('=') << reset << '\n'; \
82-
if (errors) \
83-
return 1; \
76+
#define TEST_END() \
77+
if (errors) \
78+
error("TEST_END"s, "Not all tests passed. There are {0} errors."s, { std::to_string(errors) }); \
79+
else \
80+
info("TEST_END"s, "All tests passed."s); \
81+
std::cout << colors::brightBlue << std::setw(80) << std::setfill('=') << reset << '\n'; \
82+
if (errors) \
83+
return 1; \
8484
}
8585

8686
/**

include/util.hpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,6 @@ namespace steppable::__internals::utils
8787
#include <io.h>
8888
#endif
8989

90-
void initLocale();
9190
#ifdef WINDOWS
9291
#include <fcntl.h>
9392
#include <windows.h>
@@ -182,7 +181,7 @@ namespace steppable::__internals::utils
182181
class Utf8CodePage
183182
{
184183
public:
185-
Utf8CodePage() { initLocale(); }
184+
Utf8CodePage() { ; }
186185
};
187186
#endif
188187
} // namespace steppable::__internals::utils

lib/paths.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
"""
2626

2727
from pathlib import Path
28+
from lib.constants import WINDOWS
2829

2930
# section Project paths
3031
PROJECT = "Steppable"
@@ -39,3 +40,13 @@
3940
BIN_DIR = BUILD_DIR / "bin"
4041
LIB_DIR = BUILD_DIR / "lib"
4142
# endsection
43+
44+
# section Configuration paths
45+
if not WINDOWS:
46+
CONFIG_DIR = Path().home() / ".config" / "steppable"
47+
else:
48+
CONFIG_DIR = Path().home() / "AppData" / "Roaming" / "steppable"
49+
50+
DEFAULT_CONFIG_DIR = PROJECT_PATH / "res"
51+
52+
# endsection

pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ dependencies = [
1616
"black >= 24.4.2",
1717
"isort >= 5.13.2",
1818
"setuptools >= 61.0",
19-
"nanobind >= 1.9.2"
19+
"nanobind >= 1.9.2",
20+
"pyyaml >= 6.0.1",
2021
]
2122

2223
[build-system]

res/translations/add.stp_strings

Lines changed: 33 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

res/translations/baseConvert.stp_strings

Lines changed: 37 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)