-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstringstream.pas
259 lines (227 loc) · 6.37 KB
/
stringstream.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
{$MODE OBJFPC} { -*- delphi -*- }
{$INCLUDE settings.inc}
unit stringstream;
// This is for reading null-separated UTF-8-encoded data consisting of
// just integers, floating-point numbers, booleans, or strings.
interface
type
TStringStreamReader = class
// this class can never fail. invalid input will just start returning empty strings and 0s.
private
FInput: UTF8String;
FPosition: Cardinal; // last character to have been read
FEnded: Boolean;
function ReadUntilNull(const Terminal: Char = #0): UTF8String;
procedure Close();
public
constructor Create(const Input: UTF8String);
function ReadLongint(): Longint;
function ReadCardinal(): Cardinal; // only supports values up to High(Longint)
function ReadDouble(): Double;
function ReadString(): UTF8String;
function ReadString(const MaxLength: Cardinal): UTF8String;
function ReadBoolean(): Boolean;
procedure ReadEnd();
procedure Bail(); // call this when you can't be bothered to check if the rest of the data is valid and you just want to stop reading
property Ended: Boolean read FEnded;
end;
TStringStreamWriter = class
private
FValue: UTF8String;
FClosed: Boolean;
protected
{$IFOPT C+} function GetDebugStarted(): Boolean; {$ENDIF}
public
constructor Create();
procedure WriteLongint(const Value: Longint);
procedure WriteCardinal(const Value: Cardinal);
procedure WriteDouble(const Value: Double);
procedure WriteString(const Value: UTF8String);
procedure WriteBoolean(const Value: Boolean);
procedure Reset();
procedure Close();
function Serialize(): UTF8String;
property Closed: Boolean read FClosed;
{$IFOPT C+} property DebugStarted: Boolean read GetDebugStarted; {$ENDIF}
end;
implementation
uses
sysutils, intutils, exceptions, utf8 {$IFOPT C+}, math {$ENDIF};
const FloatFormat: TFormatSettings = (
CurrencyFormat: 1;
NegCurrFormat: 1;
ThousandSeparator: ',';
DecimalSeparator: '.';
CurrencyDecimals: 2;
DateSeparator: '-';
TimeSeparator: ':';
ListSeparator: ',';
CurrencyString: '$';
ShortDateFormat: 'yyyy-mm-dd';
LongDateFormat: 'dd" "mmmm" "yyyy';
TimeAMString: 'AM';
TimePMString: 'PM';
ShortTimeFormat: 'hh:nn';
LongTimeFormat: 'hh:nn:ss';
ShortMonthNames: ('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec');
LongMonthNames: ('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');
ShortDayNames: ('Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat');
LongDayNames: ('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday');
TwoDigitYearCenturyWindow: 50
);
constructor TStringStreamReader.Create(const Input: UTF8String);
begin
Assert(Length(Input) < High(Cardinal));
FInput := Input;
end;
function TStringStreamReader.ReadUntilNull(const Terminal: Char): UTF8String;
var
Start: Cardinal;
begin
Start := FPosition+1; // $R-
repeat
Inc(FPosition);
if (FPosition > Length(FInput)) then
begin
Result := '';
Close();
exit;
end;
until (FInput[FPosition] = Terminal);
Result := Copy(FInput, Start, FPosition - Start);
end;
procedure TStringStreamReader.Close();
begin
// move pointer to past the end
FPosition := Length(FInput)+1; // $R-
end;
function TStringStreamReader.ReadCardinal(): Cardinal;
var
Value: Int64;
begin
Value := ParseInt64(ReadUntilNull(), Low(Int64));
if ((Value < Low(Cardinal)) or (Value > High(Cardinal))) then
begin
Value := 0;
Close();
end;
Result := Value; // $R-
end;
function TStringStreamReader.ReadLongint(): Longint;
var
Value: Int64;
begin
Value := ParseInt64(ReadUntilNull(), Low(Int64));
if ((Value < Low(Longint)) or (Value > High(Longint))) then
begin
Value := 0;
Close();
end;
Result := Value; // $R-
end;
function TStringStreamReader.ReadDouble(): Double;
begin
Result := StrToFloatDef(ReadUntilNull(), 0.0, FloatFormat); // $R-
end;
function TStringStreamReader.ReadString(): UTF8String;
begin
Result := ReadUntilNull();
if (not IsValidUTF8(Result)) then
begin
Result := '';
Close();
end;
end;
function TStringStreamReader.ReadString(const MaxLength: Cardinal): UTF8String;
begin
Result := ReadString();
if (Length(Result) > MaxLength) then
begin
Result := '';
Close();
end;
end;
function TStringStreamReader.ReadBoolean(): Boolean;
var
Buffer: UTF8String;
begin
Buffer := ReadUntilNull();
if (Buffer = 'T') then
begin
Result := True;
end
else
if (Buffer = 'F') then
begin
Result := False;
end
else
begin
Result := False;
Close();
end;
end;
procedure TStringStreamReader.ReadEnd();
begin
if (FPosition = Length(FInput)) then // pointer just reached past the end of the input string
FEnded := True;
Close();
end;
procedure TStringStreamReader.Bail();
begin
FEnded := True;
Close();
end;
constructor TStringStreamWriter.Create();
begin
end;
{$IFOPT C+}
function TStringStreamWriter.GetDebugStarted(): Boolean;
begin
Result := FValue <> '';
end;
{$ENDIF}
// TODO: this should not keep copying the string around
procedure TStringStreamWriter.WriteCardinal(const Value: Cardinal);
begin
FValue := FValue + IntToStr(Value) + #0;
end;
procedure TStringStreamWriter.WriteLongint(const Value: Longint);
begin
FValue := FValue + IntToStr(Value) + #0;
end;
procedure TStringStreamWriter.WriteDouble(const Value: Double);
begin
Assert(not IsInfinite(Value));
Assert(not IsNaN(Value));
FValue := FValue + FloatToStrF(Value, ffExponent, 15, 0, FloatFormat) + #0;
end;
procedure TStringStreamWriter.WriteString(const Value: UTF8String);
begin
Assert(IsValidUTF8(Value));
Assert(Pos(#0, Value) = 0);
FValue := FValue + Value + #0;
end;
procedure TStringStreamWriter.WriteBoolean(const Value: Boolean);
begin
if (Value) then
FValue := FValue + 'T' + #0
else
FValue := FValue + 'F' + #0;
end;
procedure TStringStreamWriter.Reset();
begin
Assert(not FClosed);
FValue := '';
end;
procedure TStringStreamWriter.Close();
begin
Assert(not FClosed);
FClosed := True;
end;
function TStringStreamWriter.Serialize(): UTF8String;
begin
Assert(FClosed);
Result := FValue;
end;
end.