File: n_stringclass.sru
Size: 8497
Date: Mon, 31 Dec 2018 21:14:39 +0100
$PBExportHeader$n_stringclass.sru
$PBExportComments$StringClass object
forward
global type n_stringclass from nonvisualobject
end type
end forward

global type n_stringclass from nonvisualobject autoinstantiate
end type

type prototypes

end prototypes

type variables
Private:

Blob iblob_string
Long il_position
Long il_currentsize

end variables

forward prototypes
public function string value ()
public subroutine alloc (long al_size)
public subroutine free ()
public function long size ()
public function boolean concat (readonly string as_string)
public function boolean copy (readonly string as_string)
public function long split (string as_text, string as_sep, ref string as_array[])
public function string replaceall (readonly string as_oldstring, string as_findstr, string as_replace, boolean ab_case)
end prototypes

public function string value ();// -----------------------------------------------------------------------------
// SCRIPT:     Value
//
// PURPOSE:    This function returns the value from the blob instance variable.
//
// RETURN:     Current string value
//
// DATE        CHANGED BY  DESCRIPTION OF CHANGE / REASON
// ----------  ----------  -----------------------------------------------------
// 10/19/2016  RolandS     Initial creation
// -----------------------------------------------------------------------------

Return String(iblob_string)

end function

public subroutine alloc (long al_size);// -----------------------------------------------------------------------------
// SCRIPT:     Alloc
//
// PURPOSE:    This function allocates the blob instance variable. It needs
//             to be run once per instantiation.
//
// ARGUMENTS:  al_size  - The size of the allocated blob (in characters)
//
// DATE        CHANGED BY  DESCRIPTION OF CHANGE / REASON
// ----------  ----------  -----------------------------------------------------
// 10/19/2016  RolandS     Initial creation
// -----------------------------------------------------------------------------

Long ll_newsize

// multiply by 2 because Unicode
ll_newsize = al_size * 2

// allocate the instance blob if newsize is larger than current
If al_size > il_currentsize Then
   iblob_string = Blob(Space(ll_newsize), EncodingAnsi!)
   il_currentsize = ll_newsize
End If

il_position = 1

end subroutine

public subroutine free ();// -----------------------------------------------------------------------------
// SCRIPT:     Free
//
// PURPOSE:    This function initializes the blob instance variable.
//
// DATE        CHANGED BY  DESCRIPTION OF CHANGE / REASON
// ----------  ----------  -----------------------------------------------------
// 10/19/2016  RolandS     Initial creation
// -----------------------------------------------------------------------------

SetNull(iblob_string)
il_position = 0

end subroutine

public function long size ();// -----------------------------------------------------------------------------
// SCRIPT:     Size
//
// PURPOSE:    This function returns the size of the blob instance variable.
//
// RETURN:     Current blob value
//
// DATE        CHANGED BY  DESCRIPTION OF CHANGE / REASON
// ----------  ----------  -----------------------------------------------------
// 10/19/2016  RolandS     Initial creation
// -----------------------------------------------------------------------------

Return il_currentsize

end function

public function boolean concat (readonly string as_string);// -----------------------------------------------------------------------------
// SCRIPT:     Concat
//
// PURPOSE:    This function concatenates the passed string to the instance
//             blob variable.
//
// ARGUMENTS:  as_string   - The string to concatenate
//
// RETURN:     True=Success, False=Failed - not enough space
//
// DATE        CHANGED BY  DESCRIPTION OF CHANGE / REASON
// ----------  ----------  -----------------------------------------------------
// 10/19/2016  RolandS     Initial creation
// -----------------------------------------------------------------------------

ULong lul_rtn
Long ll_strlen

// default to 4k
If il_currentsize = 0 Then
   Alloc(4096)
End If

// string length is times 2 because Unicode
ll_strlen = Len(as_string) * 2

// append more space to end equal to current size
If (il_position + ll_strlen) > il_currentsize Then
   iblob_string = iblob_string + Blob(Space(il_currentsize), EncodingAnsi!)
   il_currentsize = il_currentsize * 2
