#177 Vec::split(j) doesn't work when j==length(vector)

release_4.0.x
closed
5
2012-09-15
2008-08-07
Yngve Selén
No

In it++3.xx the following lines are possible:

vec tst = "1,2,3,4,5";
vec tst2 = tst.split(5);

after which tst == "" and tst2 == "1,2,3,4,5".

However, it++4.0.5 and earlier versions require that the argument passed to Vec::split is < the size of the vector, via the function in_range on line 513 of vec.h, which says:

return ((i < datasize) && (i >= 0));

In this case, "i <= datasize" should be allowed, but in_range is used by other functions (such as del etc) which require "i < datasize".

The fix (which I have not tested, however) seems to be to replace line 1328 in vec.h,

it_assert_debug(in_range(pos), "Vec<>::split(): Index out of range");

by

it_assert_debug((pos <= datasize) && (pos >= 0), "Vec<>::split(): Index out of range");

Also, I believe the help text for split should be improved (vec.h line 415) to something along the lines of

//! Split the vector into two parts at element \c pos. Return the first part containing elements 0 to \c pos -1, and keep the second containing the remaining elements starting from \c pos (empty vector if \pos == the length of the vector).

As it stands currently, it is not obvious (at least not to me) if the element pos belongs to the first or the second part.

Cheers,

// Yngve

Discussion

  • Logged In: YES
    user_id=1004597
    Originator: NO

    This bug is now fixed in our SVN repository. Will be the part of the next IT++ releases.

    Thanks!
    /Adam