2
2
#include < Python.h>
3
3
4
4
#include < cstddef>
5
- #include < wordlist_rule.hpp>
6
5
#include < string>
7
6
#include < string_view>
8
7
#include < vector>
8
+ #include < wordlist_rule.hpp>
9
9
10
10
using namespace OS2DSRules ::WordListRule;
11
11
@@ -14,145 +14,148 @@ extern "C" {
14
14
#endif
15
15
16
16
typedef struct {
17
- PyObject_HEAD
18
- WordListRule *rule;
17
+ PyObject_HEAD WordListRule *rule;
19
18
} PyWordListRule;
20
19
21
- static void PyWordListRule_dealloc (PyWordListRule *self) {
22
- if (self->rule )
23
- delete self->rule ;
24
- Py_TYPE (self)->tp_free ((PyObject *) self);
25
- }
20
+ static void PyWordListRule_dealloc (PyWordListRule *self) {
21
+ if (self->rule )
22
+ delete self->rule ;
23
+ Py_TYPE (self)->tp_free ((PyObject *)self);
24
+ }
26
25
27
- static PyObject *PyWordListRule_new (PyTypeObject *type, PyObject *args, PyObject *kwds) {
28
- PyWordListRule *self;
29
- self = (PyWordListRule *) type->tp_alloc (type, 0 );
30
- if (self != NULL )
31
- self->rule = nullptr ;
32
- return (PyObject *) self;
33
- }
26
+ static PyObject *PyWordListRule_new (PyTypeObject *type, PyObject *args,
27
+ PyObject *kwds) {
28
+ PyWordListRule *self;
29
+ self = (PyWordListRule *)type->tp_alloc (type, 0 );
30
+ if (self != NULL )
31
+ self->rule = nullptr ;
32
+ return (PyObject *)self;
33
+ }
34
34
35
- static int PyWordListRule_init (PyWordListRule *self, PyObject *args, PyObject *kwds) {
36
- static char *kwlist[] = {" words" , NULL };
37
- PyObject *words = NULL ;
35
+ static int PyWordListRule_init (PyWordListRule *self, PyObject *args,
36
+ PyObject *kwds) {
37
+ static char *kwlist[] = {" words" , NULL };
38
+ PyObject *words = NULL ;
38
39
39
- if (!PyArg_ParseTupleAndKeywords (args, kwds, " O" , kwlist, &words)) {
40
- return -1 ;
41
- }
40
+ if (!PyArg_ParseTupleAndKeywords (args, kwds, " O" , kwlist, &words)) {
41
+ return -1 ;
42
+ }
43
+
44
+ if (words && PyList_Check (words)) {
45
+ std::vector<std::string_view> words_v;
46
+ for (Py_ssize_t i = 0 ; i < PyList_Size (words); ++i) {
47
+ PyObject *py_string = PyList_GetItem (words, i);
42
48
43
- if (words && PyList_Check (words)) {
44
- std::vector<std::string_view> words_v;
45
- for (Py_ssize_t i = 0 ; i < PyList_Size (words); ++i) {
46
- PyObject *py_string = PyList_GetItem (words, i);
47
-
48
- if (PyUnicode_Check (py_string)) {
49
- words_v.push_back (std::string_view (PyBytes_AsString (PyUnicode_AsUTF8String (py_string))));
50
- }
49
+ if (PyUnicode_Check (py_string)) {
50
+ words_v.push_back (std::string_view (
51
+ PyBytes_AsString (PyUnicode_AsUTF8String (py_string))));
51
52
}
52
-
53
- self->rule = new WordListRule (words_v.begin (), words_v.end ());
54
53
}
55
54
56
- return 0 ;
55
+ self-> rule = new WordListRule (words_v. begin (), words_v. end ()) ;
57
56
}
58
57
59
- static PyObject *PyWordListRule_find_matches (PyWordListRule *self, PyObject *args) {
60
- const char *content;
58
+ return 0 ;
59
+ }
60
+
61
+ static PyObject *PyWordListRule_find_matches (PyWordListRule *self,
62
+ PyObject *args) {
63
+ const char *content;
61
64
62
- if (!PyArg_ParseTuple (args, " s" , &content))
63
- return NULL ;
65
+ if (!PyArg_ParseTuple (args, " s" , &content))
66
+ return NULL ;
64
67
65
- std::string text (content);
66
- auto results = self->rule ->find_matches (text);
68
+ std::string text (content);
69
+ auto results = self->rule ->find_matches (text);
67
70
68
- Py_ssize_t len = Py_ssize_t (results.size ());
71
+ Py_ssize_t len = Py_ssize_t (results.size ());
69
72
70
- PyObject *list_of_results = PyList_New (len);
73
+ PyObject *list_of_results = PyList_New (len);
71
74
72
- for (Py_ssize_t i = 0 ; i < len; ++i) {
73
- auto res = results[i];
74
- PyObject *obj = Py_BuildValue (
75
+ for (Py_ssize_t i = 0 ; i < len; ++i) {
76
+ auto res = results[i];
77
+ PyObject *obj = Py_BuildValue (
75
78
" {s:s, s:i, s:i, s:d}" , " match" , res.match ().c_str (), " start" ,
76
79
res.start (), " end" , res.end (), " probability" , res.probability ());
77
- PyList_SetItem (list_of_results, i, obj);
78
- }
79
-
80
- return list_of_results;
80
+ PyList_SetItem (list_of_results, i, obj);
81
81
}
82
82
83
- static PyMethodDef PyWordListRule_methods[] = {
84
- {" find_matches" , (PyCFunction) PyWordListRule_find_matches, METH_VARARGS,
83
+ return list_of_results;
84
+ }
85
+
86
+ static PyMethodDef PyWordListRule_methods[] = {
87
+ {" find_matches" , (PyCFunction)PyWordListRule_find_matches, METH_VARARGS,
85
88
" Find matches in a text." },
86
89
{NULL } /* Sentinel */
87
- };
88
-
89
- static PyTypeObject PyWordListRuleType = {
90
- PyVarObject_HEAD_INIT (NULL , 0 )
91
- " wordlist_rule.WordListRule" , /* tp_name */
92
- sizeof (PyWordListRule), /* tp_basicsize */
93
- 0 , /* tp_itemsize */
94
- (destructor)PyWordListRule_dealloc, /* tp_dealloc */
95
- 0 , /* tp_vectorcall_offset */
96
- 0 , /* tp_getattr */
97
- 0 , /* tp_setattr */
98
- 0 , /* tp_as_async */
99
- 0 , /* tp_repr */
100
- 0 , /* tp_as_number */
101
- 0 , /* tp_as_sequence */
102
- 0 , /* tp_as_mapping */
103
- 0 , /* tp_hash */
104
- 0 , /* tp_call */
105
- 0 , /* tp_str */
106
- 0 , /* tp_getattro */
107
- 0 , /* tp_setattro */
108
- 0 , /* tp_as_buffer */
109
- 0 , /* tp_flags */
110
- PyDoc_STR (" WordListRule bindings" ), /* tp_doc */
111
- 0 , /* tp_traverse */
112
- 0 , /* tp_clear */
113
- 0 , /* tp_richcompare */
114
- 0 , /* tp_weaklistoffset */
115
- 0 , /* tp_iter */
116
- 0 , /* tp_iternext */
117
- PyWordListRule_methods, /* tp_methods */
118
- 0 , /* tp_members */
119
- 0 , /* tp_getset */
120
- 0 , /* tp_base */
121
- 0 , /* tp_dict */
122
- 0 , /* tp_descr_get */
123
- 0 , /* tp_descr_set */
124
- 0 , /* tp_dictoffset */
125
- (initproc) PyWordListRule_init, /* tp_init */
126
- 0 , /* tp_alloc */
127
- PyWordListRule_new, /* tp_new */
128
- };
129
-
130
- static PyModuleDef wordlistrulemodule = {
90
+ };
91
+
92
+ static PyTypeObject PyWordListRuleType = {
93
+ PyVarObject_HEAD_INIT (NULL , 0 ) " wordlist_rule.WordListRule" , /* tp_name */
94
+ sizeof (PyWordListRule), /* tp_basicsize */
95
+ 0 , /* tp_itemsize */
96
+ (destructor)PyWordListRule_dealloc, /* tp_dealloc */
97
+ 0 , /* tp_vectorcall_offset */
98
+ 0 , /* tp_getattr */
99
+ 0 , /* tp_setattr */
100
+ 0 , /* tp_as_async */
101
+ 0 , /* tp_repr */
102
+ 0 , /* tp_as_number */
103
+ 0 , /* tp_as_sequence */
104
+ 0 , /* tp_as_mapping */
105
+ 0 , /* tp_hash */
106
+ 0 , /* tp_call */
107
+ 0 , /* tp_str */
108
+ 0 , /* tp_getattro */
109
+ 0 , /* tp_setattro */
110
+ 0 , /* tp_as_buffer */
111
+ 0 , /* tp_flags */
112
+ PyDoc_STR (" WordListRule bindings" ), /* tp_doc */
113
+ 0 , /* tp_traverse */
114
+ 0 , /* tp_clear */
115
+ 0 , /* tp_richcompare */
116
+ 0 , /* tp_weaklistoffset */
117
+ 0 , /* tp_iter */
118
+ 0 , /* tp_iternext */
119
+ PyWordListRule_methods, /* tp_methods */
120
+ 0 , /* tp_members */
121
+ 0 , /* tp_getset */
122
+ 0 , /* tp_base */
123
+ 0 , /* tp_dict */
124
+ 0 , /* tp_descr_get */
125
+ 0 , /* tp_descr_set */
126
+ 0 , /* tp_dictoffset */
127
+ (initproc)PyWordListRule_init, /* tp_init */
128
+ 0 , /* tp_alloc */
129
+ PyWordListRule_new, /* tp_new */
130
+ };
131
+
132
+ static PyModuleDef wordlistrulemodule = {
131
133
PyModuleDef_HEAD_INIT,
132
134
" wordlist_rule" ,
133
135
NULL ,
134
136
-1 ,
135
- };
136
-
137
- PyMODINIT_FUNC PyInit_wordlist_rule (void ) {
138
- PyObject *m;
139
- if (PyType_Ready (&PyWordListRuleType) < 0 )
140
- return NULL ;
141
-
142
- m = PyModule_Create (&wordlistrulemodule);
143
- if (m == NULL )
144
- return NULL ;
145
-
146
- Py_INCREF (&PyWordListRuleType);
147
- if (PyModule_AddObject (m, " WordListRule" , (PyObject *) &PyWordListRuleType) < 0 ) {
148
- Py_DECREF (&PyWordListRuleType);
149
- Py_DECREF (m);
150
- return NULL ;
151
- }
152
-
153
- return m;
137
+ };
138
+
139
+ PyMODINIT_FUNC PyInit_wordlist_rule (void ) {
140
+ PyObject *m;
141
+ if (PyType_Ready (&PyWordListRuleType) < 0 )
142
+ return NULL ;
143
+
144
+ m = PyModule_Create (&wordlistrulemodule);
145
+ if (m == NULL )
146
+ return NULL ;
147
+
148
+ Py_INCREF (&PyWordListRuleType);
149
+ if (PyModule_AddObject (m, " WordListRule" , (PyObject *)&PyWordListRuleType) <
150
+ 0 ) {
151
+ Py_DECREF (&PyWordListRuleType);
152
+ Py_DECREF (m);
153
+ return NULL ;
154
154
}
155
155
156
+ return m;
157
+ }
158
+
156
159
#ifdef __cplusplus
157
160
}
158
161
#endif
0 commit comments