util: Add Join helper to join a list of strings #16670

pull MarcoFalke wants to merge 2 commits into bitcoin:master from MarcoFalke:1908-utilStringJoin changing 5 files +61 −8
  1. MarcoFalke commented at 6:59 pm on August 20, 2019: member

    We have a lot of enumerations in the code and sometimes those enumerations need to be mentioned in the RPC or command line documentation. Previously, each caller would have a couple of lines inline to join the strings or the joined string is hardcoded in the documentation. A helper to join strings would make code such as #16629 (review) less verbose and easier to read.

    Also, warnings commonly accumulate in complex RPCs, since a warning doesn’t lead to an early return. A helper to join those warnings would make code such as https://github.com/bitcoin/bitcoin/pull/16394/files#r309324997 less verbose and easier to read.

  2. MarcoFalke added the label Utils/log/libs on Aug 20, 2019
  3. MarcoFalke force-pushed on Aug 20, 2019
  4. practicalswift commented at 8:02 pm on August 20, 2019: contributor

    ACK fa09713060ced21905108161d53e6de79f104649 – diff looks correct

    Perhaps put this handy function in real-world use outside of the tests as part of this PR?

    Could these be candidates?

    https://github.com/bitcoin/bitcoin/blob/70b12af87eb036623f5dd1f3a519efe6c156570d/src/rpc/util.cpp#L648-L652

    https://github.com/bitcoin/bitcoin/blob/70b12af87eb036623f5dd1f3a519efe6c156570d/src/script/script.cpp#L256-L261

  5. util: Add Join helper to join a list of strings fa8cd6f9c1
  6. rpc: Use Join helper in rpc/util faebf62714
  7. MarcoFalke force-pushed on Aug 20, 2019
  8. MarcoFalke commented at 9:05 pm on August 20, 2019: member

    Perhaps put this handy function in real-world use outside of the tests as part of this PR?

    Done. (Now used in rpc/util. I checked that all generated rpc helps are identical before and after this change)

  9. practicalswift commented at 9:47 pm on August 20, 2019: contributor

    Very nice!

    ACK faebf6271467048dc8a9a0c526a0f8565023a966

  10. sipa commented at 10:07 pm on August 20, 2019: member
    I think you can also use std::accumulate(list.begin(), list.end(), ""); it’s also a lot more generic.
  11. DrahtBot commented at 10:08 pm on August 20, 2019: 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:

    • #15934 (Separate settings merging from parsing by ryanofsky)

    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.

  12. MarcoFalke commented at 10:16 pm on August 20, 2019: member

    I think you can also use std::accumulate(list.begin(), list.end(), “”); it’s also a lot more generic.

    It seems less generic, since it does not accept a separator, nor a unary operator.

  13. emilengler commented at 2:22 pm on August 21, 2019: contributor
    Concept ACK
  14. NicolasDorier commented at 5:56 am on August 22, 2019: contributor

    @sipa accumulate is not recommended https://stackoverflow.com/questions/15347123/how-to-construct-a-stdstring-from-a-stdvectorstring/18703743

    0Don't use std::accumulate for string concatenation, it is a classic Schlemiel the Painter's algorithm, even worse than the usual example using strcat in C. Without C++11 move semantics, it incurs two unnecessary copies of the accumulator for each element of the vector. Even with move semantics, it still incurs one unnecessary copy of the accumulator for each element.
    1
    2The three examples above are O(n).
    3
    4std::accumulate is O(n²) for strings.
    
  15. MarcoFalke referenced this in commit 52b9797119 on Aug 22, 2019
  16. MarcoFalke merged this on Aug 22, 2019
  17. MarcoFalke closed this on Aug 22, 2019

  18. MarcoFalke deleted the branch on Aug 22, 2019
  19. sidhujag referenced this in commit 6745db91ae on Aug 23, 2019
  20. hebasto commented at 6:56 pm on May 10, 2020: member
    @MarcoFalke On Linux I can compile, link, and pass unit tests without util/string.cpp. Has this source file any special mission?
  21. MarcoFalke commented at 7:11 pm on May 10, 2020: member
    No, it has no meaning. Just a placeholder when someone wants to add stuff. For example the things in strencodings that have nothing to do with encoding/decoding strings can be moved here. Not saying that should be done now, but it can be done in the future.
  22. deadalnix referenced this in commit 4c00f172dd on Jun 4, 2020
  23. kittywhiskers referenced this in commit c6b8561c66 on May 25, 2021
  24. UdjinM6 referenced this in commit ac863d3955 on May 28, 2021
  25. furszy referenced this in commit 170beab56c on Jul 1, 2021
  26. random-zebra referenced this in commit b4751e10ce on Aug 11, 2021
  27. DrahtBot locked this on Feb 15, 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-06-02 01:13 UTC

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