From e0831d2c74d311af5e57602dc9891f8810608d80 Mon Sep 17 00:00:00 2001 From: Keith Hall Date: Wed, 27 Jun 2018 14:25:35 +0300 Subject: [PATCH 1/4] Improve build instructions --- README.md | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index d7e0e20..c655ae6 100644 --- a/README.md +++ b/README.md @@ -11,9 +11,9 @@ Building First, get a copy of Onigmo or Oniguruma. * https://github.com/k-takata/Onigmo -* http://www.geocities.jp/kosako3/oniguruma/ (We don't actively test against oniguruma, but it should work.) +* https://github.com/kkos/oniguruma (We don't actively test against oniguruma, but it should work.) -Copy oniguruma.h into the onigwrap folder, alongside onigwrap.c and onigwrap.h. +Copy `oniguruma.h` into the `onigwrap` folder (or copy `onigmo.h` and rename it to `oniguruma.h` if using Onigmo), alongside `onigwrap.c` and `onigwrap.h`. From here, the build steps diverge for each platform: @@ -26,7 +26,7 @@ Configure and build onig. The defaults should work, but Mono on Mac is usually 3 `make` -Copy .libs/libonig.a to the onigwrap folder. +Copy `.libs/libonig.a` to the `onigwrap` folder. Now we build onigwrap: @@ -37,15 +37,15 @@ Take the dylib and put it alongside your binary. Windows ------- -Build and configure onig. Copy the win32/Makefile and win32/config.h to onig's root directory and run `nmake`. If you're building Onig as 64 bit, you'll need to edit the Makefile and add /MACHINE:X64 to the LINKFLAGS +Build and configure onig. Copy the `win32/Makefile` and `win32/config.h` to onig's root directory and run `nmake`. If you're building Onig as 64 bit, you'll need to edit the Makefile and add `/MACHINE:X64` to the LINKFLAGS -Copy onig\_s.lib and oniguruma.h to the onigwrap folder. +Copy `onig_s.lib` to the `onigwrap` folder. -Build onigwrap: +With the `onigwrap` folder as your working dir, build onigwrap: `cl.exe /DONIG_EXTERN=extern /D_USRDLL /D_WINDLL onigwrap.c /link /LTCG onig_s.lib /DLL /OUT:onigwrap.dll` -Copy onigwrap.dll to the folder with your binary. +Copy `onigwrap.dll` to the folder with your binary. (For example, `OnigRegexTests/bin/Debug` and `OnigWrapConsoleTest/bin/Debug`.) Linux ----- @@ -54,10 +54,10 @@ Build and configure onig. We need to prepare onig for static linking though, so `./configure "CFLAGS=-fPIC"` -Copy .libs/libonig.a to the onigwrap folder. +Copy `.libs/libonig.a` to the `onigwrap` folder. Build onigwrap: `gcc -shared -fPIC onigwrap.c libonig.a -o libonigwrap.so` -Copy libonigwrap.so alongside your binary. +Copy `libonigwrap.so` alongside your binary. From a8513f20b938df39f02a2dd796330ea1ea937e88 Mon Sep 17 00:00:00 2001 From: Keith Hall Date: Wed, 27 Jun 2018 14:26:15 +0300 Subject: [PATCH 2/4] Improve test console application to accept arguments --- OnigWrapConsoleTest/Program.cs | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/OnigWrapConsoleTest/Program.cs b/OnigWrapConsoleTest/Program.cs index bce90d9..bc6ab0f 100644 --- a/OnigWrapConsoleTest/Program.cs +++ b/OnigWrapConsoleTest/Program.cs @@ -16,15 +16,37 @@ class Program /// static void Main(string[] args) { - var text = "abcABC123"; - var pattern = "[A-C]+"; + var text = args?.Length == 0 ? "abcABC123" : args[0]; + var pattern = args?.Length < 2 ? @"\w(\p{L})(?=(\d))" : args[1]; + var start_position = args?.Length < 3 ? 0 : int.Parse(args[2]); Console.WriteLine("Building ORegex({0})", text); using (var re = new ORegex(pattern, false)) { - Console.WriteLine("Looking for {0} in {1}", pattern, text); - Console.WriteLine("Found a match at {0}", re.IndexIn(text)); + Console.WriteLine("Looking for '{0}' in '{1}', starting at position {2}...", pattern, text, start_position); + var result = re.SafeSearch(text, start_position); + if (!result.Any()) + { + Console.WriteLine("No matches found."); + } + else + { + foreach (var tuple in result.Select((i, m) => Tuple.Create(m, i))) + { + var index = tuple.Item1; + var match = tuple.Item2; + Console.WriteLine("Found capture group {3} at position {0} with length {1}: {2}", match.Position, match.Length, text.Substring(match.Position, match.Length), index); + } + } + } + + if (System.Diagnostics.Debugger.IsAttached) + { + // when running through Visual Studio, the console output window disappears immediately when the application exists, so prompt the user to continue + Console.WriteLine(); + Console.WriteLine("Press any key to continue..."); + Console.ReadKey(); } } } From 2b7fa5bd301b1a79d812f844c8e130d677d3efe6 Mon Sep 17 00:00:00 2001 From: Keith Hall Date: Wed, 27 Jun 2018 14:33:14 +0300 Subject: [PATCH 3/4] Add some missing doc comments to explain what the public methods do --- OnigRegex/ORegex.cs | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/OnigRegex/ORegex.cs b/OnigRegex/ORegex.cs index 829f1bc..a95105f 100644 --- a/OnigRegex/ORegex.cs +++ b/OnigRegex/ORegex.cs @@ -56,7 +56,7 @@ public int IndexIn(string text, int offset = 0) /// /// The text to search /// An offset from which to start - /// + /// A list of capture group matches public List SafeSearch(string text, int offset = 0) { if (disposed) throw new ObjectDisposedException("ORegex"); @@ -90,6 +90,12 @@ public List SafeSearch(string text, int offset = 0) return resultList; } + /// + /// Perform a search for the given text starting at the specified offset + /// + /// The text to search + /// An offset from which to start + /// Nothing. Use MatchPosition and MatchLength to query the results public void Search(string text, int offset = 0) { if (disposed) throw new ObjectDisposedException("ORegex"); @@ -103,6 +109,11 @@ public void Search(string text, int offset = 0) regionSet = true; } + /// + /// Get the start match position of the specified capture group + /// + /// The capture group index + /// The position in the string that was searched where the specified capture group was matched public int MatchPosition(int nth) { if (disposed) throw new ObjectDisposedException("ORegex"); @@ -112,6 +123,11 @@ public int MatchPosition(int nth) return OnigInterop.onigwrap_pos(region, nth); } + /// + /// Get the length of the specified capture group match + /// + /// The capture group index + /// The length in the string that was matched inside the specified capture group public int MatchLength(int nth) { if (disposed) throw new ObjectDisposedException("ORegex"); @@ -121,6 +137,11 @@ public int MatchLength(int nth) return OnigInterop.onigwrap_len(region, nth); } + /// + /// Get the text that matched inside the specified capture group + /// + /// The capture group index + /// The text that was matched inside the specified capture group public string Capture(int nth) { if (disposed) throw new ObjectDisposedException("ORegex"); From a870b53ffa269ef1b94c655ab86236915950c269 Mon Sep 17 00:00:00 2001 From: Keith Hall Date: Sat, 23 Jul 2022 22:49:14 +0300 Subject: [PATCH 4/4] fix console output --- OnigWrapConsoleTest/Program.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/OnigWrapConsoleTest/Program.cs b/OnigWrapConsoleTest/Program.cs index bc6ab0f..e3b6041 100644 --- a/OnigWrapConsoleTest/Program.cs +++ b/OnigWrapConsoleTest/Program.cs @@ -20,7 +20,7 @@ static void Main(string[] args) var pattern = args?.Length < 2 ? @"\w(\p{L})(?=(\d))" : args[1]; var start_position = args?.Length < 3 ? 0 : int.Parse(args[2]); - Console.WriteLine("Building ORegex({0})", text); + Console.WriteLine("Building ORegex({0})", pattern); using (var re = new ORegex(pattern, false)) {