UriParamsIter#
Added in version 2.66.
- class UriParamsIter(*args, **kwargs)#
Many URI schemes include one or more attribute/value pairs as part of the URI
value. For example scheme://server/path?query=string&is=there has two
attributes – query=string and is=there – in its query part.
A UriParamsIter structure represents an iterator that can be used to
iterate over the attribute/value pairs of a URI query string. UriParamsIter
structures are typically allocated on the stack and then initialized with
init(). See the documentation for init()
for a usage example.
Methods#
- class UriParamsIter
- init(params: str, length: int, separators: str, flags: UriParamsFlags) None#
Initializes an attribute/value pair iterator.
The iterator keeps pointers to the
paramsandseparatorsarguments, those variables must thus outlive the iterator and not be modified during the iteration.If
WWW_FORMis passed inflags,+characters in the param string will be replaced with spaces in the output. For example,foo=bar+bazwill give attributefoowith valuebar baz. This is commonly used on the web (thehttpsandhttpschemes only), but is deprecated in favour of the equivalent of encoding spaces as%20.Unlike with
parse_params(),CASE_INSENSITIVEhas no effect if passed toflagsforinit(). The caller is responsible for doing their own case-insensitive comparisons.GUriParamsIter iter; GError *error = NULL; gchar *unowned_attr, *unowned_value; g_uri_params_iter_init (&iter, "foo=bar&baz=bar&Foo=frob&baz=bar2", -1, "&", G_URI_PARAMS_NONE); while (g_uri_params_iter_next (&iter, &unowned_attr, &unowned_value, &error)) { g_autofree gchar *attr = g_steal_pointer (&unowned_attr); g_autofree gchar *value = g_steal_pointer (&unowned_value); // do something with attr and value; this code will be called 4 times // for the params string in this example: once with attr=foo and value=bar, // then with baz/bar, then Foo/frob, then baz/bar2. } if (error) // handle parsing error
Added in version 2.66.
- Parameters:
params – a
%-encoded string containingattribute=valueparameterslength – the length of
params, or-1if it is nul-terminatedseparators – the separator byte character set between parameters. (usually
&, but sometimes;or both&;). Note that this function works on bytes not characters, so it can’t be used to delimit UTF-8 strings for anything but ASCII characters. You may pass an empty set, in which case no splitting will occur.flags – flags to modify the way the parameters are handled.
- next() tuple[bool, str | None, str | None]#
Advances
iterand retrieves the next attribute/value.Falseis returned if an error has occurred (in which caseerroris set), or if the end of the iteration is reached (in which caseattributeandvalueare set toNoneand the iterator becomes invalid). IfTrueis returned,next()may be called again to receive another attribute/value pair.Note that the same
attributemay be returned multiple times, since URIs allow repeated attributes.Added in version 2.66.