| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | #nullable enable |
| | 5 | |
|
| | 6 | | using System; |
| | 7 | | using System.Diagnostics; |
| | 8 | | using System.Globalization; |
| | 9 | | using System.Xml; |
| | 10 | |
|
| | 11 | | namespace Azure.Core |
| | 12 | | { |
| | 13 | | internal class TypeFormatters |
| | 14 | | { |
| | 15 | | private const string RoundtripZFormat = "yyyy-MM-ddTHH:mm:ss.fffffffZ"; |
| 122 | 16 | | public static string DefaultNumberFormat { get; } = "G"; |
| | 17 | |
|
| 0 | 18 | | public static string ToString(bool value) => value ? "true" : "false"; |
| | 19 | |
|
| 3440 | 20 | | public static string ToString(DateTimeOffset value, string format) => format switch |
| 3440 | 21 | | { |
| 0 | 22 | | "D" => value.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture), |
| 0 | 23 | | "U" => value.ToUnixTimeSeconds().ToString(CultureInfo.InvariantCulture), |
| 10320 | 24 | | "O" when value.Offset == TimeSpan.Zero => value.ToString(RoundtripZFormat, CultureInfo.InvariantCulture), |
| 0 | 25 | | "o" when value.Offset == TimeSpan.Zero => value.ToString(RoundtripZFormat, CultureInfo.InvariantCulture), |
| 0 | 26 | | _ => value.ToString(format, CultureInfo.InvariantCulture) |
| 3440 | 27 | | }; |
| | 28 | |
|
| 0 | 29 | | public static string ToString(TimeSpan value, string format) => format switch |
| 0 | 30 | | { |
| 0 | 31 | | "P" => XmlConvert.ToString(value), |
| 0 | 32 | | _ => value.ToString(format, CultureInfo.InvariantCulture) |
| 0 | 33 | | }; |
| | 34 | |
|
| | 35 | | public static string ToBase64UrlString(byte[] value) |
| | 36 | | { |
| 0 | 37 | | var numWholeOrPartialInputBlocks = checked(value.Length + 2) / 3; |
| 0 | 38 | | var size = checked(numWholeOrPartialInputBlocks * 4); |
| 0 | 39 | | var output = new char[size]; |
| | 40 | |
|
| 0 | 41 | | var numBase64Chars = Convert.ToBase64CharArray(value, 0, value.Length, output, 0); |
| | 42 | |
|
| | 43 | | // Fix up '+' -> '-' and '/' -> '_'. Drop padding characters. |
| 0 | 44 | | int i = 0; |
| 0 | 45 | | for (; i < numBase64Chars; i++) |
| | 46 | | { |
| 0 | 47 | | var ch = output[i]; |
| 0 | 48 | | if (ch == '+') |
| | 49 | | { |
| 0 | 50 | | output[i] = '-'; |
| | 51 | | } |
| 0 | 52 | | else if (ch == '/') |
| | 53 | | { |
| 0 | 54 | | output[i] = '_'; |
| | 55 | | } |
| 0 | 56 | | else if (ch == '=') |
| | 57 | | { |
| | 58 | | // We've reached a padding character; truncate the remainder. |
| | 59 | | break; |
| | 60 | | } |
| | 61 | | } |
| | 62 | |
|
| 0 | 63 | | return new string(output, 0, i); |
| | 64 | | } |
| | 65 | |
|
| | 66 | | public static byte[] FromBase64UrlString(string value) |
| | 67 | | { |
| 0 | 68 | | var paddingCharsToAdd = GetNumBase64PaddingCharsToAddForDecode(value.Length); |
| | 69 | |
|
| 0 | 70 | | var output = new char[value.Length + paddingCharsToAdd]; |
| | 71 | |
|
| | 72 | | int i; |
| 0 | 73 | | for (i = 0; i < value.Length; i++) |
| | 74 | | { |
| 0 | 75 | | var ch = value[i]; |
| 0 | 76 | | if (ch == '-') |
| | 77 | | { |
| 0 | 78 | | output[i] = '+'; |
| | 79 | | } |
| 0 | 80 | | else if (ch == '_') |
| | 81 | | { |
| 0 | 82 | | output[i] = '/'; |
| | 83 | | } |
| | 84 | | else |
| | 85 | | { |
| 0 | 86 | | output[i] = ch; |
| | 87 | | } |
| | 88 | | } |
| | 89 | |
|
| 0 | 90 | | for (; i < output.Length; i++) |
| | 91 | | { |
| 0 | 92 | | output[i] = '='; |
| | 93 | | } |
| | 94 | |
|
| 0 | 95 | | return Convert.FromBase64CharArray(output, 0, output.Length); |
| | 96 | | } |
| | 97 | |
|
| | 98 | |
|
| | 99 | | private static int GetNumBase64PaddingCharsToAddForDecode(int inputLength) |
| | 100 | | { |
| 0 | 101 | | switch (inputLength % 4) |
| | 102 | | { |
| | 103 | | case 0: |
| 0 | 104 | | return 0; |
| | 105 | | case 2: |
| 0 | 106 | | return 2; |
| | 107 | | case 3: |
| 0 | 108 | | return 1; |
| | 109 | | default: |
| 0 | 110 | | throw new InvalidOperationException("Malformed input"); |
| | 111 | | } |
| | 112 | | } |
| | 113 | |
|
| | 114 | | public static DateTimeOffset ParseDateTimeOffset(string value, string format) |
| | 115 | | { |
| 12 | 116 | | return format switch |
| 12 | 117 | | { |
| 0 | 118 | | "U" => DateTimeOffset.FromUnixTimeSeconds(long.Parse(value, CultureInfo.InvariantCulture)), |
| 24 | 119 | | _ => DateTimeOffset.Parse(value, CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal) |
| 12 | 120 | | }; |
| | 121 | | } |
| | 122 | |
|
| 0 | 123 | | public static TimeSpan ParseTimeSpan(string value, string format) => format switch |
| 0 | 124 | | { |
| 0 | 125 | | "P" => XmlConvert.ToTimeSpan(value), |
| 0 | 126 | | _ => TimeSpan.ParseExact(value, format, CultureInfo.InvariantCulture) |
| 0 | 127 | | }; |
| | 128 | | } |
| | 129 | | } |