Hydrogen Framework  1.3.1
 All Classes Namespaces Functions Variables Enumerations Enumerator Properties Events
Hydrogen.Regex Class Reference

Additional static functions and constants used to extend existing Regex support inside of Unity. More...

Static Public Member Functions

static string PregReplace (this string target, string[] pattern, string[] replacements)
 Replaces all matches based on pattern with their associated replacement in the target. More...
 
static string ReplaceMetaChars (string input)
 Replaces the string representations of meta chars with their corresponding character values. More...
 
static string Sprintf (string format, params object[] parameters)
 Returns a string produced according to the formatting string format. More...
 

Detailed Description

Additional static functions and constants used to extend existing Regex support inside of Unity.

Definition at line 38 of file Regex.cs.

Member Function Documentation

static string Hydrogen.Regex.PregReplace ( this string  target,
string[]  pattern,
string[]  replacements 
)
static

Replaces all matches based on pattern with their associated replacement in the target.

Returns
The replaced target string.
Parameters
targetThe target string to search through.
patternRegular expression patterns.
replacementsReplacement strings.

Definition at line 50 of file Regex.cs.

51  {
52  if (replacements.Length != pattern.Length)
53  throw new ArgumentException ("Replacement and Pattern Arrays must be balanced");
54 
55  for (var i = 0; i < pattern.Length; i++) {
56  target = System.Text.RegularExpressions.Regex.Replace (target, pattern [i], replacements [i]);
57  }
58 
59  return target;
60  }
static string Hydrogen.Regex.ReplaceMetaChars ( string  input)
static

Replaces the string representations of meta chars with their corresponding character values.

Parameters
inputThe input.
Returns
A string with all string meta chars are replaced.

Definition at line 67 of file Regex.cs.

68  {
69  return System.Text.RegularExpressions.Regex.Replace (input, @"(\\)(\d{3}|[^\d])?", new MatchEvaluator (ReplaceMetaCharsMatch));
70  }
static string Hydrogen.Regex.Sprintf ( string  format,
params object[]  parameters 
)
static

Returns a string produced according to the formatting string format.

Parameters
formatFormat.
parametersParameters.

Definition at line 77 of file Regex.cs.

