Skip to content

Commit 4950d04

Browse files
committed
span() for MatchObject
Whitespace cleanup for Match.h
1 parent 3370ffa commit 4950d04

4 files changed

Lines changed: 45 additions & 13 deletions

File tree

PythonScript/python_tests/tests/ReplaceUTF8PythonFunction.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,5 +130,21 @@ def test_end(self):
130130
text = editor.getText()
131131
self.assertEqual(text, '1 2 3\r\nä4 ü5 ö6\r\n')
132132

133+
def span_check(self, m):
134+
global counter
135+
counter += 1
136+
start1_data_correct = [-1, 0, 7, 15, 23, 31, 40]
137+
start2_data_correct = [-1, 3, 10, 17, 25, 33, 41]
138+
end1_data_correct = [-1, 3, 10, 17, 25, 33, 41]
139+
end2_data_correct = [-1, 6, 14, 19, 28, 37, 43]
140+
self.assertEqual(m.span(1), (start1_data_correct[counter], end1_data_correct[counter]))
141+
self.assertEqual(m.span(2), (start2_data_correct[counter], end2_data_correct[counter]))
142+
self.assertEqual(m.span(), (start1_data_correct[counter], end2_data_correct[counter]))
143+
return counter
144+
145+
def test_span(self):
146+
editor.rereplace(r'([a-z]+)([0-9]+)', lambda m: self.span_check(m))
147+
text = editor.getText()
148+
self.assertEqual(text, '1 2 3\r\nä4 ü5 ö6\r\n')
133149

134150
suite = unittest.TestLoader().loadTestsFromTestCase(ReplaceUTF8PythonFunctionTestCase)

PythonScript/src/Match.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,15 @@ int Match::py_end_name(boost::python::str groupName)
109109
return result;
110110
}
111111

112+
boost::python::tuple Match::py_span(int groupIndex)
113+
{
114+
return boost::python::make_tuple(py_start(groupIndex), py_end(groupIndex));
115+
}
116+
117+
boost::python::tuple Match::py_span_name(boost::python::str groupName)
118+
{
119+
return boost::python::make_tuple(py_start_name(groupName), py_end_name(groupName));
120+
}
112121

113122
boost::python::tuple Match::py_groups()
114123
{

PythonScript/src/Match.h

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,19 @@
33

44
namespace NppPythonScript
55
{
6-
7-
class GroupDetail
8-
{
9-
public:
6+
7+
class GroupDetail
8+
{
9+
public:
1010
virtual int start() const = 0;
1111
virtual int end() const = 0;
1212
virtual bool matched() const = 0;
13-
14-
};
13+
14+
};
1515

1616
class Match
17-
{
18-
public:
17+
{
18+
public:
1919
virtual int groupCount() = 0;
2020
virtual GroupDetail* group(int groupNumber) = 0;
2121
virtual GroupDetail* groupName(const char *groupName) = 0;
@@ -27,24 +27,28 @@ namespace NppPythonScript
2727
boost::python::str py_group_name(boost::python::str groupName);
2828
boost::python::str py_expand(boost::python::object replaceFormat);
2929
boost::python::tuple py_groups();
30-
int py_start_group_0() { return py_start(0); }
30+
int py_start_group_0() { return py_start(0); }
3131
int py_start(int group);
3232
int py_start_name(boost::python::str groupName);
3333

34-
int py_end_group_0() { return py_end(0); }
34+
int py_end_group_0() { return py_end(0); }
3535
int py_end(int group);
3636
int py_end_name(boost::python::str groupName);
3737

38+
boost::python::tuple py_span_group_0() { return py_span(0); }
39+
boost::python::tuple py_span(int groupIndex);
40+
boost::python::tuple py_span_name(boost::python::str groupName);
41+
3842
boost::python::dict py_groupdict();
3943

4044
boost::python::tuple py_group_tuple2(boost::python::object group1, boost::python::object group2);
4145
boost::python::tuple py_group_tuple3(boost::python::object group1, boost::python::object group2, boost::python::object group3);
4246
boost::python::tuple py_group_tuple4(boost::python::object group1, boost::python::object group2, boost::python::object group3, boost::python::object group4);
4347

44-
private:
48+
private:
4549
boost::python::str getGroup(boost::python::object groupIdentifier);
4650

47-
};
51+
};
4852

4953

5054

PythonScript/src/MatchPython.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@ void export_match()
2323
.def("end", &NppPythonScript::Match::py_end, boost::python::args("group"), "Returns the start position of the given group.")
2424
.def("end", &NppPythonScript::Match::py_end_name, boost::python::args("group"), "Returns the start position of the given group.")
2525
.def("end", &NppPythonScript::Match::py_end_group_0, "Returns the start position of group 0 (the whole match).")
26-
// TODO: groupdict, start, end, span, pos, endpos, lastindex, lastgroup, re (throw exception?), string (throw exception?)
26+
.def("span", &NppPythonScript::Match::py_span_group_0, "Returns a tuple (m.start(group), m.end(group)). Group defaults to 0 (the whole match)")
27+
.def("span", &NppPythonScript::Match::py_span, boost::python::args("group"), "Returns a tuple (m.start(group), m.end(group)). Group defaults to 0 (the whole match)")
28+
.def("span", &NppPythonScript::Match::py_span_name, boost::python::args("group"), "Returns a tuple (m.start(group), m.end(group)). Group defaults to 0 (the whole match)")
29+
// TODO: groupdict, span, pos, endpos, lastindex, lastgroup, re (throw exception?), string (throw exception?)
2730
// See: http://docs.python.org/2/library/re.html#match-objects
2831
;
2932
//lint +e1793

0 commit comments

Comments
 (0)