Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using NUnit.Framework;
- namespace TableParser
- {
- [TestFixture]
- public class QuotedFieldTaskTests
- {
- [TestCase("''", 0, "", 2)]
- [TestCase("'a'", 0, "a", 3)]
- [TestCase("asd\"bad\"ds", 3, "bad", 5)]
- [TestCase("\"\\\\\" b", 0, "\\", 4)]
- [TestCase("'a\\\' b'", 0, "a' b", 7)]
- [TestCase("'\\\\\\\\\\\\\\\\\\' 1", 0, "\\\\\\\\' 1", 13)]
- [TestCase("\"a \\\"c\\\"\"", 0, "a \"c\"", 9)]
- public void Test(string line, int startIndex, string expectedValue, int expectedLength)
- {
- var actualToken = QuotedFieldTask.ReadQuotedField(line, startIndex);
- Assert.AreEqual(new Token(expectedValue, startIndex, expectedLength), actualToken);
- }
- }
- class QuotedFieldTask
- {
- public static Token ReadQuotedField(string line, int startIndex)
- {
- string value = "";
- bool flag = false;
- if ((line[startIndex] == '"') || (line[startIndex] == '\''))
- {
- for (int i = startIndex + 1; i < line.Length; i++)
- {
- if ((line[i] == '\\') && (flag == false))
- {
- flag = true;
- continue;
- }
- if ((line[i] == line[startIndex]) && (flag == false))
- return new Token(value, startIndex, i - startIndex + 1);
- value += line[i];
- flag = false;
- }
- }
- else
- {
- for (int i = startIndex; i < line.Length; i++)
- {
- if ((line[i] == '"') || (line[i] == '\'')) return new Token(value, startIndex, i - startIndex + 1);
- value += line[i];
- }
- }
- return new Token(value, startIndex, line.Length - startIndex);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement