1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Application to test regular expression (wxRegEx)
7 // Copyright: (c) 2003 Ryan Norton <wxprojects@comcast.net>
8 // Licence: wxWindows license
9 /////////////////////////////////////////////////////////////////////////////
11 //===========================================================================
13 //===========================================================================
15 //wxWindows regular expression library
18 //RN (Ryan Norton's) regular expression library
19 //#define wxUSE_RNWXRE
21 //GRETA, Microsoft Research's templated regex library
22 //[http://research.microsoft.com/projects/greta/]
23 //Install - Get it from .net powertools, put the directory in this directory
26 //PCRE (Perl Compatible Regular Expressions) [sourceforge.net/projects/pcre]
27 //Install - Get the GnuWin32 version and put the files in this directory
30 //===========================================================================
32 //===========================================================================
34 // For compilers that support precompilation, includes "wx/wx.h".
35 #include "wx/wxprec.h"
41 // for all others, include the necessary headers (this file is usually all you
42 // need because it includes almost all "standard" wxWindows headers)
51 // This is the required header for wxRegEx
53 # include <wx/regex.h>
62 # pragma warning(disable:4018)
63 # pragma warning(disable:4100)
64 # pragma warning(disable:4146)
65 # pragma warning(disable:4244)
66 # pragma warning(disable:4663)
68 int __cdecl
_resetstkoflw(void) {return 0;}
71 # include "greta/regexpr2.h"
72 using namespace regex
;
78 # pragma comment(lib, "libpcre.a")
82 //===========================================================================
84 //===========================================================================
87 // One of the best sources for regex info is IEEE document/standard 1003.2
88 // From the open group.
89 // A current link - (http://www.opengroup.org/onlinepubs/007904975/basedefs/xbd_chap09.html).
92 //---------------------------------------------------------------------------
94 //---------------------------------------------------------------------------
96 class MyDialog
: public wxFrame
100 //IDs for our controls
120 MyDialog() : wxFrame( NULL
, -1, _("regextest - wxRegEx Testing App"),
121 wxPoint(20,20), wxSize(300,400), wxDEFAULT_FRAME_STYLE
| wxTAB_TRAVERSAL
)
123 //Set the background to something light gray-ish
124 SetBackgroundColour(wxColour(150,150,150));
127 // Create the menus (Exit & About)
130 wxMenu
*FileMenu
= new wxMenu
;
131 OptionsMenu
= new wxMenu
;
132 wxMenu
*HelpMenu
= new wxMenu
;
134 FileMenu
->Append(wxID_EXIT
, _T("&Exit"), _("Quit this program"));
136 OptionsMenu
->AppendCheckItem(ExtendedID
, _T("wxRE_EXTENDED"), _("Extended Regular Expressions?"));
137 OptionsMenu
->AppendCheckItem(ICaseID
, _T("wxRE_ICASE"), _("Enable case-insensitive matching?"));
138 OptionsMenu
->AppendCheckItem(NewLineID
, _T("wxRE_NEWLINE"), _("Treat \n as special?"));
139 OptionsMenu
->AppendSeparator();
140 OptionsMenu
->AppendCheckItem(NotBolID
, _T("wxRE_NOTBOL"), _("Use functionality of ^ character?"));
141 OptionsMenu
->AppendCheckItem(NotEolID
, _T("wxRE_NOTEOL"), _("Use functionality of $ character?"));
142 OptionsMenu
->AppendSeparator();
143 OptionsMenu
->AppendSeparator();
144 OptionsMenu
->AppendCheckItem(CompID
, _T("Test Compile"), _("Added Compiling to Match Time?"));
145 OptionsMenu
->Check(ExtendedID
, true);
147 HelpMenu
->Append(wxID_ABOUT
, _T("&About...\tF1"), _("Show about dialog"));
149 wxMenuBar
*MenuBar
= new wxMenuBar();
150 MenuBar
->Append(FileMenu
, _T("&File"));
151 MenuBar
->Append(OptionsMenu
, _T("&Options"));
152 MenuBar
->Append(HelpMenu
, _T("&Help"));
155 #endif // wxUSE_MENUS
158 PatternText
.Create(this, PatternTextID
, _(""), wxPoint(5, 30));
159 SearchText
.Create(this, SearchTextID
, _(""), wxPoint(5, 75));
160 //reset size of text controls
161 wxSize TextSize
= PatternText
.GetSize();
162 TextSize
.SetWidth(200);
163 PatternText
.SetSize(TextSize
);
164 SearchText
.SetSize(TextSize
);
167 PatternHeader
.Create(this, -1, _("Regular Expression:"), wxPoint(5, 10));
168 SearchHeader
.Create(this, -1, _("String to Search:"), wxPoint(5, 55));
169 IterHeader
.Create(this, -1, _("Iterations (Match Time):"), wxPoint(100, 100));
171 ResultText
.Create(this, -1, _(""), wxPoint(5, 150), wxSize(100,200), wxST_NO_AUTORESIZE
);
172 ResultText2
.Create(this, -1, _(""), wxPoint(115, 150), wxSize(100,200), wxST_NO_AUTORESIZE
);
175 OkButton
.Create(this, OkButtonID
, _("OK"), wxPoint(20, 120));
177 NumIters
.Create(this, -1, _("15000"), wxPoint(100, 120));
180 void OnAbout(wxCommandEvent
& WXUNUSED(evt
))
185 f
.Open(_("README.txt"), wxFile::read
);
186 szBuffer
= new wxChar
[f
.Length() + 1];
187 f
.Read(szBuffer
, f
.Length());
190 wxMessageBox(wxString::Format(
192 _("-----------Regular Expression Test Application-----------\n"),
194 _("\n\n\n(c) 2003 Ryan Norton <wxprojects@comcast.net>\n"),
202 void OnMatch(wxCommandEvent
& WXUNUSED(evt
))
204 wxString szPattern
= PatternText
.GetValue();
205 wxString szSearch
= SearchText
.GetValue();
206 wxString szResult
, szResult2
, szResult3
, szResult4
; //Will be displayed in ResultText
207 wxString szStatus
, szStatus2
, szStatus3
, szStatus4
;
209 int nCompileFlags
= 0, nCompileFlags2
= 0, nCompileFlags3
= 0, nCompileFlags4
= 0;
210 int nMatchFlags
= 0, nMatchFlags2
= 0, nMatchFlags3
= 0, nMatchFlags4
= 0;
212 if (!(OptionsMenu
->IsChecked(ExtendedID
)))
215 nCompileFlags
|= wxRE_BASIC
;
221 nCompileFlags2
|= wxRe::wxRE_EXTENDED
;
223 // nCompileFlags3 |= EXTENDED;
226 if (OptionsMenu
->IsChecked(ICaseID
))
229 nCompileFlags
|= wxRE_ICASE
;
232 nCompileFlags2
|= wxRe::wxRE_ICASE
;
235 nCompileFlags3
|= NOCASE
;
239 if (OptionsMenu
->IsChecked(NewLineID
))
242 nCompileFlags
|= wxRE_NEWLINE
;
245 nCompileFlags2
|= wxRe::wxRE_NEWLINE
;
248 nCompileFlags3
|= MULTILINE
;
252 if (OptionsMenu
->IsChecked(NotBolID
))
255 nMatchFlags
|= wxRE_NOTBOL
;
258 nMatchFlags2
|= wxRe::wxRE_NOTBOL
;
262 if (OptionsMenu
->IsChecked(NotEolID
))
265 nMatchFlags
|= wxRE_NOTEOL
;
268 nMatchFlags2
|= wxRe::wxRE_NOTEOL
;
272 //Our regular expression object
274 //Success! Here we'll display some
275 //information to the user
276 size_t dwStartIndex
= 0, dwEndIndex
= 0,
277 dwStartIndex2
= 0, dwEndIndex2
= 0,
278 dwStartIndex3
= 0, dwEndIndex3
= 0,
279 dwStartIndex4
= 0, dwEndIndex4
= 0;
281 time_t dwStartTime
= 0, dwEndTime
= 0,
282 dwStartTime2
= 0, dwEndTime2
= 0,
283 dwStartTime3
= 0, dwEndTime3
= 0,
284 dwStartTime4
= 0, dwEndTime4
= 0;
288 if (!NumIters
.GetValue().ToLong(&n
))
294 //Regular Expressions must be compiled before
295 //you can search a string with them, or
296 //at least most implementations do.
297 //(Boost and Microsoft have templated
298 //versions that don't require compilation)
299 //Basically compilation breaks it down into
300 //something that's easier to parse, due
301 //to the syntax of regular expressions
302 if (!Regex
.Compile(szPattern
, nCompileFlags
))
303 szStatus
+= _("\nCompile Failed!\n");
306 //Here's where we actually search our string
307 if (!Regex
.Matches(szSearch
, nMatchFlags
))
308 szStatus
+= _("\nExecution/Matching Failed!\n");
311 Regex
.GetMatch(&dwStartIndex
, &dwEndIndex
);
313 szStatus
= _("Success");
315 //We're going to go ahead and time the match
316 //for fun (clock() is a c library routine that
317 //returns the current time since a certian point
319 dwStartTime
= clock();
321 if (OptionsMenu
->IsChecked(CompID
))
323 for(i
= 0; i
< n
; ++i
)
325 Regex
.Compile(szPattern
, nCompileFlags
);
326 Regex
.Matches(szSearch
, nMatchFlags
);
331 for(i
= 0; i
< n
; ++i
)
333 Regex
.Matches(szSearch
, nMatchFlags
);
337 //This line gets the difference in time between now
338 //and when we first initialized dwStartTime.
339 dwEndTime
= clock() - dwStartTime
;
344 szResult
= wxString::Format(
345 _("--wxRegEx--\nIndex:[%i]-[%i]\nString:%s\nMatch Time:%ums\nStatus:%s"),
346 dwStartIndex
, dwEndIndex
+dwStartIndex
,
347 szSearch
.Mid(dwStartIndex
, dwEndIndex
),
352 #endif //wxUSE_WXREGEX
357 if ((e
= Re
.Comp(szPattern
, nCompileFlags2
)) != wxRe::wxRE_OK
)
358 szStatus2
= wxString::Format(_("\nCompile Failed!\n%s\n"), wxRe::ErrorToString(e
));
361 //Here's where we actually search our string
362 if ((e
= Re
.Exec(szSearch
, nMatchFlags2
)) != wxRe::wxRE_OK
)
363 szStatus2
= wxString::Format(_("\nExecution/Matching Failed!\n%s\n"), wxRe::ErrorToString(e
));
366 dwStartIndex2
= Re
.GetMatch(0).first
;
367 dwEndIndex2
= Re
.GetMatch(0).second
;
369 szStatus2
= _("Success");
371 dwStartTime2
= clock();
373 if (OptionsMenu
->IsChecked(CompID
))
375 for(i
= 0; i
< n
; ++i
)
377 Re
.Comp(szPattern
, nCompileFlags2
);
378 Re
.Exec(szSearch
, nMatchFlags2
);
383 for(i
= 0; i
< n
; ++i
)
385 Re
.Exec(szSearch
, nMatchFlags2
);
389 dwEndTime2
= clock() - dwStartTime2
;
392 szResult2
= wxString::Format(
393 _("--Ryan's wxRe--\nIndex:[%i]-[%i]\nString:%s\nMatch Time:%ums\nStatus:%s"),
394 dwStartIndex2
, dwEndIndex2
+dwStartIndex2
,
395 szSearch
.Mid(dwStartIndex2
, dwEndIndex2
),
399 #endif //wxUSE_RNWXRE
402 std::string
stdszPattern(szPattern
);
403 rpattern
Greta (stdszPattern
,EXTENDED
,MODE_MIXED
);
405 std::string
stdszSearch(szSearch
);
407 //Here's where we actually search our string
408 if (!Greta
.match(stdszSearch
, r
).matched
)
409 szStatus3
+= _("\nExecution/Matching Failed!\n");
412 szStatus3
= _("Success");
414 dwStartTime3
= clock();
416 if (OptionsMenu
->IsChecked(CompID
))
418 for(i
= 0; i
< n
; ++i
)
420 Greta
= rpattern(stdszPattern
,EXTENDED
,MODE_MIXED
);
421 Greta
.match(stdszSearch
, r
);
426 for(i
= 0; i
< n
; ++i
)
428 Greta
.match(stdszSearch
, r
);
432 dwEndTime3
= clock() - dwStartTime3
;
435 szResult3
= wxString::Format(
436 _("--Greta--\nIndex:[%i]-[%i]\nString:%s\nMatch Time:%ums\nStatus:%s"),
437 r
.rstart(), r
.rlength() + r
.rstart(),
438 szSearch
.Mid(r
.rstart(), r
.rlength()),
445 const wxChar
* szError
;
448 if ((pPcre
= pcre_compile(szPattern
, nCompileFlags4
, &szError
, &nErrOff
, 0)) == NULL
)
449 szStatus4
= wxString::Format(_("\nCompile Failed!\nError:%s\nOffset:%i\n"), szError
, nErrOff
);
453 pcre_fullinfo(pPcre
, 0, PCRE_INFO_CAPTURECOUNT
, &msize
);
455 int *m
= new int[msize
];
457 //Here's where we actually search our string
458 if (!pcre_exec(pPcre
, 0, szSearch
, szSearch
.Length(), 0, 0, m
, msize
))
459 szStatus4
= wxString::Format(_("\nExecution/Matching Failed!\n"));
462 dwStartIndex4
= m
[0];
463 dwEndIndex4
= m
[1] - m
[0];
465 szStatus4
= _("Success");
467 dwStartTime4
= clock();
469 if (OptionsMenu
->IsChecked(CompID
))
471 for(i
= 0; i
< n
; ++i
)
473 pPcre
= pcre_compile(szPattern
, nCompileFlags4
, &szError
, &nErrOff
, 0);
474 pcre_exec(pPcre
, 0, szSearch
, szSearch
.Length(), 0, 0, m
, msize
);
479 for(i
= 0; i
< n
; ++i
)
481 pcre_exec(pPcre
, 0, szSearch
, szSearch
.Length(), 0, 0, m
, msize
);
485 dwEndTime4
= clock() - dwStartTime4
;
488 szResult4
= wxString::Format(
489 _("--PCRE--\nIndex:[%i]-[%i]\nString:%s\nMatch Time:%ums\nStatus:%s"),
490 dwStartIndex4
, dwEndIndex4
+dwStartIndex4
,
491 szSearch
.Mid(dwStartIndex4
, dwEndIndex4
),
497 ResultText
.SetLabel(szResult
+ _("\n\n") + szResult2
);
498 ResultText2
.SetLabel(szResult3
+ _("\n\n") + szResult4
);
501 void OnQuit(wxCommandEvent
& WXUNUSED(evt
))
506 wxTextCtrl PatternText
, SearchText
, NumIters
;
507 wxStaticText PatternHeader
, SearchHeader
, IterHeader
, ResultText
, ResultText2
;
512 DECLARE_EVENT_TABLE()
515 BEGIN_EVENT_TABLE(MyDialog
, wxFrame
)
518 EVT_MENU(wxID_EXIT
, MyDialog::OnQuit
)
519 EVT_MENU(wxID_ABOUT
, MyDialog::OnAbout
)
522 EVT_TEXT_ENTER(MyDialog::PatternTextID
, MyDialog::OnMatch
)
523 EVT_TEXT_ENTER(MyDialog::SearchTextID
, MyDialog::OnMatch
)
526 EVT_BUTTON(MyDialog::OkButtonID
, MyDialog::OnMatch
)
530 //---------------------------------------------------------------------------
532 //---------------------------------------------------------------------------
534 class MyApp
: public wxApp
539 MyDialog
* dialog
= new MyDialog();