• Welcome to Jose's Read Only Forum 2023.
 

Trim function will GPF if given string is a null string

Started by Chris Chancellor, November 16, 2018, 05:41:44 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Chris Chancellor

Hello Charles

previosly you suggested that the PB Trim function can be replaced
by a macro as below


' Trim function
def Trim ltrim(rtrim(%1))



but i tested this code with a null string, the program just GPF

' TestGPF.o2bas

$ filename "TestGPF.exe"
uses rtl64

' Trim function
def Trim ltrim(rtrim(%1))


' This will GPF
string  tty = ""
string  tgy
tgy =  Trim(tty)    ' this will GPF

print tgy


it means that its string argument cannot be a blank string ""


and if i use the following code it will not GPF

' TestGPF.o2bas

$ filename "TestGPF.exe"
uses rtl64

' Trim function
def Trim ltrim(rtrim(%1))



' This will not GPF
string  tty = " you are most welcome           "
string  tgy
tgy =  Trim(tty)   
print tgy


so we need to protect this macro from GPF by blocking any blank argument
from accessing Trim()

how to do this ?  how to write this macro ?


Charles Pegge

Hi Chris,

It works okay on the new system.

But try this trim function. It is quite efficient, and you can derive other useful string functions from it.


$filename "c.exe"
'uses rtl64

function trim(string s) as string
=================================
int i,j
int ls=len s
if ls=0 then return ""
byte bb at strptr(s)
indexbase 1
for i=1 to ls
   if bb[i]>32 then exit for
next
if i>ls then return ""
for j=ls to 1 step -1
   if bb[j]>32 then exit for
next
return mid(s,i,j-i+1)
end function

string s,t
t=trim(s)
print ">" t "<"
s=""
t=trim(s)
print ">" t "<"
s=" "
t=trim(s)
print ">" t "<"
s="  ABCDEF   "
t=trim(s)
print ">" t "<"

Chris Chancellor

Thanxx a lot Charles

i did a lot of tests and the new Trim function will be very useful
you are a meastro

Brian Alvarez

These are the ones i made, support for ANY switch and trimming words:

// returns a trimed string
FUNCTION LTRIM(string src, long a = 0, string ch = " ") as string

    byte srcchar at strptr(src)
    byte trichar at strptr(ch)
    long p1 = 1
    long index   
    long cha   
       
    if a then
        for index = 1 to len(src)       
            for cha = 1 to len(ch)       
                if srcchar[index] = trichar[cha] then
                    goto checknextchar                     
                end if
            next
            p1 = index
            exit for
            checknextchar:
        next
        return mid(src, p1)
    else       
        for index = 1 to len(src)
            for cha = 1 to len(ch)       
                if srcchar[index+cha-1] <> trichar[cha] then
                    goto nomorematches
                end if               
            next
            p1 += len(ch)             
        next
        nomorematches:       
        return mid(src, p1)
    end if
   
END FUNCTION


// returns a trimed string   
FUNCTION RTRIM(string src, long a = 0, string ch = " ") as string

    byte srcchar at strptr(src)
    byte trichar at strptr(ch)
    long p1 = len(src)
    long index   
    long cha   
       
    if a then
        for index = len(src) TO 1 step -1       
            for cha = 1 to len(ch)       
                if srcchar[index] = trichar[cha] then
                    goto checknextchar                     
                end if
            next
            p1 = index
            exit for
            checknextchar:
        next
        return mid(src, 1, p1)
    else       
        for index = len(src)-len(ch) TO 1 step -1
            for cha = 1 to len(ch)       
                if srcchar[index+cha-1] <> trichar[cha] then
                    goto nomorematches
                end if               
            next
            p1 = index-1             
        next
        nomorematches:       
        return mid(src, 1, p1)
    end if
   
END FUNCTION

// returns a trimed string
FUNCTION TRIM(string inp, long a = 0, string chrs = " ") as string
    RETURN RTRIM(LTRIM(inp, a, chrs), a, chrs)     
END FUNCTION