예제 #1
0
        /// <summary>
        ///     Reads the next word using the given IDelimiters. If a delimiter
        ///     can be matched to the beginning of the string, it will be
        ///     preferred. Otherwise, the text up to the first delimiter will be
        ///     returned.
        /// </summary>
        /// <returns>The next word from the stream.</returns>
        /// <param name="delimiter">
        ///     The delimiter that defines word boundaries. In practice, DelimiterCollection is
        ///     likely the best type to use here.
        /// </param>
        public string Read(IDelimiter delimiter)
        {
            // Read until we have a match.
            string word = Read(s =>
            {
                var m = delimiter.Match(s);
                if (!m.Success)
                {
                    return(-1);
                }
                return(m.Index);
            });

            // Read until we find a delimiter match.
            if (String.IsNullOrEmpty(word))
            {
                word = Read(s =>
                {
                    int matchLength = delimiter.Match(s).Length;
                    return(matchLength == 0 || matchLength == s.Length ? -1 : matchLength);
                });
            }
            return(word);
        }