KeyValuePairUtils.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /* Copyright (C) 2012-2016 Tal Aloni <tal.aloni.il@gmail.com>. All rights reserved.
  2. *
  3. * You can redistribute this program and/or modify it under the terms of
  4. * the GNU Lesser Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. */
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Text;
  10. namespace Utilities
  11. {
  12. public class KeyValuePairUtils
  13. {
  14. public static KeyValuePairList<string, string> GetKeyValuePairList(string nullDelimitedString)
  15. {
  16. KeyValuePairList<string, string> result = new KeyValuePairList<string, string>();
  17. string[] entries = nullDelimitedString.Split('\0');
  18. foreach (string entry in entries)
  19. {
  20. string[] pair = entry.Split('=');
  21. if (pair.Length >= 2)
  22. {
  23. string key = pair[0];
  24. string value = pair[1];
  25. result.Add(key, value);
  26. }
  27. }
  28. return result;
  29. }
  30. public static string ToNullDelimitedString(KeyValuePairList<string, string> list)
  31. {
  32. StringBuilder builder = new StringBuilder();
  33. foreach (KeyValuePair<string, string> pair in list)
  34. {
  35. builder.AppendFormat("{0}={1}\0", pair.Key, pair.Value);
  36. }
  37. return builder.ToString();
  38. }
  39. public static string ToString(KeyValuePairList<string, string> list)
  40. {
  41. StringBuilder builder = new StringBuilder();
  42. for (int index = 0; index < list.Count; index++)
  43. {
  44. if (index > 0)
  45. {
  46. builder.Append(", ");
  47. }
  48. builder.AppendFormat("{0}={1}", list[index].Key, list[index].Value);
  49. }
  50. return builder.ToString();
  51. }
  52. }
  53. }