Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-devel / 2012-03

TStrings Add(array of string)

6 messages · 5 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Andrew Haines

I was thinking about TStrings possibly having an overloaded function Add or AddStrings where the argument is an array of string?

procedure Add(strs: array of string); overload;
or
procedure AddStrings(strs: array of string); overload;
Specifically I want this for TProcess.Parameters. since it's a bit of a
pain to type
Proc.Parameters.Add('-Arg1');
Proc.Parameters.Add('Arg1_Value');
Proc.Parameters.Add('-Arg2');
Proc.Parameters.Add('Arg2_Value');
Proc.Parameters.Add('-Arg3');
Proc.Parameters.Add('Arg3_Value');

even "with Proc.Parameters do begin Add(n); ... end; " is a bit ugly when parameters have arguments.

when I could type:

Proc.Parameters.Add(['-Arg1', 'Arg1_Value']);
Proc.Parameters.Add(['-Arg2', 'Arg2_Value']);
Proc.Parameters.Add(['-Arg3', 'Arg3_Value']);

or even

Proc.Parameters.Add(['-Arg1','Arg1_Value','-Arg2','Arg2_Value','-Arg3',
'Arg3_Value']);
A less generic alternative would be adding a procedure to TProcess
AddArgs(args: array of string);

Is this a good idea?

If yes then I can create a patch.

Regards,

Andrew

Reply by email

Sven Barth
to Andrew Haines
"Andrew Haines" wrote 37 lines
I was thinking about TStrings possibly having an overloaded function Add or AddStrings where the argument is an array of string? procedure Add(strs: array of string); overload; or procedure AddStrings(strs: array of string); overload; Specifically I want this for TProcess.Parameters. since it's a bit of a pain to type Proc.Parameters.Add('-Arg1'); Proc.Parameters.Add('Arg1_Value'); Proc.Parameters.Add('-Arg2'); Proc.Parameters.Add('Arg2_Value'); Proc.Parameters.Add('-Arg3'); Proc.Parameters.Add('Arg3_Value'); even "with Proc.Parameters do begin Add(n); ... end; " is a bit ugly when parameters have arguments. when I could type: Proc.Parameters.Add(['-Arg1', 'Arg1_Value']); Proc.Parameters.Add(['-Arg2', 'Arg2_Value']); Proc.Parameters.Add(['-Arg3', 'Arg3_Value']); or even Proc.Parameters.Add(['-Arg1','Arg1_Value','-Arg2','Arg2_Value','-Arg3', 'Arg3_Value']); A less generic alternative would be adding a procedure to TProcess AddArgs(args: array of string); Is this a good idea? If yes then I can create a patch.

Independently of whether a patch will be accepted or not, you can do the following beginning with 2.6.0:

=== source begin ===

type
  TStringsHelper = class helper for TStrings
   procedure AddStrings(const aStr: array of String); overload; // this is
needed if we don't want to hide the original AddStrings method
  end;
procedure TStringsHelper.AddStrings(const aStr: array of String);
var
  s: String;
begin
  for s in aStr do
    Add(s);
end;
// anywhere you want to use AddStrings with a TStrings instance or one of
its descendants you need to put the unit of TStringsHelper into the
uses-clause and then do
YourTStringsVar.AddStrings(['Hello', 'World']);

=== source end ===

You can do something similar for TProcess.

Regards,
Sven

Reply by email

Michael Van Canneyt
to Andrew Haines: I was thinking about TStrings possibly having an overloaded function Add or AddStrings…
Andrew Haines wrote 7 lines
I was thinking about TStrings possibly having an overloaded function Add or AddStrings where the argument is an array of string? procedure Add(strs: array of string); overload; or procedure AddStrings(strs: array of string); overload;

Better implement the second.
AddStrings already exists for a TStrings, it is more similar in functionality.

Michael.

Reply by email

Marco van de Voort
to Michael Van Canneyt

In our previous episode, Michael Van Canneyt said:

Show quoted text 10 lines
> I was thinking about TStrings possibly having an overloaded function Add > or AddStrings where the argument is an array of string? > > procedure Add(strs: array of string); overload; > or > procedure AddStrings(strs: array of string); overload; > Better implement the second. AddStrings already exists for a TStrings, it is more similar in functionality.

Maybe better extending tprocess to have a array of const function than tstrings?

Reply by email

Andrew Haines
to Marco van de Voort
Marco van de Voort wrote 17 lines
In our previous episode, Michael Van Canneyt said: >> I was thinking about TStrings possibly having an overloaded function Add >> or AddStrings where the argument is an array of string? >> >> procedure Add(strs: array of string); overload; >> or >> procedure AddStrings(strs: array of string); overload; >> > > Better implement the second. > AddStrings already exists for a TStrings, it is more similar in functionality. Maybe better extending tprocess to have a array of const function than tstrings? _______________________________________________ fpc-devel maillist - http://lists.freepascal.org/mailman/listinfo/fpc-devel

Here are two patches that do both. One extends TStrings with AddStrings and the other extends TProcess with AddParams.

I'll let someone else decide which is better. :)

Regards,

Andrew

Reply by email

Unknown author
to Andrew Haines
Andrew Haines wrote 23 lines
On 04/01/12 05:13, Marco van de Voort wrote: > In our previous episode, Michael Van Canneyt said: >>> I was thinking about TStrings possibly having an overloaded function Add >>> or AddStrings where the argument is an array of string? >>> >>> procedure Add(strs: array of string); overload; >>> or >>> procedure AddStrings(strs: array of string); overload; >>> >> >> Better implement the second. >> AddStrings already exists for a TStrings, it is more similar in functionality. > > Maybe better extending tprocess to have a array of const function than > tstrings? > _______________________________________________ > fpc-devel maillist - > http://lists.freepascal.org/mailman/listinfo/fpc-devel Here are two patches that do both. One extends TStrings with AddStrings and the other extends TProcess with AddParams. I'll let someone else decide which is better. :)

I added the overloaded version of AddStrings.

It comes in handy for other cases as well. Rev 20691.

Michael.

Reply by email