File tree Expand file tree Collapse file tree 2 files changed +27
-5
lines changed Expand file tree Collapse file tree 2 files changed +27
-5
lines changed Original file line number Diff line number Diff line change @@ -346,6 +346,28 @@ namespace bencode {
346
346
template <typename T>
347
347
inline constexpr bool is_iterable_v = is_iterable<T>::value;
348
348
349
+ template <typename T>
350
+ std::string quoted_key (const T &key) {
351
+ std::string s;
352
+ s.reserve (key.size () + 2 );
353
+ s.push_back (' "' );
354
+ for (auto &&i : key) {
355
+ char c = static_cast <char >(i);
356
+ switch (c) {
357
+ case ' \n ' :
358
+ s.append (" \\ n" );
359
+ break ;
360
+ case ' "' :
361
+ s.append (" \\\" " );
362
+ break ;
363
+ default :
364
+ s.push_back (c);
365
+ }
366
+ }
367
+ s.push_back (' "' );
368
+ return s;
369
+ }
370
+
349
371
template <typename Integer>
350
372
inline void check_overflow (Integer value, Integer digit) {
351
373
using limits = std::numeric_limits<Integer>;
@@ -541,7 +563,7 @@ namespace bencode {
541
563
auto i = p->emplace (std::move (dict_key), std::move (thing));
542
564
if (!i.second ) {
543
565
throw syntax_error (
544
- " duplicated key in dict: " + std::string (i.first ->first )
566
+ " duplicated key in dict: " + quoted_key (i.first ->first )
545
567
);
546
568
}
547
569
return &i.first ->second ;
Original file line number Diff line number Diff line change @@ -417,10 +417,10 @@ suite<> test_decode("test decoder", [](auto &_) {
417
417
});
418
418
419
419
_.test (" duplicated key" , []() {
420
- expect (
421
- []() { bencode::decode ( " d3:fooi1e3:fooi1ee " ); },
422
- decode_error<bencode::syntax_error>( " duplicated key in dict: foo" , 17 )
423
- );
420
+ expect ([]() { bencode::decode ( " d3:fooi1e3:fooi1ee " ); },
421
+ decode_error< bencode::syntax_error>(
422
+ " duplicated key in dict: \" foo\" " , 17
423
+ ) );
424
424
});
425
425
});
426
426
You can’t perform that action at this time.
0 commit comments