private static int GetNextNonEmptyOrWhitespaceIndex(
            string input,
            int startIndex,
            out bool separatorFound)
        {
            Debug.Assert(input != null);
            Debug.Assert(startIndex <= input.Length); // it's OK if index == value.Length.

            separatorFound = false;
            var current = startIndex + HttpTokenParsingRules.GetWhitespaceLength(input, startIndex);

            if ((current == input.Length) || (input[current] != ','))
            {
                return(current);
            }

            // If we have a separator, skip the separator and all following whitespaces, and
            // continue until the current character is neither a separator nor a whitespace.
            separatorFound = true;
            current++; // skip delimiter.
            current = current + HttpTokenParsingRules.GetWhitespaceLength(input, current);

            while ((current < input.Length) && (input[current] == ','))
            {
                current++; // skip delimiter.
                current = current + HttpTokenParsingRules.GetWhitespaceLength(input, current);
            }

            return(current);
        }
            private static int GetValueLength(string input, int startIndex, out StringSegment value)
            {
                var current = startIndex;

                current++; // skip '='.
                current += HttpTokenParsingRules.GetWhitespaceLength(input, current);

                var valueLength = HttpTokenParsingRules.GetTokenLength(input, current);

                if (valueLength == 0)
                {
                    // A value can either be a token or a quoted string. Check if it is a quoted string.
                    var result = HttpTokenParsingRules.GetQuotedStringLength(input, current, out valueLength);
                    if (result != HttpParseResult.Parsed)
                    {
                        // We have an invalid value. Reset the name and return.
                        value = default(StringSegment);
                        return(0);
                    }

                    // Quotation marks are not part of a quoted parameter value.
                    value = new StringSegment(input, current + 1, valueLength - 2);
                }
                else
                {
                    value = new StringSegment(input, current, valueLength);
                }

                current += valueLength;
                current += HttpTokenParsingRules.GetWhitespaceLength(input, current);

                return(current - startIndex);
            }
        private static int GetSubtypeLength(string input, int offset, out StringSegment subType)
        {
            var current = offset;

            // Parse the separator between type and subtype
            if (current < 0 || current >= input.Length || input[current] != '/')
            {
                subType = default(StringSegment);
                return(0);
            }

            current++; // skip delimiter.
            current += HttpTokenParsingRules.GetWhitespaceLength(input, current);

            var subtypeLength = HttpTokenParsingRules.GetTokenLength(input, current);

            if (subtypeLength == 0)
            {
                subType = default(StringSegment);
                return(0);
            }

            subType = new StringSegment(input, current, subtypeLength);

            current += subtypeLength;
            current += HttpTokenParsingRules.GetWhitespaceLength(input, current);

            return(current - offset);
        }
        // All GetXXXLength methods work in the same way. They expect to be on the right position for
        // the token they are parsing, for example, the beginning of the media type or the delimiter
        // from a previous token, like '/', ';' or '='.
        // Each method consumes the delimiter token if any, the leading whitespace, then the given token
        // itself, and finally the trailing whitespace.
        private static int GetTypeLength(string input, int offset, out StringSegment type)
        {
            if (offset < 0 || offset >= input.Length)
            {
                type = default(StringSegment);
                return(0);
            }

            var current = offset + HttpTokenParsingRules.GetWhitespaceLength(input, offset);

            // Parse the type, i.e. <type> in media type string "<type>/<subtype>; param1=value1; param2=value2"
            var typeLength = HttpTokenParsingRules.GetTokenLength(input, current);

            if (typeLength == 0)
            {
                type = default(StringSegment);
                return(0);
            }

            type = new StringSegment(input, current, typeLength);

            current += typeLength;
            current += HttpTokenParsingRules.GetWhitespaceLength(input, current);

            return(current - offset);
        }
            private static int GetNameLength(string input, int startIndex, out StringSegment name)
            {
                var current = startIndex;

                current++; // skip ';'
                current += HttpTokenParsingRules.GetWhitespaceLength(input, current);

                var nameLength = HttpTokenParsingRules.GetTokenLength(input, current);

                if (nameLength == 0)
                {
                    name = default(StringSegment);
                    return(0);
                }

                name = new StringSegment(input, current, nameLength);

                current += nameLength;
                current += HttpTokenParsingRules.GetWhitespaceLength(input, current);

                return(current - startIndex);
            }