Skip to content
Permalink
Browse files

vendor: update vendor.{h,c}

Mostly new match rules.

Signed-off-by: Burt P <pburt0@gmail.com>
  • Loading branch information...
bp0 authored and lpereira committed Aug 24, 2019
1 parent a08f2a9 commit 348d6d919c7d128cd6300e9a7f02483850edd079
Showing with 194 additions and 103 deletions.
  1. +8 −3 deps/sysobj_early/include/strstr_word.h
  2. +36 −20 deps/sysobj_early/src/strstr_word.c
  3. +128 −78 hardinfo/vendor.c
  4. +22 −2 includes/vendor.h
@@ -18,13 +18,18 @@
*
*/

/* versions of strstr() and strcasestr() where the match must be preceded and
* succeded by a non-alpha-numeric character. */

#ifndef __STRSTR_WORD_H__
#define __STRSTR_WORD_H__

/* versions of strstr() and strcasestr() where the match must be preceded and
* succeded by a non-alpha-numeric character. */
char *strstr_word(const char *haystack, const char *needle);
char *strcasestr_word(const char *haystack, const char *needle);

/* word boundary at start only (prefix), or end only (suffix) */
char *strstr_word_prefix(const char *haystack, const char *needle);
char *strcasestr_word_prefix(const char *haystack, const char *needle);
char *strstr_word_suffix(const char *haystack, const char *needle);
char *strcasestr_word_suffix(const char *haystack, const char *needle);

#endif
@@ -25,40 +25,56 @@
#include <string.h>
#include <ctype.h>

char *strstr_word(const char *haystack, const char *needle) {
static char *_strstr(const char *haystack, const char *needle, int anycase) {
return anycase
? strcasestr(haystack, needle)
: strstr(haystack, needle);
}

static char *_strstr_word(const char *haystack, const char *needle,
int anycase, int prefix_ok, int suffix_ok) {

if (!haystack || !needle)
return NULL;

char *c;
const char *p = haystack;
size_t l = strlen(needle);
while(c = strstr(p, needle)) {
while((c = _strstr(p, needle, anycase))) {
const char *before = (c == haystack) ? NULL : c-1;
const char *after = c + l;
int ok = 1;
if (isalnum(*after)) ok = 0;
if (before && isalnum(*before)) ok = 0;
int ok = 1, wbs = 1, wbe = 1;
if (isalnum(*after)) wbe = 0;
if (before && isalnum(*before)) wbs = 0;
if (!wbe && !prefix_ok) ok = 0;
if (!wbs && !suffix_ok) ok = 0;
if (!(wbs || wbe)) ok = 0;
if (ok) return c;
p++;
}
return NULL;
}

char *strstr_word(const char *haystack, const char *needle) {
return _strstr_word(haystack, needle, 0, 0, 0);
}

char *strcasestr_word(const char *haystack, const char *needle) {
if (!haystack || !needle)
return NULL;
return _strstr_word(haystack, needle, 1, 0, 0);
}

char *c;
const char *p = haystack;
size_t l = strlen(needle);
while(c = strcasestr(p, needle)) {
const char *before = (c == haystack) ? NULL : c-1;
const char *after = c + l;
int ok = 1;
if (isalnum(*after)) ok = 0;
if (before && isalnum(*before)) ok = 0;
if (ok) return c;
p++;
}
return NULL;
char *strstr_word_prefix(const char *haystack, const char *needle) {
return _strstr_word(haystack, needle, 0, 1, 0);
}

char *strcasestr_word_prefix(const char *haystack, const char *needle) {
return _strstr_word(haystack, needle, 1, 1, 0);
}

char *strstr_word_suffix(const char *haystack, const char *needle) {
return _strstr_word(haystack, needle, 0, 0, 1);
}

char *strcasestr_word_suffix(const char *haystack, const char *needle) {
return _strstr_word(haystack, needle, 1, 0, 1);
}

0 comments on commit 348d6d9

Please sign in to comment.
You can’t perform that action at this time.