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

            Bug ID: 86386
           Summary: A better Mid-function
           Product: LibreOffice
           Version: 4.2.7.2 rc
          Hardware: All
                OS: All
            Status: UNCONFIRMED
          Severity: enhancement
          Priority: medium
         Component: BASIC
          Assignee: [email protected]
          Reporter: [email protected]

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!! 

Taking inspiration from the way ruby handles strings, I propose improvements as
follows. 

- Mid should take negative length which runs from left to right
- it should take negative start positions which are counted to the left, from
the last position in the String i.e. the last Position is -1, the next to last
is 2 etc.. 0 is the first position before the start of the string, as is 
-Len(ofthestring) - 1

For details please refer to the definition of the Middle-function below which
is largely backwards compatible to Mid, unless you count the throwing of errors
and "!br0ken!!" as intended behaviour. It might break programs which rely on a
negative length being treated as no length.

Sub MiddleDemo
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, "456") 'replace three characters 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
If IsMissing(lStart) Then lStart = 1 
Dim lLenText As Long 
lLenText = Len(sText) 
If IsMissing(lLen) Then 
  If lStart >= 0 Then 
    lLen = lLenText 'get whole text up to the end of the string
  Else
    lLen = -lLenText 'get whole text up to the start of the string
  End If
End If
Dim lMidStart As Long 'start of the string to return, we intend to use the
orginal Mid-function
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 on the 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 cab
use thr 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 ist 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) & sInsert & Mid(sText, lMidStart + lMidLen)
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