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(@"""\\""", 0, @"\", 4)]
- [TestCase(@"hello world ""hello world 'hello'""", 12, "hello world 'hello'", 21)]
- public void Test(string line, int startIndex, string expectedValue, int expectedLength)
- {
- var actualToken = QuotedFieldTask.ReadQuotedField(line, startIndex);
- Assert.AreEqual(actualToken, new Token(expectedValue, startIndex, expectedLength));
- }
- }
- class QuotedFieldTask
- {
- public static Token ReadQuotedField(string line, int startIndex)
- {
- var newLine = new StringBuilder();
- var stopIndex = 0;
- var index = startIndex + 1;
- var length = 0;
- while (index < line.Length)
- {
- if (line[index] == '\\' && (line[index + 1] == '\"'
- || line[index + 1] == '\''))
- {
- if (line.Length - 1 == index + 1)
- break;
- newLine.Append(line[index + 1]);
- index += 2;
- length++;
- continue;
- }
- if (line[index] == '\\')
- {
- index++;
- if (index >= line.Length)
- break;
- }
- if (line[index] == line[startIndex])
- {
- stopIndex = index;
- break;
- }
- newLine.Append(line[index]);
- index++;
- }
- if (stopIndex == 0)
- length += line.Length - startIndex;
- else
- length += stopIndex - startIndex + 1;
- return new Token(newLine.ToString(), startIndex, length);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement