| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System; |
| | | 5 | | using System.Collections.Generic; |
| | | 6 | | using System.Collections.Specialized; |
| | | 7 | | using System.Text; |
| | | 8 | | using CoreWCF.Runtime; |
| | | 9 | | |
| | | 10 | | namespace CoreWCF |
| | | 11 | | { |
| | | 12 | | // Thin wrapper around formatted string; use type system to help ensure we |
| | | 13 | | // are doing canonicalization right/consistently - the literal sections are held in an |
| | | 14 | | // un-escaped format |
| | | 15 | | // We are assuming that the string will be always built as Lit{Var}Lit[{Var}Lit[{Var}Lit[...]]], |
| | | 16 | | // when the first and last literals may be empty |
| | | 17 | | internal class UriTemplateCompoundPathSegment : UriTemplatePathSegment, IComparable<UriTemplateCompoundPathSegment> |
| | | 18 | | { |
| | | 19 | | private readonly string _firstLiteral; |
| | | 20 | | private readonly List<VarAndLitPair> _varLitPairs; |
| | | 21 | | private CompoundSegmentClass _csClass; |
| | | 22 | | |
| | | 23 | | private UriTemplateCompoundPathSegment(string originalSegment, bool endsWithSlash, string firstLiteral) |
| | 24 | 24 | | : base(originalSegment, UriTemplatePartType.Compound, endsWithSlash) |
| | | 25 | | { |
| | 24 | 26 | | _firstLiteral = firstLiteral; |
| | 24 | 27 | | _varLitPairs = new List<VarAndLitPair>(); |
| | 24 | 28 | | } |
| | | 29 | | |
| | | 30 | | public static new UriTemplateCompoundPathSegment CreateFromUriTemplate(string segment, UriTemplate template) |
| | | 31 | | { |
| | 24 | 32 | | string origSegment = segment; |
| | 24 | 33 | | bool endsWithSlash = segment.EndsWith("/", StringComparison.Ordinal); |
| | 24 | 34 | | if (endsWithSlash) |
| | | 35 | | { |
| | 0 | 36 | | segment = segment.Remove(segment.Length - 1); |
| | | 37 | | } |
| | | 38 | | |
| | 24 | 39 | | int nextVarStart = segment.IndexOf("{", StringComparison.Ordinal); |
| | | 40 | | Fx.Assert(nextVarStart >= 0, "The method is only called after identifying a '{' character in the segment"); |
| | 24 | 41 | | string firstLiteral = ((nextVarStart > 0) ? segment.Substring(0, nextVarStart) : string.Empty); |
| | 24 | 42 | | if (firstLiteral.IndexOf(UriTemplate.WildcardPath, StringComparison.Ordinal) != -1) |
| | | 43 | | { |
| | 0 | 44 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new FormatException( |
| | 0 | 45 | | SR.Format(SR.UTInvalidWildcardInVariableOrLiteral, template._originalTemplate, UriTemplate.WildcardP |
| | | 46 | | } |
| | | 47 | | |
| | 24 | 48 | | UriTemplateCompoundPathSegment result = new UriTemplateCompoundPathSegment(origSegment, endsWithSlash, |
| | 24 | 49 | | ((firstLiteral != string.Empty) ? Uri.UnescapeDataString(firstLiteral) : string.Empty)); |
| | | 50 | | do |
| | | 51 | | { |
| | 48 | 52 | | int nextVarEnd = segment.IndexOf("}", nextVarStart + 1, StringComparison.Ordinal); |
| | 48 | 53 | | if (nextVarEnd < nextVarStart + 2) |
| | | 54 | | { |
| | 0 | 55 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new FormatException( |
| | 0 | 56 | | SR.Format(SR.UTInvalidFormatSegmentOrQueryPart, segment))); |
| | | 57 | | } |
| | | 58 | | |
| | 48 | 59 | | string varName = template.AddPathVariable(UriTemplatePartType.Compound, |
| | 48 | 60 | | segment.Substring(nextVarStart + 1, nextVarEnd - nextVarStart - 1), out bool hasDefault); |
| | 48 | 61 | | if (hasDefault) |
| | | 62 | | { |
| | 0 | 63 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException( |
| | 0 | 64 | | SR.Format(SR.UTDefaultValueToCompoundSegmentVar, template, origSegment, varName))); |
| | | 65 | | } |
| | | 66 | | |
| | 48 | 67 | | nextVarStart = segment.IndexOf("{", nextVarEnd + 1, StringComparison.Ordinal); |
| | | 68 | | string literal; |
| | 48 | 69 | | if (nextVarStart > 0) |
| | | 70 | | { |
| | 24 | 71 | | if (nextVarStart == nextVarEnd + 1) |
| | | 72 | | { |
| | 0 | 73 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(nameof(template), |
| | 0 | 74 | | SR.Format(SR.UTDoesNotSupportAdjacentVarsInCompoundSegment, template, segment)); |
| | | 75 | | } |
| | 24 | 76 | | literal = segment.Substring(nextVarEnd + 1, nextVarStart - nextVarEnd - 1); |
| | | 77 | | } |
| | 24 | 78 | | else if (nextVarEnd + 1 < segment.Length) |
| | | 79 | | { |
| | 0 | 80 | | literal = segment.Substring(nextVarEnd + 1); |
| | | 81 | | } |
| | | 82 | | else |
| | | 83 | | { |
| | 24 | 84 | | literal = string.Empty; |
| | | 85 | | } |
| | | 86 | | |
| | 48 | 87 | | if (literal.IndexOf(UriTemplate.WildcardPath, StringComparison.Ordinal) != -1) |
| | | 88 | | { |
| | 0 | 89 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new FormatException( |
| | 0 | 90 | | SR.Format(SR.UTInvalidWildcardInVariableOrLiteral, template._originalTemplate, UriTemplate.Wildc |
| | | 91 | | } |
| | | 92 | | |
| | 48 | 93 | | if (literal.IndexOf('}') != -1) |
| | | 94 | | { |
| | 0 | 95 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new FormatException( |
| | 0 | 96 | | SR.Format(SR.UTInvalidFormatSegmentOrQueryPart, segment))); |
| | | 97 | | } |
| | | 98 | | |
| | 48 | 99 | | result._varLitPairs.Add(new VarAndLitPair(varName, ((literal == string.Empty) ? string.Empty : Uri.Unesc |
| | 48 | 100 | | } while (nextVarStart > 0); |
| | | 101 | | |
| | 24 | 102 | | if (string.IsNullOrEmpty(result._firstLiteral)) |
| | | 103 | | { |
| | 24 | 104 | | if (string.IsNullOrEmpty(result._varLitPairs[result._varLitPairs.Count - 1].Literal)) |
| | | 105 | | { |
| | 24 | 106 | | result._csClass = CompoundSegmentClass.HasNoPrefixNorSuffix; |
| | | 107 | | } |
| | | 108 | | else |
| | | 109 | | { |
| | 0 | 110 | | result._csClass = CompoundSegmentClass.HasOnlySuffix; |
| | | 111 | | } |
| | | 112 | | } |
| | | 113 | | else |
| | | 114 | | { |
| | 0 | 115 | | if (string.IsNullOrEmpty(result._varLitPairs[result._varLitPairs.Count - 1].Literal)) |
| | | 116 | | { |
| | 0 | 117 | | result._csClass = CompoundSegmentClass.HasOnlyPrefix; |
| | | 118 | | } |
| | | 119 | | else |
| | | 120 | | { |
| | 0 | 121 | | result._csClass = CompoundSegmentClass.HasPrefixAndSuffix; |
| | | 122 | | } |
| | | 123 | | } |
| | | 124 | | |
| | 24 | 125 | | return result; |
| | | 126 | | } |
| | | 127 | | |
| | | 128 | | public override void Bind(string[] values, ref int valueIndex, StringBuilder path) |
| | | 129 | | { |
| | | 130 | | Fx.Assert(valueIndex + _varLitPairs.Count <= values.Length, "Not enough values to bind"); |
| | | 131 | | |
| | 0 | 132 | | path.Append(_firstLiteral); |
| | 0 | 133 | | for (int pairIndex = 0; pairIndex < _varLitPairs.Count; pairIndex++) |
| | | 134 | | { |
| | 0 | 135 | | path.Append(values[valueIndex++]); |
| | 0 | 136 | | path.Append(_varLitPairs[pairIndex].Literal); |
| | | 137 | | } |
| | | 138 | | |
| | 0 | 139 | | if (EndsWithSlash) |
| | | 140 | | { |
| | 0 | 141 | | path.Append("/"); |
| | | 142 | | } |
| | 0 | 143 | | } |
| | | 144 | | |
| | | 145 | | public override bool IsEquivalentTo(UriTemplatePathSegment other, bool ignoreTrailingSlash) |
| | | 146 | | { |
| | 0 | 147 | | if (other == null) |
| | | 148 | | { |
| | | 149 | | Fx.Assert("why would we ever call this?"); |
| | 0 | 150 | | return false; |
| | | 151 | | } |
| | | 152 | | |
| | 0 | 153 | | if (!ignoreTrailingSlash && (EndsWithSlash != other.EndsWithSlash)) |
| | | 154 | | { |
| | 0 | 155 | | return false; |
| | | 156 | | } |
| | | 157 | | |
| | 0 | 158 | | if (!(other is UriTemplateCompoundPathSegment otherAsCompound)) |
| | | 159 | | { |
| | | 160 | | // if other can't be cast as a compound then it can't be equivalent |
| | 0 | 161 | | return false; |
| | | 162 | | } |
| | | 163 | | |
| | 0 | 164 | | if (_varLitPairs.Count != otherAsCompound._varLitPairs.Count) |
| | | 165 | | { |
| | 0 | 166 | | return false; |
| | | 167 | | } |
| | | 168 | | |
| | 0 | 169 | | if (StringComparer.OrdinalIgnoreCase.Compare(_firstLiteral, otherAsCompound._firstLiteral) != 0) |
| | | 170 | | { |
| | 0 | 171 | | return false; |
| | | 172 | | } |
| | | 173 | | |
| | 0 | 174 | | for (int pairIndex = 0; pairIndex < _varLitPairs.Count; pairIndex++) |
| | | 175 | | { |
| | 0 | 176 | | if (StringComparer.OrdinalIgnoreCase.Compare(_varLitPairs[pairIndex].Literal, |
| | 0 | 177 | | otherAsCompound._varLitPairs[pairIndex].Literal) != 0) |
| | | 178 | | { |
| | 0 | 179 | | return false; |
| | | 180 | | } |
| | | 181 | | } |
| | | 182 | | |
| | 0 | 183 | | return true; |
| | | 184 | | } |
| | | 185 | | |
| | | 186 | | public override bool IsMatch(UriTemplateLiteralPathSegment segment, bool ignoreTrailingSlash) |
| | | 187 | | { |
| | 1 | 188 | | if (!ignoreTrailingSlash && (EndsWithSlash != segment.EndsWithSlash)) |
| | | 189 | | { |
| | 0 | 190 | | return false; |
| | | 191 | | } |
| | | 192 | | |
| | 1 | 193 | | return TryLookup(segment.AsUnescapedString(), null); |
| | | 194 | | } |
| | | 195 | | |
| | | 196 | | public override void Lookup(string segment, NameValueCollection boundParameters) |
| | | 197 | | { |
| | 1 | 198 | | if (!TryLookup(segment, boundParameters)) |
| | | 199 | | { |
| | | 200 | | Fx.Assert("How can that be? Lookup is expected to be called after IsMatch"); |
| | 0 | 201 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException( |
| | 0 | 202 | | SR.Format(SR.UTCSRLookupBeforeMatch))); |
| | | 203 | | } |
| | 1 | 204 | | } |
| | | 205 | | |
| | | 206 | | private bool TryLookup(string segment, NameValueCollection boundParameters) |
| | | 207 | | { |
| | 2 | 208 | | int segmentPosition = 0; |
| | 2 | 209 | | if (!string.IsNullOrEmpty(_firstLiteral)) |
| | | 210 | | { |
| | 0 | 211 | | if (segment.StartsWith(_firstLiteral, StringComparison.Ordinal)) |
| | | 212 | | { |
| | 0 | 213 | | segmentPosition = _firstLiteral.Length; |
| | | 214 | | } |
| | | 215 | | else |
| | | 216 | | { |
| | 0 | 217 | | return false; |
| | | 218 | | } |
| | | 219 | | } |
| | | 220 | | |
| | 8 | 221 | | for (int pairIndex = 0; pairIndex < _varLitPairs.Count - 1; pairIndex++) |
| | | 222 | | { |
| | 2 | 223 | | int nextLiteralPosition = segment.IndexOf(_varLitPairs[pairIndex].Literal, segmentPosition, StringCompar |
| | 2 | 224 | | if (nextLiteralPosition < segmentPosition + 1) |
| | | 225 | | { |
| | 0 | 226 | | return false; |
| | | 227 | | } |
| | 2 | 228 | | if (boundParameters != null) |
| | | 229 | | { |
| | 1 | 230 | | string varValue = segment.Substring(segmentPosition, nextLiteralPosition - segmentPosition); |
| | 1 | 231 | | boundParameters.Add(_varLitPairs[pairIndex].VarName, varValue); |
| | | 232 | | } |
| | 2 | 233 | | segmentPosition = nextLiteralPosition + _varLitPairs[pairIndex].Literal.Length; |
| | | 234 | | } |
| | | 235 | | |
| | 2 | 236 | | if (segmentPosition < segment.Length) |
| | | 237 | | { |
| | 2 | 238 | | if (string.IsNullOrEmpty(_varLitPairs[_varLitPairs.Count - 1].Literal)) |
| | | 239 | | { |
| | 2 | 240 | | if (boundParameters != null) |
| | | 241 | | { |
| | 1 | 242 | | boundParameters.Add(_varLitPairs[_varLitPairs.Count - 1].VarName, |
| | 1 | 243 | | segment.Substring(segmentPosition)); |
| | | 244 | | } |
| | 2 | 245 | | return true; |
| | | 246 | | } |
| | 0 | 247 | | else if ((segmentPosition + _varLitPairs[_varLitPairs.Count - 1].Literal.Length < segment.Length) && |
| | 0 | 248 | | segment.EndsWith(_varLitPairs[_varLitPairs.Count - 1].Literal, StringComparison.Ordinal)) |
| | | 249 | | { |
| | 0 | 250 | | if (boundParameters != null) |
| | | 251 | | { |
| | 0 | 252 | | boundParameters.Add(_varLitPairs[_varLitPairs.Count - 1].VarName, |
| | 0 | 253 | | segment.Substring(segmentPosition, segment.Length - segmentPosition - _varLitPairs[_varLitPa |
| | | 254 | | } |
| | 0 | 255 | | return true; |
| | | 256 | | } |
| | | 257 | | else |
| | | 258 | | { |
| | 0 | 259 | | return false; |
| | | 260 | | } |
| | | 261 | | } |
| | | 262 | | else |
| | | 263 | | { |
| | 0 | 264 | | return false; |
| | | 265 | | } |
| | | 266 | | } |
| | | 267 | | |
| | | 268 | | // A note about comparing compound segments: |
| | | 269 | | // We are using this for generating the sorted collections at the nodes of the UriTemplateTrieNode. |
| | | 270 | | // The idea is that we are sorting the segments based on preferred matching, when we have two |
| | | 271 | | // compound segments matching the same wire segment, we will give preference to the preceding one. |
| | | 272 | | // The order is based on the following concepts: |
| | | 273 | | // - We are defining four classes of compound segments: prefix+suffix, prefix-only, suffix-only |
| | | 274 | | // and none |
| | | 275 | | // - Whenever we are comparing segments from different class the preferred one is the segment with |
| | | 276 | | // the prefared class, based on the order we defined them (p+s \ p \ s \ n). |
| | | 277 | | // - Within each class the preference is based on the prefix\suffix, while prefix has precedence |
| | | 278 | | // over suffix if both exists. |
| | | 279 | | // - If after comparing the class, as well as the prefix\suffix, we didn't reach to a conclusion, |
| | | 280 | | // the preference is given to the segment with more variables parts. |
| | | 281 | | // This order mostly follows the intuitive common sense; the major issue comes from preferring the |
| | | 282 | | // prefix over the suffix in the case where both exist. This is derived from the problematic of any |
| | | 283 | | // other type of solution that don't prefere the prefix over the suffix or vice versa. To better |
| | | 284 | | // understanding lets considered the following example: |
| | | 285 | | // In comparing 'foo{x}bar' and 'food{x}ar', unless we are preferring prefix or suffix, we have |
| | | 286 | | // to state that they have the same order. So is the case with 'foo{x}babar' and 'food{x}ar', which |
| | | 287 | | // will lead us to claiming the 'foo{x}bar' and 'foo{x}babar' are from the same order, which they |
| | | 288 | | // clearly are not. |
| | | 289 | | // Taking other approaches to this problem results in similar cases. The only solution is preferring |
| | | 290 | | // either the prefix or the suffix over the other; since we already preferred prefix over suffix |
| | | 291 | | // implicitly (we preferred the prefix only class over the suffix only, we also prefared literal |
| | | 292 | | // over variable, if in the same path segment) that still maintain consistency. |
| | | 293 | | // Therefore: |
| | | 294 | | // - 'food{var}' should be before 'foo{var}'; '{x}.{y}.{z}' should be before '{x}.{y}'. |
| | | 295 | | // - the order between '{var}bar' and '{var}qux' is not important |
| | | 296 | | // - '{x}.{y}' and '{x}_{y}' should have the same order |
| | | 297 | | // - 'foo{x}bar' is less preferred than 'food{x}ar' |
| | | 298 | | // In the above third case - if we are opening the table with allowDuplicate=false, we will throw; |
| | | 299 | | // if we are opening it with allowDuplicate=true we will let it go and might match both templates |
| | | 300 | | // for certain wire candidates. |
| | | 301 | | int IComparable<UriTemplateCompoundPathSegment>.CompareTo(UriTemplateCompoundPathSegment other) |
| | | 302 | | { |
| | | 303 | | Fx.Assert(other != null, "We are only expected to get here for comparing real compound segments"); |
| | | 304 | | |
| | 0 | 305 | | switch (_csClass) |
| | | 306 | | { |
| | | 307 | | case CompoundSegmentClass.HasPrefixAndSuffix: |
| | 0 | 308 | | switch (other._csClass) |
| | | 309 | | { |
| | | 310 | | case CompoundSegmentClass.HasPrefixAndSuffix: |
| | 0 | 311 | | return CompareToOtherThatHasPrefixAndSuffix(other); |
| | | 312 | | |
| | | 313 | | case CompoundSegmentClass.HasOnlyPrefix: |
| | | 314 | | case CompoundSegmentClass.HasOnlySuffix: |
| | | 315 | | case CompoundSegmentClass.HasNoPrefixNorSuffix: |
| | 0 | 316 | | return -1; |
| | | 317 | | |
| | | 318 | | default: |
| | | 319 | | Fx.Assert("Invalid other.CompoundSegmentClass"); |
| | 0 | 320 | | return 0; |
| | | 321 | | } |
| | | 322 | | |
| | | 323 | | case CompoundSegmentClass.HasOnlyPrefix: |
| | 0 | 324 | | switch (other._csClass) |
| | | 325 | | { |
| | | 326 | | case CompoundSegmentClass.HasPrefixAndSuffix: |
| | 0 | 327 | | return 1; |
| | | 328 | | |
| | | 329 | | case CompoundSegmentClass.HasOnlyPrefix: |
| | 0 | 330 | | return CompareToOtherThatHasOnlyPrefix(other); |
| | | 331 | | |
| | | 332 | | case CompoundSegmentClass.HasOnlySuffix: |
| | | 333 | | case CompoundSegmentClass.HasNoPrefixNorSuffix: |
| | 0 | 334 | | return -1; |
| | | 335 | | |
| | | 336 | | default: |
| | | 337 | | Fx.Assert("Invalid other.CompoundSegmentClass"); |
| | 0 | 338 | | return 0; |
| | | 339 | | } |
| | | 340 | | |
| | | 341 | | case CompoundSegmentClass.HasOnlySuffix: |
| | 0 | 342 | | switch (other._csClass) |
| | | 343 | | { |
| | | 344 | | case CompoundSegmentClass.HasPrefixAndSuffix: |
| | | 345 | | case CompoundSegmentClass.HasOnlyPrefix: |
| | 0 | 346 | | return 1; |
| | | 347 | | |
| | | 348 | | case CompoundSegmentClass.HasOnlySuffix: |
| | 0 | 349 | | return CompareToOtherThatHasOnlySuffix(other); |
| | | 350 | | |
| | | 351 | | case CompoundSegmentClass.HasNoPrefixNorSuffix: |
| | 0 | 352 | | return -1; |
| | | 353 | | |
| | | 354 | | default: |
| | | 355 | | Fx.Assert("Invalid other.CompoundSegmentClass"); |
| | 0 | 356 | | return 0; |
| | | 357 | | } |
| | | 358 | | |
| | | 359 | | case CompoundSegmentClass.HasNoPrefixNorSuffix: |
| | 0 | 360 | | switch (other._csClass) |
| | | 361 | | { |
| | | 362 | | case CompoundSegmentClass.HasPrefixAndSuffix: |
| | | 363 | | case CompoundSegmentClass.HasOnlyPrefix: |
| | | 364 | | case CompoundSegmentClass.HasOnlySuffix: |
| | 0 | 365 | | return 1; |
| | | 366 | | |
| | | 367 | | case CompoundSegmentClass.HasNoPrefixNorSuffix: |
| | 0 | 368 | | return CompareToOtherThatHasNoPrefixNorSuffix(other); |
| | | 369 | | |
| | | 370 | | default: |
| | | 371 | | Fx.Assert("Invalid other.CompoundSegmentClass"); |
| | 0 | 372 | | return 0; |
| | | 373 | | } |
| | | 374 | | |
| | | 375 | | default: |
| | | 376 | | Fx.Assert("Invalid this.CompoundSegmentClass"); |
| | 0 | 377 | | return 0; |
| | | 378 | | } |
| | | 379 | | } |
| | | 380 | | |
| | | 381 | | private int CompareToOtherThatHasPrefixAndSuffix(UriTemplateCompoundPathSegment other) |
| | | 382 | | { |
| | | 383 | | Fx.Assert(_csClass == CompoundSegmentClass.HasPrefixAndSuffix, "Otherwise, how did we got here?"); |
| | | 384 | | Fx.Assert(other._csClass == CompoundSegmentClass.HasPrefixAndSuffix, "Otherwise, how did we got here?"); |
| | | 385 | | |
| | | 386 | | // In this case we are determining the order based on the prefix of the two segments, |
| | | 387 | | // then by their suffix and then based on the number of variables |
| | 0 | 388 | | int prefixOrder = ComparePrefixToOtherPrefix(other); |
| | 0 | 389 | | if (prefixOrder == 0) |
| | | 390 | | { |
| | 0 | 391 | | int suffixOrder = CompareSuffixToOtherSuffix(other); |
| | 0 | 392 | | if (suffixOrder == 0) |
| | | 393 | | { |
| | 0 | 394 | | return (other._varLitPairs.Count - _varLitPairs.Count); |
| | | 395 | | } |
| | | 396 | | else |
| | | 397 | | { |
| | 0 | 398 | | return suffixOrder; |
| | | 399 | | } |
| | | 400 | | } |
| | | 401 | | else |
| | | 402 | | { |
| | 0 | 403 | | return prefixOrder; |
| | | 404 | | } |
| | | 405 | | } |
| | | 406 | | |
| | | 407 | | private int CompareToOtherThatHasOnlyPrefix(UriTemplateCompoundPathSegment other) |
| | | 408 | | { |
| | | 409 | | Fx.Assert(_csClass == CompoundSegmentClass.HasOnlyPrefix, "Otherwise, how did we got here?"); |
| | | 410 | | Fx.Assert(other._csClass == CompoundSegmentClass.HasOnlyPrefix, "Otherwise, how did we got here?"); |
| | | 411 | | |
| | | 412 | | // In this case we are determining the order based on the prefix of the two segments, |
| | | 413 | | // then based on the number of variables |
| | 0 | 414 | | int prefixOrder = ComparePrefixToOtherPrefix(other); |
| | 0 | 415 | | if (prefixOrder == 0) |
| | | 416 | | { |
| | 0 | 417 | | return (other._varLitPairs.Count - _varLitPairs.Count); |
| | | 418 | | } |
| | | 419 | | else |
| | | 420 | | { |
| | 0 | 421 | | return prefixOrder; |
| | | 422 | | } |
| | | 423 | | } |
| | | 424 | | |
| | | 425 | | private int CompareToOtherThatHasOnlySuffix(UriTemplateCompoundPathSegment other) |
| | | 426 | | { |
| | | 427 | | Fx.Assert(_csClass == CompoundSegmentClass.HasOnlySuffix, "Otherwise, how did we got here?"); |
| | | 428 | | Fx.Assert(other._csClass == CompoundSegmentClass.HasOnlySuffix, "Otherwise, how did we got here?"); |
| | | 429 | | |
| | | 430 | | // In this case we are determining the order based on the suffix of the two segments, |
| | | 431 | | // then based on the number of variables |
| | 0 | 432 | | int suffixOrder = CompareSuffixToOtherSuffix(other); |
| | 0 | 433 | | if (suffixOrder == 0) |
| | | 434 | | { |
| | 0 | 435 | | return (other._varLitPairs.Count - _varLitPairs.Count); |
| | | 436 | | } |
| | | 437 | | else |
| | | 438 | | { |
| | 0 | 439 | | return suffixOrder; |
| | | 440 | | } |
| | | 441 | | } |
| | | 442 | | |
| | | 443 | | private int CompareToOtherThatHasNoPrefixNorSuffix(UriTemplateCompoundPathSegment other) |
| | | 444 | | { |
| | | 445 | | Fx.Assert(_csClass == CompoundSegmentClass.HasNoPrefixNorSuffix, "Otherwise, how did we got here?"); |
| | | 446 | | Fx.Assert(other._csClass == CompoundSegmentClass.HasNoPrefixNorSuffix, "Otherwise, how did we got here?"); |
| | | 447 | | |
| | | 448 | | // In this case the order is determined by the number of variables |
| | 0 | 449 | | return (other._varLitPairs.Count - _varLitPairs.Count); |
| | | 450 | | } |
| | | 451 | | |
| | 0 | 452 | | private int ComparePrefixToOtherPrefix(UriTemplateCompoundPathSegment other) => string.Compare(other._firstLiter |
| | | 453 | | |
| | | 454 | | private int CompareSuffixToOtherSuffix(UriTemplateCompoundPathSegment other) |
| | | 455 | | { |
| | 0 | 456 | | string reversedSuffix = ReverseString(_varLitPairs[_varLitPairs.Count - 1].Literal); |
| | 0 | 457 | | string reversedOtherSuffix = ReverseString(other._varLitPairs[other._varLitPairs.Count - 1].Literal); |
| | | 458 | | |
| | 0 | 459 | | return string.Compare(reversedOtherSuffix, reversedSuffix, StringComparison.OrdinalIgnoreCase); |
| | | 460 | | } |
| | | 461 | | |
| | | 462 | | private static string ReverseString(string stringToReverse) |
| | | 463 | | { |
| | 0 | 464 | | char[] reversedString = new char[stringToReverse.Length]; |
| | 0 | 465 | | for (int i = 0; i < stringToReverse.Length; i++) |
| | | 466 | | { |
| | 0 | 467 | | reversedString[i] = stringToReverse[stringToReverse.Length - i - 1]; |
| | | 468 | | } |
| | | 469 | | |
| | 0 | 470 | | return new string(reversedString); |
| | | 471 | | } |
| | | 472 | | |
| | | 473 | | internal enum CompoundSegmentClass |
| | | 474 | | { |
| | | 475 | | Undefined, |
| | | 476 | | HasPrefixAndSuffix, |
| | | 477 | | HasOnlyPrefix, |
| | | 478 | | HasOnlySuffix, |
| | | 479 | | HasNoPrefixNorSuffix |
| | | 480 | | } |
| | | 481 | | |
| | | 482 | | internal struct VarAndLitPair |
| | | 483 | | { |
| | | 484 | | public VarAndLitPair(string varName, string literal) |
| | | 485 | | { |
| | 48 | 486 | | VarName = varName; |
| | 48 | 487 | | Literal = literal; |
| | 48 | 488 | | } |
| | | 489 | | |
| | 30 | 490 | | public string Literal { get; } |
| | 2 | 491 | | public string VarName { get; } |
| | | 492 | | } |
| | | 493 | | } |
| | | 494 | | } |