query string parsing utils
parent
c0e91aff7e
commit
466a5b2c3b
|
@ -44,19 +44,33 @@ int urlenc::query_key_pos(const char * query, const char * key)
|
|||
{
|
||||
int i = 0, j = 0;
|
||||
char q, k;
|
||||
|
||||
if (key[0] == '\0') return -1;
|
||||
|
||||
do {
|
||||
q = query[i+j];
|
||||
k = key[j];
|
||||
if (k == '\0' && q == '=')
|
||||
return i;
|
||||
|
||||
if (q == k) {
|
||||
j++;
|
||||
} else i++;
|
||||
|
||||
} while (q != '\0');
|
||||
return -1;
|
||||
}
|
||||
|
||||
int urlenc::query_val_pos(const char * query, const char * key)
|
||||
{
|
||||
int i = 0;
|
||||
int pos = query_key_pos(query, key);
|
||||
if (pos == -1) return -1;
|
||||
while (key[i] != '\0') i++;
|
||||
i++;
|
||||
return pos + i;
|
||||
}
|
||||
|
||||
int urlenc::query_val_len(const char * query, const char * key)
|
||||
{
|
||||
int len = 0, i = query_val_pos(query, key);
|
||||
if (i == -1) return -1;
|
||||
while (query[i + len] != '\0' && query[i + len] != '&') len++;
|
||||
return len;
|
||||
}
|
||||
|
|
|
@ -7,4 +7,6 @@ namespace urlenc
|
|||
char hexval(char);
|
||||
|
||||
int query_key_pos(const char *, const char *);
|
||||
int query_val_pos(const char *, const char *);
|
||||
int query_val_len(const char *, const char *);
|
||||
}
|
||||
|
|
|
@ -92,4 +92,31 @@ TEST_CASE("Locate position of a key/value pair in the query component","[urlenc]
|
|||
CHECK(query_key_pos("","") == -1);
|
||||
CHECK(query_key_pos("","bar") == -1);
|
||||
CHECK(query_key_pos("foo=bar&baz=qux","") == -1);
|
||||
CHECK(query_key_pos("foo=bar&bar=baz","bar") == 8);
|
||||
}
|
||||
|
||||
TEST_CASE("Location position of a value in a key/value pair in the query component","[urlenc][query_val_pos]")
|
||||
{
|
||||
CHECK(query_val_pos("foo=1&bar=2&baz=3","bar") == 10);
|
||||
CHECK(query_val_pos("foo=1&bar=2&baz=3","foo") == 4);
|
||||
CHECK(query_val_pos("foo=1&bar=2&baz=3","qux") == -1);
|
||||
CHECK(query_val_pos("foo=1&bar=&baz=3","baz") == 15);
|
||||
CHECK(query_val_pos("j=10&bar=&baz=3","bar") == 9);
|
||||
CHECK(query_val_pos("","") == -1);
|
||||
CHECK(query_val_pos("","bar") == -1);
|
||||
CHECK(query_val_pos("foo=bar&baz=qux","") == -1);
|
||||
CHECK(query_val_pos("foo=bar&bar=baz","bar") == 12);
|
||||
}
|
||||
|
||||
TEST_CASE("Determine length of value in key/value pair in the query component", "[urlenc][query_val_len]")
|
||||
{
|
||||
CHECK(query_val_len("foo=1&bar=2&baz=3","bar") == 1);
|
||||
CHECK(query_val_len("foo=1&bar=2&baz=3","foo") == 1);
|
||||
CHECK(query_val_len("foo=1&bar=2&baz=3","qux") == -1);
|
||||
CHECK(query_val_len("foo=1&bar=&baz=39","baz") == 2);
|
||||
CHECK(query_val_len("j=10&bar=&baz=3","bar") == 0);
|
||||
CHECK(query_val_len("","") == -1);
|
||||
CHECK(query_val_len("","bar") == -1);
|
||||
CHECK(query_val_len("foo=bar&baz=qux","") == -1);
|
||||
CHECK(query_val_len("foo=bar&bar=baz","bar") == 3);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue