Author Topic: Trim function will GPF if given string is a null string  (Read 181 times)

0 Members and 1 Guest are viewing this topic.

Offline Chris Chancellor

  • Sr. Member
  • ****
  • Posts: 269
Trim function will GPF if given string is a null string
« on: November 16, 2018, 05:41:44 AM »
Hello Charles

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

Code: [Select]
' Trim function
 def Trim ltrim(rtrim(%1))


but i tested this code with a null string, the program just GPF
Code: [Select]
' 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
Code: [Select]
' 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 ?


Offline Charles Pegge

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 665
    • Charles Pegge
Re: Trim function will GPF if given string is a null string
« Reply #1 on: November 16, 2018, 06:41:47 AM »
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.

Code: [Select]
$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 "<"

Offline Chris Chancellor

  • Sr. Member
  • ****
  • Posts: 269
Re: Trim function will GPF if given string is a null string
« Reply #2 on: November 16, 2018, 05:35:26 PM »
Thanxx a lot Charles

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

Offline Brian Alvarez

  • Full Member
  • ***
  • Posts: 133
    • PluriBASIC
Re: Trim function will GPF if given string is a null string
« Reply #3 on: November 16, 2018, 10:13:17 PM »
These are the ones i made, support for ANY switch and trimming words:

Code: [Select]
// 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