Skip to content

Commit 1cc3406

Browse files
committed
improved TValue conversion of string to other types
1 parent 409cf53 commit 1cc3406

File tree

2 files changed

+102
-3
lines changed

2 files changed

+102
-3
lines changed

Source/Base/Spring.pas

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6205,6 +6205,45 @@ procedure MakeDynArray(typeInfo: PTypeInfo; count: NativeInt; var result: TValue
62056205
TValue.MakeWithoutCopy(@p, typeInfo, result);
62066206
end;
62076207

6208+
function SplitString(const s, delimiter: string): TStringDynArray;
6209+
6210+
function ScanChar(const s: string; var index: Integer): Boolean;
6211+
var
6212+
level: Integer;
6213+
begin
6214+
Result := False;
6215+
level := 0;
6216+
while index <= Length(s) do
6217+
begin
6218+
case s[index] of
6219+
'[': Inc(level);
6220+
']': Dec(level);
6221+
else
6222+
if Copy(s, index, Length(delimiter)) = delimiter then
6223+
if level = 0 then
6224+
Exit(True);
6225+
end;
6226+
Inc(index);
6227+
Result := level = 0;
6228+
end;
6229+
end;
6230+
6231+
var
6232+
startPos, index, len: Integer;
6233+
begin
6234+
Result := nil;
6235+
startPos := 1;
6236+
index := 1;
6237+
while ScanChar(s, index) do
6238+
begin
6239+
len := Length(Result);
6240+
SetLength(Result, len + 1);
6241+
Result[len] := Copy(s, startPos, index - startPos);
6242+
Inc(index);
6243+
startPos := index;
6244+
end;
6245+
end;
6246+
62086247
function ConvStr2DynArray(const source: TValue; target: PTypeInfo;
62096248
out value: TValue; const formatSettings: TFormatSettings): Boolean;
62106249
var
@@ -6222,7 +6261,16 @@ function ConvStr2DynArray(const source: TValue; target: PTypeInfo;
62226261
elType := target.TypeData.DynArrElType^;
62236262
for i := 0 to High(values) do
62246263
begin
6225-
v1 := TValue.From(Trim(values[i]));
6264+
case elType.Kind of
6265+
tkString, tkLString, tkWString, tkUString: ;
6266+
tkChar, tkWChar:
6267+
if Length(values[i]) > 1 then
6268+
values[i] := Trim(values[i]);
6269+
else
6270+
values[i] := Trim(values[i]);
6271+
end;
6272+
6273+
v1 := TValue.From(values[i]);
62266274
if not v1.TryConvert(elType, v2) then
62276275
Exit(False);
62286276
res.SetArrayElement(i, v2);
@@ -6255,7 +6303,16 @@ function ConvStr2Array(const source: TValue; target: PTypeInfo;
62556303
TValue.Make(nil, target, res);
62566304
for i := 0 to arrData.ElCount - 1 do
62576305
begin
6258-
v1 := TValue.From(Trim(values[i]));
6306+
case elType.Kind of
6307+
tkString, tkLString, tkWString, tkUString: ;
6308+
tkChar, tkWChar:
6309+
if Length(values[i]) > 1 then
6310+
values[i] := Trim(values[i]);
6311+
else
6312+
values[i] := Trim(values[i]);
6313+
end;
6314+
6315+
v1 := TValue.From(values[i]);
62596316
if not v1.TryConvert(elType, v2) then
62606317
Exit(False);
62616318
res.SetArrayElement(i, v2);

Tests/Source/Spring.Testing.pas

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,8 @@ procedure TRttiMethodHelper.ConvertValues(const values: TArray<TValue>;
199199
value := values[i]
200200
else
201201
value := TValue.Empty;
202+
if value.IsString and not parameters[i].ParamType.IsString then
203+
value := Trim(value.AsString);
202204
value.TryConvert(parameters[i].ParamType.Handle, arguments[i], ISO8601FormatSettings);
203205
end;
204206
retType := ReturnType;
@@ -234,8 +236,48 @@ function IsTestMethod(const method: TRttiMethod;
234236
{$REGION 'TTestingAttribute'}
235237

236238
constructor TTestingAttribute.Create(const values: string; const delimiter: string);
239+
240+
function SplitString(const s, delimiter: string): TArray<string>;
241+
242+
function ScanChar(const s: string; var index: Integer): Boolean;
243+
var
244+
level: Integer;
245+
begin
246+
Result := False;
247+
level := 0;
248+
while index <= Length(s) do
249+
begin
250+
case s[index] of
251+
'[': Inc(level);
252+
']': Dec(level);
253+
else
254+
if Copy(s, index, Length(delimiter)) = delimiter then
255+
if level = 0 then
256+
Exit(True);
257+
end;
258+
Inc(index);
259+
Result := level = 0;
260+
end;
261+
end;
262+
263+
var
264+
startPos, index, len: Integer;
265+
begin
266+
Result := nil;
267+
startPos := 1;
268+
index := 1;
269+
while ScanChar(s, index) do
270+
begin
271+
len := Length(Result);
272+
SetLength(Result, len + 1);
273+
Result[len] := Copy(s, startPos, index - startPos);
274+
Inc(index);
275+
startPos := index;
276+
end;
277+
end;
278+
237279
var
238-
tempValues: TStringDynArray;
280+
tempValues: TArray<string>;
239281
i: Integer;
240282
begin
241283
inherited Create;

0 commit comments

Comments
 (0)