Advertisement
osence

OOP 4

Oct 29th, 2020
548
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.05 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using NUnit.Framework;
  7.  
  8. namespace TableParser
  9. {
  10.     [TestFixture]
  11.     public class QuotedFieldTaskTests
  12.     {
  13.         [TestCase("''", 0, "", 2)]
  14.         [TestCase("'a'", 0, "a", 3)]
  15.         [TestCase("asd\"bad\"ds", 3, "bad", 5)]
  16.         [TestCase("\"\\\\\" b", 0, "\\", 4)]
  17.         [TestCase("'a\\\' b'", 0, "a' b", 7)]
  18.         [TestCase("'\\\\\\\\\\\\\\\\\\' 1", 0, "\\\\\\\\' 1", 13)]
  19.         [TestCase("\"a \\\"c\\\"\"", 0, "a \"c\"", 9)]
  20.         public void Test(string line, int startIndex, string expectedValue, int expectedLength)
  21.         {
  22.             var actualToken = QuotedFieldTask.ReadQuotedField(line, startIndex);
  23.             Assert.AreEqual(new Token(expectedValue, startIndex, expectedLength), actualToken);
  24.         }
  25.     }
  26.  
  27.     class QuotedFieldTask
  28.     {
  29.         public static Token ReadQuotedField(string line, int startIndex)
  30.         {
  31.             string value = "";
  32.             bool flag = false;
  33.             if ((line[startIndex] == '"') || (line[startIndex] == '\''))
  34.             {
  35.                 for (int i = startIndex + 1; i < line.Length; i++)
  36.                 {
  37.                     if ((line[i] == '\\') && (flag == false))
  38.                     {
  39.                         flag = true;
  40.                         continue;
  41.                     }
  42.                     if ((line[i] == line[startIndex]) && (flag == false))
  43.                         return new Token(value, startIndex, i - startIndex + 1);
  44.                     value += line[i];
  45.                     flag = false;
  46.                 }
  47.             }
  48.             else
  49.             {
  50.                 for (int i = startIndex; i < line.Length; i++)
  51.                 {
  52.                     if ((line[i] == '"') || (line[i] == '\'')) return new Token(value, startIndex, i - startIndex + 1);
  53.                     value += line[i];
  54.                 }
  55.             }
  56.             return new Token(value, startIndex, line.Length - startIndex);
  57.         }
  58.     }
  59. }
  60.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement