@@ -19,145 +19,7 @@ using UtfConversion::toStringType;
1919typedef boost::basic_regex<U32 , u32_regex_traits> u32_regex;
2020typedef boost::regex_iterator<UTF8Iterator, U32 , u32_regex_traits> u32_regex_iterator;
2121
22- class BoostRegexGroupDetail : public GroupDetail
23- {
24- public:
25- BoostRegexGroupDetail (const boost::sub_match<UTF8Iterator>& subMatch)
26- : m_subMatch(subMatch)
27- {}
28- int start () const { return m_subMatch.first .pos (); }
29- int end () const { return m_subMatch.second .pos (); }
30-
31- private:
32- boost::sub_match<UTF8Iterator> m_subMatch;
33- };
34-
35-
36- class BoostRegexMatch : public Match
37- {
38- public:
39- BoostRegexMatch (const char *text, boost::match_results<UTF8Iterator>* match)
40- : m_text(text),
41- m_match (match)
42- {}
43-
44- BoostRegexMatch (const char *text)
45- : m_text(text),
46- m_match(NULL )
47- {}
48-
49- virtual ~BoostRegexMatch ();
50- BoostRegexMatch& operator = (BoostRegexMatch& rhs) {
51- m_text = rhs.m_text ;
52- m_match = rhs.m_match ;
53-
54- /* We explicitely don't copy the list, as the allocatedGroupDetails will simply be destructed when this object gets destroyed.
55- * In theory, this would be bad, as we would delete the allocated GroupDetail objects when this object is deleted,
56- * even though the various groups may still be in use.
57- * In practice however, these GroupDetails don't actually live as long as this object, as we've created a ReplaceEntry
58- * by the time this object gets destroyed, and have no need for the allocated GroupDetails any more.
59- */
60- }
61-
62- void setMatchResults (boost::match_results<UTF8Iterator>* match) { m_match = match; }
63-
64- virtual int groupCount () { return m_match->size (); }
65-
66- virtual GroupDetail* group (int groupNo);
67- virtual GroupDetail* groupName (const char *groupName);
68- virtual void expand (const char * format, char **result, int *resultLength);
69-
70- private:
71- const char *m_text;
72- boost::match_results<UTF8Iterator>* m_match;
73- std::list<BoostRegexGroupDetail*> m_allocatedGroupDetails;
74-
75- static void deleteEntry (BoostRegexGroupDetail*);
76- };
77-
78- void BoostRegexMatch::deleteEntry (BoostRegexGroupDetail* entry)
79- {
80- delete entry;
81- }
82-
83- BoostRegexMatch::~BoostRegexMatch ()
84- {
85- for_each (m_allocatedGroupDetails.begin (), m_allocatedGroupDetails.end (), deleteEntry);
86- }
87-
88- GroupDetail* BoostRegexMatch::group (int groupNo)
89- {
90- BoostRegexGroupDetail* groupDetail = new BoostRegexGroupDetail ((*m_match)[groupNo]);
91- m_allocatedGroupDetails.push_back (groupDetail);
92- return groupDetail;
93- }
94-
95- GroupDetail* BoostRegexMatch::groupName (const char *groupName)
96- {
97- u32string groupNameU32 = toStringType<u32string>(ConstString<char >(groupName));
98- BoostRegexGroupDetail* groupDetail = new BoostRegexGroupDetail ((*m_match)[groupNameU32.c_str ()]);
99- m_allocatedGroupDetails.push_back (groupDetail);
100- return groupDetail;
101- }
102-
103- void BoostRegexMatch::expand (const char *format, char **result, int *resultLength)
104- {
105- u32string resultString = m_match->format (format);
106-
107- // TODO: There's probably more copying, allocing and deleting going on here than there actually needs to be
108- // We just want a u32string to utf8 char*
109- u8string utf8result (UtfConversion::toUtf8 (ConstString<U32 >(resultString)));
110-
111- *resultLength = utf8result.size ();
112- *result = new char [(*resultLength) + 1 ];
113- memcpy (*result, utf8result.c_str (), *resultLength);
114- (*result)[*resultLength] = ' \0 ' ;
115- }
116-
11722
118- ReplaceEntry* NppPythonScript::Replacer::matchToReplaceEntry (const char * /* text */ , Match *match, void *state)
119- {
120- // TODO: state is replacer instance, and contains the replacement string
121- // need to add format call in here,
122- Replacer *replacer = reinterpret_cast <Replacer*>(state);
123- char *replacement;
124- int replacementLength;
125- match->expand (replacer->m_replaceFormat , &replacement, &replacementLength);
126-
127- GroupDetail *fullMatch = match->group (0 );
128- ReplaceEntry* replaceEntry = new ReplaceEntry (fullMatch->start (), fullMatch->end (), replacement, replacementLength);
129- delete [] replacement;
130- return replaceEntry;
131- }
132-
133- bool NppPythonScript::Replacer::startReplace (const char *text, const int textLength, const char *search,
134- const char *replace,
135- std::list<ReplaceEntry*>& replacements)
136- {
137- m_replaceFormat = replace;
138- return startReplace (text, textLength, search, matchToReplaceEntry, this , replacements);
139- }
140-
141- bool NppPythonScript::Replacer::startReplace (const char *text, const int textLength, const char *search,
142- matchConverter converter,
143- void *converterState,
144- std::list<ReplaceEntry*> &replacements) {
145-
146- u32_regex r = u32_regex (toStringType<u32string>(ConstString<char >(search)));
147- UTF8Iterator start (text, 0 , textLength);
148- UTF8Iterator end (text, textLength, textLength);
149- u32_regex_iterator iteratorEnd;
150- BoostRegexMatch match (text);
151- for (u32_regex_iterator it (start, end, r); it != iteratorEnd; ++it) {
152- boost::match_results<UTF8Iterator> boost_match_results (*it);
153-
154- match.setMatchResults (&boost_match_results);
155- ReplaceEntry* entry = converter (text, &match, converterState);
156- replacements.push_back (entry);
157- }
158-
159- return false ;
160- }
16123
16224}
16325#endif
0 commit comments