Skip to content

Commit

Permalink
Implement compile context
Browse files Browse the repository at this point in the history
  • Loading branch information
ltrzesniewski committed Mar 6, 2015
1 parent 936001f commit 9634577
Show file tree
Hide file tree
Showing 25 changed files with 279 additions and 47 deletions.
1 change: 1 addition & 0 deletions src/PCRE.NET.Tests/PCRE.NET.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@
<Compile Include="PcreNet\ReplaceTests.cs" />
<Compile Include="PcreNet\SplitTests.cs" />
<Compile Include="PcreNet\Support\PriorityCacheTests.cs" />
<Compile Include="PcreNet\Support\RegexKeyTests.cs" />
<Compile Include="Pcre\ExpectedGroup.cs" />
<Compile Include="Pcre\ExpectedMatch.cs" />
<Compile Include="Pcre\ExpectedResult.cs" />
Expand Down
52 changes: 52 additions & 0 deletions src/PCRE.NET.Tests/PcreNet/Support/RegexKeyTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using NUnit.Framework;
using PCRE.Support;

namespace PCRE.Tests.PcreNet.Support
{
[TestFixture]
public class RegexKeyTests
{
[Test]
public void should_freeze_settings()
{
var settings = new PcreRegexSettings();
Assert.That(settings.ReadOnlySettings, Is.False);

var key = new RegexKey("test", settings);
Assert.That(key.Settings.ReadOnlySettings, Is.True);
Assert.That(settings.ReadOnlySettings, Is.False);
}

[Test]
public void should_compare_equal()
{
var implicitDefaults = new PcreRegexSettings();
var explicitDefaults = new PcreRegexSettings
{
NewLine = PcreBuildInfo.NewLine,
BackslashR = PcreBuildInfo.BackslashR,
ParensLimit = PcreBuildInfo.ParensLimit
};

var keyA = new RegexKey("test", implicitDefaults);
var keyB = new RegexKey("test", explicitDefaults);

Assert.That(keyA, Is.EqualTo(keyB));
}

[Test]
public void should_not_compare_equal()
{
var defaults = new PcreRegexSettings();
var other = new PcreRegexSettings
{
ParensLimit = 42
};

var keyA = new RegexKey("test", defaults);
var keyB = new RegexKey("test", other);

Assert.That(keyA, Is.Not.EqualTo(keyB));
}
}
}
21 changes: 20 additions & 1 deletion src/PCRE.NET.Wrapper/CompileContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@
namespace PCRE {
namespace Wrapper {

CompileContext::CompileContext()
CompileContext::CompileContext(String^ pattern)
{
if (pattern == nullptr)
throw gcnew ArgumentNullException("pattern");

_ctx = pcre2_compile_context_create(nullptr);
_pattern = pattern;
}

CompileContext::~CompileContext()
Expand All @@ -23,5 +27,20 @@ namespace PCRE {
_ctx = nullptr;
}
}

void CompileContext::NewLine::set(PCRE::Wrapper::NewLine value)
{
pcre2_set_newline(_ctx, static_cast<uint32_t>(value));
}

void CompileContext::BackslashR::set(PCRE::Wrapper::BackslashR value)
{
pcre2_set_bsr(_ctx, static_cast<uint32_t>(value));
}

void CompileContext::ParensNestLimit::set(uint32_t value)
{
pcre2_set_parens_nest_limit(_ctx, value);
}
}
}
15 changes: 13 additions & 2 deletions src/PCRE.NET.Wrapper/CompileContext.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@

#pragma once

#include "CompileContextOptions.h"
#include "InfoKey.h"
#include "PatternOptions.h"
#include "JitCompileOptions.h"

using namespace System;