End If

// append the passed string and advance the position
lul_rtn = BlobEdit(iblob_string, il_position, as_string)
If IsNull(lul_rtn) Then
   Return False
End If
il_position = il_position + ll_strlen

Return True

end function

public function boolean copy (readonly string as_string);// -----------------------------------------------------------------------------
// SCRIPT:     Copy
//
// PURPOSE:    This function moves the position to 1 and calls Concat so that
//             passed string is placed at the beginning.
//
// ARGUMENTS:  as_string   - The string to concatenate
//
// RETURN:     True=Success, False=Failed - not enough space
//
// DATE        CHANGED BY  DESCRIPTION OF CHANGE / REASON
// ----------  ----------  -----------------------------------------------------
// 10/19/2016  RolandS     Initial creation
// -----------------------------------------------------------------------------

il_position = 1

Return Concat(as_string)

end function

public function long split (string as_text, string as_sep, ref string as_array[]);// -----------------------------------------------------------------------------
// SCRIPT:     Split
//
// PURPOSE:    This function splits a string into an array.
//
// ARGUMENTS:  as_text  - The text to be split
//             as_sep   - The separator characters
//             as_array - By ref output array
//
// RETURN:     The number of items in the array
//
// DATE        CHANGED BY  DESCRIPTION OF CHANGE / REASON
// ----------  ----------  -----------------------------------------------------
// 10/19/2016  RolandS     Initial creation
// -----------------------------------------------------------------------------

String ls_empty[], ls_work
Long ll_pos

as_array = ls_empty

If IsNull(as_text) Or as_text = "" Then Return 0

ll_pos = Pos(as_text, as_sep)
DO WHILE ll_pos > 0
   ls_work = Trim(Left(as_text, ll_pos - 1))
   as_text = Trim(Mid(as_text, ll_pos + Len(as_sep)))
   as_array[UpperBound(as_array) + 1] = ls_work
   ll_pos = Pos(as_text, as_sep)
LOOP
If Len(as_text) > 0 Then
   as_array[UpperBound(as_array) + 1] = as_text
End If

Return UpperBound(as_array)

end function

public function string replaceall (readonly string as_oldstring, string as_findstr, string as_replace, boolean ab_case);// -----------------------------------------------------------------------------
// SCRIPT:     ReplaceAll
//
// PURPOSE:    This function replaces all occurrences of a string.
//
// ARGUMENTS:  as_oldstring   - The string to be modified
//             as_findstr     - The substring to be replaced
//             as_replace     - The new string to replace with
//             as_case        - True = case sensitive, False = case insensitive
//
// RETURN:     Updated string
//
// DATE        CHANGED BY  DESCRIPTION OF CHANGE / REASON
// ----------  ----------  -----------------------------------------------------
// 09/25/2018  RolandS     Initial creation
// -----------------------------------------------------------------------------

String ls_oldstring, ls_findstr
Long ll_pos, ll_start, ll_findstr

If ab_case Then
   ls_oldstring = as_oldstring
   ls_findstr   = as_findstr
Else
   ls_oldstring = Lower(as_oldstring)
   ls_findstr   = Lower(as_findstr)
End If

ll_findstr = Len(as_findstr)
ll_start = 1

// allocate space in the blob
Alloc(Len(ls_oldstring))

// find first occurrence
ll_pos = Pos(ls_oldstring, ls_findstr)

do while ll_pos > 0
   // concatenate the part before the hit
   Concat(Mid(as_oldstring, ll_start, ll_pos - ll_start))
   // concatenate the replacement string
   Concat(as_replace)
   // find next occurrence
   ll_start = ll_pos + ll_findstr
   ll_pos = Pos(ls_oldstring, ls_findstr, ll_start)
loop

// concatenate the part after the last hit
Concat(Mid(as_oldstring, ll_start))

Return Value()

end function

on n_stringclass.create
call super::create
TriggerEvent( this, "constructor" )
end on

on n_stringclass.destroy
TriggerEvent( this, "destructor" )
call super::destroy
end on