NameAndParameters.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.Linq;
  5. ////TODO: add array support
  6. ////TODO: switch parsing to use to Substring
  7. namespace UnityEngine.InputSystem.Utilities
  8. {
  9. /// <summary>
  10. /// A combination of a name and an optional list of named parameter values. For example, "Clamp(min=1,max=2)".
  11. /// </summary>
  12. public struct NameAndParameters
  13. {
  14. public string name { get; set; }
  15. public ReadOnlyArray<NamedValue> parameters { get; set; }
  16. public override string ToString()
  17. {
  18. if (parameters.Count == 0)
  19. return name;
  20. var parameterString = string.Join(NamedValue.Separator, parameters.Select(x => x.ToString()).ToArray());
  21. return $"{name}({parameterString})";
  22. }
  23. public static IEnumerable<NameAndParameters> ParseMultiple(string text)
  24. {
  25. List<NameAndParameters> list = null;
  26. if (!ParseMultiple(text, ref list))
  27. return Enumerable.Empty<NameAndParameters>();
  28. return list;
  29. }
  30. internal static bool ParseMultiple(string text, ref List<NameAndParameters> list)
  31. {
  32. text = text.Trim();
  33. if (string.IsNullOrEmpty(text))
  34. return false;
  35. if (list == null)
  36. list = new List<NameAndParameters>();
  37. else
  38. list.Clear();
  39. var index = 0;
  40. var textLength = text.Length;
  41. while (index < textLength)
  42. list.Add(ParseNameAndParameters(text, ref index));
  43. return true;
  44. }
  45. public static NameAndParameters Parse(string text)
  46. {
  47. if (text == null)
  48. throw new ArgumentNullException(nameof(text));
  49. var index = 0;
  50. return ParseNameAndParameters(text, ref index);
  51. }
  52. private static NameAndParameters ParseNameAndParameters(string text, ref int index)
  53. {
  54. var textLength = text.Length;
  55. // Skip whitespace.
  56. while (index < textLength && char.IsWhiteSpace(text[index]))
  57. ++index;
  58. // Parse name.
  59. var nameStart = index;
  60. while (index < textLength)
  61. {
  62. var nextChar = text[index];
  63. if (nextChar == '(' || nextChar == NamedValue.Separator[0] || char.IsWhiteSpace(nextChar))
  64. break;
  65. ++index;
  66. }
  67. if (index - nameStart == 0)
  68. throw new ArgumentException($"Expecting name at position {nameStart} in '{text}'", nameof(text));
  69. var name = text.Substring(nameStart, index - nameStart);
  70. // Skip whitespace.
  71. while (index < textLength && char.IsWhiteSpace(text[index]))
  72. ++index;
  73. // Parse parameters.
  74. NamedValue[] parameters = null;
  75. if (index < textLength && text[index] == '(')
  76. {
  77. ++index;
  78. var closeParenIndex = text.IndexOf(')', index);
  79. if (closeParenIndex == -1)
  80. throw new ArgumentException($"Expecting ')' after '(' at position {index} in '{text}'", nameof(text));
  81. var parameterString = text.Substring(index, closeParenIndex - index);
  82. parameters = NamedValue.ParseMultiple(parameterString);
  83. index = closeParenIndex + 1;
  84. }
  85. if (index < textLength && (text[index] == ',' || text[index] == InputBinding.Separator))
  86. ++index;
  87. return new NameAndParameters {name = name, parameters = new ReadOnlyArray<NamedValue>(parameters)};
  88. }
  89. }
  90. }