[PATCH 3/7] gl-renderer: Implement & use check_extension

Bill Spitzak spitzak at gmail.com
Tue Mar 22 01:30:56 UTC 2016


On Mon, Mar 21, 2016 at 9:37 AM, Miguel A. Vico <mvicomoya at nvidia.com>
wrote:

> Using strstr(3) for checking for extensions is an error-prone mechanism
> as extension names can be prefixes of other extension names (see
> https://www.opengl.org/registry/doc/rules.html#using).
>
> This change implements the check_extension() function to properly check
> for an extension and replaces all usages of strstr(3).
>
> Signed-off-by: Miguel A Vico Moya <mvicomoya at nvidia.com>
> Reviewed-by: Andy Ritger <aritger at nvidia.com>
> ---
>  src/gl-renderer.c | 56
> +++++++++++++++++++++++++++++++++++++++++--------------
>  1 file changed, 42 insertions(+), 14 deletions(-)
>
> diff --git a/src/gl-renderer.c b/src/gl-renderer.c
> index 1d6d98c..3ca1aed 100644
> --- a/src/gl-renderer.c
> +++ b/src/gl-renderer.c
> @@ -2701,6 +2701,34 @@ gl_renderer_destroy(struct weston_compositor *ec)
>         free(gr);
>  }
>
> +static
> +bool check_extension(const char *extensions, const char *extension)
> +{
> +       size_t extlen = strlen(extension);
> +       const char *end = extensions + strlen(extensions);
> +
> +       while (extensions < end) {
> +               size_t n = 0;
> +
> +               /* Skip whitespaces, if any */
> +               if (*extensions == ' ') {
> +                       extensions++;
> +                       continue;
> +               }
> +
> +               n = strcspn(extensions, " ");
> +
> +               /* Compare strings */
> +               if (n == extlen && strncmp(extension, extensions, n) == 0)
> +                       return true; /* Found */
> +
> +               extensions += n;
> +       }
> +
> +       /* Not found */
> +       return false;
> +}
> +
>

Slightly faster version that avoids some temporaries and strlen calls:

static
bool check_extension(const char *extensions, const char *extension)
{
while (*extensions) {
size_t n = strcspn(extensions, " ");

/* Skip whitespaces, if any */
if (n < 1) {
extensions++;
continue;
}

/* Compare strings */
if (strncmp(extension, extensions, n) == 0 && !extension[n])
return true; /* Found */

extensions += n;
}

/* Not found */
return false;
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.freedesktop.org/archives/wayland-devel/attachments/20160321/15bcd604/attachment-0001.html>


More information about the wayland-devel mailing list