Jypeli  9
The simple game programming library
ParseHelper.cs
Siirry tämän tiedoston dokumentaatioon.
1 #region MIT License
2 /*
3  * Copyright (c) 2005-2008 Jonathan Mark Porter. http://physics2d.googlepages.com/
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a copy
6  * of this software and associated documentation files (the "Software"), to deal
7  * in the Software without restriction, including without limitation the rights to
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9  * the Software, and to permit persons to whom the Software is furnished to do so,
10  * subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be
13  * included in all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
16  * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
17  * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20  * OTHER DEALINGS IN THE SOFTWARE.
21  */
22 #endregion
23 
24 
25 
26 #if UseDouble
27 using Scalar = System.Double;
28 #else
29 using Scalar = System.Single;
30 #endif
31 using System;
32 using System.Text;
33 
34 
35 namespace AdvanceMath
36 {
37  internal static class ParseHelper
38  {
39  public static string[] SplitStringVector(string s)
40  {
41  return SpritString(s, ',', new char[] { ' ', '(', '[', '<', ')', ']', '>' });
42  }
43  private static string[] SpritString(string s, char divider, char[] toremove)
44  {
45  StringBuilder builder = new StringBuilder(s);
46  foreach (char r in toremove)
47  {
48  builder.Replace(r, ' ');
49  }
50 #if !CompactFramework && !WindowsCE && !PocketPC && !XBOX360 && !SILVERLIGHT
51  return builder.ToString().Split(new char[] { divider, ' ' }, StringSplitOptions.RemoveEmptyEntries);
52 #else
53  string[] temp = s.Split(new char[] { divider, ' ' });
54  int index2 = 0;
55  for (int index1 = 0; index1 < temp.Length; ++index1)
56  {
57  if (temp[index1].Length > 0)
58  {
59  temp[index2++] = temp[index1];
60  }
61  }
62  string[] vals = new string[index2];
63  Array.Copy(temp, vals, vals.Length);
64  return vals;
65 #endif
66  }
67  private static string[] SpritString(string s, char divider, string[] toremove)
68  {
69  StringBuilder builder = new StringBuilder(s);
70  foreach (string r in toremove)
71  {
72  builder.Replace(r, " ");
73  }
74 #if !CompactFramework && !WindowsCE && !PocketPC && !XBOX360 && !SILVERLIGHT
75  return builder.ToString().Split(new char[] { divider, ' ' }, StringSplitOptions.RemoveEmptyEntries);
76 #else
77  string[] temp = s.Split(new char[] { divider, ' ' });
78  int index2 = 0;
79  for (int index1 = 0; index1 < temp.Length; ++index1)
80  {
81  if (temp[index1].Length > 0)
82  {
83  temp[index2++] = temp[index1];
84  }
85  }
86  string[] vals = new string[index2];
87  Array.Copy(temp, vals, vals.Length);
88  return vals;
89 #endif
90  }
91 
92 
93  public static void ParseMatrix<T>(string s, ref T valueType)
94  where T : IAdvanceValueType
95  {
96  Parse(s, ref valueType, "|\n", "|", ' ');
97  }
98  public static void Parse<T>(string s, ref T valueType, string leftParenth, string rightParenth, char divider)
99  where T : IAdvanceValueType
100  {
101  if (s == null)
102  {
103  throw new ArgumentNullException("s");
104  }
105  string[] parts = SpritString(s, divider, new string[] { leftParenth, rightParenth });
106  if (parts.Length != valueType.Count)
107  {
108  throw new FormatException(
109  string.Format("Cannot parse the text '{0}' because it does not have {1} parts.",
110  s, valueType.Count));
111  }
112  Scalar[] result = new Scalar[valueType.Count];
113  for (int index = 0; index < valueType.Count; ++index)
114  {
115  result[index] = Scalar.Parse(parts[index]);
116  }
117  valueType.CopyFrom(result, 0);
118  }
119 #if !CompactFramework && !WindowsCE && !PocketPC && !XBOX360 && !SILVERLIGHT
120  public static bool TryParseMatrix<T>(string s, ref T valueType)
121  where T : IAdvanceValueType
122  {
123  return TryParse(s, ref valueType, "|", "|\n", ' ');
124  }
125  public static bool TryParse<T>(string s, ref T valueType, string leftParenth, string rightParenth, char divider)
126  where T : IAdvanceValueType
127  {
128  if (s == null)
129  {
130  return false;
131  }
132  string[] parts = SpritString(s, divider, new string[] { leftParenth, rightParenth });
133  if (parts.Length != valueType.Count)
134  {
135  return false;
136  }
137  Scalar[] result = new Scalar[valueType.Count];
138  for (int index = 0; index < valueType.Count; ++index)
139  {
140  if (!Scalar.TryParse(parts[index], out result[index]))
141  {
142  return false;
143  }
144  }
145  valueType.CopyFrom(result, 0);
146  return true;
147  }
148 #endif
149 
150  }
151 }
AdvanceMath.ParseHelper.TryParseMatrix< T >
static bool TryParseMatrix< T >(string s, ref T valueType)
Definition: ParseHelper.cs:120
AdvanceMath.ParseHelper.Parse< T >
static void Parse< T >(string s, ref T valueType, string leftParenth, string rightParenth, char divider)
Definition: ParseHelper.cs:98
AdvanceMath.ParseHelper.SpritString
static string[] SpritString(string s, char divider, string[] toremove)
Definition: ParseHelper.cs:67
AdvanceMath.ParseHelper
Definition: ParseHelper.cs:38
Scalar
System.Single Scalar
Definition: Clamped.cs:29
AdvanceMath.ParseHelper.SpritString
static string[] SpritString(string s, char divider, char[] toremove)
Definition: ParseHelper.cs:43
AdvanceMath.IAdvanceValueType
Definition: Interfaces.cs:37
System
Definition: CFFauxAttributes.cs:29
AdvanceMath.ParseHelper.TryParse< T >
static bool TryParse< T >(string s, ref T valueType, string leftParenth, string rightParenth, char divider)
Definition: ParseHelper.cs:125
AdvanceMath.ParseHelper.ParseMatrix< T >
static void ParseMatrix< T >(string s, ref T valueType)
Definition: ParseHelper.cs:93
AdvanceMath.ParseHelper.SplitStringVector
static string[] SplitStringVector(string s)
Definition: ParseHelper.cs:39
AdvanceMath
Definition: Clamped.cs:36