예제 #1
0
        /// <summary>
        ///  Match a Regular Expression against a UTF-8 converted body of text, starting at the desired index.
        /// </summary>
        /// <param name="expressionIndex">Cached Index of Regular Expression to match; must not contain named groups or backreferences.</param>
        /// <param name="text">UTF-8 converted text to match.</param>
        /// <param name="fromIndex">Index in text to start searching from (used to resume matching).</param>
        /// <param name="matches">MatchPosition array to fill with matches found.</param>
        /// <param name="timeoutMs">Timeout in ms.</param>
        /// <returns>Count of matches found in array.</returns>
        private static unsafe int Matches(int expressionIndex, String8 text, int fromIndex, Match2[] matches, int timeoutMs)
        {
            // Validate String8 text is allocated and in range
            if (text.Length == 0)
            {
                return(0);
            }
            if (text.Array == null)
            {
                throw new ArgumentNullException("text.Array");
            }
            if (text.Index < 0 || text.Length < 0 || text.Index + text.Length > text.Array.Length)
            {
                throw new ArgumentOutOfRangeException(nameof(text));
            }

            // Validate fromIndex
            if (fromIndex < 0 || fromIndex >= text.Array.Length)
            {
                throw new ArgumentOutOfRangeException(nameof(fromIndex));
            }

            // Validate matches
            if (matches == null)
            {
                throw new ArgumentNullException(nameof(matches));
            }
            if (matches.Length == 0)
            {
                throw new ArgumentException("Matches length shoul be greater than zero", nameof(matches));
            }

            // Validate timeout (just return immediately if timeout expired)
            if (timeoutMs < 0)
            {
                return(0);
            }

            int countFound;

            fixed(byte *textPtr = text.Array)
            {
                fixed(Match2 *matchesPtr = matches)
                {
                    var text8i = new String8Interop(textPtr, text.Index, text.Length);

                    countFound = NativeMethods.Matches(expressionIndex, text8i, fromIndex, matchesPtr, matches.Length, timeoutMs);
                }
            }

            // Throw if native side found expressionIndex invalid
            return(countFound < 0
                ? throw new ArgumentOutOfRangeException($"expressionIndex (was: {expressionIndex}, nativeCount: {-countFound})")
                : countFound);
        }
예제 #2
0
 public static unsafe extern int BuildRegex(String8Interop regex, int regexOptions);
예제 #3
0
 public static int BuildRegex(String8Interop regex, int regexOptions)
 {
     return(Environment.Is64BitProcess
         ? NativeMethodsX64.BuildRegex(regex, regexOptions)
         : NativeMethodsX86.BuildRegex(regex, regexOptions));
 }
예제 #4
0
 public static unsafe int Matches(int regexIndex, String8Interop text, int fromTextIndex, Match2 *matches, int matchesLength, int timeoutMilliseconds)
 {
     return(Environment.Is64BitProcess
         ? NativeMethodsX64.Matches(regexIndex, text, fromTextIndex, matches, matchesLength, timeoutMilliseconds)
         : NativeMethodsX86.Matches(regexIndex, text, fromTextIndex, matches, matchesLength, timeoutMilliseconds));
 }
예제 #5
0
 public static unsafe extern int Matches(int regexIndex, String8Interop text, int fromTextIndex, Match2 *matches, int matchesLength, int timeoutMilliseconds);