Skip to content

Instantly share code, notes, and snippets.

@ork
Last active August 29, 2015 14:25
Show Gist options
  • Select an option

  • Save ork/fef0dcdecbbdfe3c4982 to your computer and use it in GitHub Desktop.

Select an option

Save ork/fef0dcdecbbdfe3c4982 to your computer and use it in GitHub Desktop.

Revisions

  1. ork revised this gist Jul 21, 2015. 2 changed files with 20 additions and 4 deletions.
    14 changes: 10 additions & 4 deletions fuzzy_search.c
    Original file line number Diff line number Diff line change
    @@ -36,21 +36,27 @@ _fuzzy_search(const char *needle, const char *haystack)
    #include <Python.h>

    static PyObject *
    fuzzy_search(PyObject* self, PyObject* args)
    fuzzy_search(PyObject* self, PyObject* args, PyObject *keywds)
    {
    char *needle = NULL,
    *haystack = NULL;

    if (!PyArg_ParseTuple(args, "ss", &needle, &haystack)) {
    static char *kwlist[] = {"needle", "haystack", NULL};

    if (!PyArg_ParseTupleAndKeywords(args, keywds, "ss", kwlist, &needle, &haystack)) {
    return NULL;
    }

    return PyBool_FromLong(_fuzzy_search(needle, haystack));
    }

    PyDoc_STRVAR(fuzzy_search_doc,
    "Fuzzy string search.\n"
    "Arguments: (needle, haystack)\n");

    static PyMethodDef FuzzySearchMethods[] =
    {
    {"fuzzy_search", fuzzy_search, METH_VARARGS, "Fuzzy string search." },
    {"fuzzy_search", (PyCFunction)fuzzy_search, METH_VARARGS | METH_KEYWORDS, fuzzy_search_doc},
    {NULL, NULL, 0, NULL}
    };

    @@ -84,4 +90,4 @@ int main(int argc, char *argv[])

    return 0;
    }
    #endif
    #endif
    10 changes: 10 additions & 0 deletions setup.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,10 @@
    from distutils.core import setup, Extension

    module1 = Extension(name='fuzzy_search',
    sources=['fuzzy_search.c'],
    define_macros=[('PY_MOD', '1')])

    setup(name='FuzzySearch',
    version='1.0',
    description='Fuzzy string search',
    ext_modules=[module1])
  2. ork revised this gist Jul 21, 2015. 1 changed file with 35 additions and 3 deletions.
    38 changes: 35 additions & 3 deletions fuzzy_search.c
    Original file line number Diff line number Diff line change
    @@ -3,7 +3,8 @@
    #include <string.h>
    #include <stdbool.h>

    bool fuzzy_search(const char *needle, const char *haystack)
    static bool
    _fuzzy_search(const char *needle, const char *haystack)
    {
    size_t hlen = strlen(haystack);
    size_t nlen = strlen(needle);
    @@ -31,6 +32,36 @@ bool fuzzy_search(const char *needle, const char *haystack)
    return true;
    }

    #ifdef PY_MOD
    #include <Python.h>

    static PyObject *
    fuzzy_search(PyObject* self, PyObject* args)
    {
    char *needle = NULL,
    *haystack = NULL;

    if (!PyArg_ParseTuple(args, "ss", &needle, &haystack)) {
    return NULL;
    }

    return PyBool_FromLong(_fuzzy_search(needle, haystack));
    }

    static PyMethodDef FuzzySearchMethods[] =
    {
    {"fuzzy_search", fuzzy_search, METH_VARARGS, "Fuzzy string search." },
    {NULL, NULL, 0, NULL}
    };

    PyMODINIT_FUNC
    initfuzzy_search(void)
    {
    (void) Py_InitModule("fuzzy_search", FuzzySearchMethods);
    }

    #else

    int main(int argc, char *argv[])
    {
    char *needle = NULL,
    @@ -47,9 +78,10 @@ int main(int argc, char *argv[])
    while ((read = getline(&needle, &needle_len, stdin)) != -1) {
    needle[--read] = '\0';
    printf("Needle: '%s'\tHaystack: '%s'\tFound: %d\n",
    needle, haystack, fuzzy_search(needle, haystack));
    needle, haystack, _fuzzy_search(needle, haystack));
    }
    free(needle);

    return 0;
    }
    }
    #endif
  3. ork revised this gist Jul 21, 2015. 1 changed file with 7 additions and 7 deletions.
    14 changes: 7 additions & 7 deletions fuzzy_search.c
    Original file line number Diff line number Diff line change
    @@ -33,9 +33,9 @@ bool fuzzy_search(const char *needle, const char *haystack)

    int main(int argc, char *argv[])
    {
    char *line = NULL,
    *haystack = NULL;
    size_t line_len = 0;
    char *needle = NULL,
    *haystack = NULL;
    size_t needle_len = 0;
    ssize_t read;

    if (argc < 2) {
    @@ -44,12 +44,12 @@ int main(int argc, char *argv[])
    }
    haystack = argv[1];

    while ((read = getline(&line, &line_len, stdin)) != -1) {
    line[--read] = '\0';
    while ((read = getline(&needle, &needle_len, stdin)) != -1) {
    needle[--read] = '\0';
    printf("Needle: '%s'\tHaystack: '%s'\tFound: %d\n",
    line, haystack, fuzzy_search(line, haystack));
    needle, haystack, fuzzy_search(needle, haystack));
    }
    free(line);
    free(needle);

    return 0;
    }
  4. ork created this gist Jul 21, 2015.
    55 changes: 55 additions & 0 deletions fuzzy_search.c
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,55 @@
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <stdbool.h>

    bool fuzzy_search(const char *needle, const char *haystack)
    {
    size_t hlen = strlen(haystack);
    size_t nlen = strlen(needle);

    if (nlen > hlen) {
    return false;
    }
    if (nlen == hlen) {
    return strcmp(needle, haystack) == 0;
    }

    for (size_t i = 0, j = 0; i < nlen; ++i) {
    char nch = needle[i];

    while (j < hlen) {
    if (haystack[j++] == nch) {
    goto resume;
    }
    }
    return false;

    resume: ;
    }

    return true;
    }

    int main(int argc, char *argv[])
    {
    char *line = NULL,
    *haystack = NULL;
    size_t line_len = 0;
    ssize_t read;

    if (argc < 2) {
    fprintf(stderr, "Usage: %s <HAYSTACK>\n", argv[0]);
    return 1;
    }
    haystack = argv[1];

    while ((read = getline(&line, &line_len, stdin)) != -1) {
    line[--read] = '\0';
    printf("Needle: '%s'\tHaystack: '%s'\tFound: %d\n",
    line, haystack, fuzzy_search(line, haystack));
    }
    free(line);

    return 0;
    }