Re: [wx-dev] wxArray::Join and wxArray::Split

80 views
Skip to first unread message

Vadim Zeitlin

unread,
Jan 12, 2007, 6:32:39 PM1/12/07
to wx-...@lists.wxwidgets.org
On Fri, 12 Jan 2007 16:54:18 +0100 Francesco Montorsi <f18m_cp...@yahoo.it> wrote:

FM> in a patch (not yet applied) I've proposed to add the functions:
FM>
FM> + wxString Join(const wxString& sep) const;
FM> + static wxArrayString Split(const wxString &str, const wxString &sep);
FM>
FM> functions to wxArrayString (the second is a simple call to wxStringTokenize).
FM>
FM> Vadim suggested to add similar ones also to wxList and wxArray and to move this
FM> discussion to wx-dev, so I'm doing it now.

Just the explain, the reason I'd like to have this as a function instead
of a member so that it could work with std::vector (and possibly std::list,
...) later.

Also, these functions, although not rocket-scientifically complicated, are
less trivial than what your patch implements as they have to do something
about separators. E.g. when you do Join(":") you must somehow escape any
colons inside the array elements (and Split() must undo this).

Regards,
VZ


Vadim Zeitlin

unread,
Jan 19, 2007, 11:32:18 AM1/19/07
to wx-...@lists.wxwidgets.org
On Fri, 19 Jan 2007 17:23:41 +0100 Armel Asselin <asseli...@wanadoo.fr> wrote:

AA> > I don't understand why should you do this, sorry. Join() should be a
AA> > simple and safe function. And being safe is more important that being
AA> > suitable for some esoteric uses or compatible with ECMAScript.
AA> I really do not see in which way it is less safe.

Because it makes it very easy to get wrong results.

AA> BTW is there at least a version of Join without separator?

I'm afraid we completely disagree about the purpose of this function. For
me Join() should be used to create a string from which an array can later
be recreated by Split(). It doesn't and shouldn't try to do everything. And
besides, "Join without separator" is really too trivial to speak about.

Regards,
VZ


Vadim Zeitlin

unread,
Jan 19, 2007, 11:46:45 AM1/19/07
to wx-...@lists.wxwidgets.org
On Fri, 19 Jan 2007 17:42:14 +0100 Armel Asselin <asseli...@wanadoo.fr> wrote:

AA> > AA> > I don't understand why should you do this, sorry. Join() should be a
AA> > AA> > simple and safe function. And being safe is more important that
AA> > being
AA> > AA> > suitable for some esoteric uses or compatible with ECMAScript.
AA> > AA> I really do not see in which way it is less safe.
AA> >
AA> > Because it makes it very easy to get wrong results.
AA> just stating "it's less safe because it's less safe" is not what I call an
AA> argument

I gave the argument in the very beginning. If you don't escape the
separators you can save one thing and restore something different. If you
don't see why is this a problem, I'm afraid I can't explain it any better.

AA> OK don't care, people will then continue to do their Join as "text =
AA> wxT(""); for (i=0; i<length; +i) text += array[i]", they will still claim
AA> that wx is damn slow, and we here will wonder 'but why do they say that'.

The loop will be hardly slower than Join() (faster in fact). And what does
the API design have to do with performance anyhow?

Regards,
VZ


Armel Asselin

unread,
Jan 21, 2007, 4:49:02 PM1/21/07
to wx-...@lists.wxwidgets.org
> Hi,
> little ping about this message (it could get lost in the others msg of
> this thread maybe)...
>
> Francesco Montorsi ha scritto:
>> Armel Asselin ha scritto:

>>>> AA> > I don't understand why should you do this, sorry. Join() should
>>>> be a
>>>> AA> > simple and safe function. And being safe is more important that
>>>> being
>>>> AA> > suitable for some esoteric uses or compatible with ECMAScript.
>>>> AA> I really do not see in which way it is less safe.
>>>>
>>>> Because it makes it very easy to get wrong results.
>>> just stating "it's less safe because it's less safe" is not what I call
>>> an argument. letting choice is just giving consciousness to the
>>> programmer, not making things less safe.
>> I agree with Armel: give the choice to the programmer.
>> If we keep the '\0'-disable-escaping feature _but_ we make
>> the default escape character be '\\' then only the programmers which _do_
>> know what are doing will use '\0' as escape char to disable escaping.
>>
>> If this is well documented and the programmer still uses '\0' and then
>> complains that Splitting() the string back does not return the same
>> thing, then that's not bad API design but rather a problem with that
>> programmer :)
>>
>> As I mentioned in the patch tracker:
>>
>> https://sourceforge.net/tracker/?func=detail&atid=309863&aid=1638950&group_id=9863
>> (I'm unsure if I should reply to this issue here or there), I think that
>> in some situations it can be very useful to not escape the separators in
>> wxJoin() since you're not interested to use wxSplit() with the result
>> later.
>>
>> A clear example is the wxLogBuffer::GetString() function: the resulted
>> string needs to be typically shown to the user in some way (through a log
>> file, a message box or whatever: log messages are for humans anyway) and
>> the user does not want to see escaped newlines!
>> It doesn't matter if a newline was separing two different log messages or
>> was part of a single newline...
>>
>> Francesco
your example is indeed a situation where I use a Join like of my own,
without escaping.
IMHO this is a good example.

Armel

Reply all
Reply to author
Forward
0 new messages