@@ -396,23 +396,28 @@ DecodeResult Decode(const std::string& str) {
396
396
}
397
397
398
398
/* * Find index of an incorrect character in a Bech32 string. */
399
- std::string LocateErrors (const std::string& str, std::vector<int >& error_locations) {
399
+ std::pair<std::string, std::vector<int >> LocateErrors (const std::string& str) {
400
+ std::vector<int > error_locations{};
401
+
400
402
if (str.size () > 90 ) {
401
403
error_locations.resize (str.size () - 90 );
402
404
std::iota (error_locations.begin (), error_locations.end (), 90 );
403
- return " Bech32 string too long" ;
405
+ return std::make_pair ( " Bech32 string too long" , std::move (error_locations)) ;
404
406
}
407
+
405
408
if (!CheckCharacters (str, error_locations)){
406
- return " Invalid character or mixed case" ;
409
+ return std::make_pair ( " Invalid character or mixed case" , std::move (error_locations)) ;
407
410
}
411
+
408
412
size_t pos = str.rfind (' 1' );
409
413
if (pos == str.npos ) {
410
- return " Missing separator" ;
414
+ return std::make_pair ( " Missing separator" , std::vector< int >{}) ;
411
415
}
412
416
if (pos == 0 || pos + 7 > str.size ()) {
413
417
error_locations.push_back (pos);
414
- return " Invalid separator position" ;
418
+ return std::make_pair ( " Invalid separator position" , std::move (error_locations)) ;
415
419
}
420
+
416
421
std::string hrp;
417
422
for (size_t i = 0 ; i < pos; ++i) {
418
423
hrp += LowerCase (str[i]);
@@ -425,7 +430,7 @@ std::string LocateErrors(const std::string& str, std::vector<int>& error_locatio
425
430
int8_t rev = CHARSET_REV[c];
426
431
if (rev == -1 ) {
427
432
error_locations.push_back (i);
428
- return " Invalid Base 32 character" ;
433
+ return std::make_pair ( " Invalid Base 32 character" , std::move (error_locations)) ;
429
434
}
430
435
values[i - pos - 1 ] = rev;
431
436
}
@@ -545,19 +550,19 @@ std::string LocateErrors(const std::string& str, std::vector<int>& error_locatio
545
550
}
546
551
} else {
547
552
// No errors
548
- error_locations.clear ();
549
- return " " ;
553
+ return std::make_pair (" " , std::vector<int >{});
550
554
}
551
555
552
556
if (error_locations.empty () || (!possible_errors.empty () && possible_errors.size () < error_locations.size ())) {
553
557
error_locations = std::move (possible_errors);
554
558
if (!error_locations.empty ()) error_encoding = encoding;
555
559
}
556
560
}
557
- return error_encoding == Encoding::BECH32M ? " Invalid Bech32m checksum"
558
- : error_encoding == Encoding::BECH32 ? " Invalid Bech32 checksum"
559
- : " Invalid checksum" ;
561
+ std::string error_message = error_encoding == Encoding::BECH32M ? " Invalid Bech32m checksum"
562
+ : error_encoding == Encoding::BECH32 ? " Invalid Bech32 checksum"
563
+ : " Invalid checksum" ;
560
564
565
+ return std::make_pair (error_message, std::move (error_locations));
561
566
}
562
567
563
568
} // namespace bech32
0 commit comments