| | 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.Collections.Generic; |
| | 8 | | using System.Text; |
| | 9 | |
|
| | 10 | | namespace Azure.Core |
| | 11 | | { |
| | 12 | | internal sealed class ConnectionString |
| | 13 | | { |
| | 14 | | private readonly Dictionary<string, string> _pairs; |
| | 15 | | private readonly string _pairSeparator; |
| | 16 | | private readonly string _keywordValueSeparator; |
| | 17 | |
|
| | 18 | | public static ConnectionString Parse(string connectionString, string segmentSeparator = ";", string keywordValue |
| | 19 | | { |
| 320 | 20 | | Validate(connectionString, segmentSeparator, keywordValueSeparator, allowEmptyValues); |
| 320 | 21 | | return new ConnectionString(ParseSegments(connectionString, segmentSeparator, keywordValueSeparator), segmen |
| | 22 | | } |
| | 23 | |
|
| 320 | 24 | | private ConnectionString(Dictionary<string, string> pairs, string pairSeparator, string keywordValueSeparator) |
| | 25 | | { |
| 320 | 26 | | _pairs = pairs; |
| 320 | 27 | | _pairSeparator = pairSeparator; |
| 320 | 28 | | _keywordValueSeparator = keywordValueSeparator; |
| 320 | 29 | | } |
| | 30 | |
|
| | 31 | | public string GetRequired(string keyword) => |
| 960 | 32 | | _pairs.TryGetValue(keyword, out var value) ? value : throw new InvalidOperationException($"Required keyword |
| | 33 | |
|
| | 34 | | public string? GetNonRequired(string keyword) => |
| 0 | 35 | | _pairs.TryGetValue(keyword, out var value) ? value : null; |
| | 36 | |
|
| | 37 | | public void Replace(string keyword, string value) |
| | 38 | | { |
| 0 | 39 | | if (_pairs.ContainsKey(keyword)) |
| | 40 | | { |
| 0 | 41 | | _pairs[keyword] = value; |
| | 42 | | } |
| 0 | 43 | | } |
| | 44 | |
|
| | 45 | | public override string ToString() |
| | 46 | | { |
| 0 | 47 | | var stringBuilder = new StringBuilder(); |
| 0 | 48 | | var isFirst = true; |
| 0 | 49 | | foreach (KeyValuePair<string, string> pair in _pairs) |
| | 50 | | { |
| 0 | 51 | | if (isFirst) |
| | 52 | | { |
| 0 | 53 | | isFirst = false; |
| | 54 | | } |
| | 55 | | else |
| | 56 | | { |
| 0 | 57 | | stringBuilder.Append(_pairSeparator); |
| | 58 | | } |
| | 59 | |
|
| 0 | 60 | | stringBuilder.Append(pair.Key); |
| 0 | 61 | | if (pair.Value != null) |
| | 62 | | { |
| 0 | 63 | | stringBuilder.Append(_keywordValueSeparator).Append(pair.Value); |
| | 64 | | } |
| | 65 | | } |
| | 66 | |
|
| 0 | 67 | | return stringBuilder.ToString(); |
| | 68 | | } |
| | 69 | |
|
| | 70 | | private static Dictionary<string, string> ParseSegments(in string connectionString, in string separator, in stri |
| | 71 | | { |
| 320 | 72 | | var pairs = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase); |
| | 73 | |
|
| 320 | 74 | | var segmentStart = -1; |
| 320 | 75 | | var segmentEnd = 0; |
| | 76 | |
|
| 1280 | 77 | | while (TryGetNextSegment(connectionString, separator, ref segmentStart, ref segmentEnd)) |
| | 78 | | { |
| 960 | 79 | | var kvSeparatorIndex = connectionString.IndexOf(keywordValueSeparator, segmentStart, segmentEnd - segmen |
| 960 | 80 | | int keywordStart = GetStart(connectionString, segmentStart); |
| 960 | 81 | | int keyLength = GetLength(connectionString, keywordStart, kvSeparatorIndex); |
| | 82 | |
|
| 960 | 83 | | var keyword = connectionString.Substring(keywordStart, keyLength); |
| 960 | 84 | | if (pairs.ContainsKey(keyword)) |
| | 85 | | { |
| 0 | 86 | | throw new InvalidOperationException($"Duplicated keyword '{keyword}'"); |
| | 87 | | } |
| | 88 | |
|
| 960 | 89 | | var valueStart = GetStart(connectionString, kvSeparatorIndex + keywordValueSeparator.Length); |
| 960 | 90 | | var valueLength = GetLength(connectionString, valueStart, segmentEnd); |
| 960 | 91 | | pairs.Add(keyword, connectionString.Substring(valueStart, valueLength)); |
| | 92 | | } |
| | 93 | |
|
| 320 | 94 | | return pairs; |
| | 95 | |
|
| | 96 | | static int GetStart(in string str, int start) |
| | 97 | | { |
| 1920 | 98 | | while (start < str.Length && char.IsWhiteSpace(str[start])) |
| | 99 | | { |
| 0 | 100 | | start++; |
| | 101 | | } |
| | 102 | |
|
| 1920 | 103 | | return start; |
| | 104 | | } |
| | 105 | |
|
| | 106 | | static int GetLength(in string str, in int start, int end) |
| | 107 | | { |
| 1920 | 108 | | while (end > start && char.IsWhiteSpace(str[end - 1])) |
| | 109 | | { |
| 0 | 110 | | end--; |
| | 111 | | } |
| | 112 | |
|
| 1920 | 113 | | return end - start; |
| | 114 | | } |
| | 115 | | } |
| | 116 | |
|
| | 117 | | private static bool TryGetNextSegment(in string str, in string separator, ref int start, ref int end) |
| | 118 | | { |
| 2560 | 119 | | if (start == -1) |
| | 120 | | { |
| 640 | 121 | | start = 0; |
| | 122 | | } |
| | 123 | | else |
| | 124 | | { |
| 1920 | 125 | | start = end + separator.Length; |
| 1920 | 126 | | if (start >= str.Length) |
| | 127 | | { |
| 640 | 128 | | return false; |
| | 129 | | } |
| | 130 | | } |
| | 131 | |
|
| 1920 | 132 | | end = str.IndexOf(separator, start, StringComparison.Ordinal); |
| 1920 | 133 | | if (end == -1) |
| | 134 | | { |
| 264 | 135 | | end = str.Length; |
| | 136 | | } |
| | 137 | |
|
| 1920 | 138 | | return true; |
| | 139 | | } |
| | 140 | |
|
| | 141 | | private static void Validate(string connectionString, string segmentSeparator, string keywordValueSeparator, boo |
| | 142 | | { |
| 320 | 143 | | var segmentStart = -1; |
| 320 | 144 | | var segmentEnd = 0; |
| | 145 | |
|
| 1280 | 146 | | while (TryGetNextSegment(connectionString, segmentSeparator, ref segmentStart, ref segmentEnd)) |
| | 147 | | { |
| 960 | 148 | | if (segmentStart == segmentEnd) |
| | 149 | | { |
| 0 | 150 | | if (segmentStart == 0) |
| | 151 | | { |
| 0 | 152 | | throw new InvalidOperationException($"Connection string starts with separator '{segmentSeparator |
| | 153 | | } |
| | 154 | |
|
| 0 | 155 | | throw new InvalidOperationException($"Connection string contains two following separators '{segmentS |
| | 156 | | } |
| | 157 | |
|
| 960 | 158 | | var kvSeparatorIndex = connectionString.IndexOf(keywordValueSeparator, segmentStart, segmentEnd - segmen |
| 960 | 159 | | if (kvSeparatorIndex == -1) |
| | 160 | | { |
| 0 | 161 | | throw new InvalidOperationException($"Connection string doesn't have value for keyword '{connectionS |
| | 162 | | } |
| | 163 | |
|
| 960 | 164 | | if (segmentStart == kvSeparatorIndex) |
| | 165 | | { |
| 0 | 166 | | throw new InvalidOperationException($"Connection string has value '{connectionString.Substring(segme |
| | 167 | | } |
| | 168 | |
|
| 960 | 169 | | if (!allowEmptyValues && kvSeparatorIndex + 1 == segmentEnd) |
| | 170 | | { |
| 0 | 171 | | throw new InvalidOperationException($"Connection string has keyword '{connectionString.Substring(seg |
| | 172 | | } |
| | 173 | | } |
| 320 | 174 | | } |
| | 175 | | } |
| | 176 | | } |