Implement the following rules:
- Interpret [X]:Y as host=X port=Y, if Y is an integer
- Interpret X:Y as host=X port=Y, if Y is an integer and X contains no colon
- Interpret X:Y as host=X:Y port=default otherwise
Implement the following rules:
35 | @@ -36,6 +36,24 @@ enum Network ParseNetwork(std::string net) { 36 | return NET_UNROUTABLE; 37 | } 38 | 39 | +void static SplitHostPort(std::string in, int &portOut, std::string &hostOut) { 40 | + size_t colon = in.find_last_of(':'); 41 | + // if a : is found, and it either follows a [...], or no other : is in the string, treat it as port separator 42 | + if (colon != in.npos && ((in[0]=='[' && in[colon-1]==']') || colon==0 || in.find_last_of(':',colon-1)==in.npos)) {
Are you sure the input string is well-formed? What if it is the empty string, or ":" ? Either assertions or sanity checks would be a good idea to avoid indexing out-of-bounds. And maybe unit tests for this routine...
It deals with any string:
I'll add some comments and unit tests, though.
Thanks. I agree the logic as it is now won't crash, but without unit tests I think it is much too easy for somebody to modify it in the future an introduce a bug.
(and I think there might be a problem with the empty string down on line 51, where you check if (in[0] ==...) )
Rebased, refactored the code a bit, and added unit tests. Github fails to recognize it as automatically mergable...
Implement the following rules:
* Interpret [X]:Y as host=X port=Y, if Y is an integer
* Interpret X:Y as host=X port=Y, if Y is an integer and X contains no colon
* Interpret X:Y as host=X:Y port=default otherwise
Added more tests.
Good to see it got merged :).