/// <summary> /// Compiles the given regex and returns the unmanaged pointer to it. /// </summary> private static IntPtr Compile(string pattern, IntPtr options, uint flags) { // Convert the pattern to a utf-8 encoded string. var patBytes = Encoding.UTF8.GetBytes(pattern); using (var err = new Error()) { // Compile the regex. We get back a raw handle to the underlying // Rust object. var raw = RureFfi.rure_compile( patBytes, new UIntPtr((uint)patBytes.Length), flags, options, err.Raw); // If the regex failed to compile find out what the problem was. if (raw == IntPtr.Zero) { throw new RegexCompilationException(err.Message); } return(raw); } }
private static IntPtr CompileSet(string[] patterns, RureFlags flags, IntPtr options) { var patBytes = patterns.Select(Encoding.UTF8.GetBytes).ToArray(); var patLengths = patBytes .Select(byts => new UIntPtr((uint)byts.Length)).ToArray(); var patByteHandles = patBytes .Select(a => GCHandle.Alloc(a, GCHandleType.Pinned)).ToArray(); var patBytePinnedPointers = patByteHandles .Select(h => h.AddrOfPinnedObject()).ToArray(); using (var err = new Error()) { var compiled = RureFfi.rure_compile_set(patBytePinnedPointers, patLengths, new UIntPtr((uint)patLengths.Length), (uint)flags, options, err.Raw); foreach (var handle in patByteHandles) { handle.Free(); } if (compiled != IntPtr.Zero) { return(compiled); } throw new RegexCompilationException(err.Message); } }
/// <summary> /// Test if this Regex matches <paramref name="haystack" />, starting /// at the given <paramref name="offset" />. /// </summary> /// <param name="haystack">The string to search for this pattern</param> /// <param name="offset">The offset to start searching at</param> public bool IsMatch(string haystack, uint offset) { var haystackBytes = Encoding.UTF8.GetBytes(haystack); return(RureFfi.rure_is_match( Raw, haystackBytes, new UIntPtr((uint)haystackBytes.Length), new UIntPtr(offset))); }
/// <summary> /// Get the Match at a given capture index /// <para> /// Returns detailed match information for the given capture group. /// </para> /// </summary> public Match this[int index] { get { var match = new RureMatch(); var matched = RureFfi.rure_captures_at(Raw, new UIntPtr((uint)index), out match); return(new Match(_haystack, matched, (uint)match.start, (uint)match.end)); } }
/// <summary> /// Matches - Retrieve information abut which patterns in the set match. /// </summary> public SetMatch Matches(byte[] haystack) { var results = new bool[_arity]; var overall = RureFfi.rure_set_matches(Raw, haystack, new UIntPtr((uint)haystack.Length), UIntPtr.Zero, results); return(new SetMatch(overall, results)); }
/// <summary> /// Captures - Find the extent of the capturing groups in the pattern /// in a given haystack. /// </summary> /// <param name="haystack">The string to search for this pattern</param> /// <param name="offset">The offset to start searching at</param> public Captures Captures(byte[] haystack, uint offset) { var caps = new Captures(this, haystack); var matched = RureFfi.rure_find_captures(Raw, haystack, new UIntPtr((uint)haystack.Length), new UIntPtr(offset), caps.Raw); caps.Matched = matched; return(caps); }
/// <summary> /// Find the extent of the first match. /// </summary> /// <param name="haystack">The UTF8 bytes to search for this pattern</param> /// <param name="offset">The offset to start searching at</param> public Match Find(byte[] haystack, uint offset) { var matchInfo = new RureMatch(); var matched = RureFfi.rure_find( Raw, haystack, new UIntPtr((uint)haystack.Length), new UIntPtr(offset), out matchInfo); return(new Match(haystack, matched, (uint)matchInfo.start, (uint)matchInfo.end)); }
public bool MoveNext() { while (RureFfi.rure_iter_capture_names_next(Raw, out IntPtr name)) { Current = Marshal.PtrToStringAnsi(name); if (!string.IsNullOrEmpty(Current)) { return(true); } } return(false); }
public bool MoveNext() { var caps = new Captures(Pattern, Haystack); var matched = RureFfi.rure_iter_next_captures(Raw, Haystack, new UIntPtr((uint)Haystack.Length), caps.Raw); if (matched) { caps.Matched = matched; Current = caps; return(true); } Current = null; return(false); }
public bool MoveNext() { var matched = RureFfi.rure_iter_next(Raw, Haystack, new UIntPtr((uint)Haystack.Length), out _matchInfo); if (matched) { Current = new Match(Haystack, matched, (uint)_matchInfo.start, (uint)_matchInfo.end); return(true); } Current = null; return(false); }
public CaptureNamesEnumerator(Regex regex) : base(RureFfi.rure_iter_capture_names_new(regex.Raw)) { }
internal Captures(Regex re, byte[] haystack) : base(RureFfi.rure_captures_new(re.Raw)) { _reg = re; _haystack = haystack; }
protected override void Free(IntPtr resource) { RureFfi.rure_options_free(resource); }
/// <summary> /// Get Capture Index from Name /// </summary> public int this[string capture] => RureFfi.rure_capture_name_index(Raw, Encoding.UTF8.GetBytes(capture));
/// <summary> /// Is match - Check if any of the patterns in the set match. /// </summary> public bool IsMatch(byte[] haystack) { return(RureFfi.rure_set_is_match(Raw, haystack, new UIntPtr((uint)haystack.Length), UIntPtr.Zero)); }
protected override void Free(IntPtr resource) { RureFfi.rure_iter_capture_names_free(resource); }
public Error() : base(RureFfi.rure_error_new()) { }
protected override void Free(IntPtr resource) { RureFfi.rure_error_free(resource); }
public Options() : base(RureFfi.rure_options_new()) { }
public RegexIter(Regex pattern, byte[] haystack) : base(RureFfi.rure_iter_new(pattern.Raw)) { Pattern = pattern; Haystack = haystack; }