Пример #1
0
        public string Capture(int nth)
        {
            if (disposed)
            {
                throw new ObjectDisposedException("ORegex");
            }
            if (!Valid)
            {
                throw new ArgumentException(string.Format("Invalid Onigmo regular expression: {0}", regexString));
            }
            if (!regionSet)
            {
                throw new InvalidOperationException("ORegex.Capture requires that ORegex.Search be run first.");
            }

            var pos = OnigInterop.onigwrap_pos(region, nth);

            if (pos < 0)
            {
                return(null);
            }

            var len = OnigInterop.onigwrap_len(region, nth);

            return(text.Substring(pos, len));
        }
Пример #2
0
        public ORegex(string pattern, bool ignoreCase = true, bool multiline = false)
        {
            int ignoreCaseArg = ignoreCase ? 1 : 0;
            int multilineArg  = multiline ? 1 : 0;

            regex = OnigInterop.onigwrap_create(pattern, pattern.Length * 2, ignoreCaseArg, multilineArg);

            if (!Valid)
            {
                regexString = pattern; // Save the pattern off on invalid patterns for throwing exceptions
            }
        }
Пример #3
0
        public int IndexIn(string text, int offset = 0)
        {
            if (disposed)
            {
                throw new ObjectDisposedException("ORegex");
            }
            if (!Valid)
            {
                throw new ArgumentException(string.Format("Invalid Onigmo regular expression: {0}", regexString));
            }

            return(OnigInterop.onigwrap_index_in(regex, text, offset * 2, text.Length * 2));
        }
Пример #4
0
        protected virtual void Dispose(bool disposing)
        {
            if (!disposed)
            {
                disposed = true;

                if (regex != IntPtr.Zero)
                {
                    OnigInterop.onigwrap_free(regex);
                }

                if (regionSet)
                {
                    OnigInterop.onigwrap_region_free(region);
                }
            }
        }
Пример #5
0
        public int MatchLength(int nth)
        {
            if (disposed)
            {
                throw new ObjectDisposedException("ORegex");
            }
            if (!Valid)
            {
                throw new ArgumentException(string.Format("Invalid Onigmo regular expression: {0}", regexString));
            }
            if (!regionSet)
            {
                throw new InvalidOperationException("ORegex.MatchLength requires that ORegex.Search be run first.");
            }

            return(OnigInterop.onigwrap_len(region, nth));
        }
Пример #6
0
        public void Search(string text, int offset = 0)
        {
            if (disposed)
            {
                throw new ObjectDisposedException("ORegex");
            }
            if (!Valid)
            {
                throw new ArgumentException(string.Format("Invalid Onigmo regular expression: {0}", regexString));
            }

            this.text = text;
            if (regionSet)
            {
                OnigInterop.onigwrap_region_free(region);
            }

            region    = OnigInterop.onigwrap_search(regex, text, offset * 2, text.Length * 2);
            regionSet = true;
        }
Пример #7
0
        /// <summary>
        /// Performs a thread safe search and returns the results in a list
        /// </summary>
        /// <param name="text">The text to search</param>
        /// <param name="offset">An offset from which to start</param>
        /// <returns></returns>
        public List <ORegexResult> SafeSearch(string text, int offset = 0)
        {
            if (disposed)
            {
                throw new ObjectDisposedException("ORegex");
            }
            if (!Valid)
            {
                throw new ArgumentException(string.Format("Invalid Onigmo regular expression: {0}", regexString));
            }

            var resultList = new List <ORegexResult>();

            lock (syncObject)
            {
                Search(text, offset);

                var captureCount = OnigInterop.onigwrap_num_regs(region);
                for (var capture = 0; capture < captureCount; capture++)
                {
                    var pos = OnigInterop.onigwrap_pos(region, capture);
                    if (capture == 0 && pos == -1)
                    {
                        break;
                    }

                    resultList.Add(new ORegexResult()
                    {
                        Position = pos,
                        Length   = pos == -1 ? 0 : OnigInterop.onigwrap_len(region, capture)
                    });
                }

                this.text = null;
                OnigInterop.onigwrap_region_free(region);
                regionSet = false;
            }

            return(resultList);
        }