< Summary

Class:Azure.Core.TypeFormatters
Assembly:Azure.ResourceManager.Resources
File(s):C:\Git\azure-sdk-for-net\sdk\core\Azure.Core\src\Shared\AutoRest\TypeFormatters.cs
Covered lines:13
Uncovered lines:42
Coverable lines:55
Total lines:129
Line coverage:23.6% (13 of 55)
Covered branches:3
Total branches:41
Branch coverage:7.3% (3 of 41)

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
get_DefaultNumberFormat()-100%100%
ToString(...)-0%0%
ToString(...)-0%0%
ToString(...)-80%50%
ToBase64UrlString(...)-0%0%
FromBase64UrlString(...)-0%0%
GetNumBase64PaddingCharsToAddForDecode(...)-0%0%
ParseDateTimeOffset(...)-80%50%
ParseTimeSpan(...)-80%50%

File(s)

C:\Git\azure-sdk-for-net\sdk\core\Azure.Core\src\Shared\AutoRest\TypeFormatters.cs

#LineLine coverage
 1// Copyright (c) Microsoft Corporation. All rights reserved.
 2// Licensed under the MIT License.
 3
 4#nullable enable
 5
 6using System;
 7using System.Diagnostics;
 8using System.Globalization;
 9using System.Xml;
 10
 11namespace Azure.Core
 12{
 13    internal class TypeFormatters
 14    {
 15        private const string RoundtripZFormat = "yyyy-MM-ddTHH:mm:ss.fffffffZ";
 1416        public static string DefaultNumberFormat { get; } = "G";
 17
 018        public static string ToString(bool value) => value ? "true" : "false";
 19
 020        public static string ToString(DateTimeOffset value, string format) => format switch
 021        {
 022            "D" => value.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture),
 023            "U" => value.ToUnixTimeSeconds().ToString(CultureInfo.InvariantCulture),
 024            "O" when value.Offset == TimeSpan.Zero => value.ToString(RoundtripZFormat, CultureInfo.InvariantCulture),
 025            "o" when value.Offset == TimeSpan.Zero => value.ToString(RoundtripZFormat, CultureInfo.InvariantCulture),
 026            _ => value.ToString(format, CultureInfo.InvariantCulture)
 027        };
 28
 2429        public static string ToString(TimeSpan value, string format) => format switch
 2430        {
 4831            "P" => XmlConvert.ToString(value),
 032            _ => value.ToString(format, CultureInfo.InvariantCulture)
 2433        };
 34
 35        public static string ToBase64UrlString(byte[] value)
 36        {
 037            var numWholeOrPartialInputBlocks = checked(value.Length + 2) / 3;
 038            var size = checked(numWholeOrPartialInputBlocks * 4);
 039            var output = new char[size];
 40
 041            var numBase64Chars = Convert.ToBase64CharArray(value, 0, value.Length, output, 0);
 42
 43            // Fix up '+' -> '-' and '/' -> '_'. Drop padding characters.
 044            int i = 0;
 045            for (; i < numBase64Chars; i++)
 46            {
 047                var ch = output[i];
 048                if (ch == '+')
 49                {
 050                    output[i] = '-';
 51                }
 052                else if (ch == '/')
 53                {
 054                    output[i] = '_';
 55                }
 056                else if (ch == '=')
 57                {
 58                    // We've reached a padding character; truncate the remainder.
 59                    break;
 60                }
 61            }
 62
 063            return new string(output, 0, i);
 64        }
 65
 66        public static byte[] FromBase64UrlString(string value)
 67        {
 068            var paddingCharsToAdd = GetNumBase64PaddingCharsToAddForDecode(value.Length);
 69
 070            var output = new char[value.Length + paddingCharsToAdd];
 71
 72            int i;
 073            for (i = 0; i < value.Length; i++)
 74            {
 075                var ch = value[i];
 076                if (ch == '-')
 77                {
 078                    output[i] = '+';
 79                }
 080                else if (ch == '_')
 81                {
 082                    output[i] = '/';
 83                }
 84                else
 85                {
 086                    output[i] = ch;
 87                }
 88            }
 89
 090            for (; i < output.Length; i++)
 91            {
 092                output[i] = '=';
 93            }
 94
 095            return Convert.FromBase64CharArray(output, 0, output.Length);
 96        }
 97
 98
 99        private static int GetNumBase64PaddingCharsToAddForDecode(int inputLength)
 100        {
 0101            switch (inputLength % 4)
 102            {
 103                case 0:
 0104                    return 0;
 105                case 2:
 0106                    return 2;
 107                case 3:
 0108                    return 1;
 109                default:
 0110                    throw new InvalidOperationException("Malformed input");
 111            }
 112        }
 113
 114        public static DateTimeOffset ParseDateTimeOffset(string value, string format)
 115        {
 486116            return format switch
 486117            {
 0118                "U" => DateTimeOffset.FromUnixTimeSeconds(long.Parse(value, CultureInfo.InvariantCulture)),
 972119                _ => DateTimeOffset.Parse(value, CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal)
 486120            };
 121        }
 122
 148123        public static TimeSpan ParseTimeSpan(string value, string format) => format switch
 148124        {
 296125            "P" => XmlConvert.ToTimeSpan(value),
 0126            _ => TimeSpan.ParseExact(value, format, CultureInfo.InvariantCulture)
 148127        };
 128    }
 129}