C# StreamReader, “ReadLine” For Custom Delimiters

前端 未结 4 708
长发绾君心
长发绾君心 2021-02-18 22:13

What is the best way to have the functionality of the StreamReader.ReadLine() method, but with custom (String) delimiters?

I\'d like to do something like:

4条回答
  •  误落风尘
    2021-02-18 22:59

    This code should work for any string separator.

    public static IEnumerable ReadChunks(this TextReader reader, string chunkSep)
    {
        var sb = new StringBuilder();
    
        var sepbuffer = new Queue(chunkSep.Length);
        var sepArray = chunkSep.ToCharArray();
    
        while (reader.Peek() >= 0)
        {
            var nextChar = (char)reader.Read();
            if (nextChar == chunkSep[sepbuffer.Count])
            {
                sepbuffer.Enqueue(nextChar);
                if (sepbuffer.Count == chunkSep.Length)
                {
                    yield return sb.ToString();
                    sb.Length = 0;
                    sepbuffer.Clear();
                }
            }
            else
            {
                sepbuffer.Enqueue(nextChar);
                while (sepbuffer.Count > 0)
                {
                    sb.Append(sepbuffer.Dequeue());
                    if (sepbuffer.SequenceEqual(chunkSep.Take(sepbuffer.Count)))
                        break;
                }
            }
        }
        yield return sb.ToString() + new string(sepbuffer.ToArray());
    }
    

    Disclaimer:

    I made a little testing on this and is actually slower than ReadLine method, but I suspect it is due to the enqueue/dequeue/sequenceEqual calls that in the ReadLine method can be avoided (because the separator is always \r\n).

    Again, I made few tests and it should work, but don't take it as perfect, and feel free to correct it. ;)

提交回复
热议问题