Trim() enhancement

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
paulr
User
User
Posts: 70
Joined: Sat Sep 27, 2003 2:53 pm

Trim() enhancement

Post by paulr »

How about allowing strings greater than 1 in length in the 'character to trim' argument, then trimming any of the characters from the target?

So that: Trim("<hello>", "<>")
Would return "hello"?

This would be more powerful and keep the original functionality perfectly intact.
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Trim() enhancement

Post by IdeasVacuum »

Well, PB has the Regular Expression lib for that. http://www.purebasic.com/documentation/ ... index.html
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
Little John
Addict
Addict
Posts: 4779
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Trim() enhancement

Post by Little John »

paulr wrote:How about allowing strings greater than 1 in length in the 'character to trim' argument, then trimming any of the characters from the target?
Yes, that would be very useful. E.g. PowerBasic has this feature for ages.

( Until this will be implemented in PureBasic, you can use self-written functions for this. )
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Re: Trim() enhancement

Post by PB »

> This would be more powerful

But what would it do to this?

Code: Select all

a$=Trim("<>hello<>","<>")
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
User avatar
DK_PETER
Addict
Addict
Posts: 904
Joined: Sat Feb 19, 2011 10:06 am
Location: Denmark
Contact:

Re: Trim() enhancement

Post by DK_PETER »

PB wrote:> This would be more powerful

But what would it do to this?

Code: Select all

a$=Trim("<>hello<>","<>")
@PB

You could do this:
Macro myTrim(st, remst)
Trim(ReplaceString(st, remst,""),"")
EndMacro

a$ = "<>hello<>"
Debug myTrim(a$, "<>")
Current configurations:
Ubuntu 20.04/64 bit - Window 10 64 bit
Intel 6800K, GeForce Gtx 1060, 32 gb ram.
Amd Ryzen 9 5950X, GeForce 3070, 128 gb ram.
User avatar
Shield
Addict
Addict
Posts: 1021
Joined: Fri Jan 21, 2011 8:25 am
Location: 'stralia!
Contact:

Re: Trim() enhancement

Post by Shield »

What if the string is "<>he<>llo<>"? :wink:
Image
Blog: Why Does It Suck? (http://whydoesitsuck.com/)
"You can disagree with me as much as you want, but during this talk, by definition, anybody who disagrees is stupid and ugly."
- Linus Torvalds
Little John
Addict
Addict
Posts: 4779
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Trim() enhancement

Post by Little John »

PB wrote:> This would be more powerful

But what would it do to this?

Code: Select all

a$=Trim("<>hello<>","<>")
See the link in the post directly above your post ...
Little John
Addict
Addict
Posts: 4779
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Trim() enhancement

Post by Little John »

DK_PETER wrote:@PB

You could do this:
Macro myTrim(st, remst)
Trim(ReplaceString(st, remst,""),"")
EndMacro

a$ = "<>hello<>"
Debug myTrim(a$, "<>")
No. As Shield already indicated, that's not a Trim() function.

General remark: I can't see the sense in ignoring existing solutions, that have been posted previously in a thread.
User avatar
DK_PETER
Addict
Addict
Posts: 904
Joined: Sat Feb 19, 2011 10:06 am
Location: Denmark
Contact:

Re: Trim() enhancement

Post by DK_PETER »

Little John wrote:
DK_PETER wrote:@PB

You could do this:
Macro myTrim(st, remst)
Trim(ReplaceString(st, remst,"")," ")
EndMacro

a$ = "<>hello<>"
Debug myTrim(a$, "<>")
No. As Shield already indicated, that's not a Trim() function.

General remark: I can't see the sense in ignoring existing solutions, that have been posted previously in a thread.
Easy Little John. ;-D
I completely agree, but in the spirit of a New Year - I provided a non-serious option.
It does however provide the desired result for Shield's "<>he<>llo<>".

Happy New Year!
Current configurations:
Ubuntu 20.04/64 bit - Window 10 64 bit
Intel 6800K, GeForce Gtx 1060, 32 gb ram.
Amd Ryzen 9 5950X, GeForce 3070, 128 gb ram.
paulr
User
User
Posts: 70
Joined: Sat Sep 27, 2003 2:53 pm

Re: Trim() enhancement

Post by paulr »

Thank you for your input IdeasVacuum and Little John.
PB wrote:> This would be more powerful

But what would it do to this?

Code: Select all

a$=Trim("<>hello<>","<>")
PB, I'm proposing the function work exactly like Trim, but removing characters from the start and end of a string until it hits one not in the 'characters to remove' string. So:

Trim("<>hello<>","<>") would return "hello"
Trim("<>he<>llo<>","<>") would return "he<>llo"
Trim("<<>><<>he<>>>>llo<>>>>>>","<>") would return "he<>>>>llo"
Trim(">hello","<>") would return "hello"

Hope it's clear now.
User avatar
Danilo
Addict
Addict
Posts: 3036
Joined: Sat Apr 26, 2003 8:26 am
Location: Planet Earth

Re: Trim() enhancement

Post by Danilo »

paulr wrote:PB, I'm proposing the function work exactly like Trim, but removing characters from the start and end of a string until it hits one not in the 'characters to remove' string. So:

Trim("<>hello<>","<>") would return "hello"
Trim("<>he<>llo<>","<>") would return "he<>llo"
Trim("<<>><<>he<>>>>llo<>>>>>>","<>") would return "he<>>>>llo"
Trim(">hello","<>") would return "hello"

Hope it's clear now.
Yes, it is clear now.

Code: Select all

Procedure.s TrimAll(inputString.s, characters.s)
    Protected i, *p.Character
    Protected len = Len(inputString)
    If len > 0
        len - 1
        For i = 0 To len
            *p = @characters
            While *p\c <> 0
                inputString = Trim(inputString,Chr(*p\c))
                *p + SizeOf(Character)
            Wend
        Next
    EndIf
    ;Debug "TrimAll Iterations: "+Str(i+1)
    ProcedureReturn inputString
EndProcedure

Macro Trim(string,chars=" ") : TrimAll(string,chars) : EndMacro


Debug Trim("<>hello<>","<>")                ; would return "hello"
Debug Trim("<>he<>llo<>","<>")              ; would return "he<>llo"
Debug Trim("<<>><<>he<>>>>llo<>>>>>>","<>") ; would return "he<>>>>llo"
Debug Trim(">hello","<>")                   ; would return "hello"
Debug Trim("<hello>","<>o")                 ; would return "hell"
Debug Trim("<hello>","<o>")                 ; would return "hell"
Debug Trim("   hello   ")                   ; would return "hello"
The following version trims only until no more changes are found (better for large strings):

Code: Select all

Procedure.s TrimAll(inputString.s, characters.s)
    Protected i, *p.Character, newString.s, foundChange
    Protected len = Len(inputString)
    If len > 0
        len - 1
        For i = 0 To len
            *p = @characters
            foundChange = #False
            While *p\c <> 0
                newString = Trim(inputString,Chr(*p\c))
                If newString <> inputString
                    foundChange = #True
                EndIf
                inputString = newString
                *p + SizeOf(Character)
            Wend
            If foundChange = #False
                Break
            EndIf
        Next
    EndIf
    ;Debug "TrimAll Iterations: "+Str(i+1)
    ProcedureReturn inputString
EndProcedure

Macro Trim(string,chars=" ") : TrimAll(string,chars) : EndMacro


Debug Trim("<>hello<>","<>")                ; would return "hello"
Debug Trim("<>he<>llo<>","<>")              ; would return "he<>llo"
Debug Trim("<<>><<>he<>>>>llo<>>>>>>","<>") ; would return "he<>>>>llo"
Debug Trim(">hello","<>")                   ; would return "hello"
Debug Trim("<hello>","<>o")                 ; would return "hell"
Debug Trim("<hello>","<o>")                 ; would return "hell"
Debug Trim("   hello   ")                   ; would return "hello"
paulr
User
User
Posts: 70
Joined: Sat Sep 27, 2003 2:53 pm

Re: Trim() enhancement

Post by paulr »

Thanks, that's a very neat implementation. I'd like to see that in the next version of PB!
User avatar
Andre
PureBasic Team
PureBasic Team
Posts: 2137
Joined: Fri Apr 25, 2003 6:14 pm
Location: Germany (Saxony, Deutscheinsiedel)
Contact:

Re: Trim() enhancement

Post by Andre »

paulr wrote:Thanks, that's a very neat implementation. I'd like to see that in the next version of PB!
+1 :D
Bye,
...André
(PureBasicTeam::Docs & Support - PureArea.net | Order:: PureBasic | PureVisionXP)
Thade
Enthusiast
Enthusiast
Posts: 266
Joined: Sun Aug 03, 2003 12:06 am
Location: Austria

Re: Trim() enhancement

Post by Thade »

Code: Select all

Procedure.s TrimAll(in.s, what.s)
	While FindString(what, Left(in, 1)) Or FindString(what, Right(in, 1))
		For i=1 To Len(what)
			in=Trim(in,	Mid(what, i, 1))
		Next
	Wend
	ProcedureReturn in
EndProcedure

Debug TrimAll("<>Hello<>", "<>")
Debug TrimAll("<>Hel<>lo<>", "<>")
Debug TrimAll("<>Hello<>", "<o>")
Debug TrimAll("<>Hello<>", "o><")
Debug TrimAll("<>Hello<>", "o<>")
Debug TrimAll("<>Hello<>", "<>o")
Debug TrimAll("<><.>><<>Hel<>lo<>:<>>::<><<>>", "<:>.")
or this does the same

Code: Select all

Procedure.s TrimAll(in.s, what.s)
	While FindString(what, Left(in, 1))
		in=Trim(in, Left(in,1))
	Wend
	While FindString(what, Right(in, 1))
		in=RTrim(in,	Right(in, 1))
	Wend
	ProcedureReturn in
EndProcedure

Debug TrimAll("<>Hello<>", "<>")
Debug TrimAll("<>Hel<>lo<>", "<>")
Debug TrimAll("<>Hello<>", "<o>")
Debug TrimAll("<>Hello<>", "o><")
Debug TrimAll("<>Hello<>", "o<>")
Debug TrimAll("<>Hello<>", "<>o")
Debug TrimAll("<><.>><<>Hel<>lo<>:<>>::<><<>>", "<:>.")
--------------
Yes, its an Irish Wolfhound.
Height: 107 cm; Weight: 88 kg
Post Reply