Last active
August 29, 2015 14:25
-
-
Save ork/fef0dcdecbbdfe3c4982 to your computer and use it in GitHub Desktop.
Revisions
-
ork revised this gist
Jul 21, 2015 . 2 changed files with 20 additions and 4 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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, PyObject *keywds) { char *needle = NULL, *haystack = NULL; 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", (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 This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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]) -
ork revised this gist
Jul 21, 2015 . 1 changed file with 35 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -3,7 +3,8 @@ #include <string.h> #include <stdbool.h> 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)); } free(needle); return 0; } #endif -
ork revised this gist
Jul 21, 2015 . 1 changed file with 7 additions and 7 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 *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(&needle, &needle_len, stdin)) != -1) { needle[--read] = '\0'; printf("Needle: '%s'\tHaystack: '%s'\tFound: %d\n", needle, haystack, fuzzy_search(needle, haystack)); } free(needle); return 0; } -
ork created this gist
Jul 21, 2015 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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; }