SasExtensions
Extension methods for Sas.
            
                using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
namespace Azure.Storage.Sas
{
    internal static class SasExtensions
    {
        private const string NoneName = null;
        private const string HttpsName = "https";
        private const string HttpsAndHttpName = "https,http";
        private const string HttpAndHttpsName = "http,https";
        internal static string ToPermissionsString(this AccountSasResourceTypes resourceTypes)
        {
            StringBuilder stringBuilder = new StringBuilder();
            if ((resourceTypes & 1) == 1)
                stringBuilder.Append('s');
            if ((resourceTypes & 2) == 2)
                stringBuilder.Append('c');
            if ((resourceTypes & 4) == 4)
                stringBuilder.Append('o');
            return stringBuilder.ToString();
        }
        internal static AccountSasResourceTypes ParseResourceTypes(string s)
        {
            AccountSasResourceTypes val = 0;
            foreach (char c in s) {
                AccountSasResourceTypes val2 = val;
                AccountSasResourceTypes val3;
                switch (c) {
                case 's':
                    val3 = 1;
                    break;
                case 'c':
                    val3 = 2;
                    break;
                case 'o':
                    val3 = 4;
                    break;
                default:
                    throw Errors.InvalidResourceType(c);
                }
                val = (val2 | val3);
            }
            return val;
        }
        internal static string ToProtocolString(this SasProtocol protocol)
        {
            switch ((int)protocol) {
            case 2:
                return "https";
            case 1:
                return "https,http";
            default:
                return null;
            }
        }
        public static SasProtocol ParseProtocol(string s)
        {
            if (s == null || (s != null && s.Length == 0))
                return 0;
            if (s == "https")
                return 2;
            if (s == "https,http" || s == "http,https")
                return 1;
            throw Errors.InvalidSasProtocol("s", "SasProtocol");
        }
        internal static string ToPermissionsString(this AccountSasServices services)
        {
            StringBuilder stringBuilder = new StringBuilder();
            if ((services & 1) == 1)
                stringBuilder.Append('b');
            if ((services & 4) == 4)
                stringBuilder.Append('f');
            if ((services & 2) == 2)
                stringBuilder.Append('q');
            if ((services & 8) == 8)
                stringBuilder.Append('t');
            return stringBuilder.ToString();
        }
        internal static AccountSasServices ParseAccountServices(string s)
        {
            AccountSasServices val = 0;
            foreach (char c in s) {
                AccountSasServices val2 = val;
                AccountSasServices val3;
                switch (c) {
                case 'b':
                    val3 = 1;
                    break;
                case 'q':
                    val3 = 2;
                    break;
                case 'f':
                    val3 = 4;
                    break;
                case 't':
                    val3 = 8;
                    break;
                default:
                    throw Errors.InvalidService(c);
                }
                val = (val2 | val3);
            }
            return val;
        }
        internal static string FormatTimesForSasSigning(DateTimeOffset time)
        {
            if (!(time == default(DateTimeOffset)))
                return time.ToString("yyyy-MM-ddTHH:mm:ssZ", CultureInfo.InvariantCulture);
            return "";
        }
        internal static void AddToBuilder(StringBuilder sb, string key, string value)
        {
            sb.Append((sb.Length > 0) ? "&" : "").Append(key).Append('=')
                .Append(value);
        }
        internal static string ValidateAndSanitizeRawPermissions(string permissions, List<char> validPermissionsInOrder)
        {
            if (permissions == null)
                return null;
            permissions = permissions.ToLowerInvariant();
            HashSet<char> hashSet = new HashSet<char>(validPermissionsInOrder);
            HashSet<char> hashSet2 = new HashSet<char>();
            string text = permissions;
            foreach (char c in text) {
                if (!hashSet.Contains(c))
                    throw new ArgumentException($"{c}""");
                hashSet2.Add(c);
            }
            StringBuilder stringBuilder = new StringBuilder();
            foreach (char item in validPermissionsInOrder) {
                if (hashSet2.Contains(item))
                    stringBuilder.Append(item);
            }
            return stringBuilder.ToString();
        }
    }
}