static PyObject* get_string(PyObject* value, String* string) { PyObject* obj; obj = pymod_get_string( value, &string->chars, &string->length ); return obj; }
static int automaton_contains(PyObject* self, PyObject* args) { #define automaton ((Automaton*)self) ssize_t wordlen; TRIE_LETTER_TYPE* word; PyObject* py_word; py_word = pymod_get_string(args, &word, &wordlen); if (py_word == NULL) return -1; TrieNode* node = trie_find(automaton->root, word, wordlen); Py_DECREF(py_word); return (node and node->eow); #undef automaton }
static PyObject* automaton_items_create(PyObject* self, PyObject* args, const ItemsType type) { #define automaton ((Automaton*)self) PyObject* arg1 = NULL; PyObject* arg2 = NULL; PyObject* arg3 = NULL; TRIE_LETTER_TYPE* word; ssize_t wordlen; TRIE_LETTER_TYPE wildcard; bool use_wildcard = false; PatternMatchType matchtype = MATCH_AT_LEAST_PREFIX; // arg 1: prefix/prefix pattern if (args) arg1 = PyTuple_GetItem(args, 0); else arg1 = NULL; if (arg1) { arg1 = pymod_get_string(arg1, &word, &wordlen); if (arg1 == NULL) goto error; } else { PyErr_Clear(); word = NULL; wordlen = 0; } // arg 2: wildcard if (args) arg2 = PyTuple_GetItem(args, 1); else arg2 = NULL; if (arg2) { TRIE_LETTER_TYPE* tmp; ssize_t len; arg2 = pymod_get_string(arg2, &tmp, &len); if (arg2 == NULL) goto error; else { if (len == 1) { wildcard = tmp[0]; use_wildcard = true; } else { PyErr_SetString(PyExc_ValueError, "wildcard have to be single character"); goto error; } } } else { PyErr_Clear(); wildcard = 0; use_wildcard = false; } // arg3: matchtype matchtype = MATCH_AT_LEAST_PREFIX; if (args) { arg3 = PyTuple_GetItem(args, 2); if (arg3) { Py_ssize_t val = PyNumber_AsSsize_t(arg3, PyExc_OverflowError); if (val == -1 and PyErr_Occurred()) goto error; switch ((PatternMatchType)val) { case MATCH_AT_LEAST_PREFIX: case MATCH_AT_MOST_PREFIX: case MATCH_EXACT_LENGTH: matchtype = (PatternMatchType)val; break; default: PyErr_SetString(PyExc_ValueError, "third argument have to be one of MATCH_EXACT_LENGTH, " "MATCH_AT_LEAST_PREFIX, MATCH_AT_LEAST_PREFIX" ); goto error; } } else { PyErr_Clear(); if (use_wildcard) matchtype = MATCH_EXACT_LENGTH; else matchtype = MATCH_AT_LEAST_PREFIX; } } // AutomatonItemsIter* iter = (AutomatonItemsIter*)automaton_items_iter_new( automaton, word, wordlen, use_wildcard, wildcard, matchtype ); Py_XDECREF(arg1); Py_XDECREF(arg2); if (iter) { iter->type = type; return (PyObject*)iter; } else return NULL; error: Py_XDECREF(arg1); Py_XDECREF(arg2); return NULL; #undef automaton }