1
0
mirror of synced 2024-11-24 07:40:17 +01:00

sys: Replace printf formatting with libfmt

This commit is contained in:
WerWolv 2021-03-03 19:58:22 +01:00
parent 4e86d874a7
commit 188723e888
21 changed files with 108 additions and 107 deletions

3
.gitmodules vendored
View File

@ -8,3 +8,6 @@
[submodule "external/xdgpp"]
path = external/xdgpp
url = https://git.sr.ht/~danyspin97/xdgpp
[submodule "external/fmt"]
path = external/fmt
url = https://github.com/fmtlib/fmt

View File

@ -2,6 +2,7 @@
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
<mapping directory="$PROJECT_DIR$/external/fmt" vcs="Git" />
<mapping directory="$PROJECT_DIR$/external/nativefiledialog" vcs="Git" />
<mapping directory="$PROJECT_DIR$/external/xdgpp" vcs="Git" />
<mapping directory="$PROJECT_DIR$/external/yara/yara" vcs="Git" />

1
external/fmt vendored Submodule

@ -0,0 +1 @@
Subproject commit d8b92543017053a2b2c5ca901ea310e20690b137

View File

@ -22,9 +22,9 @@ namespace hex::plugin::builtin {
if (result.has_value())
return hex::format("#%s = %Lf", input.data(), result.value());
return hex::format("#{0} = %{1}", input.data(), result.value());
else
return hex::format("#%s = ???", input.data());
return hex::format("#{0} = ???", input.data());
});
hex::ContentRegistry::CommandPaletteCommands::add(

View File

@ -32,71 +32,71 @@ namespace hex::plugin::builtin {
});
hex::ContentRegistry::DataInspector::add("hex.builtin.inspector.u8", sizeof(u8), [](auto buffer, auto endian, auto style) {
auto format = (style == Style::Decimal) ? "%u" : ((style == Style::Hexadecimal) ? "0x%X" : "0o%o");
auto format = (style == Style::Decimal) ? "{0:d}" : ((style == Style::Hexadecimal) ? "0x{0:X}" : "{0:#o}");
auto value = hex::format(format, *reinterpret_cast<u8*>(buffer.data()));
return [value] { ImGui::TextUnformatted(value.c_str()); return value; };
});
hex::ContentRegistry::DataInspector::add("hex.builtin.inspector.s8", sizeof(s8), [](auto buffer, auto endian, auto style) {
auto format = (style == Style::Decimal) ? "%d" : ((style == Style::Hexadecimal) ? "0x%X" : "0o%o");
auto format = (style == Style::Decimal) ? "{0:d}" : ((style == Style::Hexadecimal) ? "0x{0:X}" : "{0:#o}");
auto value = hex::format(format, *reinterpret_cast<s8*>(buffer.data()));
return [value] { ImGui::TextUnformatted(value.c_str()); return value; };
});
hex::ContentRegistry::DataInspector::add("hex.builtin.inspector.u16", sizeof(u16), [](auto buffer, auto endian, auto style) {
auto format = (style == Style::Decimal) ? "%u" : ((style == Style::Hexadecimal) ? "0x%X" : "0o%o");
auto format = (style == Style::Decimal) ? "{0:d}" : ((style == Style::Hexadecimal) ? "0x{0:X}" : "{0:#o}");
auto value = hex::format(format, hex::changeEndianess(*reinterpret_cast<u16*>(buffer.data()), endian));
return [value] { ImGui::TextUnformatted(value.c_str()); return value; };
});
hex::ContentRegistry::DataInspector::add("hex.builtin.inspector.s16", sizeof(s16), [](auto buffer, auto endian, auto style) {
auto format = (style == Style::Decimal) ? "%d" : ((style == Style::Hexadecimal) ? "0x%X" : "0o%o");
auto format = (style == Style::Decimal) ? "{0:d}" : ((style == Style::Hexadecimal) ? "0x{0:X}" : "{0:#o}");
auto value = hex::format(format, hex::changeEndianess(*reinterpret_cast<s16*>(buffer.data()), endian));
return [value] { ImGui::TextUnformatted(value.c_str()); return value; };
});
hex::ContentRegistry::DataInspector::add("hex.builtin.inspector.u32", sizeof(u32), [](auto buffer, auto endian, auto style) {
auto format = (style == Style::Decimal) ? "%u" : ((style == Style::Hexadecimal) ? "0x%X" : "0o%o");
auto format = (style == Style::Decimal) ? "{0:d}" : ((style == Style::Hexadecimal) ? "0x{0:X}" : "{0:#o}");
auto value = hex::format(format, hex::changeEndianess(*reinterpret_cast<u32*>(buffer.data()), endian));
return [value] { ImGui::TextUnformatted(value.c_str()); return value; };
});
hex::ContentRegistry::DataInspector::add("hex.builtin.inspector.s32", sizeof(s32), [](auto buffer, auto endian, auto style) {
auto format = (style == Style::Decimal) ? "%d" : ((style == Style::Hexadecimal) ? "0x%X" : "0o%o");
auto format = (style == Style::Decimal) ? "{0:d}" : ((style == Style::Hexadecimal) ? "0x{0:X}" : "{0:#o}");
auto value = hex::format(format, hex::changeEndianess(*reinterpret_cast<s32*>(buffer.data()), endian));
return [value] { ImGui::TextUnformatted(value.c_str()); return value; };
});
hex::ContentRegistry::DataInspector::add("hex.builtin.inspector.u64", sizeof(u64), [](auto buffer, auto endian, auto style) {
auto format = (style == Style::Decimal) ? "%llu" : ((style == Style::Hexadecimal) ? "0x%llX" : "0o%llo");
auto format = (style == Style::Decimal) ? "{0:d}" : ((style == Style::Hexadecimal) ? "0x{0:X}" : "{0:#o}");
auto value = hex::format(format, hex::changeEndianess(*reinterpret_cast<u64*>(buffer.data()), endian));
return [value] { ImGui::TextUnformatted(value.c_str()); return value; };
});
hex::ContentRegistry::DataInspector::add("hex.builtin.inspector.s64", sizeof(s64), [](auto buffer, auto endian, auto style) {
auto format = (style == Style::Decimal) ? "%lld" : ((style == Style::Hexadecimal) ? "0x%llX" : "0o%llo");
auto format = (style == Style::Decimal) ? "{0:d}" : ((style == Style::Hexadecimal) ? "0x{0:X}" : "{0:#o}");
auto value = hex::format(format, hex::changeEndianess(*reinterpret_cast<s64*>(buffer.data()), endian));
return [value] { ImGui::TextUnformatted(value.c_str()); return value; };
});
hex::ContentRegistry::DataInspector::add("hex.builtin.inspector.float", sizeof(float), [](auto buffer, auto endian, auto style) {
auto value = hex::format("%e", hex::changeEndianess(*reinterpret_cast<float*>(buffer.data()), endian));
auto value = hex::format("{0:E}", hex::changeEndianess(*reinterpret_cast<float*>(buffer.data()), endian));
return [value] { ImGui::TextUnformatted(value.c_str()); return value; };
});
hex::ContentRegistry::DataInspector::add("hex.builtin.inspector.double", sizeof(double), [](auto buffer, auto endian, auto style) {
auto value = hex::format("%e", hex::changeEndianess(*reinterpret_cast<double*>(buffer.data()), endian));
auto value = hex::format("{0:E}", hex::changeEndianess(*reinterpret_cast<float*>(buffer.data()), endian));
return [value] { ImGui::TextUnformatted(value.c_str()); return value; };
});
hex::ContentRegistry::DataInspector::add("hex.builtin.inspector.ascii", sizeof(char8_t), [](auto buffer, auto endian, auto style) {
auto value = hex::format("'%s'", makePrintable(*reinterpret_cast<char8_t*>(buffer.data())).c_str());
auto value = hex::format("'{0}'", makePrintable(*reinterpret_cast<char8_t*>(buffer.data())).c_str());
return [value] { ImGui::TextUnformatted(value.c_str()); return value; };
});
hex::ContentRegistry::DataInspector::add("hex.builtin.inspector.wide", sizeof(char16_t), [](auto buffer, auto endian, auto style) {
auto c = *reinterpret_cast<char16_t*>(buffer.data());
auto value = hex::format("'%lc'", c == 0 ? '\x01' : hex::changeEndianess(c, endian));
auto value = hex::format("'{0}'", c == 0 ? '\x01' : char16_t(hex::changeEndianess(c, endian)));
return [value] { ImGui::TextUnformatted(value.c_str()); return value; };
});
@ -109,7 +109,7 @@ namespace hex::plugin::builtin {
u8 codepointSize = ImTextCharFromUtf8(&codepoint, utf8Buffer, utf8Buffer + 4);
std::memcpy(codepointString, &codepoint, std::min(codepointSize, u8(4)));
auto value = hex::format("'%s' (U+%04lx)", codepoint == 0xFFFD ? "Invalid" :
auto value = hex::format("'{0}' (U+0x{1:04X})", codepoint == 0xFFFD ? "Invalid" :
codepoint < 0xFF ? makePrintable(codepoint).c_str() :
codepointString,
codepoint);
@ -121,11 +121,10 @@ namespace hex::plugin::builtin {
hex::ContentRegistry::DataInspector::add("hex.builtin.inspector.time32", sizeof(__time32_t), [](auto buffer, auto endian, auto style) {
auto endianAdjustedTime = hex::changeEndianess(*reinterpret_cast<__time32_t*>(buffer.data()), endian);
std::tm * ptm = _localtime32(&endianAdjustedTime);
char timeBuffer[32];
struct tm *ptm = _localtime32(&endianAdjustedTime);
std::string value;
if (ptm != nullptr && std::strftime(timeBuffer, 32, "%a, %d.%m.%Y %H:%M:%S", ptm))
value = timeBuffer;
if (ptm != nullptr)
value = hex::format("{0:%a, %d.%m.%Y %H:%M:%S}", *ptm);
else
value = "Invalid";
@ -134,11 +133,10 @@ namespace hex::plugin::builtin {
hex::ContentRegistry::DataInspector::add("hex.builtin.inspector.time64", sizeof(__time64_t), [](auto buffer, auto endian, auto style) {
auto endianAdjustedTime = hex::changeEndianess(*reinterpret_cast<__time64_t*>(buffer.data()), endian);
std::tm * ptm = _localtime64(&endianAdjustedTime);
char timeBuffer[64];
struct tm *ptm = _localtime64(&endianAdjustedTime);
std::string value;
if (ptm != nullptr && std::strftime(timeBuffer, 64, "%a, %d.%m.%Y %H:%M:%S", ptm))
value = timeBuffer;
if (ptm != nullptr)
value = hex::format("{0:%a, %d.%m.%Y %H:%M:%S}", *ptm);
else
value = "Invalid";
@ -149,11 +147,10 @@ namespace hex::plugin::builtin {
hex::ContentRegistry::DataInspector::add("hex.builtin.inspector.time", sizeof(time_t), [](auto buffer, auto endian, auto style) {
auto endianAdjustedTime = hex::changeEndianess(*reinterpret_cast<time_t*>(buffer.data()), endian);
std::tm * ptm = localtime(&endianAdjustedTime);
char timeBuffer[64];
struct tm *ptm = localtime(&endianAdjustedTime);
std::string value;
if (ptm != nullptr && std::strftime(timeBuffer, 64, "%a, %d.%m.%Y %H:%M:%S", ptm))
value = timeBuffer;
if (ptm != nullptr)
value = hex::format("{0:%a, %d.%m.%Y %H:%M:%S}", *ptm);
else
value = "Invalid";
@ -165,7 +162,7 @@ namespace hex::plugin::builtin {
hex::ContentRegistry::DataInspector::add("hex.builtin.inspector.guid", sizeof(GUID), [](auto buffer, auto endian, auto style) {
GUID guid;
std::memcpy(&guid, buffer.data(), sizeof(GUID));
auto value = hex::format("%s{%08lX-%04hX-%04hX-%02hhX%02hhX-%02hhX%02hhX%02hhX%02hhX%02hhX%02hhX}",
auto value = hex::format("{}{{{:08X}-{:04X}-{:04X}-{:02X}{:02X}-{:02X}{:02X}{:02X}{:02X}{:02X}{:02X}}}",
(hex::changeEndianess(guid.data3, endian) >> 12) <= 5 && ((guid.data4[0] >> 4) >= 8 || (guid.data4[0] >> 4) == 0) ? "" : "Invalid ",
hex::changeEndianess(guid.data1, endian),
hex::changeEndianess(guid.data2, endian),
@ -179,7 +176,7 @@ namespace hex::plugin::builtin {
hex::ContentRegistry::DataInspector::add("hex.builtin.inspector.rgba8", sizeof(u32), [](auto buffer, auto endian, auto style) {
ImColor value(hex::changeEndianess(*reinterpret_cast<u32*>(buffer.data()), endian));
auto stringValue = hex::format("(0x%02X, 0x%02X, 0x%02X, 0x%02X)", value.Value.x, value.Value.y, value.Value.z, value.Value.w);
auto stringValue = hex::format("(0x{:02X}, 0x{:02X}, 0x{:02X}, 0x{:02X})", u8(0xFF * (value.Value.x)), u8(0xFF * (value.Value.y)), u8(0xFF * (value.Value.z)), u8(0xFF * (value.Value.w)));
return [value, stringValue] {
ImGui::ColorButton("##inspectorColor", value,

View File

@ -105,7 +105,7 @@ namespace hex::plugin::builtin {
auto message = AS_TYPE(ASTNodeStringLiteral, params[1])->getString();
if (LITERAL_COMPARE(condition, condition == 0))
ctx.getConsole().abortEvaluation(hex::format("assert failed \"%s\"", message.data()));
ctx.getConsole().abortEvaluation(hex::format("assert failed \"{0}\"", message.data()));
return nullptr;
});
@ -116,7 +116,7 @@ namespace hex::plugin::builtin {
auto message = AS_TYPE(ASTNodeStringLiteral, params[1])->getString();
if (LITERAL_COMPARE(condition, condition == 0))
ctx.getConsole().log(LogConsole::Level::Warning, hex::format("assert failed \"%s\"", message.data()));
ctx.getConsole().log(LogConsole::Level::Warning, hex::format("assert failed \"{0}\"", message.data()));
return nullptr;
});

View File

@ -311,8 +311,8 @@ namespace hex::plugin::builtin {
}
auto base10String = std::to_string(number);
auto base16String = hex::format("%X", number);
auto base8String = hex::format("%o", number);
auto base16String = hex::format("0x{0:X}", number);
auto base8String = hex::format("{0:#o}", number);
auto base2String = hex::toBinaryString(number);
std::strncpy(buffer[0], base10String.c_str(), sizeof(buffer[0]));

View File

@ -60,10 +60,10 @@ namespace hex::plugin::builtin {
{ "hex.common.set", "Set" },
{ "hex.view.bookmarks.name", "Bookmarks" },
{ "hex.view.bookmarks.default_title", "Bookmark [0x%lX - 0x%lX]" },
{ "hex.view.bookmarks.default_title", "Bookmark [0x{0:X} - 0x{1:X}]" },
{ "hex.view.bookmarks.no_bookmarks", "No bookmarks created yet. Add one with Edit -> Add Bookmark" },
{ "hex.view.bookmarks.title.info", "Information" },
{ "hex.view.bookmarks.address", "0x%08lx : 0x%08lx (%lu bytes)" },
{ "hex.view.bookmarks.address", "0x{0:X} : 0x{1:X} ({2} bytes)" },
{ "hex.view.bookmarks.button.jump", "Jump to" },
{ "hex.view.bookmarks.button.remove", "Remove" },
{ "hex.view.bookmarks.header.name", "Name" },
@ -139,7 +139,7 @@ namespace hex::plugin::builtin {
{ "hex.view.hexeditor.save_data", "Save Data" },
{ "hex.view.hexeditor.open_base64", "Open Base64 File" },
{ "hex.view.hexeditor.load_enconding_file", "Load custom encoding File" },
{ "hex.view.hexeditor.page", "Page %d / %d" },
{ "hex.view.hexeditor.page", "Page {0} / {1}" },
{ "hex.view.hexeditor.save_as", "Save As" },
{ "hex.view.hexeditor.save_changes.title", "Save Changes" },
{ "hex.view.hexeditor.save_changes.desc", "You have unsaved changes made to your Project.\nAre you sure you want to exit?" },
@ -207,7 +207,7 @@ namespace hex::plugin::builtin {
{ "hex.view.information.distribution", "Byte distribution" },
{ "hex.view.information.entropy", "Entropy" },
{ "hex.view.information.block_size", "Block size" },
{ "hex.view.information.block_size.desc", "%lu blocks of %lu bytes" },
{ "hex.view.information.block_size.desc", "{0} blocks of {1} bytes" },
{ "hex.view.information.file_entropy", "File entropy" },
{ "hex.view.information.highest_entropy", "Highest entropy block" },
{ "hex.view.information.encrypted", "This data is most likely encrypted or compressed!" },
@ -267,7 +267,7 @@ namespace hex::plugin::builtin {
{ "hex.builtin.command.calc.desc", "Calculator" },
{ "hex.builtin.command.web.desc", "Website lookup" },
{ "hex.builtin.command.web.result", "Navigate to '%s'"},
{ "hex.builtin.command.web.result", "Navigate to '{0}'"},
{ "hex.builtin.inspector.binary", "Binary (8 bit)" },
{ "hex.builtin.inspector.u8", "uint8_t" },
@ -442,7 +442,7 @@ namespace hex::plugin::builtin {
{ "hex.builtin.tools.format.scientific", "Scientific" },
{ "hex.builtin.tools.format.engineering", "Engineering" },
{ "hex.builtin.tools.format.programmer", "Programmer" },
{ "hex.builtin.tools.error", "Last error: '%s'" },
{ "hex.builtin.tools.error", "Last error: '{0}'" },
{ "hex.builtin.tools.history", "History" },
{ "hex.builtin.tools.name", "Name" },
{ "hex.builtin.tools.value", "Value" },

View File

@ -6,6 +6,7 @@ set(CMAKE_CXX_STANDARD 20)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/../../external/ImGui ${CMAKE_CURRENT_BINARY_DIR}/external/ImGui)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/../../external/nlohmann_json ${CMAKE_CURRENT_BINARY_DIR}/external/nlohmann_json)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/../../external/nativefiledialog ${CMAKE_CURRENT_BINARY_DIR}/external/nativefiledialog)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/../../external/fmt ${CMAKE_CURRENT_BINARY_DIR}/external/fmt)
set(XDGPP_INCLUDE_DIRS "${CMAKE_CURRENT_SOURCE_DIR}/../../external/xdgpp")
if (WIN32)
@ -59,10 +60,10 @@ target_include_directories(libimhex PUBLIC include ${MBEDTLS_INCLUDE_DIR} ${XDGP
target_link_directories(libimhex PUBLIC ${MBEDTLS_LIBRARY_DIR})
if (WIN32)
target_link_libraries(libimhex PUBLIC imgui nlohmann_json libmbedcrypto.a nfd)
target_link_libraries(libimhex PUBLIC imgui nlohmann_json libmbedcrypto.a nfd fmt)
elseif (APPLE)
find_library(FOUNDATION NAMES Foundation)
target_link_libraries(libimhex PUBLIC imgui nlohmann_json mbedcrypto ${FOUNDATION} nfd)
target_link_libraries(libimhex PUBLIC imgui nlohmann_json mbedcrypto ${FOUNDATION} nfd fmt)
else()
target_link_libraries(libimhex PUBLIC imgui nlohmann_json mbedcrypto nfd)
target_link_libraries(libimhex PUBLIC imgui nlohmann_json mbedcrypto nfd fmt)
endif ()

View File

@ -59,7 +59,7 @@ namespace hex::dp {
auto attribute = this->getConnectedInputAttribute(index);
if (attribute == nullptr)
throwNodeError(hex::format("Nothing connected to input '%s'", static_cast<const char*>(LangEntry(this->m_attributes[index].getUnlocalizedName()))));
throwNodeError(hex::format("Nothing connected to input '{0}'", static_cast<const char*>(LangEntry(this->m_attributes[index].getUnlocalizedName()))));
if (attribute->getType() != Attribute::Type::Buffer)
throwNodeError("Tried to read buffer from non-buffer attribute");
@ -78,7 +78,7 @@ namespace hex::dp {
auto attribute = this->getConnectedInputAttribute(index);
if (attribute == nullptr)
throwNodeError(hex::format("Nothing connected to input '%s'", static_cast<const char*>(LangEntry(this->m_attributes[index].getUnlocalizedName()))));
throwNodeError(hex::format("Nothing connected to input '{0}'", static_cast<const char*>(LangEntry(this->m_attributes[index].getUnlocalizedName()))));
if (attribute->getType() != Attribute::Type::Integer)
throwNodeError("Tried to read integer from non-integer attribute");
@ -100,7 +100,7 @@ namespace hex::dp {
auto attribute = this->getConnectedInputAttribute(index);
if (attribute == nullptr)
throwNodeError(hex::format("Nothing connected to input '%s'", static_cast<const char*>(LangEntry(this->m_attributes[index].getUnlocalizedName()))));
throwNodeError(hex::format("Nothing connected to input '{0}'", static_cast<const char*>(LangEntry(this->m_attributes[index].getUnlocalizedName()))));
if (attribute->getType() != Attribute::Type::Float)
throwNodeError("Tried to read float from non-float attribute");

View File

@ -11,6 +11,9 @@
#include <type_traits>
#include <vector>
#include <fmt/format.h>
#include <fmt/chrono.h>
#ifdef __MINGW32__
#include <winsock.h>
#else
@ -85,18 +88,13 @@ namespace hex {
void openWebpage(std::string_view url);
template<typename ... Args>
inline std::string format(const char *format, Args ... args) {
ssize_t size = snprintf( nullptr, 0, format, args ... );
inline std::string format(std::string_view format, Args ... args) {
return fmt::format(format, args...);
}
if (size <= 0)
return "";
std::vector<char> buffer(size + 1, 0x00);
if (snprintf(buffer.data(), size + 1, format, args ...) <= 0)
return "";
return std::string(buffer.data(), buffer.data() + size);
template<typename ... Args>
inline void print(std::string_view format, Args ... args) {
fmt::print(format, args...);
}
[[nodiscard]] constexpr inline u64 extract(u8 from, u8 to, const hex::unsigned_integral auto &value) {

View File

@ -298,7 +298,7 @@ namespace hex::lang {
provider->read(this->getOffset(), &data, this->getSize());
data = hex::changeEndianess(data, this->getSize(), this->getEndian());
this->createDefaultEntry(hex::format("%llu (0x%0*llX)", data, this->getSize() * 2, data));
this->createDefaultEntry(hex::format("{:d} (0x{:0{}X})", data, data, this->getSize() * 2));
}
[[nodiscard]] std::string getFormattedName() const override {
@ -329,7 +329,7 @@ namespace hex::lang {
s64 signedData = hex::signExtend(data, this->getSize(), 64);
this->createDefaultEntry(hex::format("%lld (0x%0*llX)", signedData, this->getSize() * 2, data));
this->createDefaultEntry(hex::format("{:d} (0x{:0{}X})", signedData, data, this->getSize() * 2));
}
[[nodiscard]] std::string getFormattedName() const override {
@ -359,13 +359,13 @@ namespace hex::lang {
provider->read(this->getOffset(), &data, 4);
data = hex::changeEndianess(data, 4, this->getEndian());
this->createDefaultEntry(hex::format("%e (0x%0*lX)", *reinterpret_cast<float*>(&data), this->getSize() * 2, data));
this->createDefaultEntry(hex::format("{:e} (0x{:0{}X})", *reinterpret_cast<float*>(&data), data, this->getSize() * 2));
} else if (this->getSize() == 8) {
u64 data = 0;
provider->read(this->getOffset(), &data, 8);
data = hex::changeEndianess(data, 8, this->getEndian());
this->createDefaultEntry(hex::format("%e (0x%0*llX)", *reinterpret_cast<double*>(&data), this->getSize() * 2, data));
this->createDefaultEntry(hex::format("{:e} (0x{:0{}X})", *reinterpret_cast<double*>(&data), data, this->getSize() * 2));
}
}
@ -417,7 +417,7 @@ namespace hex::lang {
char character;
provider->read(this->getOffset(), &character, 1);
this->createDefaultEntry(hex::format("'%c'", character));
this->createDefaultEntry(hex::format("'{0}'", character));
}
[[nodiscard]] std::string getFormattedName() const override {
@ -439,7 +439,7 @@ namespace hex::lang {
provider->read(this->getOffset(), buffer.data(), this->getSize());
buffer[this->getSize()] = '\0';
this->createDefaultEntry(hex::format("\"%s\"", makeDisplayable(buffer.data(), this->getSize()).c_str()));
this->createDefaultEntry(hex::format("\"{0}\"", makeDisplayable(buffer.data(), this->getSize()).c_str()));
}
[[nodiscard]] std::string getFormattedName() const override {
@ -757,7 +757,7 @@ namespace hex::lang {
ImGui::TableNextColumn();
ImGui::TextColored(ImColor(0xFFD69C56), "enum"); ImGui::SameLine(); ImGui::Text("%s", PatternData::getTypeName().c_str());
ImGui::TableNextColumn();
ImGui::Text("%s", hex::format("%s (0x%0*llX)", valueString.c_str(), this->getSize() * 2, value).c_str());
ImGui::Text("%s", hex::format("{} (0x{:0{}X})", valueString.c_str(), value, this->getSize() * 2).c_str());
}
[[nodiscard]] std::string getFormattedName() const override {
@ -803,7 +803,7 @@ namespace hex::lang {
std::string valueString = "{ ";
for (u64 i = 0; i < value.size(); i++)
valueString += hex::format("%02x ", value[i]);
valueString += hex::format("{0:02X} ", value[i]);
valueString += "}";
ImGui::TextUnformatted(valueString.c_str());

View File

@ -60,7 +60,7 @@ namespace hex {
break;
}
std::string result = hex::format("%.2f", value);
std::string result = hex::format("{0:.2f}", value);
switch (unitIndex) {
case 0: result += " Bytes"; break;
@ -168,11 +168,11 @@ namespace hex {
void openWebpage(std::string_view url) {
#if defined(OS_WINDOWS)
system(hex::format("start %s", url.data()).c_str());
system(hex::format("start {0}", url.data()).c_str());
#elif defined(OS_MACOS)
system(hex::format("open %s", url.data()).c_str());
system(hex::format("open {0}", url.data()).c_str());
#elif defined(OS_LINUX)
system(hex::format("xdg-open %s", url.data()).c_str());
system(hex::format("xdg-open {0}", url.data()).c_str());
#else
#warning "Unknown OS, can't open webpages"
#endif

View File

@ -61,7 +61,7 @@ namespace hex::lang {
if (candidate != currMembers.end())
currPattern = *candidate;
else
this->getConsole().abortEvaluation(hex::format("could not find identifier '%s'", identifier.c_str()));
this->getConsole().abortEvaluation(hex::format("could not find identifier '{0}'", identifier.c_str()));
}
if (auto pointerPattern = dynamic_cast<PatternDataPointer*>(currPattern); pointerPattern != nullptr)
@ -134,7 +134,7 @@ namespace hex::lang {
}
if (!ContentRegistry::PatternLanguageFunctions::getEntries().contains(node->getFunctionName().data()))
this->getConsole().abortEvaluation(hex::format("no function named '%s' found", node->getFunctionName().data()));
this->getConsole().abortEvaluation(hex::format("no function named '{0}' found", node->getFunctionName().data()));
auto &function = ContentRegistry::PatternLanguageFunctions::getEntries()[node->getFunctionName().data()];
@ -143,12 +143,12 @@ namespace hex::lang {
}
else if (function.parameterCount & ContentRegistry::PatternLanguageFunctions::LessParametersThan) {
if (evaluatedParams.size() >= (function.parameterCount & ~ContentRegistry::PatternLanguageFunctions::LessParametersThan))
this->getConsole().abortEvaluation(hex::format("too many parameters for function '%s'. Expected %d", node->getFunctionName().data(), function.parameterCount & ~ContentRegistry::PatternLanguageFunctions::LessParametersThan));
this->getConsole().abortEvaluation(hex::format("too many parameters for function '{0}'. Expected {1}", node->getFunctionName().data(), function.parameterCount & ~ContentRegistry::PatternLanguageFunctions::LessParametersThan));
} else if (function.parameterCount & ContentRegistry::PatternLanguageFunctions::MoreParametersThan) {
if (evaluatedParams.size() <= (function.parameterCount & ~ContentRegistry::PatternLanguageFunctions::MoreParametersThan))
this->getConsole().abortEvaluation(hex::format("too few parameters for function '%s'. Expected %d", node->getFunctionName().data(), function.parameterCount & ~ContentRegistry::PatternLanguageFunctions::MoreParametersThan));
this->getConsole().abortEvaluation(hex::format("too few parameters for function '{0}'. Expected {1}", node->getFunctionName().data(), function.parameterCount & ~ContentRegistry::PatternLanguageFunctions::MoreParametersThan));
} else if (function.parameterCount != evaluatedParams.size()) {
this->getConsole().abortEvaluation(hex::format("invalid number of parameters for function '%s'. Expected %d", node->getFunctionName().data(), function.parameterCount));
this->getConsole().abortEvaluation(hex::format("invalid number of parameters for function '{0}'. Expected {1}", node->getFunctionName().data(), function.parameterCount));
}
return function.func(*this, evaluatedParams);
@ -654,7 +654,7 @@ namespace hex::lang {
else
this->getConsole().abortEvaluation("ASTNodeVariableDecl had an invalid type. This is a bug!");
entry->setVariableName(hex::format("[%llu]", (u64)i));
entry->setVariableName(hex::format("[{0}]", (u64)i));
entry->setEndian(this->getCurrentEndian());
if (!color.has_value())

View File

@ -45,14 +45,14 @@ namespace hex::lang {
offset += 1;
if (offset >= code.length())
throwPreprocessorError(hex::format("missing terminating '%c' character", endChar), lineNumber);
throwPreprocessorError(hex::format("missing terminating '{0}' character", endChar), lineNumber);
}
offset += 1;
if (includeFile[0] != '/') {
std::string tempPath = includeFile;
for (const auto &dir : hex::getPath(ImHexPath::PatternsInclude)) {
tempPath = hex::format("%s/%s", dir.c_str(), includeFile.c_str());
tempPath = hex::format("{0}/{1}", dir.c_str(), includeFile.c_str());
if (std::filesystem::exists(includeFile))
break;
}
@ -61,7 +61,7 @@ namespace hex::lang {
FILE *file = fopen(includeFile.c_str(), "r");
if (file == nullptr)
throwPreprocessorError(hex::format("%s: No such file or directory", includeFile.c_str()), lineNumber);
throwPreprocessorError(hex::format("{0}: No such file or directory", includeFile.c_str()), lineNumber);
fseek(file, 0, SEEK_END);
size_t size = ftell(file);
@ -199,9 +199,9 @@ namespace hex::lang {
for (const auto &[type, value] : this->m_pragmas) {
if (this->m_pragmaHandlers.contains(type)) {
if (!this->m_pragmaHandlers[type](value))
throwPreprocessorError(hex::format("invalid value provided to '%s' #pragma directive", type.c_str()), lineNumber);
throwPreprocessorError(hex::format("invalid value provided to '{0}' #pragma directive", type.c_str()), lineNumber);
} else
throwPreprocessorError(hex::format("no #pragma handler registered for type %s", type.c_str()), lineNumber);
throwPreprocessorError(hex::format("no #pragma handler registered for type {0}", type.c_str()), lineNumber);
}
}
} catch (PreprocessorError &e) {

View File

@ -22,12 +22,12 @@ namespace hex::lang {
if (auto variableDeclNode = dynamic_cast<ASTNodeVariableDecl*>(node); variableDeclNode != nullptr) {
if (!identifiers.insert(variableDeclNode->getName().data()).second)
throwValidateError(hex::format("redefinition of identifier '%s'", variableDeclNode->getName().data()), variableDeclNode->getLineNumber());
throwValidateError(hex::format("redefinition of identifier '{0}'", variableDeclNode->getName().data()), variableDeclNode->getLineNumber());
this->validate({ variableDeclNode->getType() });
} else if (auto typeDeclNode = dynamic_cast<ASTNodeTypeDecl*>(node); typeDeclNode != nullptr) {
if (!identifiers.insert(typeDeclNode->getName().data()).second)
throwValidateError(hex::format("redefinition of identifier '%s'", typeDeclNode->getName().data()), typeDeclNode->getLineNumber());
throwValidateError(hex::format("redefinition of identifier '{0}'", typeDeclNode->getName().data()), typeDeclNode->getLineNumber());
this->validate({ typeDeclNode->getType() });
} else if (auto structNode = dynamic_cast<ASTNodeStruct*>(node); structNode != nullptr) {
@ -38,7 +38,7 @@ namespace hex::lang {
std::unordered_set<std::string> enumIdentifiers;
for (auto &[name, value] : enumNode->getEntries()) {
if (!enumIdentifiers.insert(name).second)
throwValidateError(hex::format("redefinition of enum constant '%s'", name.c_str()), value->getLineNumber());
throwValidateError(hex::format("redefinition of enum constant '{0}'", name.c_str()), value->getLineNumber());
}
}
}

View File

@ -7,10 +7,10 @@ namespace hex {
namespace fs = std::filesystem;
// hex::plugin::<pluginName>::internal::initializePlugin()
constexpr auto InitializePluginSymbol = "_ZN3hex6plugin%d%s8internal16initializePluginEv";
constexpr auto GetPluginNameSymbol = "_ZN3hex6plugin%d%s8internal13getPluginNameEv";
constexpr auto GetPluginAuthorSymbol = "_ZN3hex6plugin%d%s8internal15getPluginAuthorEv";
constexpr auto GetPluginDescriptionSymbol = "_ZN3hex6plugin%d%s8internal20getPluginDescriptionEv";
constexpr auto InitializePluginSymbol = "_ZN3hex6plugin{0}{1}8internal16initializePluginEv";
constexpr auto GetPluginNameSymbol = "_ZN3hex6plugin{0}{1}8internal13getPluginNameEv";
constexpr auto GetPluginAuthorSymbol = "_ZN3hex6plugin{0}{1}8internal15getPluginAuthorEv";
constexpr auto GetPluginDescriptionSymbol = "_ZN3hex6plugin{0}{1}8internal20getPluginDescriptionEv";
Plugin::Plugin(std::string_view path) {
this->m_handle = dlopen(path.data(), RTLD_LAZY);
@ -54,7 +54,7 @@ namespace hex {
if (this->m_getPluginNameFunction != nullptr)
return this->m_getPluginNameFunction();
else
return hex::format("Unknown Plugin @ 0x016llX", this->m_handle);
return hex::format("Unknown Plugin @ 0x{0:016X}", this->m_handle);
}
std::string Plugin::getPluginAuthor() const {

View File

@ -71,7 +71,7 @@ namespace hex {
if (ImGui::CollapsingHeader((std::string(name.data()) + "###" + std::to_string((u64)comment.data())).c_str())) {
ImGui::TextUnformatted("hex.view.bookmarks.title.info"_lang);
ImGui::Separator();
ImGui::Text("hex.view.bookmarks.address"_lang, region.address, region.address + region.size - 1, region.size);
ImGui::TextUnformatted(hex::format("hex.view.bookmarks.address"_lang, region.address, region.address + region.size - 1, region.size).c_str());
{
u8 bytes[10] = { 0 };
@ -79,7 +79,7 @@ namespace hex {
std::string bytesString;
for (u8 i = 0; i < std::min(region.size, size_t(10)); i++) {
bytesString += hex::format("%02X ", bytes[i]);
bytesString += hex::format("{0:02X} ", bytes[i]);
}
if (region.size > 10) {

View File

@ -76,7 +76,7 @@ namespace hex {
disassembly.operators = instructions[instr].op_str;
for (u8 i = 0; i < instructions[instr].size; i++)
disassembly.bytes += hex::format("%02X ", instructions[instr].bytes[i]);
disassembly.bytes += hex::format("{0:02X} ", instructions[instr].bytes[i]);
disassembly.bytes.pop_back();
this->m_disassembly.push_back(disassembly);

View File

@ -33,7 +33,7 @@ namespace hex {
ImGui::Text("ImHex Hex Editor v%s by WerWolv -", IMHEX_VERSION);
#if defined(GIT_BRANCH) && defined(GIT_COMMIT_HASH)
ImGui::SameLine();
if (ImGui::Hyperlink(hex::format("%s@%s", GIT_BRANCH, GIT_COMMIT_HASH).c_str()))
if (ImGui::Hyperlink(hex::format("{0}@{1}", GIT_BRANCH, GIT_COMMIT_HASH).c_str()))
hex::openWebpage("https://github.com/WerWolv/ImHex/commit/" GIT_COMMIT_HASH);
#endif
ImGui::TextUnformatted("hex.view.help.about.translator"_lang);
@ -44,7 +44,7 @@ namespace hex {
hex::openWebpage("https://github.com/WerWolv/ImHex");
ImGui::NewLine();
ImGui::Text("hex.view.help.about.donations"_lang);
ImGui::TextUnformatted("hex.view.help.about.donations"_lang);
ImGui::Separator();
constexpr const char* Links[] = { "https://werwolv.net/donate", "https://www.patreon.com/werwolv", "https://github.com/sponsors/WerWolv" };

View File

@ -211,7 +211,7 @@ namespace hex {
ImGui::SameLine();
ImGui::Spacing();
ImGui::SameLine();
ImGui::Text("hex.view.hexeditor.page"_lang, provider->getCurrentPage() + 1, provider->getPageCount());
ImGui::TextUnformatted(hex::format("hex.view.hexeditor.page"_lang, provider->getCurrentPage() + 1, provider->getPageCount()).c_str());
ImGui::SameLine();
if (ImGui::ArrowButton("prevPage", ImGuiDir_Left)) {
@ -607,7 +607,7 @@ namespace hex {
std::string str;
for (const auto &byte : buffer)
str += hex::format("%02X ", byte);
str += hex::format("{0:02X} ", byte);
str.pop_back();
ImGui::SetClipboardText(str.c_str());
@ -645,7 +645,7 @@ namespace hex {
str += "const unsigned char data[" + std::to_string(buffer.size()) + "] = { ";
for (const auto &byte : buffer)
str += hex::format("0x%02X, ", byte);
str += hex::format("0x{0:02X}, ", byte);
// Remove trailing comma
str.pop_back();
@ -657,7 +657,7 @@ namespace hex {
str += "constexpr std::array<unsigned char, " + std::to_string(buffer.size()) + "> data = { ";
for (const auto &byte : buffer)
str += hex::format("0x%02X, ", byte);
str += hex::format("0x{0:02X}, ", byte);
// Remove trailing comma
str.pop_back();
@ -669,7 +669,7 @@ namespace hex {
str += "final byte[] data = { ";
for (const auto &byte : buffer)
str += hex::format("0x%02X, ", byte);
str += hex::format("0x{0:02X}, ", byte);
// Remove trailing comma
str.pop_back();
@ -681,7 +681,7 @@ namespace hex {
str += "const byte[] data = { ";
for (const auto &byte : buffer)
str += hex::format("0x%02X, ", byte);
str += hex::format("0x{0:02X}, ", byte);
// Remove trailing comma
str.pop_back();
@ -693,7 +693,7 @@ namespace hex {
str += "let data: [u8, " + std::to_string(buffer.size()) + "] = [ ";
for (const auto &byte : buffer)
str += hex::format("0x%02X, ", byte);
str += hex::format("0x{0:02X}, ", byte);
// Remove trailing comma
str.pop_back();
@ -705,7 +705,7 @@ namespace hex {
str += "data = bytes([ ";
for (const auto &byte : buffer)
str += hex::format("0x%02X, ", byte);
str += hex::format("0x{0:02X}, ", byte);
// Remove trailing comma
str.pop_back();
@ -717,7 +717,7 @@ namespace hex {
str += "const data = new Uint8Array([ ";
for (const auto &byte : buffer)
str += hex::format("0x%02X, ", byte);
str += hex::format("0x{0:02X}, ", byte);
// Remove trailing comma
str.pop_back();
@ -745,13 +745,13 @@ namespace hex {
for (u32 col = start >> 4; col <= (end >> 4); col++) {
str += hex::format("%08lX ", col << 4);
str += hex::format("{0:08X} ", col << 4);
for (u64 i = 0 ; i < 16; i++) {
if (col == (start >> 4) && i < (start & 0xF) || col == (end >> 4) && i > (end & 0xF))
str += " ";
else
str += hex::format("%02lX ", buffer[((col << 4) - start) + i]);
str += hex::format("{0:02X} ", buffer[((col << 4) - start) + i]);
if ((i & 0xF) == 0x7)
str += " ";
@ -766,7 +766,7 @@ namespace hex {
else {
u8 c = buffer[((col << 4) - start) + i];
char displayChar = (c < 32 || c >= 128) ? '.' : c;
str += hex::format("%c", displayChar);
str += hex::format("{0}", displayChar);
}
}
@ -804,13 +804,13 @@ R"(
for (u32 col = start >> 4; col <= (end >> 4); col++) {
str += hex::format(" <span class=\"offsetcolumn\">%08lX</span>&nbsp&nbsp<span class=\"hexcolumn\">", col << 4);
str += hex::format(" <span class=\"offsetcolumn\">{0:08X}</span>&nbsp&nbsp<span class=\"hexcolumn\">", col << 4);
for (u64 i = 0 ; i < 16; i++) {
if (col == (start >> 4) && i < (start & 0xF) || col == (end >> 4) && i > (end & 0xF))
str += "&nbsp&nbsp ";
else
str += hex::format("%02lX ", buffer[((col << 4) - start) + i]);
str += hex::format("{0:02X} ", buffer[((col << 4) - start) + i]);
if ((i & 0xF) == 0x7)
str += "&nbsp";
@ -825,7 +825,7 @@ R"(
else {
u8 c = buffer[((col << 4) - start) + i];
char displayChar = (c < 32 || c >= 128) ? '.' : c;
str += hex::format("%c", displayChar);
str += hex::format("{0}", displayChar);
}
}