| 1 | // { dg-do run { target c++20 } } |
| 2 | |
| 3 | #include <format> |
| 4 | #include <testsuite_hooks.h> |
| 5 | |
| 6 | static_assert(std::is_constructible_v<std::format_parse_context, |
| 7 | std::string_view>); |
| 8 | static_assert(std::is_constructible_v<std::wformat_parse_context, |
| 9 | std::wstring_view>); |
| 10 | |
| 11 | #if __cpp_lib_format < 202305 |
| 12 | constexpr bool construct_with_num_args = true; |
| 13 | #else |
| 14 | constexpr bool construct_with_num_args = false; |
| 15 | #endif |
| 16 | |
| 17 | static_assert(std::is_constructible_v<std::format_parse_context, |
| 18 | std::string_view, std::size_t> |
| 19 | == construct_with_num_args); |
| 20 | static_assert(std::is_constructible_v<std::wformat_parse_context, |
| 21 | std::wstring_view, std::size_t> |
| 22 | == construct_with_num_args); |
| 23 | |
| 24 | static_assert( ! std::is_constructible_v<std::format_parse_context, |
| 25 | std::wstring_view>); |
| 26 | static_assert( ! std::is_constructible_v<std::wformat_parse_context, |
| 27 | std::string_view>); |
| 28 | |
| 29 | static_assert( ! std::is_convertible_v<std::string_view, |
| 30 | std::format_parse_context> ); |
| 31 | static_assert( ! std::is_convertible_v<std::wstring_view, |
| 32 | std::wformat_parse_context> ); |
| 33 | |
| 34 | static_assert( ! std::is_default_constructible_v<std::format_parse_context> ); |
| 35 | static_assert( ! std::is_copy_constructible_v<std::format_parse_context> ); |
| 36 | static_assert( ! std::is_move_constructible_v<std::format_parse_context> ); |
| 37 | static_assert( ! std::is_copy_assignable_v<std::format_parse_context> ); |
| 38 | static_assert( ! std::is_move_assignable_v<std::format_parse_context> ); |
| 39 | |
| 40 | // This concept is satisfied if the next_arg_id() call is a constant expression |
| 41 | template<typename Ch, typename PC = std::basic_format_parse_context<Ch>> |
| 42 | concept arg_id_available = requires { |
| 43 | typename std::integral_constant<std::size_t, |
| 44 | PC({}).next_arg_id()>::type; |
| 45 | }; |
| 46 | |
| 47 | void |
| 48 | test_members() |
| 49 | { |
| 50 | std::string_view s = "spec string"; |
| 51 | |
| 52 | std::format_parse_context pc(s); |
| 53 | |
| 54 | VERIFY( pc.begin() == s.begin() ); |
| 55 | VERIFY( pc.end() == s.end() ); |
| 56 | pc.advance_to(s.begin() + 5); |
| 57 | VERIFY( pc.begin() == s.begin() + 5 ); |
| 58 | |
| 59 | // Runtime calls to these do not check for the correct number of args. |
| 60 | VERIFY( pc.next_arg_id() == 0 ); |
| 61 | VERIFY( pc.next_arg_id() == 1 ); |
| 62 | VERIFY( pc.next_arg_id() == 2 ); |
| 63 | try |
| 64 | { |
| 65 | // Cannot mix manual and automatic indexing. |
| 66 | pc.check_arg_id(0); |
| 67 | VERIFY( false ); |
| 68 | } |
| 69 | catch (const std::format_error&) |
| 70 | { |
| 71 | } |
| 72 | // But they do check during constant evaluation: |
| 73 | VERIFY( ! arg_id_available<char> ); |
| 74 | VERIFY( ! arg_id_available<wchar_t> ); |
| 75 | |
| 76 | std::format_parse_context pc2(""); |
| 77 | pc2.check_arg_id(2); |
| 78 | pc2.check_arg_id(1); |
| 79 | pc2.check_arg_id(3); |
| 80 | try |
| 81 | { |
| 82 | // Cannot mix manual and automatic indexing. |
| 83 | (void) pc2.next_arg_id(); |
| 84 | VERIFY( false ); |
| 85 | } |
| 86 | catch (const std::format_error&) |
| 87 | { |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | template<typename T, bool auto_indexing = true> |
| 92 | bool |
| 93 | is_std_format_spec_for(std::string_view spec) |
| 94 | { |
| 95 | std::format_parse_context pc(spec); |
| 96 | if (auto_indexing) |
| 97 | (void) pc.next_arg_id(); |
| 98 | else |
| 99 | pc.check_arg_id(0); |
| 100 | |
| 101 | std::formatter<T> f; |
| 102 | try { |
| 103 | auto end = f.parse(pc); |
| 104 | VERIFY( end == spec.end() || *end == '}' ); |
| 105 | return true; |
| 106 | } catch (const std::format_error&) { |
| 107 | return false; |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | #if __cpp_lib_format_ranges |
| 112 | constexpr bool escaped_strings_supported = true; |
| 113 | #else |
| 114 | constexpr bool escaped_strings_supported = false; |
| 115 | #endif |
| 116 | |
| 117 | void |
| 118 | test_char() |
| 119 | { |
| 120 | VERIFY( is_std_format_spec_for<char>("") ); |
| 121 | VERIFY( is_std_format_spec_for<char>("<") ); |
| 122 | VERIFY( is_std_format_spec_for<char>(">") ); |
| 123 | VERIFY( is_std_format_spec_for<char>("^") ); |
| 124 | VERIFY( is_std_format_spec_for<char>("0<") ); |
| 125 | VERIFY( is_std_format_spec_for<char>("0>") ); |
| 126 | VERIFY( is_std_format_spec_for<char>("0^") ); |
| 127 | VERIFY( ! is_std_format_spec_for<char>("{^") ); |
| 128 | VERIFY( ! is_std_format_spec_for<char>("+") ); |
| 129 | VERIFY( ! is_std_format_spec_for<char>("-") ); |
| 130 | VERIFY( ! is_std_format_spec_for<char>(" ") ); |
| 131 | VERIFY( ! is_std_format_spec_for<char>("#") ); |
| 132 | VERIFY( is_std_format_spec_for<char>("0d") ); |
| 133 | VERIFY( ! is_std_format_spec_for<char>("0") ); |
| 134 | VERIFY( ! is_std_format_spec_for<char>("00d") ); |
| 135 | VERIFY( is_std_format_spec_for<char>("01d") ); |
| 136 | VERIFY( is_std_format_spec_for<char>("0{}d") ); |
| 137 | VERIFY( ! is_std_format_spec_for<char>("0{1}d") ); |
| 138 | VERIFY(( is_std_format_spec_for<char, false>("0{1}d") )); |
| 139 | VERIFY( is_std_format_spec_for<char>("1") ); |
| 140 | VERIFY( ! is_std_format_spec_for<char>("-1") ); |
| 141 | VERIFY( is_std_format_spec_for<char>("-1d") ); // sign and width |
| 142 | VERIFY( ! is_std_format_spec_for<char>(".") ); |
| 143 | VERIFY( ! is_std_format_spec_for<char>(".1") ); |
| 144 | VERIFY( is_std_format_spec_for<char>("c") ); |
| 145 | VERIFY( is_std_format_spec_for<char>("b") ); |
| 146 | VERIFY( is_std_format_spec_for<char>("B") ); |
| 147 | VERIFY( is_std_format_spec_for<char>("d") ); |
| 148 | VERIFY( is_std_format_spec_for<char>("o") ); |
| 149 | VERIFY( is_std_format_spec_for<char>("x") ); |
| 150 | VERIFY( is_std_format_spec_for<char>("X") ); |
| 151 | VERIFY( ! is_std_format_spec_for<char>("s") ); |
| 152 | VERIFY( is_std_format_spec_for<char>("?") == escaped_strings_supported ); |
| 153 | VERIFY( ! is_std_format_spec_for<char>("a") ); |
| 154 | VERIFY( ! is_std_format_spec_for<char>("A") ); |
| 155 | VERIFY( ! is_std_format_spec_for<char>("f") ); |
| 156 | VERIFY( ! is_std_format_spec_for<char>("F") ); |
| 157 | VERIFY( ! is_std_format_spec_for<char>("g") ); |
| 158 | VERIFY( ! is_std_format_spec_for<char>("G") ); |
| 159 | VERIFY( ! is_std_format_spec_for<char>("+c") ); |
| 160 | VERIFY( ! is_std_format_spec_for<char>("+?") ); |
| 161 | VERIFY( is_std_format_spec_for<char>("+d") ); |
| 162 | } |
| 163 | |
| 164 | void |
| 165 | test_int() |
| 166 | { |
| 167 | VERIFY( is_std_format_spec_for<int>("") ); |
| 168 | VERIFY( is_std_format_spec_for<int>("<") ); |
| 169 | VERIFY( is_std_format_spec_for<int>(">") ); |
| 170 | VERIFY( is_std_format_spec_for<int>("^") ); |
| 171 | VERIFY( is_std_format_spec_for<int>("0<") ); |
| 172 | VERIFY( is_std_format_spec_for<int>("0>") ); |
| 173 | VERIFY( is_std_format_spec_for<int>("0^") ); |
| 174 | VERIFY( ! is_std_format_spec_for<int>("{^") ); |
| 175 | VERIFY( is_std_format_spec_for<int>("+") ); |
| 176 | VERIFY( is_std_format_spec_for<int>("-") ); |
| 177 | VERIFY( is_std_format_spec_for<int>(" ") ); |
| 178 | VERIFY( is_std_format_spec_for<int>("#") ); |
| 179 | VERIFY( is_std_format_spec_for<int>("0d") ); |
| 180 | VERIFY( is_std_format_spec_for<int>("0") ); |
| 181 | VERIFY( ! is_std_format_spec_for<int>("00d") ); |
| 182 | VERIFY( is_std_format_spec_for<int>("01d") ); |
| 183 | VERIFY( ! is_std_format_spec_for<int>("0{1}d") ); |
| 184 | VERIFY(( is_std_format_spec_for<int>("0{}d") )); |
| 185 | VERIFY(( ! is_std_format_spec_for<int>("0{1}d") )); |
| 186 | VERIFY(( is_std_format_spec_for<int, false>("0{1}d") )); |
| 187 | VERIFY( is_std_format_spec_for<int>("1") ); |
| 188 | VERIFY( is_std_format_spec_for<int>("-1") ); // sign and width |
| 189 | VERIFY( ! is_std_format_spec_for<int>(".") ); |
| 190 | VERIFY( ! is_std_format_spec_for<int>(".1") ); |
| 191 | VERIFY( is_std_format_spec_for<int>("c") ); |
| 192 | VERIFY( is_std_format_spec_for<int>("b") ); |
| 193 | VERIFY( is_std_format_spec_for<int>("B") ); |
| 194 | VERIFY( is_std_format_spec_for<int>("d") ); |
| 195 | VERIFY( is_std_format_spec_for<int>("o") ); |
| 196 | VERIFY( is_std_format_spec_for<int>("x") ); |
| 197 | VERIFY( is_std_format_spec_for<int>("X") ); |
| 198 | VERIFY( ! is_std_format_spec_for<int>("s") ); |
| 199 | VERIFY( ! is_std_format_spec_for<int>("?") ); |
| 200 | VERIFY( ! is_std_format_spec_for<int>("a") ); |
| 201 | VERIFY( ! is_std_format_spec_for<int>("A") ); |
| 202 | VERIFY( ! is_std_format_spec_for<int>("f") ); |
| 203 | VERIFY( ! is_std_format_spec_for<int>("F") ); |
| 204 | VERIFY( ! is_std_format_spec_for<int>("g") ); |
| 205 | VERIFY( ! is_std_format_spec_for<int>("G") ); |
| 206 | VERIFY( ! is_std_format_spec_for<int>("p") ); |
| 207 | VERIFY( ! is_std_format_spec_for<int>("P") ); |
| 208 | VERIFY( is_std_format_spec_for<int>("+c") ); // But LWG 3644 would change it. |
| 209 | VERIFY( ! is_std_format_spec_for<int>("+?") ); |
| 210 | VERIFY( is_std_format_spec_for<int>("+d") ); |
| 211 | } |
| 212 | |
| 213 | void |
| 214 | test_bool() |
| 215 | { |
| 216 | VERIFY( is_std_format_spec_for<bool>("") ); |
| 217 | VERIFY( is_std_format_spec_for<bool>("<") ); |
| 218 | VERIFY( is_std_format_spec_for<bool>(">") ); |
| 219 | VERIFY( is_std_format_spec_for<bool>("^") ); |
| 220 | VERIFY( is_std_format_spec_for<bool>("0<") ); |
| 221 | VERIFY( is_std_format_spec_for<bool>("0>") ); |
| 222 | VERIFY( is_std_format_spec_for<bool>("0^") ); |
| 223 | VERIFY( ! is_std_format_spec_for<bool>("{^") ); |
| 224 | VERIFY( ! is_std_format_spec_for<bool>("+") ); |
| 225 | VERIFY( ! is_std_format_spec_for<bool>("-") ); |
| 226 | VERIFY( ! is_std_format_spec_for<bool>(" ") ); |
| 227 | VERIFY( ! is_std_format_spec_for<bool>("#") ); |
| 228 | VERIFY( is_std_format_spec_for<bool>("0d") ); |
| 229 | VERIFY( ! is_std_format_spec_for<bool>("0") ); |
| 230 | VERIFY( ! is_std_format_spec_for<bool>("00d") ); |
| 231 | VERIFY( is_std_format_spec_for<bool>("01d") ); |
| 232 | VERIFY( is_std_format_spec_for<bool>("1") ); |
| 233 | VERIFY( ! is_std_format_spec_for<bool>("-1") ); |
| 234 | VERIFY( is_std_format_spec_for<bool>("-1d") ); // sign and width |
| 235 | VERIFY( ! is_std_format_spec_for<bool>(".") ); |
| 236 | VERIFY( ! is_std_format_spec_for<bool>(".1") ); |
| 237 | VERIFY( ! is_std_format_spec_for<bool>("c") ); |
| 238 | VERIFY( is_std_format_spec_for<bool>("b") ); |
| 239 | VERIFY( is_std_format_spec_for<bool>("B") ); |
| 240 | VERIFY( is_std_format_spec_for<bool>("d") ); |
| 241 | VERIFY( is_std_format_spec_for<bool>("o") ); |
| 242 | VERIFY( is_std_format_spec_for<bool>("x") ); |
| 243 | VERIFY( is_std_format_spec_for<bool>("X") ); |
| 244 | VERIFY( is_std_format_spec_for<bool>("s") ); |
| 245 | VERIFY( ! is_std_format_spec_for<bool>("?") ); |
| 246 | VERIFY( ! is_std_format_spec_for<bool>("a") ); |
| 247 | VERIFY( ! is_std_format_spec_for<bool>("A") ); |
| 248 | VERIFY( ! is_std_format_spec_for<bool>("f") ); |
| 249 | VERIFY( ! is_std_format_spec_for<bool>("F") ); |
| 250 | VERIFY( ! is_std_format_spec_for<bool>("g") ); |
| 251 | VERIFY( ! is_std_format_spec_for<bool>("G") ); |
| 252 | VERIFY( ! is_std_format_spec_for<bool>("p") ); |
| 253 | VERIFY( ! is_std_format_spec_for<bool>("P") ); |
| 254 | VERIFY( ! is_std_format_spec_for<bool>("+s") ); |
| 255 | VERIFY( is_std_format_spec_for<bool>("+d") ); |
| 256 | } |
| 257 | |
| 258 | void |
| 259 | test_float() |
| 260 | { |
| 261 | VERIFY( is_std_format_spec_for<float>("") ); |
| 262 | VERIFY( is_std_format_spec_for<float>("<") ); |
| 263 | VERIFY( is_std_format_spec_for<float>(">") ); |
| 264 | VERIFY( is_std_format_spec_for<float>("^") ); |
| 265 | VERIFY( is_std_format_spec_for<float>("0<") ); |
| 266 | VERIFY( is_std_format_spec_for<float>("0>") ); |
| 267 | VERIFY( is_std_format_spec_for<float>("0^") ); |
| 268 | VERIFY( ! is_std_format_spec_for<float>("{^") ); |
| 269 | VERIFY( is_std_format_spec_for<float>("+") ); |
| 270 | VERIFY( is_std_format_spec_for<float>("-") ); |
| 271 | VERIFY( is_std_format_spec_for<float>(" ") ); |
| 272 | VERIFY( is_std_format_spec_for<float>("#") ); |
| 273 | VERIFY( is_std_format_spec_for<float>("0f") ); |
| 274 | VERIFY( is_std_format_spec_for<float>("0") ); |
| 275 | VERIFY( ! is_std_format_spec_for<float>("00f") ); |
| 276 | VERIFY( is_std_format_spec_for<float>("01f") ); |
| 277 | VERIFY( is_std_format_spec_for<float>("0{}f") ); |
| 278 | VERIFY( ! is_std_format_spec_for<float>("0{1}f") ); |
| 279 | VERIFY( ! is_std_format_spec_for<float>("0{1}f") ); |
| 280 | VERIFY(( is_std_format_spec_for<float, false>("0{1}f") )); |
| 281 | VERIFY( is_std_format_spec_for<float>("1") ); |
| 282 | VERIFY( is_std_format_spec_for<float>("-1") ); // sign and width |
| 283 | VERIFY( ! is_std_format_spec_for<float>(".") ); |
| 284 | VERIFY( is_std_format_spec_for<float>(".1") ); |
| 285 | VERIFY( is_std_format_spec_for<float>(".{}") ); |
| 286 | VERIFY( ! is_std_format_spec_for<float>(".{1}") ); |
| 287 | VERIFY(( is_std_format_spec_for<float, false>(".{1}") )); |
| 288 | VERIFY( is_std_format_spec_for<float>("{}.{}") ); |
| 289 | VERIFY(( is_std_format_spec_for<float, false>("{1}.{1}") )); |
| 290 | VERIFY(( is_std_format_spec_for<float, false>("{2}.{1}") )); |
| 291 | VERIFY( ! is_std_format_spec_for<float>("c") ); |
| 292 | VERIFY( ! is_std_format_spec_for<float>("b") ); |
| 293 | VERIFY( ! is_std_format_spec_for<float>("B") ); |
| 294 | VERIFY( ! is_std_format_spec_for<float>("d") ); |
| 295 | VERIFY( ! is_std_format_spec_for<float>("o") ); |
| 296 | VERIFY( ! is_std_format_spec_for<float>("x") ); |
| 297 | VERIFY( ! is_std_format_spec_for<float>("X") ); |
| 298 | VERIFY( ! is_std_format_spec_for<float>("s") ); |
| 299 | VERIFY( ! is_std_format_spec_for<float>("?") ); |
| 300 | VERIFY( is_std_format_spec_for<float>("a") ); |
| 301 | VERIFY( is_std_format_spec_for<float>("A") ); |
| 302 | VERIFY( is_std_format_spec_for<float>("f") ); |
| 303 | VERIFY( is_std_format_spec_for<float>("F") ); |
| 304 | VERIFY( is_std_format_spec_for<float>("g") ); |
| 305 | VERIFY( is_std_format_spec_for<float>("G") ); |
| 306 | VERIFY( ! is_std_format_spec_for<float>("p") ); |
| 307 | VERIFY( ! is_std_format_spec_for<float>("P") ); |
| 308 | VERIFY( is_std_format_spec_for<float>("+f") ); |
| 309 | |
| 310 | VERIFY( is_std_format_spec_for<float>("_<+#09.6Lf") ); |
| 311 | VERIFY( is_std_format_spec_for<float>("<+#09.6Lf") ); |
| 312 | VERIFY( is_std_format_spec_for<float>("<+#9.6Lf") ); |
| 313 | VERIFY( is_std_format_spec_for<float>(".0006f") ); |
| 314 | } |
| 315 | |
| 316 | void |
| 317 | test_pointer() |
| 318 | { |
| 319 | VERIFY( is_std_format_spec_for<void*>("") ); |
| 320 | VERIFY( is_std_format_spec_for<void*>("<") ); |
| 321 | VERIFY( is_std_format_spec_for<void*>(">") ); |
| 322 | VERIFY( is_std_format_spec_for<void*>("^") ); |
| 323 | VERIFY( is_std_format_spec_for<void*>("0<") ); |
| 324 | VERIFY( is_std_format_spec_for<void*>("0>") ); |
| 325 | VERIFY( is_std_format_spec_for<void*>("0^") ); |
| 326 | VERIFY( ! is_std_format_spec_for<void*>("{^") ); |
| 327 | VERIFY( ! is_std_format_spec_for<void*>("+") ); |
| 328 | VERIFY( ! is_std_format_spec_for<void*>("-") ); |
| 329 | VERIFY( ! is_std_format_spec_for<void*>(" ") ); |
| 330 | VERIFY( ! is_std_format_spec_for<void*>("#") ); |
| 331 | VERIFY( is_std_format_spec_for<void*>("1") ); |
| 332 | VERIFY( ! is_std_format_spec_for<void*>("-1") ); |
| 333 | VERIFY( ! is_std_format_spec_for<void*>("-1p") ); |
| 334 | VERIFY( ! is_std_format_spec_for<void*>(".") ); |
| 335 | VERIFY( ! is_std_format_spec_for<void*>(".1") ); |
| 336 | VERIFY( ! is_std_format_spec_for<void*>("c") ); |
| 337 | VERIFY( ! is_std_format_spec_for<void*>("b") ); |
| 338 | VERIFY( ! is_std_format_spec_for<void*>("B") ); |
| 339 | VERIFY( ! is_std_format_spec_for<void*>("d") ); |
| 340 | VERIFY( ! is_std_format_spec_for<void*>("o") ); |
| 341 | VERIFY( ! is_std_format_spec_for<void*>("x") ); |
| 342 | VERIFY( ! is_std_format_spec_for<void*>("X") ); |
| 343 | VERIFY( ! is_std_format_spec_for<void*>("s") ); |
| 344 | VERIFY( ! is_std_format_spec_for<void*>("?") ); |
| 345 | VERIFY( is_std_format_spec_for<void*>("p") ); |
| 346 | VERIFY( ! is_std_format_spec_for<void*>("a") ); |
| 347 | VERIFY( ! is_std_format_spec_for<void*>("A") ); |
| 348 | VERIFY( ! is_std_format_spec_for<void*>("f") ); |
| 349 | VERIFY( ! is_std_format_spec_for<void*>("F") ); |
| 350 | VERIFY( ! is_std_format_spec_for<void*>("g") ); |
| 351 | VERIFY( ! is_std_format_spec_for<void*>("G") ); |
| 352 | VERIFY( ! is_std_format_spec_for<void*>("+p") ); |
| 353 | |
| 354 | #if __cpp_lib_format >= 202304L |
| 355 | // P2510R3 Formatting pointers |
| 356 | VERIFY( is_std_format_spec_for<void*>("P") ); |
| 357 | VERIFY( is_std_format_spec_for<void*>("0p") ); |
| 358 | VERIFY( is_std_format_spec_for<void*>("0P") ); |
| 359 | VERIFY( is_std_format_spec_for<void*>("0") ); |
| 360 | VERIFY( is_std_format_spec_for<void*>("01p") ); |
| 361 | VERIFY( ! is_std_format_spec_for<void*>("00p") ); |
| 362 | #endif |
| 363 | } |
| 364 | |
| 365 | void |
| 366 | test_string() |
| 367 | { |
| 368 | VERIFY( is_std_format_spec_for<const char*>("") ); |
| 369 | VERIFY( is_std_format_spec_for<const char*>("<") ); |
| 370 | VERIFY( is_std_format_spec_for<const char*>(">") ); |
| 371 | VERIFY( is_std_format_spec_for<const char*>("^") ); |
| 372 | VERIFY( is_std_format_spec_for<const char*>("0<") ); |
| 373 | VERIFY( is_std_format_spec_for<const char*>("0>") ); |
| 374 | VERIFY( is_std_format_spec_for<const char*>("0^") ); |
| 375 | VERIFY( ! is_std_format_spec_for<const char*>("{^") ); |
| 376 | VERIFY( ! is_std_format_spec_for<const char*>("+") ); |
| 377 | VERIFY( ! is_std_format_spec_for<const char*>("-") ); |
| 378 | VERIFY( ! is_std_format_spec_for<const char*>(" ") ); |
| 379 | VERIFY( ! is_std_format_spec_for<const char*>("#") ); |
| 380 | VERIFY( ! is_std_format_spec_for<const char*>("0") ); |
| 381 | VERIFY( ! is_std_format_spec_for<const char*>("01s") ); |
| 382 | VERIFY( is_std_format_spec_for<const char*>("1") ); |
| 383 | VERIFY( ! is_std_format_spec_for<const char*>("-1") ); |
| 384 | VERIFY( ! is_std_format_spec_for<const char*>("-1s") ); |
| 385 | VERIFY( ! is_std_format_spec_for<const char*>(".") ); |
| 386 | VERIFY( is_std_format_spec_for<const char*>(".1") ); |
| 387 | VERIFY( is_std_format_spec_for<const char*>(".{}") ); |
| 388 | VERIFY(( is_std_format_spec_for<const char*, false>(".{0}") )); |
| 389 | VERIFY(( is_std_format_spec_for<const char*, false>(".{1}") )); |
| 390 | VERIFY( ! is_std_format_spec_for<const char*>("c") ); |
| 391 | VERIFY( ! is_std_format_spec_for<const char*>("b") ); |
| 392 | VERIFY( ! is_std_format_spec_for<const char*>("B") ); |
| 393 | VERIFY( ! is_std_format_spec_for<const char*>("d") ); |
| 394 | VERIFY( ! is_std_format_spec_for<const char*>("o") ); |
| 395 | VERIFY( ! is_std_format_spec_for<const char*>("x") ); |
| 396 | VERIFY( ! is_std_format_spec_for<const char*>("X") ); |
| 397 | VERIFY( is_std_format_spec_for<const char*>("s") ); |
| 398 | VERIFY( is_std_format_spec_for<const char*>("?") == escaped_strings_supported ); |
| 399 | VERIFY( ! is_std_format_spec_for<const char*>("p") ); |
| 400 | VERIFY( ! is_std_format_spec_for<const char*>("P") ); |
| 401 | VERIFY( ! is_std_format_spec_for<const char*>("a") ); |
| 402 | VERIFY( ! is_std_format_spec_for<const char*>("A") ); |
| 403 | VERIFY( ! is_std_format_spec_for<const char*>("f") ); |
| 404 | VERIFY( ! is_std_format_spec_for<const char*>("F") ); |
| 405 | VERIFY( ! is_std_format_spec_for<const char*>("g") ); |
| 406 | VERIFY( ! is_std_format_spec_for<const char*>("G") ); |
| 407 | |
| 408 | VERIFY( is_std_format_spec_for<const char*>("*^6s") ); |
| 409 | VERIFY( is_std_format_spec_for<const char*>(">6s") ); |
| 410 | VERIFY( is_std_format_spec_for<const char*>("_<6.4?") == escaped_strings_supported ); |
| 411 | } |
| 412 | |
| 413 | struct S { }; |
| 414 | |
| 415 | template<> |
| 416 | struct std::formatter<S, char> |
| 417 | { |
| 418 | constexpr std::format_parse_context::iterator |
| 419 | parse(std::format_parse_context& pc) |
| 420 | { |
| 421 | std::string_view spec(pc.begin(), pc.end()); |
| 422 | auto p = spec.find('}'); |
| 423 | if (p == std::string_view::npos) |
| 424 | p = spec.size(); |
| 425 | if (p == 0) |
| 426 | throw std::format_error("empty format-spec"); |
| 427 | if (spec != "custom") |
| 428 | throw std::format_error("invalid format-spec"); |
| 429 | return pc.begin() + p; |
| 430 | } |
| 431 | |
| 432 | std::format_context::iterator |
| 433 | format(const S&, std::format_context&) const; |
| 434 | }; |
| 435 | |
| 436 | void |
| 437 | test_custom() |
| 438 | { |
| 439 | VERIFY( is_std_format_spec_for<S>("custom") ); |
| 440 | VERIFY( ! is_std_format_spec_for<S>("customer") ); |
| 441 | VERIFY( ! is_std_format_spec_for<S>("custard") ); |
| 442 | VERIFY( ! is_std_format_spec_for<S>("") ); |
| 443 | } |
| 444 | |
| 445 | #if __cpp_lib_format >= 202305 |
| 446 | struct X { }; |
| 447 | |
| 448 | template<> |
| 449 | struct std::formatter<X, char> |
| 450 | { |
| 451 | constexpr std::format_parse_context::iterator |
| 452 | parse(std::format_parse_context& pc) |
| 453 | { |
| 454 | std::string_view spec(pc.begin(), pc.end()); |
| 455 | auto p = spec.find('}'); |
| 456 | if (p != std::string_view::npos) |
| 457 | spec = spec.substr(0, p); // truncate to closing brace |
| 458 | if (spec == "int") |
| 459 | { |
| 460 | pc.check_dynamic_spec_integral(pc.next_arg_id()); |
| 461 | integer = true; |
| 462 | } |
| 463 | else if (spec == "str") |
| 464 | { |
| 465 | pc.check_dynamic_spec_string(pc.next_arg_id()); |
| 466 | integer = false; |
| 467 | } |
| 468 | else |
| 469 | throw std::format_error("invalid format-spec"); |
| 470 | return pc.begin() + spec.size(); |
| 471 | } |
| 472 | |
| 473 | std::format_context::iterator |
| 474 | format(X, std::format_context& c) const |
| 475 | { |
| 476 | std::visit_format_arg([this]<typename T>(T) { // { dg-warning "deprecated" "" { target c++26 } } |
| 477 | if (is_integral_v<T> != this->integer) |
| 478 | throw std::format_error("invalid argument type"); |
| 479 | }, c.arg(1)); |
| 480 | return c.out(); |
| 481 | } |
| 482 | private: |
| 483 | bool integer = false; |
| 484 | }; |
| 485 | #endif |
| 486 | |
| 487 | void |
| 488 | test_dynamic_type_check() |
| 489 | { |
| 490 | #if __cpp_lib_format >= 202305 |
| 491 | std::format_parse_context pc("{1}.{2}"); |
| 492 | |
| 493 | // None of these calls should do anything at runtime, only during consteval: |
| 494 | pc.check_dynamic_spec<int, const char*>(0); |
| 495 | pc.check_dynamic_spec_integral(0); |
| 496 | pc.check_dynamic_spec_string(0); |
| 497 | |
| 498 | (void) std::format("{:int}", X{}, 42L); |
| 499 | (void) std::format("{:str}", X{}, "H2G2"); |
| 500 | #endif |
| 501 | } |
| 502 | |
| 503 | int main() |
| 504 | { |
| 505 | test_char(); |
| 506 | test_int(); |
| 507 | test_bool(); |
| 508 | test_float(); |
| 509 | test_string(); |
| 510 | test_pointer(); |
| 511 | test_custom(); |
| 512 | test_dynamic_type_check(); |
| 513 | } |