Expand All @@ -11,16 +13,25 @@ namespace PCRE {
public ref class CompileContext sealed
{
public:
CompileContext();
CompileContext(String^ pattern);
~CompileContext();
!CompileContext();

property String^ Pattern { String^ get() { return _pattern; } }
property PatternOptions Options;
property JitCompileOptions JitCompileOptions;

property NewLine NewLine { void set(PCRE::Wrapper::NewLine); }
property BackslashR BackslashR { void set(PCRE::Wrapper::BackslashR); }
property uint32_t ParensNestLimit { void set(uint32_t); }

internal:
property pcre2_compile_context* Context {
pcre2_compile_context* get() { return _ctx; }
}

private:
initonly String^ _pattern;
pcre2_compile_context* _ctx;
};
}
Expand Down
10 changes: 0 additions & 10 deletions src/PCRE.NET.Wrapper/CompileContextOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,6 @@ using namespace System;
namespace PCRE {
namespace Wrapper {

[Flags]
public enum struct CompileContextNewLineOptions
{
NewLineCr = PCRE2_NEWLINE_CR,
NewLineLf = PCRE2_NEWLINE_LF,
NewLineCrLf = PCRE2_NEWLINE_CRLF,
NewLineAny = PCRE2_NEWLINE_ANY,
NewLineAnyCrLf = PCRE2_NEWLINE_ANYCRLF
};

[Flags]
public enum struct CompileContextBackslashROptions
{
Expand Down
6 changes: 6 additions & 0 deletions src/PCRE.NET.Wrapper/InfoKey.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,11 @@ namespace PCRE {
AnyCrLf = PCRE2_NEWLINE_ANYCRLF
};

public enum struct BackslashR
{
Unicode = PCRE2_BSR_UNICODE,
AnyCrLf = PCRE2_BSR_ANYCRLF
};

}
}
14 changes: 7 additions & 7 deletions src/PCRE.NET.Wrapper/InternalRegex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,17 @@ namespace PCRE {
return reinterpret_cast<interior_ptr<const PCRE2_UCHAR>>(PtrToStringChars(string));
}

InternalRegex::InternalRegex(String^ pattern, PatternOptions options, JitCompileOptions jitCompileOptions)
InternalRegex::InternalRegex(CompileContext^ context)
{
int errorCode;
PCRE2_SIZE errorOffset;

pin_ptr<const PCRE2_UCHAR> pinnedPattern = GetPtrToString(pattern);
pin_ptr<const PCRE2_UCHAR> pinnedPattern = GetPtrToString(context->Pattern);

_re = pcre2_compile(
pinnedPattern,
pattern->Length,
static_cast<int>(options),
context->Pattern->Length,
static_cast<int>(context->Options),
&errorCode,
&errorOffset,
nullptr);
Expand All @@ -40,11 +40,11 @@ namespace PCRE {
? gcnew String(reinterpret_cast<const wchar_t*>(errorBuffer))
: "Unknown error";

throw gcnew ArgumentException(String::Format("Invalid pattern '{0}': {1} at offset {2}", pattern, errorMessage, errorOffset));
throw gcnew ArgumentException(String::Format("Invalid pattern '{0}': {1} at offset {2}", context->Pattern, errorMessage, errorOffset));
}

if (jitCompileOptions != JitCompileOptions::None)
pcre2_jit_compile(_re, static_cast<uint32_t>(jitCompileOptions));
if (context->JitCompileOptions != JitCompileOptions::None)
pcre2_jit_compile(_re, static_cast<uint32_t>(context->JitCompileOptions));

_captureCount = GetInfoInt32(InfoKey::CaptureCount);

Expand Down
3 changes: 2 additions & 1 deletion src/PCRE.NET.Wrapper/InternalRegex.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#pragma once

#include "JitCompileOptions.h"
#include "CompileContext.h"
#include "MatchContext.h"

using namespace System;
Expand All @@ -16,7 +17,7 @@ namespace PCRE {
public ref class InternalRegex sealed
{
public:
InternalRegex(String^ pattern, PatternOptions options, JitCompileOptions jitCompileOptions);
InternalRegex(CompileContext^ context);
~InternalRegex();
!InternalRegex();

Expand Down
4 changes: 2 additions & 2 deletions src/PCRE.NET.Wrapper/MatchContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ namespace PCRE {
}
}

void MatchContext::MatchLimit::set(unsigned int value)
void MatchContext::MatchLimit::set(uint32_t value)
{
pcre2_set_match_limit(_ctx, value);
}

void MatchContext::RecursionLimit::set(unsigned int value)
void MatchContext::RecursionLimit::set(uint32_t value)
{
pcre2_set_recursion_limit(_ctx, value);
}
Expand Down
4 changes: 2 additions & 2 deletions src/PCRE.NET.Wrapper/MatchContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ namespace PCRE {
property int StartIndex;
property PatternOptions AdditionalOptions;
property CalloutDelegate^ CalloutHandler;
property unsigned int MatchLimit { void set(unsigned int); }
property unsigned int RecursionLimit { void set(unsigned int); }
property uint32_t MatchLimit { void set(uint32_t); }
property uint32_t RecursionLimit { void set(uint32_t); }

internal:
void EnableCallout(void* contextPtr);
Expand Down
1 change: 0 additions & 1 deletion src/PCRE.NET.Wrapper/PCRE.NET.Wrapper.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,6 @@
<ClInclude Include="..\PCRE\src\pcre2.h" />
<ClInclude Include="CalloutData.h" />
<ClInclude Include="CompileContext.h" />
<ClInclude Include="CompileContextOptions.h" />
<ClInclude Include="MatchContext.h" />
<ClInclude Include="MatchData.h" />
<ClInclude Include="InfoKey.h" />
Expand Down
3 changes: 0 additions & 3 deletions src/PCRE.NET.Wrapper/PCRE.NET.Wrapper.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,6 @@
<ClInclude Include="..\PCRE\src\pcre2.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="CompileContextOptions.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="CompileContext.h">
<Filter>Header Files</Filter>
</ClInclude>
Expand Down
9 changes: 8 additions & 1 deletion src/PCRE.NET/Dfa/PcreDfaRegex.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,18 @@ public PcreDfaRegex(string pattern)
}

public PcreDfaRegex(string pattern, PcreOptions options)
: this(pattern, new PcreRegexSettings(options))
{
}

public PcreDfaRegex(string pattern, PcreRegexSettings settings)
{
if (pattern == null)
throw new ArgumentNullException("pattern");
if (settings == null)
throw new ArgumentNullException("settings");

Key = new RegexKey(pattern, options);
Key = new RegexKey(pattern, settings);
_re = Caches.RegexCache.GetOrAdd(Key);
}

Expand Down
2 changes: 2 additions & 0 deletions src/PCRE.NET/PCRE.NET.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@
<Compile Include="IInternalRegexWrapper.cs" />
<Compile Include="IPcreGroup.cs" />
<Compile Include="IPcreGroupCollection.cs" />
<Compile Include="PcreBackslashR.cs" />
<Compile Include="PcreCallout.cs" />
<Compile Include="PcreCalloutResult.cs" />
<Compile Include="PcreMatchOptions.cs" />
Expand All @@ -106,6 +107,7 @@
<Compile Include="PcreNewLine.cs" />
<Compile Include="PcreOptions.cs" />
<Compile Include="PcrePatternInfo.cs" />
<Compile Include="PcreRegexSettings.cs" />
<Compile Include="PcreSplitOptions.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Support\PriorityCache.cs" />
Expand Down
11 changes: 11 additions & 0 deletions src/PCRE.NET/PcreBackslashR.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using PCRE.Wrapper;

namespace PCRE
{
public enum PcreBackslashR
{
Default = 0,
Unicode = BackslashR.Unicode,
AnyCrLf = BackslashR.AnyCrLf
}
}
4 changes: 2 additions & 2 deletions src/PCRE.NET/PcreBuildInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ namespace PCRE
{
public static class PcreBuildInfo
{
// TODO : Bsr

static PcreBuildInfo()
{
BackslashR = (PcreBackslashR)GetConfigUInt32(ConfigKey.Bsr);
Jit = GetConfigBool(ConfigKey.Jit);
JitTarget = PcreBuild.GetConfigString(ConfigKey.JitTarget);
LinkSize = GetConfigUInt32(ConfigKey.LinkSize);
Expand All @@ -22,6 +21,7 @@ static PcreBuildInfo()
Version = GetConfigString(ConfigKey.Version);
}

public static PcreBackslashR BackslashR { get; private set; }
public static bool Jit { get; private set; }
public static string JitTarget { get; private set; }
public static uint LinkSize { get; private set; }
Expand Down
2 changes: 1 addition & 1 deletion src/PCRE.NET/PcreMatchSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace PCRE
{
public class PcreMatchSettings
public sealed class PcreMatchSettings
{
private uint? _matchLimit;
private uint? _recursionLimit;
Expand Down
2 changes: 1 addition & 1 deletion src/PCRE.NET/PcreNewLine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace PCRE
{
public enum PcreNewLine
{
Unknown = 0,
Default = 0,
Cr = NewLine.Cr,
Lf = NewLine.Lf,
CrLf = NewLine.CrLf,
Expand Down
7 changes: 6 additions & 1 deletion src/PCRE.NET/PcrePatternInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,14 @@ public string PatternString
get { return _re.Key.Pattern; }
}

public PcreRegexSettings Settings
{
get { return _re.Key.Settings; }
}

public PcreOptions Options
{
get { return _re.Key.Options; }
get { return _re.Key.Settings.Options; }
}

public int MaxBackReference
Expand Down
1 change: 0 additions & 1 deletion src/PCRE.NET/PcreRegex.Match.cs
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,5 @@ public static IEnumerable<PcreMatch> Matches(string subject, string pattern, Pcr
{
return new PcreRegex(pattern, options).Matches(subject, startIndex);
}

}
}
9 changes: 8 additions & 1 deletion src/PCRE.NET/PcreRegex.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,18 @@ public PcreRegex(string pattern)
}

public PcreRegex(string pattern, PcreOptions options)
: this(pattern, new PcreRegexSettings(options))
{
}

public PcreRegex(string pattern, PcreRegexSettings settings)
{
if (pattern == null)
throw new ArgumentNullException("pattern");
if (settings == null)
throw new ArgumentNullException("settings");

Key = new RegexKey(pattern, options);
Key = new RegexKey(pattern, settings);
_re = Caches.RegexCache.GetOrAdd(Key);
}

Expand Down
Loading

0 comments on commit 9634577

Please sign in to comment.