-
-
Notifications
You must be signed in to change notification settings - Fork 155
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Formatting Integers Using toml::value_flags
#108
Comments
Ah, so you want insertion to also copy value flags when inserting by copying an from existing
Not presently, unless you do it in two steps, e.g.: arr.push_back(0x123abc);
arr.back().as<int64_t>()->flags(format_flags::format_as_hexadecimal); Which is admittedly quite clunky, though easily fixed with a helper function. As an aside/FYI: // this:
std::cout << toml::default_formatter { t, toml::format_flags::allow_value_format_flags } << std::endl;
// is the same as this:
std::cout << t << std::endl; |
Another point of clarification. When you say this:
What do you mean? The idea of 'formatting' TOML data in |
@marzer Thank you for the solution. My code now looks like this: #include <iostream>
#include "toml.hpp"
int main(void) {
auto v = toml::value { 0x123abc };
auto t = toml::table {{ { "hex", v } }};
t.get("hex")->as_integer()->flags(toml::value_flags::format_as_hexadecimal);
std::cout << t << std::endl;
} and produces this output: hex = 0x123ABC
I meant that when I was trying to insert formatted integers into a auto v = toml::value { 0x123abc };
v = v.flags(toml::value_flags::format_as_hexadecimal);
std::cout << v << std::endl; |
@LebJe Ah, cool. Glad you were able to sort it out. |
@LebJe FYI I've made improvements in this area on the v3 branch, such that value flags are now copied properly on insertion, and can also be overridden at insertion too: toml::array arr;
// copying value flags
auto v = toml::value{ 10 };
v.flags(toml::value_flags::format_as_hexadecimal);
arr.push_back(v);
// setting/overriding them on array insertion
arr.push_back(10, toml::value_flags::format_as_hexadecimal);
std::cout << arr << std::endl; would print [ 0xA, 0xA ] The full list of changes in the v3 branch can be found here. |
Environmenttoml++ version and/or commit hash: Compiler: clang++ --version
Homebrew clang version 15.0.7
Target: arm64-apple-darwin22.1.0
Thread model: posix
InstalledDir: /Users/lebje/homebrew/opt/llvm/bin C++ standard mode: Target arch: Library configuration overrides: Relevant compilation flags: I'm not sure if something in the library has changed, or if I'm doing something wrong, but it seems that there are problems with inserting formatted integers into arrays and tables:
The code below demonstrates all the above issues: #include "toml.hpp"
#include <iostream>
int main(void) {
auto table = toml::table();
auto innerTable = toml::table();
auto array = toml::array();
auto value = toml::value(0x123);
value.flags(toml::value_flags::format_as_hexadecimal);
array.push_back(value);
array.push_back(value, toml::value_flags::format_as_hexadecimal);
innerTable.insert("preserve_source_value_flags", value);
innerTable.insert("format_as_hexadecimal", value, toml::value_flags::format_as_hexadecimal);
innerTable.insert("array", array);
std::cout << "# Inner table: \n" << innerTable << "\n\n";
table.insert("preserve_source_value_flags", value);
table.insert("format_as_hexadecimal", value, toml::value_flags::format_as_hexadecimal);
table.insert("array", array);
table.insert("innerTable", innerTable, toml::preserve_source_value_flags);
std::cout << "# Outer table: \n" << table << "\n";
} Result: # Inner table:
array = [ 291, 0x123 ]
format_as_hexadecimal = 0x123
preserve_source_value_flags = 291
# Outer table:
array = [ 291, 0x123 ]
format_as_hexadecimal = 0x123
preserve_source_value_flags = 291
[innerTable]
array = [ 291, 0x123 ]
format_as_hexadecimal = 291
preserve_source_value_flags = 291 Please let me know if I should create another issue, or continue in this issue. |
Nope, not intentionally at least. Looks like a regression. Thanks for letting me know, I'll look into this some time this week. |
also: - fixed `toml::value::flags()` not being cleared when `std::move`-ing a value - fixed #195
It works perfectly! Thank you! |
Environment
toml++ version and/or commit hash:
1c3957ca206fb3915c123933b4a9b59ef6cf296a
Compiler:
C++ standard mode:
C++ 17
Target arch:
AArch64 (ARM64)
Library configuration overrides:
None
Relevant compilation flags:
None
Describe the bug
I would like to format integers in my TOML table as hexadecimal, octal, or binary, but it seems that
toml::value_flags
only formats integers when they are sent to a stream.Steps to reproduce (or a small repro code sample)
Output:
Additional information
Is it possible to insert formatted integers into a
toml::table
ortoml::array
?The text was updated successfully, but these errors were encountered: