Skip to content
This repository was archived by the owner on Oct 2, 2023. It is now read-only.

Commit e739e22

Browse files
Format new code for WordListRule.
1 parent 3810d94 commit e739e22

File tree

2 files changed

+130
-120
lines changed

2 files changed

+130
-120
lines changed

lib/wordlist_rule.cpp

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,21 @@ namespace OS2DSRules {
88

99
namespace WordListRule {
1010

11-
void WordListRule::check_match(MatchResults &results, const std::string candidate, const std::size_t start, const std::size_t stop) const noexcept {
11+
void WordListRule::check_match(MatchResults &results,
12+
const std::string candidate,
13+
const std::size_t start,
14+
const std::size_t stop) const noexcept {
1215
if (contains(candidate)) {
1316
results.push_back(MatchResult(candidate, start, stop));
1417
}
1518
}
1619

17-
[[nodiscard]] MatchResults WordListRule::find_matches(const std::string &content) const noexcept {
20+
[[nodiscard]] MatchResults
21+
WordListRule::find_matches(const std::string &content) const noexcept {
1822
MatchResults results;
1923

20-
static const auto is_delimiter = make_predicate(' ', '\n', '.', ',', '\t', '!', '?');
24+
static const auto is_delimiter =
25+
make_predicate(' ', '\n', '.', ',', '\t', '!', '?');
2126

2227
std::size_t start = 0;
2328
for (std::size_t i = 0; i < content.size(); ++i) {
@@ -32,17 +37,19 @@ void WordListRule::check_match(MatchResults &results, const std::string candidat
3237
return results;
3338
}
3439

35-
36-
[[nodiscard]] bool WordListRule::contains(const std::string_view target) const noexcept {
40+
[[nodiscard]] bool
41+
WordListRule::contains(const std::string_view target) const noexcept {
3742
return words_.contains(target);
3843
}
3944

40-
[[nodiscard]] bool WordListRule::contains(const std::string target) const noexcept {
45+
[[nodiscard]] bool
46+
WordListRule::contains(const std::string target) const noexcept {
4147
return contains(std::string_view(target.cbegin(), target.cend()));
4248
}
4349

44-
[[nodiscard]] bool WordListRule::contains(const std::string::const_iterator start,
45-
const std::string::const_iterator stop) const noexcept {
50+
[[nodiscard]] bool
51+
WordListRule::contains(const std::string::const_iterator start,
52+
const std::string::const_iterator stop) const noexcept {
4653
return contains(std::string_view(start, stop));
4754
}
4855

src/os2ds_rules/wordlist_rule.cpp

Lines changed: 115 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
#include <Python.h>
33

44
#include <cstddef>
5-
#include <wordlist_rule.hpp>
65
#include <string>
76
#include <string_view>
87
#include <vector>
8+
#include <wordlist_rule.hpp>
99

1010
using namespace OS2DSRules::WordListRule;
1111

@@ -14,145 +14,148 @@ extern "C" {
1414
#endif
1515

1616
typedef struct {
17-
PyObject_HEAD
18-
WordListRule *rule;
17+
PyObject_HEAD WordListRule *rule;
1918
} PyWordListRule;
2019

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+
}
2625

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+
}
3434

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;
3839

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);
4248

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))));
5152
}
52-
53-
self->rule = new WordListRule(words_v.begin(), words_v.end());
5453
}
5554

56-
return 0;
55+
self->rule = new WordListRule(words_v.begin(), words_v.end());
5756
}
5857

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;
6164

62-
if (!PyArg_ParseTuple(args, "s", &content))
63-
return NULL;
65+
if (!PyArg_ParseTuple(args, "s", &content))
66+
return NULL;
6467

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);
6770

68-
Py_ssize_t len = Py_ssize_t(results.size());
71+
Py_ssize_t len = Py_ssize_t(results.size());
6972

70-
PyObject *list_of_results = PyList_New(len);
73+
PyObject *list_of_results = PyList_New(len);
7174

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(
7578
"{s:s, s:i, s:i, s:d}", "match", res.match().c_str(), "start",
7679
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);
8181
}
8282

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,
8588
"Find matches in a text."},
8689
{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 = {
131133
PyModuleDef_HEAD_INIT,
132134
"wordlist_rule",
133135
NULL,
134136
-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;
154154
}
155155

156+
return m;
157+
}
158+
156159
#ifdef __cplusplus
157160
}
158161
#endif

0 commit comments

Comments
 (0)