| 1 |
using System; |
| 2 |
using System.Linq; |
| 3 |
using System.Collections.Generic; |
| 4 |
namespace Mono.Documentation.Updater.Frameworks |
| 5 |
{ |
| 6 |
public static class FXUtils |
| 7 |
{ |
| 8 |
public static string AddFXToList (string existingValue, string newFX) |
| 9 |
{ |
| 10 |
var cachedValue = GetCache (existingValue, newFX, CacheAction.Add); |
| 11 |
if (!string.IsNullOrWhiteSpace (cachedValue)) |
| 12 |
return cachedValue; |
| 13 |
|
| 14 |
var splitValue = SplitList (existingValue); |
| 15 |
if (!splitValue.Contains (newFX)) splitValue.Add (newFX); |
| 16 |
var returnVal = JoinList (splitValue); |
| 17 |
|
| 18 |
SetCache (existingValue, newFX, returnVal, CacheAction.Add); |
| 19 |
|
| 20 |
return returnVal; |
| 21 |
} |
| 22 |
|
| 23 |
public static string RemoveFXFromList (string existingValue, string FXToRemove) |
| 24 |
{ |
| 25 |
var cachedValue = GetCache (existingValue, FXToRemove, CacheAction.Remove); |
| 26 |
if (!string.IsNullOrWhiteSpace (cachedValue)) |
| 27 |
return cachedValue; |
| 28 |
|
| 29 |
var splitValue = SplitList (existingValue); |
| 30 |
splitValue.Remove (FXToRemove); |
| 31 |
var returnVal = JoinList (splitValue); |
| 32 |
|
| 33 |
SetCache (existingValue, FXToRemove, returnVal, CacheAction.Remove); |
| 34 |
|
| 35 |
return returnVal; |
| 36 |
} |
| 37 |
|
| 38 |
/// <summary>Returns a list of all previously processed frameworks (not including the current)</summary> |
| 39 |
internal static string PreviouslyProcessedFXString (FrameworkTypeEntry typeEntry) |
| 40 |
{ |
| 41 |
if (typeEntry == null) |
| 42 |
return string.Empty; |
| 43 |
|
| 44 |
return string.Join (";", typeEntry |
| 45 |
.PreviouslyProcessedFrameworkTypes |
| 46 |
.Select (previous => previous?.Framework?.Name) |
| 47 |
.Where (n => !string.IsNullOrWhiteSpace (n)) |
| 48 |
.ToArray ()); |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
static List<string> SplitList (string existingValue) |
| 53 |
{ |
| 54 |
existingValue = existingValue ?? string.Empty; |
| 55 |
|
| 56 |
return existingValue.Split (new[] { ';' }, StringSplitOptions.RemoveEmptyEntries).ToList (); |
| 57 |
} |
| 58 |
|
| 59 |
static string JoinList (List<string> splitValue) |
| 60 |
{ |
| 61 |
return string.Join (";", splitValue.ToArray ()); |
| 62 |
} |
| 63 |
|
| 64 |
#region Framework String Cache Stuff |
| 65 |
|
| 66 |
/// <summary>Cache for modified framework strings</summary> |
| 67 |
static Dictionary<string, ValueTuple<Dictionary<string, string>, Dictionary<string, string>>> map = new Dictionary<string, ValueTuple<Dictionary<string, string>, Dictionary<string, string>>> (); |
| 68 |
enum CacheAction { Add, Remove } |
| 69 |
|
| 70 |
static string GetCache (string currentValue, string value, CacheAction action) |
| 71 |
{ |
| 72 |
ValueTuple<Dictionary<string, string>, Dictionary<string, string>> cacheKey; |
| 73 |
if (map.TryGetValue (currentValue, out cacheKey)) |
| 74 |
{ |
| 75 |
string cachedValue = string.Empty; |
| 76 |
switch (action) |
| 77 |
{ |
| 78 |
case CacheAction.Add: |
| 79 |
cacheKey.Item1.TryGetValue (value, out cachedValue); |
| 80 |
break; |
| 81 |
case CacheAction.Remove: |
| 82 |
cacheKey.Item2.TryGetValue (value, out cachedValue); |
| 83 |
break; |
| 84 |
} |
| 85 |
return cachedValue; |
| 86 |
} |
| 87 |
return string.Empty; |
| 88 |
} |
| 89 |
static void SetCache (string currentValue, string value, string newValue, CacheAction action) |
| 90 |
{ |
| 91 |
ValueTuple<Dictionary<string, string>, Dictionary<string, string>> outerCacheValue; |
| 92 |
if (!map.TryGetValue (currentValue, out outerCacheValue)) |
| 93 |
{ |
| 94 |
outerCacheValue = new ValueTuple<Dictionary<string, string>, Dictionary<string, string>> (new Dictionary<string, string> (), new Dictionary<string, string> ()); |
| 95 |
map.Add (currentValue, outerCacheValue); |
| 96 |
} |
| 97 |
|
| 98 |
Dictionary<string, string> innerCacheContainer = null; |
| 99 |
switch (action) |
| 100 |
{ |
| 101 |
case CacheAction.Add: |
| 102 |
innerCacheContainer = outerCacheValue.Item1; |
| 103 |
break; |
| 104 |
case CacheAction.Remove: |
| 105 |
innerCacheContainer = outerCacheValue.Item2; |
| 106 |
break; |
| 107 |
} |
| 108 |
|
| 109 |
if (!innerCacheContainer.ContainsKey (value)) |
| 110 |
{ |
| 111 |
innerCacheContainer.Add (value, newValue); |
| 112 |
} |
| 113 |
|
| 114 |
} |
| 115 |
|
| 116 |
#endregion |
| 117 |
} |
| 118 |
} |