Returns a string produced according to the formatting string format.
80 var f =
new StringBuilder ();
83 int defaultParamIx = 0;
89 bool flagPositiveSign;
90 bool flagPositiveSpace;
92 bool flagGroupThousands;
96 char shortLongIndicator;
98 char paddingCharacter;
102 m = R.Match (f.ToString ());
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;
112 flagAlternate =
false;
113 flagLeft2Right =
false;
114 flagPositiveSign =
false;
115 flagPositiveSpace =
false;
116 flagZeroPadding =
false;
117 flagGroupThousands =
false;
119 if (m.Groups [2] != null && m.Groups [2].Value.Length > 0) {
120 var flags = m.Groups [2].Value;
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);
129 flagPositiveSpace &= !flagPositiveSign || !flagPositiveSpace;
133 paddingCharacter =
' ';
134 fieldLength = int.MinValue;
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');
142 paddingCharacter =
'0';
145 if (flagLeft2Right && flagZeroPadding) {
146 paddingCharacter =
' ';
150 fieldPrecision = int.MinValue;
151 if (m.Groups [4] != null && m.Groups [4].Value.Length > 0)
152 fieldPrecision = Convert.ToInt32 (m.Groups [4].Value);
155 shortLongIndicator = Char.MinValue;
156 if (m.Groups [5] != null && m.Groups [5].Value.Length > 0)
157 shortLongIndicator = m.Groups [5].Value [0];
160 formatSpecifier = Char.MinValue;
161 if (m.Groups [6] != null && m.Groups [6].Value.Length > 0)
162 formatSpecifier = m.Groups [6].Value [0];
165 if (fieldPrecision ==
int.MinValue && formatSpecifier !=
's' && formatSpecifier !=
'c' &&
166 Char.ToUpper (formatSpecifier) !=
'X' && formatSpecifier !=
'o')
170 if (parameters == null || paramIx >= parameters.Length)
173 o = parameters [paramIx];
175 if (shortLongIndicator ==
'h') {
179 o = (short)((
long)o);
181 o = (ushort)((uint)o);
183 o = (ushort)((ulong)o);
184 }
else if (shortLongIndicator ==
'l') {
186 o = (long)((
short)o);
189 else if (o is ushort)
190 o = (ulong)((ushort)o);
192 o = (ulong)((uint)o);
198 switch (formatSpecifier) {
203 w = FormatNumber ((flagGroupThousands ?
"n" :
"d"), fieldLength,
int.MinValue,
204 flagLeft2Right, flagPositiveSign, flagPositiveSpace, paddingCharacter, o);
210 w = FormatOct (flagAlternate, fieldLength, flagLeft2Right, paddingCharacter, o);
214 w = FormatHex (
"x", flagAlternate, fieldLength, fieldPrecision, flagLeft2Right,
215 paddingCharacter, o);
219 w = FormatHex (
"X", flagAlternate, fieldLength, fieldPrecision, flagLeft2Right,
220 paddingCharacter, o);
224 w = FormatNumber ((flagGroupThousands ?
"n" :
"d"), fieldLength,
int.MinValue,
225 flagLeft2Right,
false,
false, paddingCharacter, Math.ToUnsigned (o));
229 if (o.IsNumericType ())
230 w = Convert.ToChar (o).ToString ();
232 w = ((char)o).ToString ();
233 else if (o is
string && ((
string)o).Length > 0)
234 w = ((string)o) [0].ToString ();
242 if (fieldPrecision >= 0)
243 w = w.Substring (0, fieldPrecision);
245 if (fieldLength !=
int.MinValue)
247 w = w.PadRight (fieldLength, paddingCharacter);
249 w = w.PadLeft (fieldLength, paddingCharacter);
253 w = FormatNumber ((flagGroupThousands ?
"n" :
"f"), fieldLength, fieldPrecision,
254 flagLeft2Right, flagPositiveSign, flagPositiveSpace, paddingCharacter, o);
258 w = FormatNumber (
"e",
259 fieldLength, fieldPrecision, flagLeft2Right, flagPositiveSign,
260 flagPositiveSpace, paddingCharacter, o);
264 w = FormatNumber (
"E",
265 fieldLength, fieldPrecision, flagLeft2Right, flagPositiveSign,
266 flagPositiveSpace, paddingCharacter, o);
270 w = FormatNumber (
"g", fieldLength, fieldPrecision, flagLeft2Right,
271 flagPositiveSign, flagPositiveSpace, paddingCharacter, o);
275 w = FormatNumber (
"G", fieldLength, fieldPrecision, flagLeft2Right,
276 flagPositiveSign, flagPositiveSpace, paddingCharacter, o);
282 w = ( (IntPtr)o ).ToString();
284 w =
"0x" + ((IntPtr)o).ToString (
"x");
289 w = FormatNumber (
"d", fieldLength,
int.MinValue, flagLeft2Right,
290 flagPositiveSign, flagPositiveSpace, paddingCharacter, m.Index);
303 f.Remove (m.Index, m.Length);
304 f.Insert (m.Index, w);
305 m = R.Match (f.ToString (), m.Index + w.Length);
308 return f.ToString ();