util: Make Parse{Int,UInt}{32,64} use locale independent std::from_chars(…) (C++17) instead of locale dependent strto{l,ll,ul,ull} #20457

pull practicalswift wants to merge 1 commits into bitcoin:master from practicalswift:locale-independent-parseint changing 5 files +270 −74
  1. practicalswift commented at 9:58 am on November 23, 2020: contributor

    Make Parse{Int,UInt}{32,64} use locale independent std::from_chars(…) (C++17) instead of locale dependent strto{l,ll,ul,ull}.

    About std::from_chars: “Unlike other parsing functions in C++ and C libraries, std::from_chars is locale-independent, non-allocating, and non-throwing.”

  2. fanquake added the label Utils/log/libs on Nov 23, 2020
  3. laanwj commented at 10:07 am on November 23, 2020: member
    Concept ACK, see also discussion in #20452 (comment)
  4. DrahtBot commented at 10:53 am on November 23, 2020: member

    The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

    Conflicts

    Reviewers, this pull request conflicts with the following ones:

    • #20452 (util: Replace use of locale dependent atoi(…) with locale-independent std::from_chars(…) (C++17) by practicalswift)

    If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

  5. practicalswift force-pushed on Nov 23, 2020
  6. practicalswift force-pushed on Nov 23, 2020
  7. practicalswift force-pushed on Nov 23, 2020
  8. MarcoFalke referenced this in commit 9402159b9b on Nov 24, 2020
  9. practicalswift force-pushed on Nov 24, 2020
  10. sidhujag referenced this in commit 3a1441004a on Nov 24, 2020
  11. DrahtBot added the label Needs rebase on Nov 30, 2020
  12. practicalswift force-pushed on Dec 1, 2020
  13. DrahtBot removed the label Needs rebase on Dec 1, 2020
  14. practicalswift commented at 3:57 pm on December 1, 2020: contributor
    Like #20452 this one is blocked on #20460. That’s the reason for the CI failures :)
  15. MarcoFalke added the label Waiting for author on Dec 1, 2020
  16. practicalswift commented at 12:57 pm on December 15, 2020: contributor
    Could remove “Waiting for author”? I don’t know if we have any “Blocked on another PR/issue” (or “Waiting for toolchain upgrade”) tag, but this PR is blocked on #20460 :)
  17. MarcoFalke added the label Waiting for other on Dec 15, 2020
  18. MarcoFalke removed the label Waiting for author on Dec 15, 2020
  19. DrahtBot added the label Needs rebase on Dec 15, 2020
  20. practicalswift force-pushed on Dec 16, 2020
  21. DrahtBot removed the label Needs rebase on Dec 16, 2020
  22. practicalswift force-pushed on Mar 2, 2021
  23. jonatack commented at 9:43 pm on March 2, 2021: member
    Concept ACK. Looks very good; hope to see this unblocked with std::fs.
  24. DrahtBot added the label Needs rebase on Mar 19, 2021
  25. practicalswift force-pushed on Mar 19, 2021
  26. DrahtBot removed the label Needs rebase on Mar 19, 2021
  27. practicalswift force-pushed on Apr 24, 2021
  28. MarcoFalke added this to the milestone 23.0 on Apr 24, 2021
  29. practicalswift force-pushed on May 12, 2021
  30. practicalswift force-pushed on Jun 13, 2021
  31. practicalswift force-pushed on Jun 22, 2021
  32. practicalswift force-pushed on Jun 22, 2021
  33. practicalswift force-pushed on Jun 22, 2021
  34. PastaPastaPasta referenced this in commit 46a05ad537 on Jun 27, 2021
  35. PastaPastaPasta referenced this in commit f059da98e6 on Jun 28, 2021
  36. PastaPastaPasta referenced this in commit 840d3332e4 on Jun 29, 2021
  37. practicalswift force-pushed on Jun 30, 2021
  38. PastaPastaPasta referenced this in commit 1c80f26441 on Jul 1, 2021
  39. PastaPastaPasta referenced this in commit dea0bfecd1 on Jul 1, 2021
  40. PastaPastaPasta referenced this in commit 1621639e85 on Jul 15, 2021
  41. practicalswift force-pushed on Aug 15, 2021
  42. practicalswift force-pushed on Sep 5, 2021
  43. PastaPastaPasta referenced this in commit 0bb4bb8db0 on Sep 17, 2021
  44. util: Introduce ToIntegral<T>(const std::string&) for locale independent parsing using std::from_chars(…) (C++17)
    util: Avoid locale dependent functions strtol/strtoll/strtoul/strtoull in ParseInt32/ParseInt64/ParseUInt32/ParseUInt64
    
    fuzz: Assert equivalence between new and old Parse{Int,Uint}{8,32,64} functions
    
    test: Add unit tests for ToIntegral<T>(const std::string&)
    4747db8761
  45. practicalswift force-pushed on Sep 18, 2021
  46. PastaPastaPasta referenced this in commit 4485b3ea20 on Sep 19, 2021
  47. PastaPastaPasta referenced this in commit 40f2d3e692 on Sep 21, 2021
  48. PastaPastaPasta referenced this in commit 2328ead832 on Sep 24, 2021
  49. fanquake referenced this in commit 67eae69f3f on Sep 28, 2021
  50. MarcoFalke removed the label Waiting for other on Sep 28, 2021
  51. practicalswift commented at 2:18 pm on September 29, 2021: contributor

    Now that #20460 has been merged I think this PR should be ready for final review :)

    See also related PR #20452.

  52. laanwj commented at 12:59 pm on September 30, 2021: member
    Nice! Code review ACK 4747db876154ddd828c03d9eda10ecf8b25d8dc8
  53. laanwj merged this on Sep 30, 2021
  54. laanwj closed this on Sep 30, 2021

  55. sidhujag referenced this in commit ce7734b7e0 on Sep 30, 2021
  56. in src/util/strencodings.h:114 in 4747db8761
    109+    T result;
    110+    const auto [first_nonmatching, error_condition] = std::from_chars(str.data(), str.data() + str.size(), result);
    111+    if (first_nonmatching != str.data() + str.size() || error_condition != std::errc{}) {
    112+        return std::nullopt;
    113+    }
    114+    return {result};
    


    MarcoFalke commented at 10:02 am on October 1, 2021:
    Can you explain what the { are supposed to do? I think they can be removed, as they are confusing as-is. If you want them to check (for whatever reason) that result fits in type T, you will have to type return T{result};

    practicalswift commented at 3:27 pm on October 1, 2021:

    In this specific case return result; should be equivalent to return {result}; since the return type is std::optional<T> and result is of type T. I can see your point about braces looking a bit odd here: feel free to change :)

    Some context behind the “braced return” style more generally: it can help avoid nasty implicit narrowing conversions like in the following example.

     0$ cat without-list-initialization.cpp
     1#include <cstdint>
     2
     3struct F {
     4  F(uint8_t) {}
     5};
     6
     7F f() { return 1024; }
     8$ clang++ -c without-list-initialization.cpp
     9# No errors
    10$ echo $?
    110
    
     0$ cat with-list-initialization.cpp
     1#include <cstdint>
     2
     3struct F {
     4  F(uint8_t) {}
     5};
     6
     7F f() { return {1024}; }
     8$ clang++ -c with-list-initialization.cpp
     9with-list-initialization.cpp:7:18: error: constant expression evaluates to 1024 which cannot be narrowed to type 'uint8_t' (aka 'unsigned char') [-Wc++11-narrowing]
    10$ echo $?
    111
    

    See item (8) and “narrowing conversions” in https://en.cppreference.com/w/cpp/language/list_initialization :)



    MarcoFalke commented at 3:47 pm on October 1, 2021:
  57. MarcoFalke commented at 10:19 am on October 1, 2021: member
    crACK
  58. in src/util/strencodings.h:106 in 4747db8761
    101+ *
    102+ * @returns std::nullopt if the entire string could not be parsed, or if the
    103+ *   parsed value is not in the range representable by the type T.
    104+ */
    105+template <typename T>
    106+std::optional<T> ToIntegral(const std::string& str)
    


    MarcoFalke commented at 4:01 pm on October 1, 2021:
    I am wondering if it makes sense to explain in simple terms the format that is required to be parsed successfully. In regex terms it would be -?[0-9]+?
  59. MarcoFalke commented at 4:10 pm on October 1, 2021: member
    Created a follow-up in #23156
  60. MarcoFalke referenced this in commit 42fedb4acd on Oct 4, 2021
  61. sidhujag referenced this in commit 9f1da3bdbe on Oct 4, 2021
  62. kittywhiskers referenced this in commit 91f5c0d329 on Oct 12, 2021
  63. rebroad commented at 4:17 pm on October 13, 2021: contributor
     0  CXX      qt/qt_bitcoin_qt-main.o
     1In file included from qt/main.cpp:5:
     2In file included from ./qt/bitcoin.h:12:
     3In file included from ./interfaces/node.h:10:
     4In file included from ./net.h:9:
     5In file included from ./addrman.h:9:
     6In file included from ./netaddress.h:19:
     7./util/strencodings.h:16:10: fatal error: 'charconv' file not found
     8#include <charconv>
     9         ^~~~~~~~~~
    101 error generated.
    11Makefile:12673: recipe for target 'qt/qt_bitcoin_qt-main.o' failed
    12make: *** [qt/qt_bitcoin_qt-main.o] Error 1
    
  64. sipa commented at 4:19 pm on October 13, 2021: member
    @rebroad Bizarre, sounds like you’re not using a C++17 compliant compiler.
  65. rebroad commented at 4:35 pm on October 13, 2021: contributor

    @rebroad Bizarre, sounds like you’re not using a C++17 compliant compiler.

    Still getting the same error when compiling in a clean directory… could it be an issue caused by ccache?

  66. rebroad commented at 6:53 pm on October 13, 2021: contributor
     04747db876154ddd828c03d9eda10ecf8b25d8dc8 is the first bad commit
     1commit 4747db876154ddd828c03d9eda10ecf8b25d8dc8
     2Author: practicalswift <practicalswift@users.noreply.github.com>
     3Date:   Sat Sep 18 04:30:30 2021 +0000
     4
     5    util: Introduce ToIntegral<T>(const std::string&) for locale independent parsing using std::from_chars() (C++17)
     6
     7    util: Avoid locale dependent functions strtol/strtoll/strtoul/strtoull in ParseInt32/ParseInt64/ParseUInt32/ParseUInt64
     8
     9    fuzz: Assert equivalence between new and old Parse{Int,Uint}{8,32,64} functions
    10
    11    test: Add unit tests for ToIntegral<T>(const std::string&)
    12
    13:040000 040000 ec09e7aa0a2bec6f5021c2bf2994a0fc67e65c1f aa5170295604fa240939c86c02f26a6e436be964 M      src
    14:040000 040000 aa260b3d4bb2c554e68a7b0baa241a2cfc97818a c5d3df2e7337c8cc6a5a2141844b5c1b81a6ba51 M      test
    
  67. kittywhiskers referenced this in commit b306f19560 on Oct 21, 2022
  68. kittywhiskers referenced this in commit e329b303aa on Oct 21, 2022
  69. kittywhiskers referenced this in commit 3e658233e4 on Oct 27, 2022
  70. kittywhiskers referenced this in commit 3a3eb6e66a on Oct 27, 2022
  71. kittywhiskers referenced this in commit 609344ee82 on Oct 30, 2022
  72. kittywhiskers referenced this in commit eaa517a3e9 on Oct 30, 2022
  73. DrahtBot locked this on Oct 30, 2022

github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2024-07-03 10:13 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me