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("'q'", 0, "q", 3)]
- [TestCase("\"qw'e", 0, "qw'e", 5)]
- [TestCase("q \"'w'\"\"'", 2, "'w'", 5)]
- [TestCase(@"'q\' w'", 0, "q' w", 7)]
- 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)
- {
- var tokenString = new StringBuilder();
- var quote = line[startIndex];
- var index = startIndex + 1;
- for (; index < line.Length; index++)
- {
- if (line[index] == quote)
- break;
- if (line[index] == '\\')
- index++;
- tokenString.Append(line[index]);
- }
- if (index < line.Length)
- index++;
- return new Token(tokenString.ToString(), startIndex, index - startIndex);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment