Skip to content

Commit 7346078

Browse files
committed
Moved numerics parsing extensions to StringExtensions type
1 parent 6ec8c8c commit 7346078

File tree

2 files changed

+171
-157
lines changed

2 files changed

+171
-157
lines changed
Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
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.Globalization;
7+
using System.Linq;
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"/> to <see cref="Vector2"/>
19+
/// </summary>
20+
/// <param name="str">A string in the format of "float, float"</param>
21+
/// <returns><see cref="Vector2"/></returns>
22+
public static Vector2 ToVector2(this string str)
23+
{
24+
try
25+
{
26+
var strLength = str.Count();
27+
if (strLength < 1)
28+
{
29+
throw new Exception();
30+
}
31+
else if (str[0] == '<' && str[strLength - 1] == '>')
32+
{
33+
str = str.Substring(1, strLength - 2);
34+
}
35+
36+
string[] values = str.Split(',');
37+
38+
var count = values.Count();
39+
Vector2 vector;
40+
41+
if (count == 1)
42+
{
43+
vector = new Vector2(float.Parse(values[0], CultureInfo.InvariantCulture));
44+
}
45+
else if (count == 2)
46+
{
47+
vector = new Vector2(float.Parse(values[0], CultureInfo.InvariantCulture), float.Parse(values[1], CultureInfo.InvariantCulture));
48+
}
49+
else
50+
{
51+
throw new Exception();
52+
}
53+
54+
return vector;
55+
}
56+
catch (Exception)
57+
{
58+
throw new FormatException($"Cannot convert {str} to Vector2. Use format \"float, float\"");
59+
}
60+
}
61+
62+
/// <summary>
63+
/// Converts a <see cref="string"/> to <see cref="Vector3"/>
64+
/// </summary>
65+
/// <param name="str">A string in the format of "float, float, float"</param>
66+
/// <returns><see cref="Vector3"/></returns>
67+
public static Vector3 ToVector3(this string str)
68+
{
69+
try
70+
{
71+
var strLength = str.Count();
72+
if (strLength < 1)
73+
{
74+
throw new Exception();
75+
}
76+
else if (str[0] == '<' && str[strLength - 1] == '>')
77+
{
78+
str = str.Substring(1, strLength - 2);
79+
}
80+
81+
string[] values = str.Split(',');
82+
83+
var count = values.Count();
84+
Vector3 vector;
85+
86+
if (count == 1)
87+
{
88+
vector = new Vector3(float.Parse(values[0], CultureInfo.InvariantCulture));
89+
}
90+
else if (count == 3)
91+
{
92+
vector = new Vector3(
93+
float.Parse(values[0], CultureInfo.InvariantCulture),
94+
float.Parse(values[1], CultureInfo.InvariantCulture),
95+
float.Parse(values[2], CultureInfo.InvariantCulture));
96+
}
97+
else
98+
{
99+
throw new Exception();
100+
}
101+
102+
return vector;
103+
}
104+
catch (Exception)
105+
{
106+
throw new FormatException($"Cannot convert {str} to Vector3. Use format \"float, float, float\"");
107+
}
108+
}
109+
110+
/// <summary>
111+
/// Converts a <see cref="string"/> to <see cref="Vector4"/>
112+
/// </summary>
113+
/// <param name="str">A string in the format of "float, float, float, float"</param>
114+
/// <returns><see cref="Vector4"/></returns>
115+
public static Vector4 ToVector4(this string str)
116+
{
117+
try
118+
{
119+
var strLength = str.Count();
120+
if (strLength < 1)
121+
{
122+
throw new Exception();
123+
}
124+
else if (str[0] == '<' && str[strLength - 1] == '>')
125+
{
126+
str = str.Substring(1, strLength - 2);
127+
}
128+
129+
string[] values = str.Split(',');
130+
131+
var count = values.Count();
132+
Vector4 vector;
133+
134+
if (count == 1)
135+
{
136+
vector = new Vector4(float.Parse(values[0], CultureInfo.InvariantCulture));
137+
}
138+
else if (count == 4)
139+
{
140+
vector = new Vector4(
141+
float.Parse(values[0], CultureInfo.InvariantCulture),
142+
float.Parse(values[1], CultureInfo.InvariantCulture),
143+
float.Parse(values[2], CultureInfo.InvariantCulture),
144+
float.Parse(values[3], CultureInfo.InvariantCulture));
145+
}
146+
else
147+
{
148+
throw new Exception();
149+
}
150+
151+
return vector;
152+
}
153+
catch (Exception)
154+
{
155+
throw new FormatException($"Cannot convert {str} to Vector4. Use format \"float, float, float, float\"");
156+
}
157+
}
158+
159+
/// <summary>
160+
/// Converts a <see cref="string"/> to <see cref="Quaternion"/>
161+
/// </summary>
162+
/// <param name="str">A string in the format of "float, float, float, float"</param>
163+
/// <returns><see cref="Quaternion"/></returns>
164+
public static unsafe Quaternion ToQuaternion(this string str)
165+
{
166+
Vector4 vector = str.ToVector4();
167+
168+
return *(Quaternion*)&vector;
169+
}
170+
}
171+
}

Microsoft.Toolkit.Uwp.UI/Extensions/VisualExtensions.cs

