Trim with mutliple characters

Share your advanced PureBasic knowledge/code with the community.
User avatar
Michael Vogel
Addict
Addict
Posts: 2797
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

Trim with mutliple characters

Post by Michael Vogel »

Code: Select all

Procedure.s StrTrim(String.s,Character.s=" ")

	Protected a,b
	
	a=1
	b=Len(String)

	While FindString(Character,Mid(String,b,1))
		b-1
		If b=0
			ProcedureReturn ""
		EndIf
	Wend
	
	While FindString(Character,Mid(String,a,1))
		a+1
	Wend
	
	ProcedureReturn Mid(String,a,b-a+1)

EndProcedure
Allows StrTrim(s.s,#CRLF$+#Tab$+" ")
Little John
Addict
Addict
Posts: 4779
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Trim with mutliple characters

Post by Little John »

From my private string library:

Code: Select all

#WHITESPACE$ = " " + #TAB$ + #CRLF$

Procedure.s LTrimChars (source$, charlist$=#WHITESPACE$)
   ; removes from source$ all leading characters which are contained in charlist$
   Protected p, last=Len(source$)
   
   p = 1
   While p <= last And FindString(charlist$, Mid(source$,p,1)) <> 0
      p + 1
   Wend
   
   ProcedureReturn Mid(source$, p)
EndProcedure

Procedure.s RTrimChars (source$, charlist$=#WHITESPACE$)
   ; removes from source$ all trailing characters which are contained in charlist$
   Protected p
   
   p = Len(source$)
   While p >= 1 And FindString(charlist$, Mid(source$,p,1)) <> 0
      p - 1
   Wend
   
   ProcedureReturn Left(source$, p)
EndProcedure

Macro TrimChars (_source_, _charlist_=#WHITESPACE$)
   LtrimChars(RtrimChars(_source_, _charlist_), _charlist_)
EndMacro
User avatar
skywalk
Addict
Addict
Posts: 4211
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Trim with mutliple characters

Post by skywalk »

Are you sure this is an intended trick?

Code: Select all

  Debug "~"+strTrim("11   2345 67891111 11 11 ", "11 ")+"~" ; ~2345 6789~
  Debug "~"+strTrim("11   2345 67891111 11 11 ", "1 ")+"~"   ; ~2345 6789~
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Re: Trim with mutliple characters

Post by PB »

> From my private string library:
> #WHITESPACE$ = " " + #TAB$ + #CRLF$

Don't forget this nasty little bastard: Chr(160)
It's given me so much grief in the past! :twisted:
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
Little John
Addict
Addict
Posts: 4779
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Trim with mutliple characters

Post by Little John »

PB wrote:> From my private string library:
> #WHITESPACE$ = " " + #TAB$ + #CRLF$

Don't forget this nasty little bastard: Chr(160)
There is no universal, unambiguous definition of "whitespace".
It depends on the context which characters are considered "whitespace".
I did not forget Chr(160) -- that's deliberately not considered whitespace in my private string library (in conformance e.g. with RFC 2045 and RFC 2047).
User avatar
Demivec
Addict
Addict
Posts: 4260
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Trim with mutliple characters

Post by Demivec »

Here's my version of a trim with multiple characters:

Code: Select all

Procedure.s MTrim(string.s, character.s = " ")
  Protected firstChar, lastChar, numChars, *ptrString.character
  
  lastChar = Len(string)
  If lastChar > 0
    
    *ptrString = @string + lastChar - 1
    
    While FindString(character, Chr(*ptrString\c))
      lastChar - 1
      If lastChar = 0
        ProcedureReturn ""
      EndIf
      *ptrString - SizeOf(character)
    Wend
    
    numChars = lastChar
    *ptrString = @string
    For firstChar = 1 To lastChar
      If Not FindString(character, Chr(*ptrString\c))
        Break ;always exits loop here
      EndIf
      numChars - 1
      *ptrString + SizeOf(character)
    Next
    
    ProcedureReturn Mid(string, firstChar, numChars)
  EndIf
  
EndProcedure
From my own tests it is faster than the other PureBasic examples when tested with these examples:

Code: Select all

);before, middle, after ;basic test
MTrim("<>abcd<>e><", "><")

;empty search string
MTrim("", "><") 

;long search string, empty character string
MTrim("abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz", "") 
; long search string, minimal character string
MTrim("abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz", "<")

;minimal search and character string
MTrim("<", ">")
;long character string
MTrim("<", "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz")
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Re: Trim with mutliple characters

Post by PB »

> There is no universal, unambiguous definition of "whitespace".
> It depends on the context which characters are considered "whitespace".
> I did not forget Chr(160) -- that's deliberately not considered whitespace
> in my private string library

Easy man; I was just trying to help, since your other chars in your
private string library are all non-visible characters, as Chr(160) is.
Chr(160) is really no different to Chr(32), which you included, so
I thought it a valid suggestion for you. Can't win them all.
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
Little John
Addict
Addict
Posts: 4779
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Trim with mutliple characters

Post by Little John »

PB wrote:> There is no universal, unambiguous definition of "whitespace".
> It depends on the context which characters are considered "whitespace".
> I did not forget Chr(160) -- that's deliberately not considered whitespace
> in my private string library

Easy man; I was just trying to help
Easy, man. I was just trying to help, too, by providing some additional information.
User avatar
Shield
Addict
Addict
Posts: 1021
Joined: Fri Jan 21, 2011 8:25 am
Location: 'stralia!
Contact:

Re: Trim with mutliple characters

Post by Shield »

There are technically a lot more. For example, this is what is considered whitespace under .NET:

Code: Select all

#DOT_NET_WHITESPACE = Chr($9) + Chr($A) + Chr($B) + Chr($C) + Chr($D) + Chr($20) + Chr($85) + Chr($A0) +
                      Chr($1680) + Chr($180E) + Chr($2000) + Chr($2001) + Chr($2002) + Chr($2003) + Chr($2004) +
                      Chr($2005) + Chr($2006) + Chr($2007) + Chr($2008) + Chr($2009) + Chr($200A) + Chr($2028) +
                      Chr($2029) + Chr($202F) + Chr($205F) + Chr($3000)
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
Post Reply