Hello, this is my first post - please be gentle if I do something wrong. During programming I found that the mid()-function changes its bevahior. In LO 3 mid(Text T, Start) results an error if Start=0 and an empty string if Start is negative or Start is larger then the length of the text T. Now, tested in LO 4.2 and 4.3, mid(Text T, Start) results "!!br0ken!!" if Start is negative or if Start is larger then the length of the string +1. Examples: mid("test",4) -> "t" mid("test",5) -> "" mid("test",6) -> "!!br0ken!!" mid("test",-1) -> "!!br0ken!!" mid("test",0) -> error In the "Open Document Format for Office Applications (OpenDocument) Version 1.2" I found that the "normal" behavior should be: Returns the characters from T, starting at character position Start, for up to Length characters. For the integer conversions, Start=INT(Start), and Length=INT(Length). If there are less than Length characters starting at start, it returns as many characters as it can beginning with Start. In particular, if Start > LEN(T), it returns the empty string (""). If Start < 0, it returns an Error. If Start >=0, and Length=0, it returns the empty string. Note that MID(T;1;Length) produces the same results as LEFT(T;Length). Is the "new" behavior intended or is it a bug?
CC'ing Noel for advice on BASIC behavior.
Duplicate of bug 86386?
(In reply to raal from comment #2) > Duplicate of bug 86386? Hello, I think it's similar to bug 86386. But it seems to me that there the focus is on negative length and to expand the functionality of the mid(). Bug 73771 (on-standard behaviour for Function Mid) shows the same as I intended. There is said, that it is fixed within LibO 4.1.5, but it isn't - or am I wrong?
Julien, could you look at this? You've fixed bug 73771. Thanks
Will take a look.
I took a look at http://cgit.freedesktop.org/libreoffice/core/tree/basic/source/runtime/methods.cxx and I believe the copy()-method is responsible for this behavior. Maybe a additional line with if (nStartPos<0 || nStartPos>aArgStr.getLength()) { nStartPos=aArgStr.getLength()+1 } solves the problem.
Sorry I forgot the line number: between 1211 and 1212
Michael: would you have Excel to compare the different result of mid tests? Indeed, I don't know if we must only follow ODF or if we must also take into account Excel (since VB is from Microsoft) Eike: since you had also review my patch and work on Calc thought you might be interested in this bugtracker. (just for ref: I had also put some unittests here)
It seems there are several cases to take into account: - mid with 2 args (I didn't know it existed, since I just had used "mid" in function, not in macro) - mid with 3 args - mid$ - Excel compatibility - bWrite parameter that I don't understand Badfully, I'm not enough "fluent" in Calc/VB to fix this.
Julian: I just tested the mid()-function under MS Excel (Version 2003; but the vb description shows the same): In VB mid(text, start [,count]) there are the following cases: 1) if count is <0 then error 2) if start is <1 then error 3) if count=0 or start>length(text) then "" (empty string) 4) if start+count>length(text) then as if count isn't used In my eyes the problem in Calc is the result string "!!br0ken!!". So I would prefer (additional to the current state): A1) if start>length(text) then "" (as in Calc version 3.x) A2) if start<1 then error message (instead of "only" if start=0) In the last case (start<1) we can discuss, if it would make more sense to set start=1 or set the result to "" to avoid any error message. (The case with start=0 is a little bit confusing in ODF. There should be no error, but what should be the result, because the counting of letter begins with 1 as fas as I know ...)
I just tested furthermore a little bit. mid("test",7,1) results "" (as expected) but mid("test",7) results "!!br0ken!!" instead of "" and mid("test",-7,1) results "!!br0ken!!" instead of an error message It seems that the one problem (start>length) only occurs if mid() is used with only 2 parameters mid() can be used with 2 or 3 parameters as function or with up to 4 parameters as command (and I believe it is all inside the RTLFUNC(Mid)) mid(text,start [,count]) as function or command results a substring mid(text,start [,count], newtext) as command results a string where in the string text the substring of count letters, beginning from start is substitute by newstring e. g. t="the last year" mid t,5,4,"new" msgbox (t) results a messagebox with "the new year"
I submitted this patch for review: https://gerrit.libreoffice.org/13707
Great job! I'm fascinated and i believe it is a real improvement. Tank you, Julian.
Julien Nabet committed a patch related to this issue. It has been pushed to "master": http://cgit.freedesktop.org/libreoffice/core/commit/?id=b7d02d7a5a374da3e01e0abc4022fba35daa1840 Resolves fdo#87834: strange behavior of mid()-function It will be available in 4.5.0. The patch should be included in the daily builds available at http://dev-builds.libreoffice.org/daily/ in the next 24-48 hours. More information about daily builds can be found at: http://wiki.documentfoundation.org/Testing_Daily_Builds Affected users are encouraged to test the fix and report feedback.
Julien Nabet committed a patch related to this issue. It has been pushed to "master": http://cgit.freedesktop.org/libreoffice/core/commit/?id=4e00df4f0b05b3d6c8f185e08499d40964e7182f Related fdo#87834: add 2 unit tests It will be available in 4.5.0. The patch should be included in the daily builds available at http://dev-builds.libreoffice.org/daily/ in the next 24-48 hours. More information about daily builds can be found at: http://wiki.documentfoundation.org/Testing_Daily_Builds Affected users are encouraged to test the fix and report feedback.
Julien Nabet committed a patch related to this issue. It has been pushed to "libreoffice-4-4": http://cgit.freedesktop.org/libreoffice/core/commit/?id=82a505aae7fdb532e250b6acce1d33431cedfa88&h=libreoffice-4-4 Resolves fdo#87834: strange behavior of mid()-function It will be available in 4.4.2. The patch should be included in the daily builds available at http://dev-builds.libreoffice.org/daily/ in the next 24-48 hours. More information about daily builds can be found at: http://wiki.documentfoundation.org/Testing_Daily_Builds Affected users are encouraged to test the fix and report feedback.