Advertisement
Guest User

Untitled

a guest
Oct 18th, 2019
1,045
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.56 KB | None | 0 0
  1. using System.Collections.Generic;
  2. using NUnit.Framework;
  3.  
  4. namespace TableParser
  5. {
  6. [TestFixture]
  7. public class FieldParserTaskTests
  8. {
  9. public static void Test(string input, string[] expectedResult)
  10. {
  11. var actualResult = FieldsParserTask.ParseLine(input);
  12. Assert.AreEqual(expectedResult.Length, actualResult.Count);
  13. for (int i = 0; i < expectedResult.Length; ++i)
  14. {
  15. Assert.AreEqual(expectedResult[i], actualResult[i].Value);
  16. }
  17. }
  18.  
  19. // Скопируйте сюда метод с тестами из предыдущей задачи.
  20. }
  21.  
  22. public class FieldsParserTask
  23. {
  24. // При решении этой задаче постарайтесь избежать создания методов, длиннее 10 строк.
  25. // Подумайте как можно использовать ReadQuotedField и Token в этой задаче.
  26. public static List<Token> ParseLine(string line)
  27. {
  28. var tokensList = new List<Token>();
  29. if (line[0] == '\"' || line[0] == '\'')
  30. tokensList.Add(ReadQuotedField(line, 0));
  31. else
  32. {
  33. var token = ReadField(line, 0);
  34. if (token.Value.Trim() != "")
  35. tokensList.Add(token);
  36. }
  37. while (tokensList[tokensList.Count - 1].GetIndexNextToToken() < line.Length - 1)
  38. {
  39. var index = tokensList[tokensList.Count - 1].GetIndexNextToToken();
  40. if (line[index] == '\"' || line[index] == '\'')
  41. tokensList.Add(ReadQuotedField(line, index));
  42. else
  43. tokensList.Add(ReadField(line, index));
  44. }
  45. for (var i = 0; i < tokensList.Count; i++)
  46. {
  47. if (string.IsNullOrEmpty(tokensList[i].Value))
  48. tokensList.RemoveAt(i);
  49. }
  50. return tokensList; // сокращенный синтаксис для инициализации коллекции.
  51. }
  52.  
  53. private static Token ReadField(string line, int startIndex)
  54. {
  55. var token = QuotedFieldTask.ReadQuotedField(line, startIndex);
  56. return new Token(token.Value.Trim(), token.Position, token.Length);
  57. }
  58.  
  59. public static Token ReadQuotedField(string line, int startIndex)
  60. {
  61. return QuotedFieldTask.ReadQuotedField(line, startIndex);
  62. }
  63. }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement