private void ExtractTokens(FastReplacerSnippet snippet) { int last = 0; while (last < snippet.Text.Length) { // Find next token position in snippet.Text: int start = snippet.Text.IndexOf(TokenOpen, last, StringComparison.InvariantCultureIgnoreCase); if (start == -1) { return; } int end = snippet.Text.IndexOf(TokenClose, start + TokenOpen.Length, StringComparison.InvariantCultureIgnoreCase); if (end == -1) { throw new ArgumentException(string.Format("Token is opened but not closed in text \"{0}\".", snippet.Text)); } int eol = snippet.Text.IndexOf('\n', start + TokenOpen.Length); if (eol != -1 && eol < end) { last = eol + 1; continue; } // Take the token from snippet.Text: end += TokenClose.Length; string token = snippet.Text.Substring(start, end - start); string context = snippet.Text; ValidateToken(token, context, true); // Add the token to the dictionary: var tokenOccurrence = new TokenOccurrence { Snippet = snippet, Start = start, End = end }; List <TokenOccurrence> occurrences; if (OccurrencesOfToken.TryGetValue(token, out occurrences)) { occurrences.Add(tokenOccurrence); } else { OccurrencesOfToken.Add(token, new List <TokenOccurrence> { tokenOccurrence }); } last = end; } }
private void ExtractTokens(FastReplacerSnippet snippet) { int last = 0; while (last < snippet.Text.Length) { //TokenOpen的位置 int start = snippet.Text.IndexOf(TokenOpen, last); //未找到tokenopen if (start == -1) { return; } //TokenClose的位置 int end = snippet.Text.IndexOf(TokenClose, start + TokenOpen.Length); //未找到TokenClose,表明没有正确闭合 if (end == -1) { throw new ArgumentException(string.Format("Token is opened but not closed in text \"{0}\".", snippet.Text)); } end += TokenClose.Length; //截取token string token = snippet.Text.Substring(start, end - start); string context = snippet.Text; ValidateToken(token, context, true); //组织token数据到字典上 以token为key TokenOccurrence包含token的起始位置与结束位置 TokenOccurrence tokenOccurrence = new TokenOccurrence { Snippet = snippet, Start = start, End = end }; List <TokenOccurrence> occurrences; if (OccurrencesOfToken.TryGetValue(token, out occurrences)) { occurrences.Add(tokenOccurrence); } else { OccurrencesOfToken.Add(token, new List <TokenOccurrence> { tokenOccurrence }); } last = end; } }
private void ExtractTokens(FastReplacerSnippet snippet) { int last = 0; while (last < snippet.Text.Length) { // Find next token position in snippet.Text: int start = snippet.Text.IndexOf(TokenOpen, last, StringComparison.InvariantCultureIgnoreCase); if (start == -1) return; int end = snippet.Text.IndexOf(TokenClose, start + TokenOpen.Length, StringComparison.InvariantCultureIgnoreCase); if (end == -1) throw new ArgumentException(string.Format("Token is opened but not closed in text \"{0}\".", snippet.Text)); int eol = snippet.Text.IndexOf('\n', start + TokenOpen.Length); if (eol != -1 && eol < end) { last = eol + 1; continue; } // Take the token from snippet.Text: end += TokenClose.Length; string token = snippet.Text.Substring(start, end - start); string context = snippet.Text; ValidateToken(token, context, true); // Add the token to the dictionary: var tokenOccurrence = new TokenOccurrence { Snippet = snippet, Start = start, End = end }; List<TokenOccurrence> occurrences; if (OccurrencesOfToken.TryGetValue(token, out occurrences)) occurrences.Add(tokenOccurrence); else OccurrencesOfToken.Add(token, new List<TokenOccurrence> { tokenOccurrence }); last = end; } }
private void ExtractTokens(FastReplacerSnippet snippet) { int last = 0; while (last < snippet.Text.Length) { //TokenOpen的位置 int start = snippet.Text.IndexOf(TokenOpen, last); //未找到tokenopen if (start == -1) return; //TokenClose的位置 int end = snippet.Text.IndexOf(TokenClose, start + TokenOpen.Length); //未找到TokenClose,表明没有正确闭合 if (end == -1) throw new ArgumentException(string.Format("Token is opened but not closed in text \"{0}\".", snippet.Text)); end += TokenClose.Length; //截取token string token = snippet.Text.Substring(start, end - start); string context = snippet.Text; ValidateToken(token, context, true); //组织token数据到字典上 以token为key TokenOccurrence包含token的起始位置与结束位置 TokenOccurrence tokenOccurrence = new TokenOccurrence { Snippet = snippet, Start = start, End = end }; List<TokenOccurrence> occurrences; if (OccurrencesOfToken.TryGetValue(token, out occurrences)) { occurrences.Add(tokenOccurrence); } else { OccurrencesOfToken.Add(token, new List<TokenOccurrence> { tokenOccurrence }); } last = end; } }