-
Notifications
You must be signed in to change notification settings - Fork 36.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor: Make Join() util work with any container type #25879
The head ref may contain hidden characters: "2208-join-\u{1F3B6}"
Conversation
Also remove redundant return type that can be deduced by the compiler.
LGTM |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Approach ACK fa75227
src/util/string.h
Outdated
@@ -58,34 +58,29 @@ void ReplaceAll(std::string& in_out, const std::string& search, const std::strin | |||
} | |||
|
|||
/** | |||
* Join a list of items | |||
* Join items |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: perhaps a bit more info:
* Join items | |
* Join all collection items into an object with the same type as the first item, separated by a separator |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That'd be incorrect. It isn't the type of the first item, but the type returned by the unary op executed on the first item. In practise this will (almost) always be std::string
, so it seems odd to be overly verbose?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay, no strong feelings about it. For a helper function that's meant to be used all over the codebase I just think it's worth thinking more about a helpful docstring, maybe a join function is not be trivial to everyone? Final suggestion but I don't want to waste time over this so feel free to ignore if you don't think it's helpful:
* Join items | |
* Join all container items. Typically used to concatenate strings but accepts containers with | |
* elements of any type. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thx, done
src/util/string.h
Outdated
bool first{true}; | ||
for (const auto& item : collection) { | ||
if (!first) ret += separator; | ||
ret += unary_op(item); | ||
first = false; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could potentially do away with first
:
bool first{true}; | |
for (const auto& item : collection) { | |
if (!first) ret += separator; | |
ret += unary_op(item); | |
first = false; | |
for (const auto& item : collection) { | |
if (&item != &(*collection.begin())) ret += separator; | |
ret += unary_op(item); | |
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That wouldn't compile for all types. For example, https://en.cppreference.com/w/cpp/container/vector_bool/reference has no operator&
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see, thanks for the info. Didn't know about this, that's annoying!
src/util/string.h
Outdated
* | ||
* @param list The list to join | ||
* @param collection The items to join |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: container
seems to be the term used in c++, would it be better to be consistent with that?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I think I got collection from python https://docs.python.org/3/library/collections.html
src/util/string.h
Outdated
template <typename T, typename T2> | ||
T Join(const std::vector<T>& list, const T2& separator) | ||
template <typename C, typename S> | ||
auto Join(const C& list, const S& separator) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: parameter name inconsistent
auto Join(const C& list, const S& separator) | |
auto Join(const C& collection, const S& separator) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thx, done
utACK fabedbc btw, do you wanna update PR name and commit messages to use |
Also, remove helper that is only used in tests.
thx, renamed title |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ACK fa95315
ACK fa95315 |
…er type fa95315 Use new Join() helper for ListBlockFilterTypes() (MacroFake) fa1c716 Make Join() util work with any container type (MacroFake) faf8da3 Remove Join() helper only used in tests (MacroFake) Pull request description: This allows to drop some code ACKs for top commit: naumenkogs: ACK fa95315 stickies-v: ACK [fa95315](bitcoin@fa95315) Tree-SHA512: efd65b65722f46b221bd53140ff22bd8e45adc83617980233f28f695be3108a6ab01affd751d715134ffcb9762228ba8952e9467e590cff022c83e0f5404cb74
This allows to drop some code