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, "", 2)]
- [TestCase("\'a\'", 0, "a", 3)]
- [TestCase(@"some text ""QF \"""" other text", 10, "QF \"", 7)]
- [TestCase("\"a", 0, "a", 2)]
- 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 result = new StringBuilder();
- var whatQuote = line[startIndex];
- var lastQuote = line.LastIndexOf(whatQuote);
- var endIndex = lastQuote;
- if (lastQuote == 0 || lastQuote == startIndex)
- {
- lastQuote = line.Length - 1;
- endIndex = lastQuote + 1;
- }
- var deltaIndex = lastQuote - startIndex;
- line = DeleteSlashes(line, startIndex, endIndex);
- return new Token(line, startIndex, deltaIndex + 1);
- }
- public static string DeleteSlashes(string line, int startIndex, int endIndex)
- {
- var result = new StringBuilder();
- for (var i = startIndex + 1; i < endIndex; i++)
- {
- if (line[i] == '\\')
- {
- if (line[i + 1] == '\"' || line[i + 1] == '\'' || line[i + 1] == '\\')
- {
- result.Append(line[i + 1]);
- i++;
- }
- }
- else result.Append(line[i]);
- }
- return result.ToString();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement