Advertisement
Guest User

Untitled

a guest
Oct 20th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.83 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 System.Windows.Forms;
  7. using NUnit.Framework;
  8.  
  9. namespace TableParser
  10. {
  11.     [TestFixture]
  12.     public class QuotedFieldTaskTests
  13.     {
  14.         [TestCase("''", 0, "", 2)]
  15.         [TestCase("'a'", 0, "a", 3)]
  16.         public void Test(string line, int startIndex, string expectedValue, int expectedLength)
  17.         {
  18.             var actualToken = QuotedFieldTask.ReadQuotedField(line, startIndex);
  19.             Assert.AreEqual(actualToken, new Token(expectedValue, startIndex, expectedLength));
  20.         }
  21.  
  22.         // Добавьте свои тесты
  23.     }
  24.  
  25.     class QuotedFieldTask
  26.     {
  27.         public static Token ReadQuotedField(string line, int startIndex)
  28.         {
  29.             var character = line[startIndex];
  30.             var len = 1;
  31.             var value = "";
  32.             var slash = false;
  33.             for (var i = startIndex + 1; i <= line.Length; i++)
  34.             {
  35.                 if (i == line.Length)
  36.                 {
  37.                     len = line.Length - startIndex;
  38.                     break;
  39.                 }
  40.                 if (line[i] == '\\' && !slash)
  41.                 {
  42.                     slash = true;
  43.                     len++;
  44.                 }
  45.                 else if (slash)
  46.                 {
  47.                     len++;
  48.                     value += line[i];
  49.                     slash = !slash;
  50.                 }
  51.                 else if (line[i] == character)
  52.                 {
  53.                     len++;
  54.                     break;
  55.                 }
  56.                 else
  57.                 {
  58.                     len++;
  59.                     value += line[i];
  60.                 }
  61.             }
  62.             return new Token(value, startIndex, len);
  63.         }
  64.     }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement