Advertisement
Guest User

Untitled

a guest
Oct 20th, 2019
451
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.07 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  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. [TestCase("'abc'", 0, "abc", 5)]
  17. [TestCase("'a\\\''", 0, "a\'", 5)]
  18. [TestCase("\'\'", 0, "", 2)]
  19. [TestCase("'a\"\"'", 0, "a\"\"", 5)]
  20. [TestCase("'b\"a'\"", 2, "a\'", 4)]
  21.  
  22. //[TestCase("\'\'\'\'", 0, "", 5)]
  23. //[TestCase("\'a\'", 0, "a", 3)]
  24.  
  25. public void Test(string line, int startIndex, string expectedValue, int expectedLength)
  26. {
  27. var actualToken = QuotedFieldTask.ReadQuotedField(line, startIndex);
  28. Assert.AreEqual(actualToken, new Token(expectedValue, startIndex, expectedLength));
  29. }
  30.  
  31. // Добавьте свои тесты
  32. }
  33.  
  34. class QuotedFieldTask
  35. {
  36. public static Token ReadQuotedField(string line, int startIndex)
  37. {
  38. var q = line[startIndex];
  39. var otherQ = q.ToString() == "\'" ? "\"" : "\'";
  40. var i = startIndex+1;
  41. while (line[i] != q || line.Length>1 && line[i] == q && line[i - 1].ToString() == "\\")
  42. {
  43. if (line[i] == q && line[i - 1].ToString() == "\\") i++;
  44. if (i == line.Length - 1) break;
  45. i++;
  46. }
  47.  
  48. line = line.Substring(startIndex+1, i - startIndex-1);
  49. Console.WriteLine(line);
  50.  
  51. var res = "";
  52. var j = 0;
  53. while (j < line.Length)
  54. {
  55. if (j == line.Length-1)
  56. {
  57. res+= line[line.Length-1];
  58. break;
  59. }
  60. if ((line[j].ToString() == "\\" || line[j].ToString() == otherQ))
  61. {
  62. j++;
  63. continue;
  64. }
  65. res += line[j];
  66. j++;
  67. }
  68.  
  69. return new Token(res, startIndex, line.Length+2);
  70. }
  71. }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement