Skip to content

Latest commit

 

History

History
185 lines (149 loc) · 6.03 KB

183.md

File metadata and controls

185 lines (149 loc) · 6.03 KB
Info

replacement_field ::=  "{" [arg_id] [":" format_spec] "}"
arg_id            ::=  integer | identifier
integer           ::=  digit+
digit             ::=  "0"..."9"
identifier        ::=  id_start id_continue*
id_start          ::=  "a"..."z" | "A"..."Z" | "_"
id_continue       ::=  id_start | digit

Example

int main() {
  std::cout << fmt::format("1 + 1 = {}", 1+1); // prints 1 + 1 = 2
}

https://godbolt.org/z/GsbsWj

Puzzle

  • Can you fill TODO with appropriate formattings?
int main() {
  using namespace boost::ut;
  using namespace std::literals;

  "format single"_test = [] {
    expect("42"sv         == fmt::format("TODO", 42));
    expect("42.."sv       == fmt::format("TODO", 42));
    expect("..42"sv       == fmt::format("TODO", 42));
    expect("42 0x2a"sv    == fmt::format("TODO", 42)); // use 0 parameter
    expect("  QL  "sv     == fmt::format("TODO", "QL"));
    expect("  QL  "sv     == fmt::format("TODO", "QL"));
    expect("QL        "sv == fmt::format("TODO", "QL", 10));
  };

  "format multiple"_test = [] {
    expect("1+2=3"sv              == fmt::format("TODO", 1, 2, 1+2));
    expect("1.23, 0b0000, 0x2a"sv == fmt::format("TODO", 1.234, 2, 0, 42));
  };

  "format named"_test = [] {
    using namespace fmt::literals;
    expect("named=quant:lab"sv == fmt::format("TODO", "arg1"_a = "quant", "arg2"_a = "lab"));
  };
}

https://godbolt.org/z/aWsMa3

Solutions

"format single"_test = [] {
  expect("42"sv         == fmt::format("{}", 42));
  expect("42.."sv       == fmt::format("{:.<4}", 42));
  expect("..42"sv       == fmt::format("{:.>4}", 42));
  expect("42 0x2a"sv    == fmt::format("{0} {0:#x}", 42)); // use 0 parameter
  expect("  QL  "sv     == fmt::format("{:^6}", "QL"));
  expect("  QL  "sv     == fmt::format("{:>4}  ", "QL"));
  expect("QL        "sv == fmt::format("{:<{}}", "QL", 10));
};

"format multiple"_test = [] {
  expect("1+2=3"sv              == fmt::format("{}+{}={}", 1, 2, 1+2));
  expect("1.23, 0b0000, 0x2a"sv == fmt::format("{0:.{1}f}, {2:#06b}, {3:#x}", 1.234, 2, 0, 42));
};

"format named"_test = [] {
  using namespace fmt::literals;
  expect("named=quant:lab"sv == fmt::format("named={arg1}:{arg2}", "arg1"_a = "quant", "arg2"_a = "lab"));
};

https://godbolt.org/z/5nq88W

"format single"_test = [] {
  expect("42"sv         == fmt::format("{}", 42));
  expect("42.."sv       == fmt::format("{}..", 42));
  expect("..42"sv       == fmt::format("..{}", 42));
  expect("42 0x2a"sv    == fmt::format("{0} {0:#4x}", 42)); // use 0 parameter
  expect("  QL  "sv     == fmt::format("  {}  ", "QL"));
  expect("  QL  "sv     == fmt::format("{:^6}", "QL"));
  expect("QL        "sv == fmt::format("{:<{}}", "QL", 10));
};

"format multiple"_test = [] {
  expect("1+2=3"sv              == fmt::format("{}+{}={}", 1, 2, 1+2));
  expect("1.23, 0b0000, 0x2a"sv == fmt::format("{:.{}f}, {:#06b}, {:#4x}", 1.234, 2, 0, 42));
};

"format named"_test = [] {
  using namespace fmt::literals;
  expect("named=quant:lab"sv == fmt::format("named={arg1}:{arg2}", "arg1"_a = "quant", "arg2"_a = "lab"));
};

https://godbolt.org/z/e6oqdv

"format single"_test = [] {
  expect("42"sv         == fmt::format("{}", 42));
  expect("42.."sv       == fmt::format("{}..", 42));
  expect("..42"sv       == fmt::format("..{}", 42));
  expect("42 0x2a"sv    == fmt::format("{0} {0:#x}", 42)); // use 0 parameter
  expect("  QL  "sv     == fmt::format("  {}  ", "QL"));
  expect("  QL  "sv     == fmt::format("  {}  ", "QL")); // Duplicate??
  expect("QL        "sv == fmt::format("{:<{}}", "QL", 10));
};

"format multiple"_test = [] {
  expect("1+2=3"sv              == fmt::format("{}+{}={}", 1, 2, 1+2));
  expect("1.23, 0b0000, 0x2a"sv == fmt::format("{:0.{}f}, {:#06b}, {:#x}", 1.234, 2, 0, 42));
};

"format named"_test = [] {
  using namespace fmt::literals;
  expect("named=quant:lab"sv == fmt::format("named={arg1}:{arg2}", "arg1"_a = "quant", "arg2"_a = "lab"));
};

https://godbolt.org/z/G83ofz

"format single"_test = [] {
  expect("42"sv         == fmt::format("{}", 42));
  expect("42.."sv       == fmt::format("{0:.<4}", 42));
  expect("..42"sv       == fmt::format("{0:.>4}", 42));
  expect("42 0x2a"sv    == fmt::format("{0:<3}{0:#x}", 42)); // use 0 parameter
  expect("  QL  "sv     == fmt::format("{0:^6}", "QL"));
  expect("  QL  "sv     == fmt::format("{0:^6}", "QL"));
  expect("QL        "sv == fmt::format("{0:{1}}", "QL", 10));
};

"format multiple"_test = [] {
  expect("1+2=3"sv              == fmt::format("{0}+{1}={2}", 1, 2, 1+2));
  expect("1.23, 0b0000, 0x2a"sv == fmt::format("{0:.{1}f}, {2:#06b}, {3:#x}", 1.234, 2, 0, 42));
};

"format named"_test = [] {
  using namespace fmt::literals;
  expect("named=quant:lab"sv == fmt::format("named={arg1}:{arg2}", "arg1"_a = "quant", "arg2"_a = "lab"));
};

https://godbolt.org/z/boErsd

"format single"_test = [] {
  expect("42"sv         == fmt::format("{:d}", 42));
  expect("42.."sv       == fmt::format("{:.<4d}", 42));
  expect("..42"sv       == fmt::format("{:.>4d}", 42));
  expect("42 0x2a"sv    == fmt::format("{0:d} {0:#x}", 42)); // use 0 parameter
  expect("  QL  "sv     == fmt::format("{:^6}", "QL"));
  expect("  QL  "sv     == fmt::format("{:^6}", "QL"));
  expect("QL        "sv == fmt::format("{:<10}", "QL", 10));
};

"format multiple"_test = [] {
  expect("1+2=3"sv              == fmt::format("{0:d}+{1:d}={2:d}", 1, 2, 1+2));
  expect("1.23, 0b0000, 0x2a"sv == fmt::format("{0:4.2f}, {2:#06b}, {3:#x}", 1.234, 2, 0, 42));
};

"format named"_test = [] {
  using namespace fmt::literals;
  expect("named=quant:lab"sv == fmt::format("named={arg1}:{arg2}", "arg1"_a = "quant", "arg2"_a = "lab"));
};

https://godbolt.org/z/oxhKEf