Lines changed: 0 additions & 157 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5-
using System;
6-
using System.Globalization;
7-
using System.Linq;
85
using System.Numerics;
96
using Windows.UI.Composition;
107
using Windows.UI.Xaml;
@@ -17,160 +14,6 @@ namespace Microsoft.Toolkit.Uwp.UI
1714
/// </summary>
1815
public static class VisualExtensions
1916
{
20-
/// <summary>
21-
/// Converts a <see cref="string"/> to <see cref="Vector2"/>
22-
/// </summary>
23-
/// <param name="str">A string in the format of "float, float"</param>
24-
/// <returns><see cref="Vector2"/></returns>
25-
public static Vector2 ToVector2(this string str)
26-
{
27-
try
28-
{
29-
var strLength = str.Count();
30-
if (strLength < 1)
31-
{
32-
throw new Exception();
33-
}
34-
else if (str[0] == '<' && str[strLength - 1] == '>')
35-
{
36-
str = str.Substring(1, strLength - 2);
37-
}
38-
39-
string[] values = str.Split(',');
40-
41-
var count = values.Count();
42-
Vector2 vector;
43-
44-
if (count == 1)
45-
{
46-
vector = new Vector2(float.Parse(values[0], CultureInfo.InvariantCulture));
47-
}
48-
else if (count == 2)
49-
{
50-
vector = new Vector2(float.Parse(values[0], CultureInfo.InvariantCulture), float.Parse(values[1], CultureInfo.InvariantCulture));
51-
}
52-
else
53-
{
54-
throw new Exception();
55-
}
56-
57-
return vector;
58-
}
59-
catch (Exception)
60-
{
61-
throw new FormatException($"Cannot convert {str} to Vector2. Use format \"float, float\"");
62-
}
63-
}
64-
65-
/// <summary>
66-
/// Converts a <see cref="string"/> to <see cref="Vector3"/>
67-
/// </summary>
68-
/// <param name="str">A string in the format of "float, float, float"</param>
69-
/// <returns><see cref="Vector3"/></returns>
70-
public static Vector3 ToVector3(this string str)
71-
{
72-
try
73-
{
74-
var strLength = str.Count();
75-
if (strLength < 1)
76-
{
77-
throw new Exception();
78-
}
79-
else if (str[0] == '<' && str[strLength - 1] == '>')
80-
{
81-
str = str.Substring(1, strLength - 2);
82-
}
83-
84-
string[] values = str.Split(',');
85-
86-
var count = values.Count();
87-
Vector3 vector;
88-
89-
if (count == 1)
90-
{
91-
vector = new Vector3(float.Parse(values[0], CultureInfo.InvariantCulture));
92-
}
93-
else if (count == 3)
94-
{
95-
vector = new Vector3(
96-
float.Parse(values[0], CultureInfo.InvariantCulture),
97-
float.Parse(values[1], CultureInfo.InvariantCulture),
98-
float.Parse(values[2], CultureInfo.InvariantCulture));
99-
}
100-
else
101-
{
102-
throw new Exception();
103-
}
104-
105-
return vector;
106-
}
107-
catch (Exception)
108-
{
109-
throw new FormatException($"Cannot convert {str} to Vector3. Use format \"float, float, float\"");
110-
}
111-
}
112-
113-
/// <summary>
114-
/// Converts a <see cref="string"/> to <see cref="Vector4"/>
115-
/// </summary>
116-
/// <param name="str">A string in the format of "float, float, float, float"</param>
117-
/// <returns><see cref="Vector4"/></returns>
118-
public static Vector4 ToVector4(this string str)
119-
{
120-
try
121-
{
122-
var strLength = str.Count();
123-
if (strLength < 1)
124-
{
125-
throw new Exception();
126-
}
127-
else if (str[0] == '<' && str[strLength - 1] == '>')
128-
{
129-
str = str.Substring(1, strLength - 2);
130-
}
131-
132-
string[] values = str.Split(',');
133-
134-
var count = values.Count();
135-
Vector4 vector;
136-
137-
if (count == 1)
138-
{
139-
vector = new Vector4(float.Parse(values[0], CultureInfo.InvariantCulture));
140-
}
141-
else if (count == 4)
142-
{
143-
vector = new Vector4(
144-
float.Parse(values[0], CultureInfo.InvariantCulture),
145-
float.Parse(values[1], CultureInfo.InvariantCulture),
146-
float.Parse(values[2], CultureInfo.InvariantCulture),
147-
float.Parse(values[3], CultureInfo.InvariantCulture));
148-
}
149-
else
150-
{
151-
throw new Exception();
152-
}
153-
154-
return vector;
155-
}
156-
catch (Exception)
157-
{
158-
throw new FormatException($"Cannot convert {str} to Vector4. Use format \"float, float, float, float\"");
159-
}
160-
}
161-
162-
/// <summary>
163-
/// Converts a <see cref="string"/> to <see cref="Quaternion"/>
164-
/// </summary>
165-
/// <param name="str">A string in the format of "float, float, float, float"</param>
166-
/// <returns><see cref="Quaternion"/></returns>
167-
public static unsafe Quaternion ToQuaternion(this string str)
168-
{
169-
Vector4 vector = str.ToVector4();
170-
171-
return *(Quaternion*)&vector;
172-
}
173-
17417
/// <summary>
17518
/// Retrieves the <see cref="Visual"/> object of a <see cref="UIElement"/>
17619
/// </summary>

0 commit comments

Comments
 (0)