78  {
79 
80  var f = new StringBuilder ();
81  Match m;
82  string w;
83  int defaultParamIx = 0;
84  int paramIx;
85  object o;
86 
87  bool flagLeft2Right;
88  bool flagAlternate;
89  bool flagPositiveSign;
90  bool flagPositiveSpace;
91  bool flagZeroPadding;
92  bool flagGroupThousands;
93 
94  int fieldLength;
95  int fieldPrecision;
96  char shortLongIndicator;
97  char formatSpecifier;
98  char paddingCharacter;
99 
100  // find all format parameters in format string
101  f.Append (format);
102  m = R.Match (f.ToString ());
103  while (m.Success) {
104 
105  paramIx = defaultParamIx;
106  if (m.Groups [1] != null && m.Groups [1].Value.Length > 0) {
107  var val = m.Groups [1].Value.Substring (0, m.Groups [1].Value.Length - 1);
108  paramIx = Convert.ToInt32 (val) - 1;
109  }
110 
111  // extract format flags
112  flagAlternate = false;
113  flagLeft2Right = false;
114  flagPositiveSign = false;
115  flagPositiveSpace = false;
116  flagZeroPadding = false;
117  flagGroupThousands = false;
118 
119  if (m.Groups [2] != null && m.Groups [2].Value.Length > 0) {
120  var flags = m.Groups [2].Value;
121 
122  flagAlternate = (flags.IndexOf ('#') >= 0);
123  flagLeft2Right = (flags.IndexOf ('-') >= 0);
124  flagPositiveSign = (flags.IndexOf ('+') >= 0);
125  flagPositiveSpace = (flags.IndexOf (' ') >= 0);
126  flagGroupThousands = (flags.IndexOf ('\'') >= 0);
127 
128  // positive + indicator overrides a positive space character
129  flagPositiveSpace &= !flagPositiveSign || !flagPositiveSpace;
130  }
131 
132  // Extract field length and pading character
133  paddingCharacter = ' ';
134  fieldLength = int.MinValue;
135 
136  if (m.Groups [3] != null && m.Groups [3].Value.Length > 0) {
137  fieldLength = Convert.ToInt32 (m.Groups [3].Value);
138  flagZeroPadding = (m.Groups [3].Value [0] == '0');
139  }
140 
141  if (flagZeroPadding)
142  paddingCharacter = '0';
143 
144  // Left2Right allignment overrides zero padding
145  if (flagLeft2Right && flagZeroPadding) {
146  paddingCharacter = ' ';
147  }
148 
149  // Extract field precision
150  fieldPrecision = int.MinValue;
151  if (m.Groups [4] != null && m.Groups [4].Value.Length > 0)
152  fieldPrecision = Convert.ToInt32 (m.Groups [4].Value);
153 
154  // Extract short / long indicator
155  shortLongIndicator = Char.MinValue;
156  if (m.Groups [5] != null && m.Groups [5].Value.Length > 0)
157  shortLongIndicator = m.Groups [5].Value [0];
158 
159  // Extract format
160  formatSpecifier = Char.MinValue;
161  if (m.Groups [6] != null && m.Groups [6].Value.Length > 0)
162  formatSpecifier = m.Groups [6].Value [0];
163 
164  // Default precision is 6 digits if none is specified except
165  if (fieldPrecision == int.MinValue && formatSpecifier != 's' && formatSpecifier != 'c' &&
166  Char.ToUpper (formatSpecifier) != 'X' && formatSpecifier != 'o')
167  fieldPrecision = 6;
168 
169  // Get next value parameter and convert value parameter depending on short / long indicator
170  if (parameters == null || paramIx >= parameters.Length)
171  o = null;
172  else {
173  o = parameters [paramIx];
174 
175  if (shortLongIndicator == 'h') {
176  if (o is int)
177  o = (short)((int)o);
178  else if (o is long)
179  o = (short)((long)o);
180  else if (o is uint)
181  o = (ushort)((uint)o);
182  else if (o is ulong)
183  o = (ushort)((ulong)o);
184  } else if (shortLongIndicator == 'l') {
185  if (o is short)
186  o = (long)((short)o);
187  else if (o is int)
188  o = (long)((int)o);
189  else if (o is ushort)
190  o = (ulong)((ushort)o);
191  else if (o is uint)
192  o = (ulong)((uint)o);
193  }
194  }
195 
196  // convert value parameters to a string depending on the formatSpecifier
197  w = String.Empty;
198  switch (formatSpecifier) {
199  case '%': // % character
200  w = "%";
201  break;
202  case 'd': // integer
203  w = FormatNumber ((flagGroupThousands ? "n" : "d"), fieldLength, int.MinValue,
204  flagLeft2Right, flagPositiveSign, flagPositiveSpace, paddingCharacter, o);
205  defaultParamIx++;
206  break;
207  case 'i': // integer
208  goto case 'd';
209  case 'o': // octal integer - no leading zero
210  w = FormatOct (flagAlternate, fieldLength, flagLeft2Right, paddingCharacter, o);
211  defaultParamIx++;
212  break;
213  case 'x': // hex integer - no leading zero
214  w = FormatHex ("x", flagAlternate, fieldLength, fieldPrecision, flagLeft2Right,
215  paddingCharacter, o);
216  defaultParamIx++;
217  break;
218  case 'X': // same as x but with capital hex characters
219  w = FormatHex ("X", flagAlternate, fieldLength, fieldPrecision, flagLeft2Right,
220  paddingCharacter, o);
221  defaultParamIx++;
222  break;
223  case 'u': // unsigned integer
224  w = FormatNumber ((flagGroupThousands ? "n" : "d"), fieldLength, int.MinValue,
225  flagLeft2Right, false, false, paddingCharacter, Math.ToUnsigned (o));
226  defaultParamIx++;
227  break;
228  case 'c': // character
229  if (o.IsNumericType ())
230  w = Convert.ToChar (o).ToString ();
231  else if (o is char)
232  w = ((char)o).ToString ();
233  else if (o is string && ((string)o).Length > 0)
234  w = ((string)o) [0].ToString ();
235  defaultParamIx++;
236  break;
237  case 's': // string
238  if (o == null)
239  break;
240 
241  w = o.ToString ();
242  if (fieldPrecision >= 0)
243  w = w.Substring (0, fieldPrecision);
244 
245  if (fieldLength != int.MinValue)
246  if (flagLeft2Right)
247  w = w.PadRight (fieldLength, paddingCharacter);
248  else
249  w = w.PadLeft (fieldLength, paddingCharacter);
250  defaultParamIx++;
251  break;
252  case 'f': // double
253  w = FormatNumber ((flagGroupThousands ? "n" : "f"), fieldLength, fieldPrecision,
254  flagLeft2Right, flagPositiveSign, flagPositiveSpace, paddingCharacter, o);
255  defaultParamIx++;
256  break;
257  case 'e': // double / exponent
258  w = FormatNumber ("e",
259  fieldLength, fieldPrecision, flagLeft2Right, flagPositiveSign,
260  flagPositiveSpace, paddingCharacter, o);
261  defaultParamIx++;
262  break;
263  case 'E': // double / exponent
264  w = FormatNumber ("E",
265  fieldLength, fieldPrecision, flagLeft2Right, flagPositiveSign,
266  flagPositiveSpace, paddingCharacter, o);
267  defaultParamIx++;
268  break;
269  case 'g': // double / exponent
270  w = FormatNumber ("g", fieldLength, fieldPrecision, flagLeft2Right,
271  flagPositiveSign, flagPositiveSpace, paddingCharacter, o);
272  defaultParamIx++;
273  break;
274  case 'G': // double / exponent
275  w = FormatNumber ("G", fieldLength, fieldPrecision, flagLeft2Right,
276  flagPositiveSign, flagPositiveSpace, paddingCharacter, o);
277  defaultParamIx++;
278  break;
279  case 'p': // pointer
280  if (o is IntPtr)
281 #if UNITY_XBOX360
282  w = ( (IntPtr)o ).ToString();
283 #else
284  w = "0x" + ((IntPtr)o).ToString ("x");
285 #endif
286  defaultParamIx++;
287  break;
288  case 'n': // number of characters so far
289  w = FormatNumber ("d", fieldLength, int.MinValue, flagLeft2Right,
290  flagPositiveSign, flagPositiveSpace, paddingCharacter, m.Index);
291  break;
292  default:
293  w = String.Empty;
294  defaultParamIx++;
295  break;
296  }
297 
298  // replace format parameter with parameter value
299  // and start searching for the next format parameter
300  // AFTER the position of the current inserted value
301  // to prohibit recursive matches if the value also
302  // includes a format specifier
303  f.Remove (m.Index, m.Length);
304  f.Insert (m.Index, w);
305  m = R.Match (f.ToString (), m.Index + w.Length);
306  }
307 
308  return f.ToString ();
309  }