/
DebugInfoDictionaryHelper.cs
47 lines (44 loc) · 1.4 KB
/
DebugInfoDictionaryHelper.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
using System;
using System.Collections.Generic;
using System.Text;
namespace Cosmos.Debug.Common
{
public static class DictionaryHelper
{
/// <summary>Build a string from collection content. Each collection item is used to produce
/// a 'key=value' string. Pairs are separated by a semi colon.</summary>
public static string DumpToString(Dictionary<string, string> value)
{
var xSB = new StringBuilder();
foreach (string xKey in value.Keys)
{
xSB.AppendFormat("{0}={1};", xKey, value[xKey]);
}
return xSB.ToString();
}
public static void LoadFromString(Dictionary<string, string> target, string value)
{
if (target.Count > 0)
{
throw new Exception("Target is not empty!");
}
if (string.IsNullOrEmpty(value))
{
return;
}
string[] xPairs = value.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
foreach (var xPair in xPairs)
{
string[] xParts = xPair.Split('=');
if (xParts.Length > 1)
{
target.Add(xParts[0], xParts[1]);
}
else
{
target.Add(xParts[0], "");
}
}
}
}
}