https://bugs.freedesktop.org/show_bug.cgi?id=86386

[email protected] changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Priority|medium                      |lowest

--- Comment #3 from [email protected] ---
Thanks for the link. 

I don't want to change the signature of Mid. I did this in the Middle function
only because I don't know whether and how to return an l-value using BASIC. 

I assume there is no code which makes use of the undefined behaviour, if you
use negative length. However I admit that my proposal might break existing code
which - through error handling - relies on the fact that Mid throws an error
when given a negative start position.

Here's my third try

Sub MiddleDemo
'The Mid-function is less then optimal
'Problems are
'- a negative length is treated like no length given 
'- a start position of 0 results in error 5 
'- a negative start positions returns !br0ken!! 
Dim sText As String
sText = "abcdefghijkl"
Print "The Mid-function is " & Mid(sText, -1)
Print Middle(sText, 0, 3) 'the first two characters 
Print Middle(sText, -1, -4) 'the last four characters
Call Middle(sText, 6, -3, "45678") 'replace three charactes counted to the left
from the sixth position
Print sText
End Sub

Function Middle(sText As String, Optional lStart As Long, Optional lLen As
Long, Optional sInsert As String) As String
'like Mid, but takes negative lLen which runs from left to right
'also negative lStart which are counted to the left, from the 
'last position in the String i.e. the last Position is -1 
'On Error GoTo Err_Middle
If IsMissing(lStart) Then lStart = 1 
Dim lLenText As Long 
lLenText = Len(sText) 
If IsMissing(lLen) Then 
  Select Case lStart 
  Case > 0 
    lLen = lLenText 'get whole text up to the end of the string
  Case 0 
    lLen = lLenText + 1
  Case Else
    'I don't know if this is a godd idea, or if it still should be lLen =
lLenText
    lLen = -lLenText 'get whole text up to the start of the string
  End Select
End If
'we intend to use the orginal Mid-function
Dim lMidStart As Long 'so we calculate the start of the string to return in a
coordinate system, which Mid understands  
If lStart < 0 Then 
  lMidStart = lLenText + 1 + lStart 'convert negative position to corresponding
positive position 
Else
  lMidStart = lStart
End If
Dim lMidLen As Long 'length of the string to return 
If lLen < 0 Then 'the string to return is left to lMidStart 
  lMidStart = lMidStart + 1 + lLen 'start of the string 
  lMidLen = -lLen
Else 'the string to return is right to lMidStart 
  lMidLen = lLen
End If 
If lMidStart < 1 Then 'start still negative, 
  lMidLen = lMidLen + lMidStart - 1 'length must be shortened, so that we can
use the original Mid-function
  lMidStart = 1 'the string to return starts from the first position
End If  
'if lMidLen isn't positiv, return an empty string  
If lMidLen < 1 Then 'we can't give a negative length to the original Mid,
because it would return the whole string sText 
  Middle = ""
Else 'ready to use the original Mid 
  Middle = Mid(sText, lMidStart, lMidLen)
End If
If IsMissing(sInsert) Then Exit Function 'don't change the string sText, so we
are done 
sText = Left(sText, lMidStart - 1) & Left(sInsert, lMidLen) & Mid(sText,
lMidStart + lMidLen)
Exit_Middle:
Exit Function

Err_Middle:
Print StdErrMsg & CStr(Err) & " in Line " & CStr(Erl) & Chr$(13) & Chr$(10) &
Error$ & " StringMod.Middle"
Resume Next
End Function

-- 
You are receiving this mail because:
You are the assignee for the bug.
_______________________________________________
Libreoffice-bugs mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/libreoffice-bugs

Reply via email to