Untitled

mail@pastecode.io avatar
unknown
csharp
7 months ago
1.3 kB
5
Indexable
Never
class QuotedFieldTask
{
    public static Token ReadQuotedField(string line, int startIndex)
    {
        var builder = new StringBuilder();
        var tokenStartIndex = startIndex;
        var quotes = 0;
        var isShielded = false;
        var numberOfShieldedSymbols = 0;
        
        for (var index = 0; index < line[startIndex..].Length; index++)
        {
            var character = line[startIndex..][index];
            if (character == line[startIndex..][0] && !isShielded)
            {
                if (quotes == 0)
                {
                    tokenStartIndex += index;
                    quotes++;
                    continue;
                }
                quotes++;
                if(quotes == 2) break;
            }
            isShielded = IsShielded(character);
            if(isShielded)
            {
                numberOfShieldedSymbols++;
                continue;
            }
            if (quotes > 0) builder.Append(character);
        }
        var length = quotes + builder.Length + numberOfShieldedSymbols;
        return new Token(builder.ToString(), tokenStartIndex, length);
    }
    private static bool IsShielded(char character)
    {
        return character.ToString() == @"\";
    }
}