Skip to content

ShellStream.Expect(Regex, TimeSpan) can return "too much" string #454

Closed
@keithallenjackson

Description

@keithallenjackson

ShellStream.Expect(Regex, TimeSpan) properly matches and dequeues data from the _incoming data queue. However, once a match is made, instead of returning all text up to the point of a match, it returns all data within the queue.

Current:

public string Expect(Regex regex, TimeSpan timeout)
        {
           var text = string.Empty;

            while (true)
            {
                lock (_incoming)
                {
                    if (_incoming.Count > 0)
                    {
                        text = _encoding.GetString(_incoming.ToArray(), 0, _incoming.Count);
                    }

                    var match = regex.Match(text);

                    if (match.Success)
                    {
                        //  Remove processed items from the queue
                        for (var i = 0; i < match.Index + match.Length && _incoming.Count > 0; i++)
                        {
                            _incoming.Dequeue();
                        }
                        break;
                    }
                }

               if (timeout.Ticks > 0)
                    {
                        if (!_dataReceived.WaitOne(timeout))
                        {
                            return null;
                        }
                    }
                    else
                    {
                        _dataReceived.WaitOne();
                    }
           }

            return text;
        }

Fix:

public string Expect(Regex regex, TimeSpan timeout)
        {
           var text = string.Empty;

            while (true)
            {
                lock (_incoming)
                {
                    if (_incoming.Count > 0)
                    {
                        text = _encoding.GetString(_incoming.ToArray(), 0, _incoming.Count);
                    }

                    var match = regex.Match(text);

                    if (match.Success)
                    {
                        //  Remove processed items from the queue
                        for (var i = 0; i < match.Index + match.Length && _incoming.Count > 0; i++)
                        {
                            _incoming.Dequeue();
                        }
                        text = text.Substring(0, match.Index + match.Length); // <-- This one line of code fixes it!
                        break;
                    }
                }

               if (timeout.Ticks > 0)
                    {
                        if (!_dataReceived.WaitOne(timeout))
                        {
                            return null;
                        }
                    }
                    else
                    {
                        _dataReceived.WaitOne();
                    }
           }

            return text;
        }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions