Skip to content

Commit

Permalink
Remove usage of strcasestr
Browse files Browse the repository at this point in the history
  • Loading branch information
tzvetkoff committed Dec 12, 2023
1 parent 746f134 commit 6c6f8a5
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 10 deletions.
2 changes: 1 addition & 1 deletion configure.ac
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Define the package version numbers, bug reporting link, project homepage.
m4_define([SQIDS_VERSION_STRING], [0.2.0])
m4_define([SQIDS_VERSION_STRING], [0.2.1])
m4_define([SQIDS_BUGREPORT_URL], [https://github.com/sqids/sqids-c/issues])
m4_define([SQIDS_PROJECT_URL], [https://github.com/sqids/sqids-c])

Expand Down
44 changes: 44 additions & 0 deletions script/release.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#!/bin/bash

if [[ -z "${1}" ]]; then
echo "usage: ${0} <version>" >&2
exit 1
fi

# root directory
root="$(cd "$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")/.." && pwd)"

# parse version
version="${1}"
major="${version%%.*}"
minor="${version%.*}"; minor="${minor#*.}"
patch="${version##*.}"

# check configure.ac
configure_version="$(awk '/m4_define.*SQIDS_VERSION_STRING/ { gsub(/[\[\]()]/, "", $2); print($2) }' "${root}/configure.ac")"
if [[ "${configure_version}" != "${version}" ]]; then
echo "version mismatch: check ${root}/configure.ac" >&2
exit 1
fi

# check sqids.h
sqids_version="$(awk '/SQIDS_VERSION_STRING/ { gsub(/"/, "", $3); print($3) }' "${root}/src/sqids.h")"
sqids_major="$(awk '/SQIDS_VERSION_MAJOR/ { print($3); } ' "${root}/src/sqids.h")"
sqids_minor="$(awk '/SQIDS_VERSION_MINOR/ { print($3); } ' "${root}/src/sqids.h")"
sqids_patch="$(awk '/SQIDS_VERSION_PATCH/ { print($3); } ' "${root}/src/sqids.h")"
if [[ "${sqids_version}" != "${version}" ||
"${sqids_major}" != "${major}" ||
"${sqids_minor}" != "${minor}" ||
"${sqids_patch}" != "${patch}" ]]; then
echo "version mismatch: check ${root}/src/sqids.h" >&2
exit 1
fi

# abort if repo is dirty
if [[ -n "$(git status --porcelain 2>/dev/null)" ]]; then
echo "error: git repo is dirty" >&2
exit 1
fi

# proceed to tagging
gh release create "v${version}" --title "v${version}"
21 changes: 14 additions & 7 deletions src/sqids.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
#include "config.h"
#endif

#include <string.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <math.h>

#include "sqids.h"
Expand Down Expand Up @@ -233,21 +233,28 @@ sqids_bl_find(sqids_bl_t *bl, char *s)
int
sqids_bl_match(char *s, char *bad_word)
{
if (strlen(s) < strlen(bad_word)) {
int slen = strlen(s), blen = strlen(bad_word);

if (slen < blen) {
return 0;
}

if (strlen(s) <= 3 || strlen(bad_word) <= 3) {
if (slen <= 3 || blen <= 3) {
return strcasecmp(s, bad_word) == 0;
}

if (strpbrk(bad_word, "0123456789")) {
return strncasecmp(s, bad_word, strlen(bad_word)) == 0 ||
strncasecmp(s + strlen(s) - strlen(bad_word), bad_word,
strlen(bad_word)) == 0;
return strncasecmp(s, bad_word, blen) == 0 ||
strncasecmp(s + slen - blen, bad_word, blen) == 0;
}

return strcasestr(s, bad_word) != NULL;
for (; *s; s++) {
if (strncasecmp(s, bad_word, blen) == 0) {
return 1;
}
}

return 0;
}

/* }}} */
Expand Down
4 changes: 2 additions & 2 deletions src/sqids.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
/* {{{ version information */
/*****************************************************************************/

#define SQIDS_VERSION_STRING "0.2.0"
#define SQIDS_VERSION_STRING "0.2.1"
#define SQIDS_VERSION_MAJOR 0
#define SQIDS_VERSION_MINOR 2
#define SQIDS_VERSION_PATCH 0
#define SQIDS_VERSION_PATCH 1

/* }}} */

Expand Down

0 comments on commit 6c6f8a5

Please sign in to comment.