mikhailemv

Untitled

Oct 20th, 2022
1,091
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.42 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("'q'", 0, "q", 3)]
  15.         [TestCase("\"qw'e", 0, "qw'e", 5)]
  16.         [TestCase("q \"'w'\"\"'", 2, "'w'", 5)]
  17.         [TestCase(@"'q\' w'", 0, "q' w", 7)]
  18.         public void Test(string line, int startIndex, string expectedValue, int expectedLength)
  19.         {
  20.             var actualToken = QuotedFieldTask.ReadQuotedField(line, startIndex);
  21.             Assert.AreEqual(new Token(expectedValue, startIndex, expectedLength), actualToken);
  22.         }
  23.     }
  24.    
  25.     class QuotedFieldTask
  26.     {
  27.         public static Token ReadQuotedField(string line, int startIndex)
  28.         {
  29.             var tokenString = new StringBuilder();
  30.             var quote = line[startIndex];
  31.             var index = startIndex + 1;
  32.            
  33.             for (; index < line.Length; index++)
  34.             {
  35.                 if (line[index] == quote)
  36.                     break;
  37.  
  38.                 if (line[index] == '\\')
  39.                     index++;
  40.  
  41.                 tokenString.Append(line[index]);
  42.             }
  43.  
  44.             if (index < line.Length)
  45.                 index++;
  46.  
  47.             return new Token(tokenString.ToString(), startIndex, index - startIndex);
  48.         }
  49.     }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment