Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 35 additions & 19 deletions autoload/fuzzycomt.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,12 @@ int ctrlp_comp_alpha(const void *a, const void *b) {
matchobj_t a_val = *(matchobj_t *)a;
matchobj_t b_val = *(matchobj_t *)b;

char *a_p = PyString_AsString(a_val.str);
long a_len = PyString_Size(a_val.str);
char *b_p = PyString_AsString(b_val.str);
long b_len = PyString_Size(b_val.str);
const char *a_p = PyUnicode_AsUTF8(a_val.str);
Py_ssize_t a_len = 0;
PyUnicode_AsUTF8AndSize(a_val.str, &a_len);
const char *b_p = PyUnicode_AsUTF8(b_val.str);
Py_ssize_t b_len = 0;
PyUnicode_AsUTF8AndSize(b_val.str, &b_len);

int order = 0;
if (a_len > b_len) {
Expand Down Expand Up @@ -236,7 +238,7 @@ PyObject* ctrlp_fuzzycomt_match(PyObject* self, PyObject* args) {
return 0;
}

if (PyString_Check(abbrev) != 1) {
if (PyUnicode_Check(abbrev) != 1) {
PyErr_SetString(PyExc_TypeError,"expected a string");
return 0;
}
Expand All @@ -247,7 +249,9 @@ PyObject* ctrlp_fuzzycomt_match(PyObject* self, PyObject* args) {
limit = PyList_Size(paths);
}

if ( PyString_Size(abbrev) == 0) {
Py_ssize_t len = 0;
PyUnicode_AsUTF8AndSize(abbrev, &len);
if (len == 0) {
// if string is empty - just return first (:param limit) lines
PyObject *initlist;

Expand Down Expand Up @@ -304,7 +308,7 @@ PyObject* ctrlp_fuzzycomt_sorted_match_list(PyObject* self, PyObject* args) {
return 0;
}

if (PyString_Check(abbrev) != 1) {
if (PyUnicode_Check(abbrev) != 1) {
PyErr_SetString(PyExc_TypeError,"expected a string");
return 0;
}
Expand All @@ -315,7 +319,9 @@ PyObject* ctrlp_fuzzycomt_sorted_match_list(PyObject* self, PyObject* args) {
limit = PyList_Size(paths);
}

if ( PyString_Size(abbrev) == 0) {
Py_ssize_t len = 0;
PyUnicode_AsUTF8AndSize(abbrev, &len);
if (len == 0) {
// if string is empty - just return first (:param limit) lines
PyObject *initlist;

Expand Down Expand Up @@ -352,16 +358,16 @@ PyObject* ctrlp_fuzzycomt_sorted_match_list(PyObject* self, PyObject* args) {

matchobj_t ctrlp_find_match(PyObject* str, PyObject* abbrev, char *mmode)
{
long i, max;
size_t i, max;
double score;
matchobj_t returnobj;

// Make a copy of input string to replace all backslashes.
// We need to create a copy because PyString_AsString returns
// We need to create a copy because PyUnicode_AsUTF8 returns
// string that must not be changed.
// We will free() it later
char *temp_string;
temp_string = strduplicate(PyString_AsString(str));
temp_string = strduplicate(PyUnicode_AsUTF8(str));

// Replace all backslashes
for (i = 0; i < strlen(temp_string); i++) {
Expand All @@ -377,11 +383,14 @@ matchobj_t ctrlp_find_match(PyObject* str, PyObject* abbrev, char *mmode)
m.haystack_len = strlen(m.haystack_p);
}
else {
m.haystack_p = temp_string;
m.haystack_len = PyString_Size(str);
m.haystack_p = temp_string;
m.haystack_len = 0;
PyUnicode_AsUTF8AndSize(str, &m.haystack_len);
}
m.needle_p = PyString_AsString(abbrev);
m.needle_len = PyString_Size(abbrev);
m.needle_p = PyUnicode_AsUTF8(abbrev);
m.needle_len = 0;
PyUnicode_AsUTF8AndSize(abbrev, &m.needle_len);

m.max_score_per_char = (1.0 / m.haystack_len + 1.0 / m.needle_len) / 2;
m.dot_file = 0;

Expand All @@ -392,7 +401,7 @@ matchobj_t ctrlp_find_match(PyObject* str, PyObject* abbrev, char *mmode)
if (m.needle_len == 0) {

// filter out dot files
for (i = 0; i < m.haystack_len; i++) {
for (i = 0; i < (size_t) m.haystack_len; i++) {
char c = m.haystack_p[i];
if (c == '.' && (i == 0 || m.haystack_p[i - 1] == '/')) {
score = 0.0;
Expand Down Expand Up @@ -433,8 +442,15 @@ static PyMethodDef fuzzycomt_funcs[] = {
{NULL}
};

PyMODINIT_FUNC initfuzzycomt()
static struct PyModuleDef fuzzycomt_module = {
PyModuleDef_HEAD_INIT,
"fuzzycomt",
"",
-1,
fuzzycomt_funcs,
};

PyMODINIT_FUNC PyInit_fuzzycomt()
{
Py_InitModule3("fuzzycomt", fuzzycomt_funcs,
"Fuzzy matching module");
return PyModule_Create(&fuzzycomt_module);
}
14 changes: 7 additions & 7 deletions autoload/fuzzycomt.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ typedef struct {
} matchobj_t;

typedef struct {
char *haystack_p; // pointer to string to be searched
long haystack_len; // length of same
char *needle_p; // pointer to search string (abbreviation)
long needle_len; // length of same
double max_score_per_char;
int dot_file; // boolean: true if str is a dot-file
double *memo; // memoization
char *haystack_p; // pointer to string to be searched
Py_ssize_t haystack_len; // length of same
const char *needle_p; // pointer to search string (abbreviation)
Py_ssize_t needle_len; // length of same
double max_score_per_char;
int dot_file; // boolean: true if str is a dot-file
double *memo; // memoization
} matchinfo_t;

matchobj_t ctrlp_find_match(PyObject* str, PyObject* abbrev, char *mmode);
Expand Down
4 changes: 2 additions & 2 deletions autoload/matcher.vim
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ else
endif

let s:script_folder_path = escape( expand( '<sfile>:p:h' ), '\' )
python << ImportEOF
python3 << ImportEOF
import sys, os, vim
sys.path.insert( 0, os.path.abspath( vim.eval('s:script_folder_path' ) ) )
import fuzzycomt
Expand All @@ -40,7 +40,7 @@ fu! s:matchfname(item, pat)
endf

fu! s:cmatcher(lines, input, limit, mmode, ispath, crfile)
python << EOF
python3 << EOF
lines = vim.eval('a:lines')
searchinp = vim.eval('a:input')
limit = int(vim.eval('a:limit'))
Expand Down
2 changes: 1 addition & 1 deletion autoload/setup.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from distutils.core import setup, Extension
from setuptools import setup, Extension
import os, platform

if os.name == 'nt' and platform.architecture()[0] == '64bit':
Expand Down
46 changes: 2 additions & 44 deletions install.sh
Original file line number Diff line number Diff line change
@@ -1,47 +1,5 @@
#!/usr/bin/env sh

chkPython2()
{
cmd=$1
ret=$($cmd -V 2>&1)
case "$ret" in
"Python 2."*)
return 0
;;
*)
return 1
;;
esac
}

findPython2()
{
cmd_list="python python2 python27 python2.7 python26 python2.6"
for cmd in $cmd_list; do
if chkPython2 $cmd; then
found_python=$cmd
break
fi
done

if [ "$found_python" = "" ]; then
echo "cannot find python2 automatically" >&2
while true; do
read -p "please input your python 2 command: " cmd
if chkPython2 "$cmd"; then
found_python=$cmd
break
fi
echo "verify [$cmd] with -V failed" >&2
done
fi

echo $found_python
}

python=$(findPython2)
echo "find python2 -> $python"

cd autoload
$python setup.py build
cp build/lib*/fuzzycomt.so .
python setup.py build
cp build/lib*/fuzzycomt*.so fuzzycomt.so
4 changes: 2 additions & 2 deletions install_windows.bat
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
pushd autoload
python setup.py build -c mingw32
pushd build\lib*
xcopy fuzzycomt.pyd ..\..\
copy fuzzycomt*.pyd ..\..\fuzzycomt.pyd
popd
popd
popd