| | | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | | 2 | | // Licensed under the MIT License. |
| | | 3 | | |
| | | 4 | | using System.Globalization; |
| | | 5 | | |
| | | 6 | | namespace Azure.Data.AppConfiguration |
| | | 7 | | { |
| | | 8 | | internal class SyncTokenUtils |
| | | 9 | | { |
| | | 10 | | // Sync-Token Syntax: <id>=<value>;sn=<sn> |
| | | 11 | | internal static bool TryParse(string value, out SyncToken result) |
| | | 12 | | { |
| | 624 | 13 | | result = new SyncToken(); |
| | | 14 | | |
| | 624 | 15 | | if (string.IsNullOrEmpty(value)) |
| | | 16 | | { |
| | 0 | 17 | | return false; |
| | | 18 | | } |
| | | 19 | | |
| | 624 | 20 | | string tokenId = null; |
| | 624 | 21 | | string tokenValue = null; |
| | 624 | 22 | | long sequenceNumber = 0; |
| | | 23 | | |
| | 624 | 24 | | int pos = 0; |
| | 624 | 25 | | string name = null; |
| | 624 | 26 | | string val = null; |
| | | 27 | | |
| | 43792 | 28 | | for (int i = 0; i < value.Length; i++) |
| | | 29 | | { |
| | 21272 | 30 | | char ch = value[i]; |
| | | 31 | | |
| | 21272 | 32 | | if (name == null) |
| | | 33 | | { |
| | 7600 | 34 | | if (ch == '=') |
| | | 35 | | { |
| | 1248 | 36 | | name = value.Substring(pos, i - pos); |
| | 1248 | 37 | | pos = i + 1; |
| | | 38 | | |
| | 1248 | 39 | | continue; |
| | | 40 | | } |
| | | 41 | | } |
| | | 42 | | |
| | 20024 | 43 | | if (ch == ';' || i == value.Length - 1) |
| | | 44 | | { |
| | | 45 | | // |
| | | 46 | | // Done with the current name-value pair |
| | 1248 | 47 | | if (i == value.Length - 1) |
| | | 48 | | { |
| | 624 | 49 | | i++; |
| | | 50 | | } |
| | | 51 | | |
| | 1248 | 52 | | string fragment = value.Substring(pos, i - pos); |
| | | 53 | | |
| | 1248 | 54 | | if (name != null) |
| | | 55 | | { |
| | 1248 | 56 | | val = fragment; |
| | | 57 | | } |
| | | 58 | | else |
| | | 59 | | { |
| | 0 | 60 | | name = fragment; |
| | | 61 | | } |
| | | 62 | | |
| | 1248 | 63 | | name = name.Trim(); |
| | | 64 | | |
| | | 65 | | // |
| | | 66 | | // Sequence Number |
| | 1248 | 67 | | if (name == "sn") |
| | | 68 | | { |
| | 624 | 69 | | if (!long.TryParse(val, NumberStyles.None, CultureInfo.InvariantCulture, out sequenceNumber)) |
| | | 70 | | { |
| | 0 | 71 | | return false; |
| | | 72 | | } |
| | | 73 | | } |
| | | 74 | | // |
| | | 75 | | // id-value |
| | 624 | 76 | | else if (tokenId == null) |
| | | 77 | | { |
| | 624 | 78 | | tokenValue = val; |
| | 624 | 79 | | tokenId = name; |
| | | 80 | | } |
| | | 81 | | |
| | | 82 | | // |
| | | 83 | | // Reset current |
| | 1248 | 84 | | name = null; |
| | 1248 | 85 | | val = null; |
| | | 86 | | |
| | 1248 | 87 | | pos = i + 1; |
| | | 88 | | } |
| | | 89 | | } |
| | | 90 | | |
| | | 91 | | // |
| | | 92 | | // Validate |
| | 624 | 93 | | bool success = false; |
| | 624 | 94 | | if (!(tokenId == null || tokenValue == null)) |
| | | 95 | | { |
| | 624 | 96 | | result = new SyncToken(tokenId, tokenValue, sequenceNumber); |
| | 624 | 97 | | success = true; |
| | | 98 | | } |
| | | 99 | | |
| | 624 | 100 | | return success; |
| | | 101 | | } |
| | | 102 | | } |
| | | 103 | | } |