Exemple #1
0
        /// <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);
            }
        }
Exemple #2
0
        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);
            }
        }
Exemple #3
0
        /// <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)));
        }
Exemple #4
0
 /// <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));
     }
 }
Exemple #5
0
        /// <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));
        }
Exemple #6
0
        /// <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);
        }
Exemple #7
0
        /// <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));
        }
Exemple #8
0
 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);
 }
Exemple #9
0
        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);
        }
Exemple #10
0
        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);
        }
Exemple #11
0
 public CaptureNamesEnumerator(Regex regex)
     : base(RureFfi.rure_iter_capture_names_new(regex.Raw))
 {
 }
Exemple #12
0
 internal Captures(Regex re, byte[] haystack)
     : base(RureFfi.rure_captures_new(re.Raw))
 {
     _reg      = re;
     _haystack = haystack;
 }
Exemple #13
0
 protected override void Free(IntPtr resource)
 {
     RureFfi.rure_options_free(resource);
 }
Exemple #14
0
 /// <summary>
 ///   Get Capture Index from Name
 /// </summary>
 public int this[string capture] =>
 RureFfi.rure_capture_name_index(Raw, Encoding.UTF8.GetBytes(capture));
Exemple #15
0
 /// <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));
 }
Exemple #16
0
 protected override void Free(IntPtr resource)
 {
     RureFfi.rure_iter_capture_names_free(resource);
 }
Exemple #17
0
 public Error()
     : base(RureFfi.rure_error_new())
 {
 }
Exemple #18
0
 protected override void Free(IntPtr resource)
 {
     RureFfi.rure_error_free(resource);
 }
Exemple #19
0
 public Options()
     : base(RureFfi.rure_options_new())
 {
 }
Exemple #20
0
 public RegexIter(Regex pattern, byte[] haystack)
     : base(RureFfi.rure_iter_new(pattern.Raw))
 {
     Pattern  = pattern;
     Haystack = haystack;
 }