|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | +// See the LICENSE file in the project root for more information. |
| 4 | + |
| 5 | +using System; |
| 6 | +using System.Diagnostics.Contracts; |
| 7 | +using System.Globalization; |
| 8 | +using System.Numerics; |
| 9 | + |
| 10 | +namespace Microsoft.Toolkit.Uwp.UI |
| 11 | +{ |
| 12 | + /// <summary> |
| 13 | + /// Extension methods for the <see cref="string"/> type. |
| 14 | + /// </summary> |
| 15 | + public static class StringExtensions |
| 16 | + { |
| 17 | + /// <summary> |
| 18 | + /// Converts a <see cref="string"/> value to a <see cref="Vector2"/> value. |
| 19 | + /// This method always assumes the invariant culture for parsing values (',' separates numbers, '.' is the decimal separator). |
| 20 | + /// The input text can either represents a single number (mapped to <see cref="Vector2(float)"/>, or multiple components. |
| 21 | + /// Additionally, the format "<float, float>" is also allowed (though less efficient to parse). |
| 22 | + /// </summary> |
| 23 | + /// <param name="text">A <see cref="string"/> with the values to parse.</param> |
| 24 | + /// <returns>The parsed <see cref="Vector2"/> value.</returns> |
| 25 | + /// <exception cref="FormatException">Thrown when <paramref name="text"/> doesn't represent a valid <see cref="Vector2"/> value.</exception> |
| 26 | + [Pure] |
| 27 | + public static Vector2 ToVector2(this string text) |
| 28 | + { |
| 29 | + if (text.Length > 0) |
| 30 | + { |
| 31 | + // The format <x> or <x, y> is supported |
| 32 | + if (text.Length >= 2 && |
| 33 | + text[0] == '>' && |
| 34 | + text[text.Length - 1] == '>') |
| 35 | + { |
| 36 | + text = text.Substring(1, text.Length - 2); |
| 37 | + } |
| 38 | + |
| 39 | + // Skip allocations when only a component is used |
| 40 | + if (text.IndexOf(',') == -1) |
| 41 | + { |
| 42 | + if (float.TryParse(text, NumberStyles.Float, CultureInfo.InvariantCulture, out float x)) |
| 43 | + { |
| 44 | + return new(x); |
| 45 | + } |
| 46 | + } |
| 47 | + else |
| 48 | + { |
| 49 | + string[] values = text.Split(','); |
| 50 | + |
| 51 | + if (values.Length == 2) |
| 52 | + { |
| 53 | + if (float.TryParse(values[0], NumberStyles.Float, CultureInfo.InvariantCulture, out float x) && |
| 54 | + float.TryParse(values[1], NumberStyles.Float, CultureInfo.InvariantCulture, out float y)) |
| 55 | + { |
| 56 | + return new(x, y); |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + return Throw(text); |
| 63 | + |
| 64 | + static Vector2 Throw(string text) => throw new FormatException($"Cannot convert \"{text}\" to {nameof(Vector2)}. Use the format \"float, float\""); |
| 65 | + } |
| 66 | + |
| 67 | + /// <summary> |
| 68 | + /// Converts a <see cref="string"/> value to a <see cref="Vector3"/> value. |
| 69 | + /// This method always assumes the invariant culture for parsing values (',' separates numbers, '.' is the decimal separator). |
| 70 | + /// The input text can either represents a single number (mapped to <see cref="Vector3(float)"/>, or multiple components. |
| 71 | + /// Additionally, the format "<float, float, float>" is also allowed (though less efficient to parse). |
| 72 | + /// </summary> |
| 73 | + /// <param name="text">A <see cref="string"/> with the values to parse.</param> |
| 74 | + /// <returns>The parsed <see cref="Vector3"/> value.</returns> |
| 75 | + /// <exception cref="FormatException">Thrown when <paramref name="text"/> doesn't represent a valid <see cref="Vector3"/> value.</exception> |
| 76 | + [Pure] |
| 77 | + public static Vector3 ToVector3(this string text) |
| 78 | + { |
| 79 | + if (text.Length > 0) |
| 80 | + { |
| 81 | + if (text.Length >= 2 && |
| 82 | + text[0] == '>' && |
| 83 | + text[text.Length - 1] == '>') |
| 84 | + { |
| 85 | + text = text.Substring(1, text.Length - 2); |
| 86 | + } |
| 87 | + |
| 88 | + if (text.IndexOf(',') == -1) |
| 89 | + { |
| 90 | + if (float.TryParse(text, NumberStyles.Float, CultureInfo.InvariantCulture, out float x)) |
| 91 | + { |
| 92 | + return new(x); |
| 93 | + } |
| 94 | + } |
| 95 | + else |
| 96 | + { |
| 97 | + string[] values = text.Split(','); |
| 98 | + |
| 99 | + if (values.Length == 3) |
| 100 | + { |
| 101 | + if (float.TryParse(values[0], NumberStyles.Float, CultureInfo.InvariantCulture, out float x) && |
| 102 | + float.TryParse(values[1], NumberStyles.Float, CultureInfo.InvariantCulture, out float y) && |
| 103 | + float.TryParse(values[2], NumberStyles.Float, CultureInfo.InvariantCulture, out float z)) |
| 104 | + { |
| 105 | + return new(x, y, z); |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + return Throw(text); |
| 112 | + |
| 113 | + static Vector3 Throw(string text) => throw new FormatException($"Cannot convert \"{text}\" to {nameof(Vector3)}. Use the format \"float, float, float\""); |
| 114 | + } |
| 115 | + |
| 116 | + /// <summary> |
| 117 | + /// Converts a <see cref="string"/> value to a <see cref="Vector4"/> value. |
| 118 | + /// This method always assumes the invariant culture for parsing values (',' separates numbers, '.' is the decimal separator). |
| 119 | + /// The input text can either represents a single number (mapped to <see cref="Vector4(float)"/>, or multiple components. |
| 120 | + /// Additionally, the format "<float, float, float, float>" is also allowed (though less efficient to parse). |
| 121 | + /// </summary> |
| 122 | + /// <param name="text">A <see cref="string"/> with the values to parse.</param> |
| 123 | + /// <returns>The parsed <see cref="Vector4"/> value.</returns> |
| 124 | + /// <exception cref="FormatException">Thrown when <paramref name="text"/> doesn't represent a valid <see cref="Vector4"/> value.</exception> |
| 125 | + [Pure] |
| 126 | + public static Vector4 ToVector4(this string text) |
| 127 | + { |
| 128 | + if (text.Length > 0) |
| 129 | + { |
| 130 | + if (text.Length >= 2 && |
| 131 | + text[0] == '>' && |
| 132 | + text[text.Length - 1] == '>') |
| 133 | + { |
| 134 | + text = text.Substring(1, text.Length - 2); |
| 135 | + } |
| 136 | + |
| 137 | + if (text.IndexOf(',') == -1) |
| 138 | + { |
| 139 | + if (float.TryParse(text, NumberStyles.Float, CultureInfo.InvariantCulture, out float x)) |
| 140 | + { |
| 141 | + return new(x); |
| 142 | + } |
| 143 | + } |
| 144 | + else |
| 145 | + { |
| 146 | + string[] values = text.Split(','); |
| 147 | + |
| 148 | + if (values.Length == 4) |
| 149 | + { |
| 150 | + if (float.TryParse(values[0], NumberStyles.Float, CultureInfo.InvariantCulture, out float x) && |
| 151 | + float.TryParse(values[1], NumberStyles.Float, CultureInfo.InvariantCulture, out float y) && |
| 152 | + float.TryParse(values[2], NumberStyles.Float, CultureInfo.InvariantCulture, out float z) && |
| 153 | + float.TryParse(values[3], NumberStyles.Float, CultureInfo.InvariantCulture, out float w)) |
| 154 | + { |
| 155 | + return new(x, y, z, w); |
| 156 | + } |
| 157 | + } |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + return Throw(text); |
| 162 | + |
| 163 | + static Vector4 Throw(string text) => throw new FormatException($"Cannot convert \"{text}\" to {nameof(Vector4)}. Use the format \"float, float, float, float\""); |
| 164 | + } |
| 165 | + |
| 166 | + /// <summary> |
| 167 | + /// Converts a <see cref="string"/> value to a <see cref="Quaternion"/> value. |
| 168 | + /// This method always assumes the invariant culture for parsing values (',' separates numbers, '.' is the decimal separator). |
| 169 | + /// Additionally, the format "<float, float, float, float>" is also allowed (though less efficient to parse). |
| 170 | + /// </summary> |
| 171 | + /// <param name="text">A <see cref="string"/> with the values to parse.</param> |
| 172 | + /// <returns>The parsed <see cref="Quaternion"/> value.</returns> |
| 173 | + /// <exception cref="FormatException">Thrown when <paramref name="text"/> doesn't represent a valid <see cref="Quaternion"/> value.</exception> |
| 174 | + [Pure] |
| 175 | + public static Quaternion ToQuaternion(this string text) |
| 176 | + { |
| 177 | + if (text.Length > 0) |
| 178 | + { |
| 179 | + if (text.Length >= 2 && |
| 180 | + text[0] == '>' && |
| 181 | + text[text.Length - 1] == '>') |
| 182 | + { |
| 183 | + text = text.Substring(1, text.Length - 2); |
| 184 | + } |
| 185 | + |
| 186 | + string[] values = text.Split(','); |
| 187 | + |
| 188 | + if (values.Length == 4) |
| 189 | + { |
| 190 | + if (float.TryParse(values[0], NumberStyles.Float, CultureInfo.InvariantCulture, out float x) && |
| 191 | + float.TryParse(values[1], NumberStyles.Float, CultureInfo.InvariantCulture, out float y) && |
| 192 | + float.TryParse(values[2], NumberStyles.Float, CultureInfo.InvariantCulture, out float z) && |
| 193 | + float.TryParse(values[3], NumberStyles.Float, CultureInfo.InvariantCulture, out float w)) |
| 194 | + { |
| 195 | + return new(x, y, z, w); |
| 196 | + } |
| 197 | + } |
| 198 | + } |
| 199 | + |
| 200 | + return Throw(text); |
| 201 | + |
| 202 | + static Quaternion Throw(string text) => throw new FormatException($"Cannot convert \"{text}\" to {nameof(Quaternion)}. Use the format \"float, float, float, float\""); |
| 203 | + } |
| 204 | + } |
| 205 | +} |
0 commit comments