Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Linq;
- using System.Net.Mail;
- using System.Text;
- using System.Windows.Forms;
- namespace TableParser
- {
- public class FieldsParserTask
- {
- private const char BackSlash = '\\';
- private static readonly char[] Quotes =
- {
- '\"', '\''
- };
- private enum TypeOfTocken
- {
- Simple,
- Сomposite
- }
- private enum InstructionsCurrentChar
- {
- Break,
- Shielding,
- Append,
- Skip,
- SkipQuotes,
- BreakСomposite
- }
- public static List<string> ParseLine(string line)
- {
- var parsedField = new List<string>();
- var currentIndex = 0;
- while (currentIndex < line.Length)
- {
- Token currentToken = ReadField(line, currentIndex);
- if (IsCorrectToken(currentToken))
- parsedField.Add(currentToken.Value);
- currentIndex = currentToken.GetIndexNextToToken();
- }
- return parsedField;
- }
- private static InstructionsCurrentChar CommandForSimpleToken(char currentChar)
- {
- if (currentChar == ' ' || Quotes.Contains(currentChar))
- return InstructionsCurrentChar.Break;
- return InstructionsCurrentChar.Append;
- }
- private static InstructionsCurrentChar CommandForСompositeToken(char currentChar, ref char typeQuotes)
- {
- if (Quotes.Contains(currentChar))
- {
- if (typeQuotes == '.')
- {
- typeQuotes = currentChar;
- return InstructionsCurrentChar.Skip;
- }
- if (currentChar == typeQuotes)
- return InstructionsCurrentChar.BreakСomposite;
- return InstructionsCurrentChar.SkipQuotes;
- }
- if (currentChar == BackSlash)
- return InstructionsCurrentChar.Shielding;
- return InstructionsCurrentChar.Append;
- }
- private static Token AssemblyToken(string line, int startIndex, TypeOfTocken type)
- {
- var tokensLength = 0;
- var tokensValue = new StringBuilder();
- var typeQuotes = '.';
- var skipingQuotes = false;
- for (var index = startIndex; index < line.Length; index++)
- {
- tokensLength++;
- var command = type == TypeOfTocken.Simple
- ? CommandForSimpleToken(line[index])
- : CommandForСompositeToken(line[index], ref typeQuotes);
- if (command == InstructionsCurrentChar.Append)
- tokensValue.Append(line[index]);
- else if (command == InstructionsCurrentChar.Shielding)
- {
- index++;
- tokensLength++;
- tokensValue.Append(line[index]);
- }
- else if (command == InstructionsCurrentChar.SkipQuotes)
- {
- if (skipingQuotes)
- {
- tokensValue = new StringBuilder(tokensValue.ToString().Substring(1));
- }
- else
- {
- tokensValue.Append(line[index]);
- }
- skipingQuotes = true;
- }
- else if (command == InstructionsCurrentChar.Skip)
- continue;
- else
- break;
- }
- return new Token(tokensValue.ToString(), startIndex, tokensLength);
- }
- private static Token ReadField(string line, int startIndex)
- {
- var type = Quotes.Contains(line[startIndex]) ? TypeOfTocken.Сomposite : TypeOfTocken.Simple;
- var currentToken = AssemblyToken(line, startIndex, type);
- return currentToken;
- }
- private static bool IsCorrectToken(Token token)
- {
- return token.Value != "" || token.Length == 2;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement