-
-
Notifications
You must be signed in to change notification settings - Fork 93
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
Add iterator support to url_search_params #532
Conversation
60579dd
to
bab1ae7
Compare
This comment was marked as resolved.
This comment was marked as resolved.
@jasnell Can you add documentation to each function and a small example to README? |
8e0f3ed
to
1a56d71
Compare
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.
@jasnell Looks fine to me. However, I am unsure why you don't want to use conventional C++ iterators, which allow you to automagically do this: for (auto key : search_params.get_keys()) {
// do something with key
} The approach you document...
will appear odd (I don't mean to be insulting) to many modern C++ users. It is also objectively 'busier' and easier to get wrong. Furthermore, why 'is_done' instead of the conventional 'has_next... That is, the following is easier to understand... auto keys = search_params.get_keys();
while (keys.has_next()) {
auto key = keys.next(); // "a", "c", "e", "g"
} Isn't it? Can you elaborate on your design decisions ? |
Effectively, I think you need very strong arguments if you are going to bypass the standard C++ iterators. They are easy to implement, and they lead to all sorts of goodies. They are expected in C++. |
Yep, I'd considered implementing normal c++ iterators but opted for this model to keep it conceptually similar to JavaScripts iterator pattern (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#the_iterator_protocol), which has There the pattern for wrapping these is more likely something like (forgive the mild pseudocode) class URLStringParamsKeysIterator {
public:
struct Result {
bool done;
JsValue value;
};
Result next() {
if (iter.done) return { true, JsUndefined};
return { false, iter.next() };
}
private:
ada::url_search_params_keys_iter iter;
} I wanted also to leave room for someone to implement the conventional c++ iterator pattern if they wanted -- these should be able to co-exist. I just really have no immediate need for the c++ style iterators. I need this only to support the JS pattern. And yes, I know that I could implement these using the C++ iterator pattern under the covers but this was just easier :-) Also note that the JS iterator pattern for |
@lemire @anonrig ... ok, I've updated to add the conventional c++ iterator in addition to the JS-style. For the c++ iterator I'm really just deferring to the underlying std::vector for entries, whereas the js-style has distinct instances for keys, values, and entries. I also renamed |
Approved. |
62956d1
to
856f874
Compare
I see that many of the operations in ada_c are intended to be safe when |
Does two things:
Adds simple iterators to ada::url_search_params. These are implemented as distinct types with
next()
andis_done()
methods as opposed to traditional c++ iterators.Adds url search params apis to ada_c