static bool GetMailToEndIndex(UrlMatch match, char[] text, int startIndex, int matchIndex, int endIndex) { char close = GetClosingBrace(match, text, startIndex); int contentIndex = matchIndex + match.Pattern.Length; int index = contentIndex; if (contentIndex >= endIndex) { return(false); } if (!SkipAddrspec(text, endIndex, ref index)) { index = contentIndex; } if (index < endIndex && text[index] == '?') { index++; while (index < endIndex && IsUrlSafe(text[index]) && text[index] != close) { index++; } } match.EndIndex = index; return(index > contentIndex); }
public bool Scan(char[] text, int startIndex, int count, out UrlMatch match) { GetIndexDelegate getStartIndex, getEndIndex; int endIndex = startIndex + count; UrlPattern url; string pattern; int index; if ((index = trie.Search(text, startIndex, count, out pattern)) == -1) { match = null; return(false); } if (!patterns.TryGetValue(pattern, out url)) { match = null; return(false); } match = new UrlMatch(url.Pattern, url.Prefix); switch (url.Type) { case UrlPatternType.Addrspec: getStartIndex = GetAddrspecStartIndex; getEndIndex = GetAddrspecEndIndex; break; case UrlPatternType.MailTo: getStartIndex = GetMailToStartIndex; getEndIndex = GetMailToEndIndex; break; case UrlPatternType.File: getStartIndex = GetFileStartIndex; getEndIndex = GetFileEndIndex; break; default: getStartIndex = GetWebStartIndex; getEndIndex = GetWebEndIndex; break; } if (!getStartIndex(match, text, startIndex, index, endIndex)) { match = null; return(false); } if (!getEndIndex(match, text, startIndex, index, endIndex)) { match = null; return(false); } return(true); }
static bool GetAddrspecEndIndex(UrlMatch match, char[] text, int startIndex, int matchIndex, int endIndex) { int index = matchIndex + 1; if (index == endIndex) { return(false); } if (text[index] != '[') { // domain if (!SkipDomain(text, endIndex, ref index)) { return(false); } match.EndIndex = index; return(true); } // address literal index++; // we need at least 8 more characters if (index + 8 >= endIndex) { return(false); } if (IsIPv6(text, index)) { index += "IPv6:".Length; if (!SkipIPv6Literal(text, endIndex, ref index)) { return(false); } } else { if (!SkipIPv4Literal(text, endIndex, ref index)) { return(false); } } if (index >= endIndex || text[index++] != ']') { return(false); } match.EndIndex = index; return(true); }
public bool Scan (char[] text, int startIndex, int count, out UrlMatch match) { GetIndexDelegate getStartIndex, getEndIndex; int endIndex = startIndex + count; UrlPattern url; string pattern; int index; if ((index = trie.Search (text, startIndex, count, out pattern)) == -1) { match = null; return false; } if (!patterns.TryGetValue (pattern, out url)) { match = null; return false; } match = new UrlMatch (url.Pattern, url.Prefix); switch (url.Type) { case UrlPatternType.Addrspec: getStartIndex = GetAddrspecStartIndex; getEndIndex = GetAddrspecEndIndex; break; case UrlPatternType.MailTo: getStartIndex = GetMailToStartIndex; getEndIndex = GetMailToEndIndex; break; case UrlPatternType.File: getStartIndex = GetFileStartIndex; getEndIndex = GetFileEndIndex; break; default: getStartIndex = GetWebStartIndex; getEndIndex = GetWebEndIndex; break; } if (!getStartIndex (match, text, startIndex, index, endIndex)) { match = null; return false; } if (!getEndIndex (match, text, startIndex, index, endIndex)) { match = null; return false; } return true; }
static bool GetFileEndIndex(UrlMatch match, char[] text, int startIndex, int matchIndex, int endIndex) { char close = GetClosingBrace(match, text, startIndex); int index = matchIndex + match.Pattern.Length; while (index < endIndex && IsUrlSafe(text[index]) && text[index] != close) { index++; } match.EndIndex = index; return(index > matchIndex + match.Pattern.Length); }
static char GetClosingBrace(UrlMatch match, char[] text, int startIndex) { if (match.StartIndex == startIndex) { return('\0'); } switch (text[match.StartIndex - 1]) { case '(': return(')'); case '{': return('}'); case '[': return(']'); case '<': return('>'); case '|': return('|'); default: return('\0'); } }
static bool GetAddrspecStartIndex(UrlMatch match, char[] text, int startIndex, int matchIndex, int endIndex) { int index = matchIndex - 1; if (matchIndex == startIndex) { return(false); } do { if (!SkipWordBackwards(text, startIndex, ref index)) { return(false); } if (index == startIndex) { break; } if (text[index - 1] != '.') { break; } index -= 2; if (index <= startIndex) { return(false); } } while (true); match.StartIndex = index; return(true); }
static bool GetWebEndIndex(UrlMatch match, char[] text, int startIndex, int matchIndex, int endIndex) { char close = GetClosingBrace(match, text, startIndex); int index = matchIndex + match.Pattern.Length; if (index >= endIndex || !SkipDomain(text, endIndex, ref index)) { return(false); } // check for a port if (index + 1 < endIndex && text[index] == ':' && IsDigit(text[index + 1])) { index += 2; while (index < endIndex && IsDigit(text[index])) { index++; } } // check for a path if (index < endIndex && text[index] == '/') { index++; while (index < endIndex && IsUrlSafe(text[index]) && text[index] != close) { index++; } } match.EndIndex = index; return(true); }
static bool GetWebStartIndex(UrlMatch match, char[] text, int startIndex, int matchIndex, int endIndex) { match.StartIndex = matchIndex; return(true); }
static bool GetWebStartIndex (UrlMatch match, char[] text, int startIndex, int matchIndex, int endIndex) { match.StartIndex = matchIndex; return true; }
static bool GetWebEndIndex (UrlMatch match, char[] text, int startIndex, int matchIndex, int endIndex) { char close = GetClosingBrace (match, text, startIndex); int index = matchIndex + match.Pattern.Length; if (index >= endIndex || !SkipDomain (text, endIndex, ref index)) return false; // check for a port if (index + 1 < endIndex && text[index] == ':' && IsDigit (text[index + 1])) { index += 2; while (index < endIndex && IsDigit (text[index])) index++; } // check for a path if (index < endIndex && text[index] == '/') { index++; while (index < endIndex && IsUrlSafe (text[index]) && text[index] != close) index++; } match.EndIndex = index; return true; }
static bool GetMailToEndIndex (UrlMatch match, char[] text, int startIndex, int matchIndex, int endIndex) { char close = GetClosingBrace (match, text, startIndex); int contentIndex = matchIndex + match.Pattern.Length; int index = contentIndex; if (contentIndex == endIndex) return false; if (!SkipAddrspec (text, endIndex, ref index)) index = contentIndex; if (text[index] == '?') { index++; while (index < endIndex && IsUrlSafe (text[index]) && text[index] != close) index++; } match.EndIndex = index; return index > contentIndex; }
static bool GetFileEndIndex (UrlMatch match, char[] text, int startIndex, int matchIndex, int endIndex) { char close = GetClosingBrace (match, text, startIndex); int index = matchIndex + match.Pattern.Length; while (index < endIndex && IsUrlSafe (text[index]) && text[index] != close) index++; match.EndIndex = index; return index > matchIndex + match.Pattern.Length; }
static bool GetAddrspecEndIndex (UrlMatch match, char[] text, int startIndex, int matchIndex, int endIndex) { int index = matchIndex + 1; if (index == endIndex) return false; if (text[index] != '[') { // domain if (!SkipDomain (text, endIndex, ref index)) return false; match.EndIndex = index; return true; } // address literal index++; // we need at least 8 more characters if (index + 8 >= endIndex) return false; if (IsIPv6 (text, index)) { index += "IPv6:".Length; if (!SkipIPv6Literal (text, endIndex, ref index)) return false; } else { if (!SkipIPv4Literal (text, endIndex, ref index)) return false; } if (index >= endIndex || text[index++] != ']') return false; match.EndIndex = index; return true; }
static bool GetAddrspecStartIndex (UrlMatch match, char[] text, int startIndex, int matchIndex, int endIndex) { int index = matchIndex - 1; if (matchIndex == startIndex) return false; do { if (!SkipWordBackwards (text, startIndex, ref index)) return false; if (index == startIndex) break; if (text[index - 1] != '.') break; index -= 2; if (index <= startIndex) return false; } while (true); match.StartIndex = index; return true; }
static char GetClosingBrace (UrlMatch match, char[] text, int startIndex) { if (match.StartIndex == startIndex) return '\0'; switch (text[match.StartIndex - 1]) { case '(': return ')'; case '{': return '}'; case '[': return ']'; case '<': return '>'; case '|': return '|'; default: return '\0'; } }