| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.IO; |
| | 4 | | using System.Linq; |
| | 5 | | using Itinero.IO; |
| | 6 | |
|
| | 7 | | namespace Itinero.Network.Attributes; |
| | 8 | |
|
| | 9 | | /// <summary> |
| | 10 | | /// Contains extension methods for attribute handling. |
| | 11 | | /// </summary> |
| | 12 | | public static class AttributeExtensions |
| | 13 | | { |
| | 14 | | /// <summary> |
| | 15 | | /// Tries to get the value for the given key. |
| | 16 | | /// </summary> |
| | 17 | | /// <param name="attributes">The attributes.</param> |
| | 18 | | /// <param name="key">The key.</param> |
| | 19 | | /// <param name="value">The value, if any.</param> |
| | 20 | | /// <returns>True if the key was found, false otherwise.</returns> |
| | 21 | | public static bool TryGetValue(this IEnumerable<(string key, string value)> attributes, string key, |
| | 22 | | out string value) |
| 64 | 23 | | { |
| 390 | 24 | | foreach (var (k, v) in attributes) |
| 124 | 25 | | { |
| 124 | 26 | | if (key != k) |
| 74 | 27 | | { |
| 74 | 28 | | continue; |
| | 29 | | } |
| | 30 | |
|
| 50 | 31 | | value = v; |
| 50 | 32 | | return true; |
| | 33 | | } |
| | 34 | |
|
| 14 | 35 | | value = string.Empty; |
| 14 | 36 | | return false; |
| 64 | 37 | | } |
| | 38 | |
|
| | 39 | | /// <summary> |
| | 40 | | /// Returns true if the given attribute collection contains the same attributes than the given collection. |
| | 41 | | /// </summary> |
| | 42 | | /// <param name="attributes">The attributes.</param> |
| | 43 | | /// <param name="other">The other attributes.</param> |
| | 44 | | /// <param name="exclude">Keys to exclude.</param> |
| | 45 | | /// <returns>Trie of the same attributes.</returns> |
| | 46 | | public static bool ContainsSame(this IEnumerable<(string key, string value)> attributes, |
| | 47 | | IEnumerable<(string key, string value)> other, params string[] exclude) |
| 0 | 48 | | { |
| 0 | 49 | | var attributesCount = 0; |
| 0 | 50 | | var otherCount = 0; |
| 0 | 51 | | foreach (var a in attributes) |
| 0 | 52 | | { |
| 0 | 53 | | if (!exclude.Contains(a.key)) |
| 0 | 54 | | { |
| 0 | 55 | | attributesCount++; |
| 0 | 56 | | if (!other.Contains(a)) |
| 0 | 57 | | { |
| 0 | 58 | | return false; |
| | 59 | | } |
| 0 | 60 | | } |
| 0 | 61 | | } |
| | 62 | |
|
| 0 | 63 | | foreach (var a in other) |
| 0 | 64 | | { |
| 0 | 65 | | if (exclude.Contains(a.key)) |
| 0 | 66 | | { |
| 0 | 67 | | continue; |
| | 68 | | } |
| | 69 | |
|
| 0 | 70 | | otherCount++; |
| 0 | 71 | | if (!attributes.Contains(a)) |
| 0 | 72 | | { |
| 0 | 73 | | return false; |
| | 74 | | } |
| 0 | 75 | | } |
| | 76 | |
|
| 0 | 77 | | return attributesCount == otherCount; |
| 0 | 78 | | } |
| | 79 | |
|
| | 80 | | /// <summary> |
| | 81 | | /// Removes the attribute with the given key. |
| | 82 | | /// </summary> |
| | 83 | | /// <param name="attributes">The attributes.</param> |
| | 84 | | /// <param name="key">The key.</param> |
| | 85 | | /// <returns>True if the key was found.</returns> |
| | 86 | | public static bool RemoveKey(this List<(string key, string value)> attributes, string key) |
| 0 | 87 | | { |
| 0 | 88 | | for (var i = 0; i < attributes.Count; i++) |
| 0 | 89 | | { |
| 0 | 90 | | var a = attributes[i]; |
| 0 | 91 | | if (a.key != key) |
| 0 | 92 | | { |
| 0 | 93 | | continue; |
| | 94 | | } |
| | 95 | |
|
| 0 | 96 | | attributes.RemoveAt(i); |
| 0 | 97 | | return true; |
| | 98 | | } |
| | 99 | |
|
| 0 | 100 | | return false; |
| 0 | 101 | | } |
| | 102 | |
|
| | 103 | | /// <summary> |
| | 104 | | /// Adds or replaces the value for the given attributes. |
| | 105 | | /// </summary> |
| | 106 | | /// <param name="attributes">The attributes.</param> |
| | 107 | | /// <param name="other">The other attributes.</param> |
| | 108 | | public static void AddOrReplace(this List<(string key, string value)> attributes, |
| | 109 | | IEnumerable<(string key, string value)> other) |
| 0 | 110 | | { |
| 0 | 111 | | if (attributes == null) |
| 0 | 112 | | { |
| 0 | 113 | | throw new ArgumentNullException(nameof(attributes)); |
| | 114 | | } |
| | 115 | |
|
| 0 | 116 | | foreach (var a in other) |
| 0 | 117 | | { |
| 0 | 118 | | attributes.AddOrReplace(a.key, a.value); |
| 0 | 119 | | } |
| 0 | 120 | | } |
| | 121 | |
|
| | 122 | | /// <summary> |
| | 123 | | /// Adds or replaces the value for the given key. |
| | 124 | | /// </summary> |
| | 125 | | /// <param name="attributes">The attributes.</param> |
| | 126 | | /// <param name="key">The key.</param> |
| | 127 | | /// <param name="value">The value.</param> |
| | 128 | | /// <returns>True if the key was found.</returns> |
| | 129 | | public static bool AddOrReplace(this List<(string key, string value)> attributes, |
| | 130 | | string key, string value) |
| 0 | 131 | | { |
| 0 | 132 | | for (var i = 0; i < attributes.Count; i++) |
| 0 | 133 | | { |
| 0 | 134 | | var a = attributes[i]; |
| 0 | 135 | | if (a.key != key) |
| 0 | 136 | | { |
| 0 | 137 | | continue; |
| | 138 | | } |
| | 139 | |
|
| 0 | 140 | | attributes[i] = (key, value); |
| 0 | 141 | | return true; |
| | 142 | | } |
| | 143 | |
|
| 0 | 144 | | attributes.Add((key, value)); |
| 0 | 145 | | return false; |
| 0 | 146 | | } |
| | 147 | |
|
| | 148 | | /// <summary> |
| | 149 | | /// Writes the attributes to the given stream starting at the current position of the stream. |
| | 150 | | /// </summary> |
| | 151 | | /// <param name="attributes">The attributes to write.</param> |
| | 152 | | /// <param name="stream"></param> |
| | 153 | | /// <returns>The number of byte written.</returns> |
| | 154 | | internal static long WriteAttributesTo(this IEnumerable<(string key, string value)> attributes, Stream stream) |
| 5 | 155 | | { |
| 5 | 156 | | var pos = stream.Position; |
| 25 | 157 | | foreach (var (key, value) in attributes) |
| 5 | 158 | | { |
| 5 | 159 | | var bytes = System.Text.Encoding.Unicode.GetBytes(key); |
| 5 | 160 | | stream.WriteVarInt32(bytes.Length + 1); // 0 is null, end of the attribute set. |
| 5 | 161 | | stream.Write(bytes, 0, bytes.Length); |
| | 162 | |
|
| 5 | 163 | | bytes = System.Text.Encoding.Unicode.GetBytes(value); |
| 5 | 164 | | stream.WriteVarInt32(bytes.Length); |
| 5 | 165 | | stream.Write(bytes, 0, bytes.Length); |
| 5 | 166 | | } |
| | 167 | |
|
| 5 | 168 | | stream.WriteVarInt32(0); |
| | 169 | |
|
| 5 | 170 | | return stream.Position - pos; |
| 5 | 171 | | } |
| | 172 | |
|
| | 173 | | /// <summary> |
| | 174 | | /// Reads the attributes from the given stream starting at the current position of the stream. |
| | 175 | | /// </summary> |
| | 176 | | /// <param name="stream">The stream to read from.</param> |
| | 177 | | /// <returns>The attributes read.</returns> |
| | 178 | | internal static IEnumerable<(string key, string value)> ReadAttributesFrom(this Stream stream) |
| 4 | 179 | | { |
| 4 | 180 | | var attributes = new List<(string key, string value)>(); |
| | 181 | |
|
| 4 | 182 | | var keySize = stream.ReadVarInt32(); |
| 8 | 183 | | while (keySize > 0) |
| 4 | 184 | | { |
| 4 | 185 | | var bytes = new byte[keySize - 1]; |
| 4 | 186 | | stream.Read(bytes, 0, bytes.Length); |
| 4 | 187 | | var key = System.Text.Encoding.Unicode.GetString(bytes); |
| | 188 | |
|
| 4 | 189 | | var valueSize = stream.ReadVarInt32(); |
| 4 | 190 | | bytes = new byte[valueSize]; |
| 4 | 191 | | stream.Read(bytes, 0, bytes.Length); |
| 4 | 192 | | var value = System.Text.Encoding.Unicode.GetString(bytes); |
| | 193 | |
|
| 4 | 194 | | attributes.Add((key, value)); |
| | 195 | |
|
| 4 | 196 | | keySize = stream.ReadVarInt32(); |
| 4 | 197 | | } |
| | 198 | |
|
| 4 | 199 | | return attributes; |
| 4 | 200 | | } |
| | 201 | |
|
| | 202 | | public static long GetHash(this IEnumerable<(string key, string value)> attributes) |
| 0 | 203 | | { |
| 0 | 204 | | var hash = 0; |
| 0 | 205 | | foreach (var attribute in attributes) |
| 0 | 206 | | { |
| 0 | 207 | | hash ^= attribute.GetHashCode(); |
| 0 | 208 | | } |
| | 209 | |
|
| 0 | 210 | | return hash; |
| 0 | 211 | | } |
| | 212 | |
|
| | 213 | | public static long GetDiffHash(this (string key, string value) otherValue, long originalHash) |
| 0 | 214 | | { |
| 0 | 215 | | return originalHash ^ otherValue.GetHashCode(); |
| 0 | 216 | | } |
| | 217 | | } |