/
hahahihi
/
csharpTasks
Обзор
Документация
Войти
/
hahahihi
/
csharpTasks
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
csharp/testa.cs
79 строк
3 KB
belovAndrey
create csharp/testa.cs
26 окт 2025, 18:00
26 окт 2025, 18:00
a698695
Код
Авторство
О чём код?
using NUnit.Framework; namespace TableParser; [TestFixture] public class QuotedFieldTaskTests { [TestCase("''", 0, "", 2)] [TestCase("'a'", 0, "a", 3)] [TestCase("\"\"", 0, "", 2)] [TestCase("\"abc\"", 0, "abc", 5)] [TestCase("'a\\'b'", 0, "a'b", 6)] [TestCase("\"a\\\"b\"", 0, "a\"b", 6)] [TestCase("\"a\\\\b\"", 0, "a\\b", 6)] [TestCase("'\\''", 0, "'", 4)] [TestCase("\"\\\"\"", 0, "\"", 4)] [TestCase("'a b c'", 0, "a b c", 7)] [TestCase("\"abc", 0, "abc", 4)] [TestCase("'abc", 0, "abc", 4)] [TestCase("\"a\\\"b\\\"c\"", 0, "a\"b\"c", 9)] // ← длина 9 — правильно [TestCase("'a\\\\b'", 0, "a\\b", 6)] [TestCase("\"a'b'c\"", 0, "a'b'c", 7)] [TestCase("'a\"b\"c'", 0, "a\"b\"c", 7)] public void Test(string line, int startIndex, string expectedValue, int expectedLength) { var actualToken = QuotedFieldTask.ReadQuotedField(line, startIndex); Assert.That(new Token(expectedValue, startIndex, expectedLength), Is.EqualTo(actualToken)); } } public class QuotedFieldTask { public static Token ReadQuotedField(string line, int startIndex) { if (startIndex >= line.Length) return new Token("", startIndex, 0); var quote = line[startIndex]; if (quote != '"' && quote != '\'') return new Token("", startIndex, 0); var (value, length) = ParseQuotedContent(line, startIndex + 1, quote); return new Token(value, startIndex, length); } private static (string value, int totalLength) ParseQuotedContent(string line, int pos, char quote) { var value = new System.Text.StringBuilder(); var i = pos; var escaped = false; while (i < line.Length) { var c = line[i]; if (escaped) { value.Append(c); escaped = false; } else if (c == '\\') escaped = true; else if (c == quote) { // Нашли закрывающую кавычку var tokenLength = i - pos + 2; // +2: открывающая и закрывающая кавычки return (value.ToString(), tokenLength); } else value.Append(c); i++; } // Кавычка не закрыта — поле до конца строки var unclosedLength = i - pos + 1; // +1: только открывающая кавычка учтена в startIndex return (value.ToString(), unclosedLength); } }