Checking suitable characters for a URL

In Accelerated C++ section 6.1 pg 107 (for those who have it to hand) there is a function that returns true when passed a character that cannot be part of a URL.

The specific part I'm interested in is the following:

1
2
3
4
5
6
7
bool not_url_char(char c)
{
     static const string url_ch = "~;/?:@=&$-_.+!*'(),";

     return !(isalnum(c) ||
              find(url_ch.begin(), url_ch.end(), c) != url_ch.end());
}


So, as I understand it, the function is passed a character, and on line 5 returns false if the character is an alphanumeric character, or any of the characters in the range (url_ch.begin(), url_ch.end()]

But after the call to find() on line 6, what is the use of != url_ch.end()? I don't understand why this is in the code and what its purpose is; I can't seem to relate it to anything in the function. It seems to be saying "do something if something != url_ch.end() but I don't know what either of the somethings are...I hope that makes sense.
The entire expression is this:
find(url_ch.begin(), url_ch.end(), c) != url_ch.end()

Look at std::strings's find method and you will understand for yourself. (I suppose you know wahat iallnum() and OR operator do )
It seems find returns the first iterator it finds to the given character, c.

I think the idea was that if the character isn't any of the characters in url_ch then it would end up returning the last iterator. != is the 'if not equals' operator, so returns true upon find not returning the end iterator, AKA returns true if any of the url_ch characters are found to be equal to c.

EDIT: And it returns in the range [url_ch.begin(), url_ch.end()]. It's possible for the first character to be the 'match' character.
Last edited on
~local static const? How useful.

This whole function is odd, just do the whole string at once. Not sure what isalnum() does (especially because this is just one character).

1
2
3
4
bool not_url(const string& str)
{
     return (str.find_first_of("~;/?:@=&$-_.+!*'(),") != string::npos);
}
Last edited on
I'm not sure I like the double-negatives. If isalnum is necessary, maybe this is more appropriate:

1
2
3
4
5
6
bool not_url_char(char c)
{
     static const string url_ch = "~;/?:@=&$-_.+!*'(),";

     return (!isalnum(c)) && (find(url_ch.begin(), url_ch.end(), c) == url_ch.end());
}
Topic archived. No new replies allowed.