Tuesday, October 20, 2015

LotusScript: superString Class

Originally published 

Wed 19 Sep 2007

Editors Notes
Admittedly, the usefulness of Lotus Script has waned in the past half decade as the once ubiquitous product has steadily lost market share in some regions while gaining it it others, and shifting from Lotus Scripting as the core language offering towards Java and IBM's rather bizzar implementation of JavaScript Lotus Formula language.  But, this library is one of the few that I enjoyed creating, using and sharing.  Some of these functions are no longer needed thanks to updates to the Lotus Script engine with release 8.

Original Post

Update:Added GetAllSubstrings, rewrote Substring. v 0.0.9 07/31/2008
Update:Added Prepend. v 0.0.8 10/4/2007
Update:Added a Trim wrapper that operates on the buffer. v 0.0.7 10/3/2007
Update: My appologies, some comments were lost when the Blog server took a dive this past week. Sean Burgess had previously commented and a coworker provided an update to the code, which appears below. Current version is 0.0.6 as of 9/28/2007.
Here's a little something I've been working on. It's a script library that offers a few string functions that we would otherwise be able to overload onto the Script data type if it was an actual object built on a base object like in Java, rather than a primitive.
I included some of my favorite String operations from JavaScript for the most part and added a few others I find handy. The class uses a string primitive as a buffer, which has an upper limit of 2 GB. Should be plenty for just about anything you want to do with a String.
Highlights:

  • .Slice
  • .Length
  • .Append
  • .pos
  • .subString
  • .Strip
  • .Contains
  • .ToList
  • .setText
  • .Text
Classes are hard to stop playing with once you start. If nothing else, this should serve to illustrate how handy a class can be for even mundane tasks.

'superString: 

Option Public
Option Declare
Class superString 
 
 'superString:  
 
 
''' Freely Distributable with copywrite intact
'   Copywrite 2007 - Datatribe Softwerks, Ltd. - Jerome E. Carter, II
 
''' v 0.0.9 - Jerry Carter 7/31/2008 - added GetAllSubstrings, rewrote Substring to wrap it as it's easier to understand and troubleshoot
 
''' v0.0.8 - Jerry Carter 10/4/2007 - added Prepend Subroutine
' Should have been obviouse to begin with, but I didn't think of it till I needed it!
' prepends the supplied string to the buffer
 
''' v 0.0.7 - Jerry Carter 10/3/2007 - added Trim Subroutine.  
' Simply wraps the buffer in the Trim command.  Reduces complexity of 
' code needed externally to perform the operation against the class.
 
''' v 0.0.6 - correction provided by M Burgo
' Substring was incorrectly finding the last instance of the suffix rather than the first
 
''' v 0.0.5 - Jerry Carter - 9/19/2007
' added Sub Strip which operates on the resident buffer.  end result available via Text method.
 
 
 ''' superString - by Jerry Carter - 8/31/2007 - v 0.0.4
 ' Notes base data types are not derived from an object but are static final primitives
 ' therefore we can not declare a class like  Public Class superString as String and be able to extend String
 
 ''' Private Members
 Private buff As String '  strings are limited to 2GB - should be sufficient for most things
 
 ''' Constructor
 Sub new (initVal As String)
  Me.buff = initVal  
 End Sub
 
 
 ''' Public Methods '''
 '---------------------'
 
 '''  Append  '''
 ' Adds the inbound string to the end of the buffer
 Public Sub Append(inputStr As String)
  Me.buff = Me.buff + inputStr
 End Sub
 
 ''' Prepend
 '  Add the inbound string to the beginning of the buffer
 Public Sub Prepend(inputStr As String)
  Me.buff = inputStr + Me.buff
 End Sub
 
 ''' ToList '''
 ' Breaks the buffer into an unordered list, removing the delimiter in the process
 Public Function ToList(delim As String) As Variant
  Dim tmpList List As String
  Dim tmpArr As Variant
  tmpArr = Split(Me.buff,delim)
  Dim i As Integer
  For i = 0 To Ubound(tmpArr)
   tmpList(Cstr(i)) = tmparr(i)
  Next
  ToList = tmpList
 End Function
 
 ''' Strip
 ' Removes the supplied string argument from the buffer.
 Public Sub Strip(stripStr As String)
  Me.setText Join(Split(Me.text,stripStr),"") 
 End Sub
 
 
 ''' Text '''
 ' returns the buffer as a string
 Public Function Text() As String
  Text = Me.buff
 End Function
 
 ''' Length '''
 ' returns the total number of characters as a Long
 Public Function Length() As Long
  Length = Len(Me.buff)
 End Function
 
 ''' GetAllSubstrings
 ' returns all instances of the substring found between the supplied prefix and suffix
 ' e.g.  My <%tagged%> markup should produce <%bonus%> material
 ' returns a list containing "tagged" and "bonus" if <% is the prefix and %> is the suffix
 Public Function GetAllSubstrings(prefix As String, suffix As String) As Variant
  On Error Goto eh
  Dim blist As Variant
  blist = Me.ToList(prefix)
  Dim clist List As String
  If Islist(blist) Then
   Forall chunk In blist
    If Instr(chunk,suffix) > 0 Then
     clist(Listtag(chunk))= Left(chunk,Clng(Instr(chunk,suffix)-1))
    End If
   End Forall
  Else
   clist("error") = "A substring list could not be formed with the supplied prefix and suffix"
  End If 
  GetAllSubstrings = clist  
  Exit Function
eh:
  Msgbox "Error in GetAllSubstrings: " + Error + " at " + Cstr(Erl)
  Exit Function
 End Function
 
 ''' SubString '''
 ' returns the string appearing between the prefix and the suffix
 Public Function SubString(prefix As String, suffix As String) As String
 
  ' Updated 7/31/2008 to take advantage of new function GetAllSubstrings
  Dim blist As Variant
  blist = GetAllSubstrings(prefix,suffix)
  Forall n In blist
   Substring = n
   Exit Forall
  End Forall
  
 End Function
 
 ''' Slice '''
 ' works like java string.slice(startpos,endpos)
 Public Function Slice(dstart As Long, dend As Long) As String 
  Slice = Mid$(Me.buff, dstart, dend-dstart)
 End Function
 
 ''' SetText '''
 ' replaces the buffer with the inbound string
 Public Sub SetText(newval As String)
  Me.buff = newval
 End Sub
 
 ''' Contains '''
 ' Simple test to see if the parameter is anywhere in the buffer
 Public Function Contains(strIN As String) As Boolean
  If Instr(Me.buff,strIN) > 0 Then
   Contains= True
  Else
   Contains = False
  End If
 End Function
 
 ''' Pos[ition] '''
 ' returns the position of a substring is a Long
 Public Function Pos(strIn As String) As Long
  Pos = Instr(Me.buff,strIn)
 End Function
 
 '''Trim'''
 ' performs LS trim on the buffer
 Public Sub Trim()
  Me.buff = Trim(Me.buff)
 End Sub
 
End Class



No comments:

Post a Comment