MimeTypeParameterInfo Structure

Provides the information stored in an Internet Media Type Parameter-String.

Definition

Namespace: FolkerKinzel.MimeTypes
Assembly: FolkerKinzel.MimeTypes (in FolkerKinzel.MimeTypes.dll) Version: 1.0.0+7dd5ca03f25d4802263abb2083f9c7add2cb51ec
C#
public readonly struct MimeTypeParameterInfo : ICloneable, 
	IComparable<MimeTypeParameterInfo>, IEquatable<MimeTypeParameterInfo>
Inheritance
Object    ValueType    MimeTypeParameterInfo
Implements
ICloneable, IComparableMimeTypeParameterInfo, IEquatableMimeTypeParameterInfo

Remarks

  Tip

MimeTypeParameterInfo is a quite large structure. Pass it to other methods by reference (in, ref or out parameters in C#)!

If you intend to hold a MimeTypeParameterInfo for a long time in memory and if this MimeTypeParameterInfo is parsed from a ReadOnlyMemory<Char> that comes from a very long String, keep in mind, that the MimeTypeParameterInfo holds a reference to that String. Consider in this case to make a copy of the MimeTypeInfo structure with Clone: The copy is built on a separate String that is case-normalized and only as long as needed.

Example

Efficient parsing of an Internet Media Type String:

C#
using FolkerKinzel.MimeTypes;

namespace Examples;

public static class MimeTypeInfoExample
{
    public static void Example()
    {
        const string input = """
        This is some text before the MIME type.
                             text/plain; charset=iso-8859-1; (This is a comment.)
        second-parameter*0*=utf-8'en'For%20demonstration%20purposes%20o;
        second-parameter*1*=nly%2C%20with%20a%20few%20non-ASCII%20chara;
        second-parameter*2*=cters%20%C3%A4%C3%B6%C3%BC

                        This is some Text after the MIME type.
        """;

        // MimeTypeInfo parses a specified part of a longer string
        // without having to allocate a substring.
        // White space characters before and after the MIME type are accepted.
        // The return values of the properties are portions of the input in form
        // of ReadOnlySpan<char> structs.

        MimeTypeInfo info = MimeTypeInfo.Parse(input.AsMemory(39, 275));

        Console.WriteLine("Media Type: {0}", info.MediaType.ToString());
        Console.WriteLine("Sub Type:   {0}", info.SubType.ToString());
        Console.WriteLine();
        Console.WriteLine("Is empty:        {0}", info.IsEmpty);
        Console.WriteLine("Is text:         {0}", info.IsText);
        Console.WriteLine("Is plain text:   {0}", info.IsTextPlain);
        Console.WriteLine("Is octet stream: {0}", info.IsOctetStream);

        Console.WriteLine("The file type extension for this MIME type is \"{0}\".",
                           info.GetFileTypeExtension());

        int parameterCounter = 1;
        foreach (MimeTypeParameterInfo parameter in info.Parameters())
        {
            Console.WriteLine();
            Console.WriteLine($"Parameter {parameterCounter++}:");
            Console.WriteLine("============");
            Console.WriteLine($"Key:       {parameter.Key}");
            Console.WriteLine($"Value:     {parameter.Value}");
            Console.WriteLine($"Language:  {parameter.Language}");
            Console.WriteLine($"Charset:   {parameter.CharSet}");
            Console.WriteLine("Is charset parameter:       {0}", parameter.IsCharSetParameter);
            Console.WriteLine("Is ASCII charset parameter: {0}", parameter.IsAsciiCharSetParameter);
            Console.WriteLine("Is access type parameter:   {0}", parameter.IsAccessTypeParameter);
            Console.WriteLine("Is value case sensitive:    {0}", parameter.IsValueCaseSensitive);
        }
        Console.WriteLine();

        // Compare MimeTypeInfo values using options:
        MimeTypeInfo info2 = MimeTypeInfo.Parse("TEXT/PLAIN; CHARSET=UTF-8");
        Console.WriteLine("Equal with parameters:      {0}", info.Equals(in info2));
        Console.WriteLine("Equal without parameters:   {0}", info.Equals(in info2, ignoreParameters: true));

        Console.WriteLine();
        Console.WriteLine("Default:           {0}", info2.ToString());
        Console.WriteLine("Ignore Parameters: {0}", info2.ToString(MimeFormats.IgnoreParameters));
    }
}
/*
Console output: 

Media Type: text
Sub Type:   plain

Is empty:        False
Is text:         True
Is plain text:   True
Is octet stream: False
The file type extension for this MIME type is ".txt".

Parameter 1:
============
Key:       charset
Value:     iso-8859-1
Language:
Charset:
Is charset parameter:       True
Is ASCII charset parameter: False
Is access type parameter:   False
Is value case sensitive:    False

Parameter 2:
============
Key:       second-parameter
Value:     For demonstration purposes only, with a few non-ASCII characters äöü
Language:  en
Charset:   utf-8
Is charset parameter:       False
Is ASCII charset parameter: False
Is access type parameter:   False
Is value case sensitive:    True

Equal with parameters:      False
Equal without parameters:   True

Default:           text/plain; charset=utf-8
Ignore Parameters: text/plain
 */

Properties

CharSet Gets the character set in which Value is encoded.
Empty Gets an empty MimeTypeParameterInfo structure.
IsAccessTypeParameter Indicates whether the MimeTypeParameterInfo has the Key "access-type". The comparison is case-insensitive.
IsAsciiCharSetParameter Indicates whether this instance equals "charset=us-ascii". The comparison is case-insensitive.
IsCharSetParameter Indicates whether the MimeTypeParameterInfo has the Key "charset". The comparison is case-insensitive.
IsEmpty Indicates whether the instance contains no data.
IsValueCaseSensitive Indicates whether the Value should be treated case sensitive.
Key Gets the name of the parameter.
Language Gets an IETF-Language tag that indicates the language of the parameter's value.
Value Gets the value of the parameter.

Methods

AppendTo Appends a String representation of this instance according to RFC 2045 and RFC 2231 to a StringBuilder.
Clone Creates a new MimeTypeParameterInfo that is a copy of the current instance.
CompareTo Compares the current instance with another MimeTypeParameterInfo and returns an Int32 that indicates whether the current instance precedes, follows, or occurs in the same position in the sort order as the other MimeTypeParameterInfo.
Equals(MimeTypeParameterInfo) Determines if the content of other is equal to that of the current instance.
Equals(MimeTypeParameterInfo) Determines if the content of other is equal to that of the current instance.
Equals(Object) Determines whether obj is a MimeTypeParameterInfo structure whose content is equal to that of the current instance.
(Overrides ValueTypeEquals(Object))
GetHashCode Computes a hash code for the instance.
(Overrides ValueTypeGetHashCode)
GetTypeGets the Type of the current instance.
(Inherited from Object)
ToString Creates a String representation of the instance.
(Overrides ValueTypeToString)
ToString(Boolean) Creates a String representation of the instance.

Operators

Equality(MimeTypeParameterInfo, MimeTypeParameterInfo) Returns a value that indicates whether two specified MimeTypeParameterInfo values are equal.
Inequality(MimeTypeParameterInfo, MimeTypeParameterInfo) Returns a value that indicates whether two specified MimeTypeParameterInfo values are not equal.

Explicit Interface Implementations

ICloneableCloneCreates a new object that is a copy of the current instance.

See Also