19 using System.Globalization;
20 using System.Collections.Concurrent;
21 using System.Runtime.CompilerServices;
33 private const int NoDecimalPlaces = -1;
34 private const char NoMoreData = unchecked((
char)-1);
35 private const char DefaultDelimiter =
',';
43 [MethodImpl(MethodImplOptions.AggressiveInlining)]
44 public static decimal
GetDecimal(
this StreamReader stream,
char delimiter = DefaultDelimiter)
56 [MethodImpl(MethodImplOptions.AggressiveInlining)]
57 public static decimal
GetDecimal(
this StreamReader stream, out
bool pastEndLine,
char delimiter = DefaultDelimiter)
60 var decimalPlaces = NoDecimalPlaces;
61 var current = (char)stream.Read();
63 while (current ==
' ')
65 current = (char)stream.Read();
68 var isNegative = current ==
'-';
71 current = (char)stream.Read();
74 pastEndLine = current ==
'\n' || current ==
'\r' && (stream.Peek() !=
'\n' || stream.Read() ==
'\n') || current == NoMoreData;
75 while (!(current == delimiter || pastEndLine || current ==
' '))
83 value = value * 10 + (current -
'0');
84 if (decimalPlaces != NoDecimalPlaces)
89 current = (char)stream.Read();
90 pastEndLine = current ==
'\n' || current ==
'\r' && (stream.Peek() !=
'\n' || stream.Read() ==
'\n') || current == NoMoreData;
94 var mid = (int)(value >> 32);
95 return new decimal(lo, mid, 0, isNegative, (
byte)(decimalPlaces != NoDecimalPlaces ? decimalPlaces : 0));
105 [MethodImpl(MethodImplOptions.AggressiveInlining)]
108 var current = (char)stream.Read();
109 while (current ==
' ')
111 current = (char)stream.Read();
116 var data =
new char[format.Length];
117 while (!(current == delimiter || current ==
'\n' || current ==
'\r' && (stream.Peek() !=
'\n' || stream.Read() ==
'\n') || current == NoMoreData))
119 data[index++] = current;
120 current = (char)stream.Read();
123 return DateTime.ParseExact(data,
125 CultureInfo.InvariantCulture);
134 [MethodImpl(MethodImplOptions.AggressiveInlining)]
135 public static int GetInt32(
this StreamReader stream,
char delimiter = DefaultDelimiter)
138 var current = (char)stream.Read();
140 while (current ==
' ')
142 current = (char)stream.Read();
145 var isNegative = current ==
'-';
148 current = (char)stream.Read();
151 while (!(current == delimiter || current ==
'\n' || current ==
'\r' && (stream.Peek() !=
'\n' || stream.Read() ==
'\n') || current == NoMoreData || current ==
' '))
153 result = (current -
'0') + result * 10;
154 current = (char)stream.Read();
156 return isNegative ? result * -1 : result;
159 private readonly
static ConcurrentBag<StringBuilder> StringBuilders =
new();
167 [MethodImpl(MethodImplOptions.AggressiveInlining)]
168 public static string GetString(
this StreamReader stream,
char delimiter = DefaultDelimiter)
170 if (!StringBuilders.TryTake(out var builder))
177 var current = (char)stream.Read();
179 while (!(current == delimiter || current ==
'\n' || current ==
'\r' && (stream.Peek() !=
'\n' || stream.Read() ==
'\n') || current == NoMoreData))
181 builder.Append(current);
182 current = (char)stream.Read();
184 return builder.ToString();
189 StringBuilders.Add(builder);