Skip to content

Commit

Permalink
Add translations (#40)
Browse files Browse the repository at this point in the history
  • Loading branch information
ZCG-coder authored Jul 14, 2024
2 parents 1ddbb46 + d782ce4 commit 0e23c29
Show file tree
Hide file tree
Showing 107 changed files with 3,271 additions and 636 deletions.
4 changes: 4 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
*.jpeg filter=lfs diff=lfs merge=lfs -text
*.svg filter=lfs diff=lfs merge=lfs -text

# Steppable translations
res/translations/*.stp_strings linguist-generated=true linguist-language=Bash
res/translations/**/*.stp_localized linguist-generated=true linguist-language=Bash
5 changes: 3 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"files.associations": {
".cmake-format": "python",
"array": "cpp",
"string_view": "cpp",
"any": "cpp",
Expand Down Expand Up @@ -57,7 +58,8 @@
"streambuf": "cpp",
"typeinfo": "cpp",
"variant": "cpp",
"fstream": "cpp"
"fstream": "cpp",
"__node_handle": "cpp"
},
"copyrightInserter.holder": "NWSOFT",
"copyrightInserter.license": "mit",
Expand All @@ -68,7 +70,6 @@
"editor.cursorBlinking": "smooth",
"editor.cursorSmoothCaretAnimation": "on",
"editor.cursorStyle": "line",
"clangd.path": "/usr/bin/clangd",
"C_Cpp.clang_format_path": "clang-format-17",
"C_Cpp.codeAnalysis.clangTidy.path": "clang-tidy-17",
"files.exclude": {
Expand Down
3 changes: 2 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ COMPONENTS Development Interpreter
REQUIRED)

SET(CMAKE_CXX_STANDARD 20)
SET(CMAKE_C_STANDARD 20)
SET(CMAKE_CXX_EXTENSIONS OFF)
SET(CMAKE_CXX_STANDARD_REQUIRED ON)

Expand Down Expand Up @@ -104,7 +105,7 @@ SET(COMPONENTS
# NEW_COMPONENT: PATCH Do NOT remove the previous comment.

SET(TARGETS ${COMPONENTS} util)
SET(TEST_TARGETS_TEMP util fraction number factors ${COMPONENTS})
SET(TEST_TARGETS_TEMP util fraction number factors format ${COMPONENTS})

FOREACH(TEST_TARGET IN LISTS TEST_TARGETS_TEMP)
SET(TARGET_NAME "test")
Expand Down
38 changes: 19 additions & 19 deletions include/argParse.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@

#include <map>
#include <regex>
#include <string_view>
#include <string>
#include <unordered_map>
#include <vector>

Expand All @@ -50,15 +50,15 @@
namespace steppable::__internals::utils
{
/// @brief This is the type of the positional arguments. It is equivalent to a vector of string_views.
using PosArgs = std::vector<std::string_view>;
using PosArgs = std::vector<std::string>;

/// @brief This is the correct format of a keyword argument.
// language=RegExp
[[maybe_unused]] const std::regex KEYWORD_ARG_REGEX(R"(^-([a-zA-Z]*):(-?[0-9]+)$)");
[[maybe_unused]] const std::regex KEYWORD_ARG_REGEX(R"(^-([a-zA-Z]*):(-?[0-9]+)$)"); // NOLINT(cert-err58-cpp)

/// @brief This is the correct format of a switch.
// language=RegExp
[[maybe_unused]] const std::regex SWITCH_REGEX(R"(^([-+])([a-zA-Z]*)$)");
[[maybe_unused]] const std::regex SWITCH_REGEX(R"(^([-+])([a-zA-Z]*)$)"); // NOLINT(cert-err58-cpp)

/**
* @class ProgramArgs
Expand All @@ -82,33 +82,33 @@ namespace steppable::__internals::utils
/** @brief This map is used to store the information of all switches specified. Keys are switch names, and
* values are whether the switch is enabled.
*/
std::unordered_map<std::string_view, bool> switches;
std::unordered_map<std::string, bool> switches;
/// @brief This map is used to store the descriptions of all switches specified.
std::map<std::string_view, std::string_view> switchDescriptions;
std::map<std::string, std::string> switchDescriptions;

/// @brief This vector is used to store the values of all positional arguments specified.
std::vector<std::string_view> posArgs; // Names are used for error messages only.
std::vector<std::string> posArgs; // Names are used for error messages only.
/// @brief This map is used to store the descriptions of all positional arguments specified.
std::map<char, std::string_view> posArgDescriptions;
std::map<char, std::string> posArgDescriptions;
/// @brief This map stores whether the positional arguments are required to be numbers.
std::vector<bool> posArgIsNumber;

/**
* @brief This map is used to store the values of all keyword arguments specified. Keys are keyword argument
* names and values are the values of the keyword arguments.
*/
std::unordered_map<std::string_view, int> keywordArgs;
std::unordered_map<std::string, int> keywordArgs;
/// @brief This map is used to store the descriptions of all keyword arguments specified.
std::map<std::string_view, std::string_view> keywordArgDescriptions;
std::map<std::string, std::string> keywordArgDescriptions;

/// @brief This stores the number of arguments passed to the program.
int argc;

/// @brief This stores the arguments passed to the program.
std::vector<std::string_view> argv;
std::vector<std::string> argv;

/// @brief This stores the name of the program.
std::string_view programName;
std::string programName;

public:
/**
Expand All @@ -133,7 +133,7 @@ namespace steppable::__internals::utils
* @param[in] defaultValue The default value of the switch. True = enabled, False = disabled.
* @param[in] description The description of the switch.
*/
void addSwitch(const std::string_view& name, bool defaultValue, const std::string_view& description = "");
void addSwitch(const std::string& name, bool defaultValue, const std::string& description = "");

/**
* @brief This function is used to add a positional argument to the class.
Expand All @@ -145,7 +145,7 @@ namespace steppable::__internals::utils
* @note the command-line arguments.
*/
void addPosArg(char name,
const std::string_view& description = "",
const std::string& description = "",
bool requiresNumber = true); // Positional arguments are always required and ordered

/**
Expand All @@ -154,7 +154,7 @@ namespace steppable::__internals::utils
* @param[in] defaultValue The default value of the keyword argument. The value is stored as an integer.
* @param[in] description The description of the keyword argument.
*/
void addKeywordArg(const std::string_view& name, int defaultValue, std::string_view description = "");
void addKeywordArg(const std::string& name, int defaultValue, const std::string& description = "");

/**
* @brief This function is used to get the value of a positional argument.
Expand All @@ -164,7 +164,7 @@ namespace steppable::__internals::utils
* @note If the positional argument is not specified, the function will print an error message and exit the
* program.
*/
[[nodiscard]] std::string_view getPosArg(size_t index) const;
[[nodiscard]] std::string getPosArg(size_t index) const;

/**
* @brief This function is used to get the value of a keyword argument.
Expand All @@ -174,7 +174,7 @@ namespace steppable::__internals::utils
* @note If the keyword argument is not specified, the function will print an error message and exit the
* program.
*/
int getKeywordArgument(const std::string_view& name);
int getKeywordArgument(const std::string& name);

/**
* @brief This function is used to get the value of a switch.
Expand All @@ -183,7 +183,7 @@ namespace steppable::__internals::utils
*
* @note If the switch is not specified, the function will print an error message and exit the program.
*/
bool getSwitch(const std::string_view& name);
bool getSwitch(const std::string& name);

/**
* @brief This function is used to print the possible command-line arguments. Usually called when the user
Expand All @@ -193,6 +193,6 @@ namespace steppable::__internals::utils
*
* @note The function will print the usage of the program and exit the program.
*/
void printUsage(const std::string_view& reason = "") const;
void printUsage(const std::string& reason = "") const;
};
} // namespace steppable::__internals::utils
32 changes: 14 additions & 18 deletions include/fn/basicArithm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
#include "output.hpp"

#include <string>
#include <string_view>

using namespace std::literals;

Expand All @@ -65,7 +64,7 @@ namespace steppable::__internals::arithmetic
* @param[in] steps The number of steps to perform the calculation.
* @return The absolute value of the number as a string.
*/
std::string abs(const std::string_view& _number, int steps);
std::string abs(const std::string& _number, int steps);

/**
* @brief Adds two string representations of numbers, and performs with the column method.
Expand All @@ -78,8 +77,8 @@ namespace steppable::__internals::arithmetic
* @param[in] properlyFormat Flag indicating whether to properly format the output. Default true.
* @return The sum of the two numbers as a string.
*/
std::string add(const std::string_view& a,
const std::string_view& b,
std::string add(const std::string& a,
const std::string& b,
int steps = 2,
bool negative = false,
bool properlyFormat = true);
Expand All @@ -95,7 +94,7 @@ namespace steppable::__internals::arithmetic
* "1" if a is greater than b,
* "0" if a is less than b.
*/
std::string compare(const std::string_view& _a, const std::string_view& _b, int steps = 2);
std::string compare(const std::string& _a, const std::string& _b, int steps = 2);

/**
* @brief Converts a string representation of a number from any base to decimal.
Expand All @@ -105,7 +104,7 @@ namespace steppable::__internals::arithmetic
* @param[in] steps The number of steps to perform the conversion.
* @return The converted number as a string.
*/
std::string decimalConvert(const std::string_view& _inputString, const std::string_view& baseString, int steps = 2);
std::string decimalConvert(const std::string& _inputString, const std::string& baseString, int steps = 2);

/**
* @brief Converts a string representation of a number from decimal to another one.
Expand All @@ -115,7 +114,7 @@ namespace steppable::__internals::arithmetic
* @param[in] steps The number of steps to perform the conversion.
* @return The converted number as a string.
*/
std::string baseConvert(const std::string_view& _number, const std::string_view& baseStr, int steps = 2);
std::string baseConvert(const std::string& _number, const std::string& baseStr, int steps = 2);

/**
* @brief Divides a string representation of a number by another string representation of a number.
Expand All @@ -126,10 +125,7 @@ namespace steppable::__internals::arithmetic
* @param[in] decimals The number of decimal places in the result.
* @return The quotient of the division as a string.
*/
std::string divide(const std::string_view& number,
const std::string_view& divisor,
int steps = 2,
int decimals = 5);
std::string divide(const std::string& number, const std::string& divisor, int steps = 2, int decimals = 5);

/**
* Calculates the quotient and remainder of dividing the current remainder by the divisor.
Expand All @@ -148,7 +144,7 @@ namespace steppable::__internals::arithmetic
*
* @return The greatest common divisor of the two numbers.
*/
std::string getGCD(const std::string_view& _a, const std::string_view& _b);
std::string getGCD(const std::string& _a, const std::string& _b);

/**
* @brief Multiplies two string representations of numbers.
Expand All @@ -158,7 +154,7 @@ namespace steppable::__internals::arithmetic
* @param[in] steps The number of steps to perform the multiplication.
* @return The product of the two numbers as a string.
*/
std::string multiply(const std::string_view& a, const std::string_view& b, int steps = 2);
std::string multiply(const std::string& a, const std::string& b, int steps = 2);

/**
* @brief Raises a string representation of a number to a power.
Expand All @@ -168,7 +164,7 @@ namespace steppable::__internals::arithmetic
* @param[in] steps The number of steps to perform the power operation.
* @return The result of the power operation as a string.
*/
std::string power(std::string_view _number, const std::string_view& raiseTo, int steps = 2);
std::string power(const std::string& _number, const std::string& raiseTo, int steps = 2);

/**
* @brief Subtracts one string representation of a number from another string representation of a number.
Expand All @@ -179,7 +175,7 @@ namespace steppable::__internals::arithmetic
* @param[in] noMinus Flag indicating whether to display a minus sign or not.
* @return The difference between the two numbers as a string.
*/
std::string subtract(const std::string_view& a, const std::string_view& b, int steps = 2, bool noMinus = false);
std::string subtract(const std::string& a, const std::string& b, int steps = 2, bool noMinus = false);

/**
* @brief Takes the n-th root of a numer.
Expand All @@ -191,7 +187,7 @@ namespace steppable::__internals::arithmetic
*
* @return The result of the root operation.
*/
std::string root(const std::string& _number, const std::string& base, size_t decimals = 8);
std::string root(const std::string& _number, const std::string& base, size_t decimals = 8, int steps = 0);

/**
* @brief Converts a root operation into a surd.
Expand Down Expand Up @@ -540,7 +536,7 @@ namespace steppable::__internals::arithmetic
* @param[in] predicate The predicate function to execute.
*/
template<typename Pred>
void loop(const std::string_view& times, Pred predicate)
void loop(const std::string& times, Pred predicate)
{
// We're done already!
if (times == "0")
Expand All @@ -560,7 +556,7 @@ namespace steppable::__internals::arithmetic
catch (std::exception& e)
{
output::error("loop", "Exception occurred in predicate."s);
output::error("loop", "Exception message: %s"s, e.what());
output::error("loop", "Exception message: {0}"s, { e.what() });
}
current = add(current, "1", 0);
result = compare(current, times, 0);
Expand Down
70 changes: 1 addition & 69 deletions include/format.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@

#pragma once

#include <cstdarg>
#include <string>
#include <vector>

Expand All @@ -49,72 +48,5 @@
*/
namespace steppable::__internals::format
{
// https://stackoverflow.com/a/49812356/14868780
/**
* @brief Formats a string using a variable number of arguments.
*
* This function takes a format string and a variable number of arguments,
* and returns a formatted string. It uses the same format specifiers as
* the standard library's `printf` function.
*
* @tparam CharT The character type of the string.
* @param[in] sFormat The format string.
* @param[in] ... The variable arguments.
* @return The formatted string.
*/
template<typename CharT>
std::basic_string<CharT> vFormat(const std::basic_string<CharT> sFormat, ...)
{
const CharT* const zcFormat = sFormat.c_str();

// Initialize a variable argument array
va_list vaArgs; // NOLINT(cppcoreguidelines-init-variables)
va_start(vaArgs, sFormat);

// Reliably acquire the size from a copy of the variable argument array
// and a functionally reliable call to mock the formatting
va_list vaCopy; // NOLINT(cppcoreguidelines-init-variables)
va_copy(vaCopy, vaArgs);
const int iLen = std::vsnprintf(nullptr, 0, zcFormat, vaCopy);
va_end(vaCopy);

// Return a formatted string without risking memory mismanagement
// and without assuming any compiler or platform specific behavior
std::vector<CharT> formatted(iLen + 1);
(void)std::vsnprintf(formatted.data(), formatted.size(), zcFormat, vaArgs);
va_end(vaArgs);
return std::string(formatted.data(), formatted.size());
}

/**
* @brief Formats a string using a format specifier and variable arguments.
*
* This function takes a format specifier and variable arguments, similar to the printf function,
* and returns a formatted string. The format specifier is a string that may contain placeholders
* for the variable arguments. The function uses the vsnprintf function to perform the formatting.
*
* @tparam CharT The character type of the string.
* @param[in] sFormat The format specifier string.
* @param[in] ... The variable arguments.
* @return The formatted string.
*/
template<typename CharT>
std::basic_string<CharT> vFormat(const CharT* sFormat, ...)
{
const CharT* const zcFormat = sFormat;

va_list vaArgs; // NOLINT(cppcoreguidelines-init-variables)
va_start(vaArgs, sFormat);

va_list vaCopy; // NOLINT(cppcoreguidelines-init-variables)
va_copy(vaCopy, vaArgs);
const int iLen = std::vsnprintf(nullptr, 0, zcFormat, vaCopy); // NOLINT(clang-diagnostic-format-nonliteral)
va_end(vaCopy);

std::vector<CharT> formatted(iLen + 1);
static_cast<void>(std::vsnprintf(
formatted.data(), formatted.size(), zcFormat, vaArgs)); // NOLINT(clang-diagnostic-format-nonliteral)
va_end(vaArgs);
return std::string(formatted.data(), formatted.size());
}
std::string format(const std::string& format, const std::vector<std::string>& args);
} // namespace steppable::__internals::format
Loading

0 comments on commit 0e23c29

Please sign in to comment.