Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.11 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(@"""\\""", 0, @"\", 4)]
  16. [TestCase(@"hello world ""hello world 'hello'""", 12, "hello world 'hello'", 21)]
  17. public void Test(string line, int startIndex, string expectedValue, int expectedLength)
  18. {
  19. var actualToken = QuotedFieldTask.ReadQuotedField(line, startIndex);
  20. Assert.AreEqual(actualToken, new Token(expectedValue, startIndex, expectedLength));
  21. }
  22. }
  23.  
  24. class QuotedFieldTask
  25. {
  26. public static Token ReadQuotedField(string line, int startIndex)
  27. {
  28. var newLine = new StringBuilder();
  29. var stopIndex = 0;
  30. var index = startIndex + 1;
  31. var length = 0;
  32. while (index < line.Length)
  33. {
  34. if (line[index] == '\\' && (line[index + 1] == '\"'
  35. || line[index + 1] == '\''))
  36. {
  37. if (line.Length - 1 == index + 1)
  38. break;
  39. newLine.Append(line[index + 1]);
  40. index += 2;
  41. length++;
  42. continue;
  43. }
  44. if (line[index] == '\\')
  45. {
  46. index++;
  47. if (index >= line.Length)
  48. break;
  49. }
  50. if (line[index] == line[startIndex])
  51. {
  52. stopIndex = index;
  53. break;
  54. }
  55. newLine.Append(line[index]);
  56. index++;
  57. }
  58. if (stopIndex == 0)
  59. length += line.Length - startIndex;
  60. else
  61. length += stopIndex - startIndex + 1;
  62. return new Token(newLine.ToString(), startIndex, length);
  63. }
  64. }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement