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; return(0); } current++; // skip delimiter. current += HttpTokenParsingRules.GetWhitespaceLength(input, current); var subtypeLength = HttpTokenParsingRules.GetTokenLength(input, current); if (subtypeLength == 0) { subType = default; return(0); } subType = new StringSegment(input, current, subtypeLength); current += subtypeLength; current += HttpTokenParsingRules.GetWhitespaceLength(input, current); return(current - offset); }
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); }
// 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; 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; return(0); } type = new StringSegment(input, current, typeLength); current += typeLength; current += HttpTokenParsingRules.GetWhitespaceLength(input, current); return(current - offset); }