RTrimWS

Everything else that doesn't fall into one of the other PB categories.
AZJIO
Addict
Addict
Posts: 1316
Joined: Sun May 14, 2017 1:48 am

RTrimWS

Post by AZJIO »

I often need the function to remove spaces (whitespace) at the end of a line, which is in AutoIt3 (StringStripWS).

Code: Select all

EnableExplicit

Procedure.s RTrimWS(String$)
	Protected whitespace$, Len1, Len2, Blen, Find, i, j
	Protected *mem, *memWS, *c.Character, *jc.Character

	whitespace$ = #CRLF$ + #TAB$ + #FF$ + #VT$ + " "
	Len1 = Len(whitespace$) ; set explicitly 6
	Len2 = Len(String$)
	Blen = StringByteLength(String$)

	If Not Asc(String$)
		ProcedureReturn ""
	EndIf

	*mem = @String$
	*memWS = @whitespace$
	*c.Character = *mem + Blen - SizeOf(Character)

	For i = Len2 To 1 Step -1
		Find = 1
		*jc.Character = *memWS

		For j = 1 To Len1 ; set explicitly 6
			If *c\c = *jc\c
				Len2 - 1
				Find = 0
				Break
			EndIf
			*jc + SizeOf(Character)
		Next

		If Find
			*c + SizeOf(Character)
			*c\c = 0
			Break
		EndIf
		*c - SizeOf(Character)
	Next
; 	String$ = Left(String$, Len2)

	ProcedureReturn String$
EndProcedure


Define String$ = "test" + #CRLF$ + #CRLF$ + #CRLF$ + #TAB$ + #FF$ + #VT$ + " "
Debug "|" + RTrimWS(String$) + "|"

You can set your own character set

Code: Select all

EnableExplicit

; https://www.purebasic.fr/english/viewtopic.php?t=79183
Procedure.s RTrimChar(String$, TrimChar$ = #CRLF$ + #TAB$ + #FF$ + #VT$ + " ")
    Protected Len2, Blen, i
    Protected *memChar, *c.Character, *jc.Character

    Len2 = Len(String$)
    Blen = StringByteLength(String$)

    If Not Asc(String$)
        ProcedureReturn ""
    EndIf

    *c.Character = @String$ + Blen - SizeOf(Character)
    *memChar = @TrimChar$

    For i = Len2 To 1 Step - 1
        *jc.Character = *memChar

        While *jc\c
            If *c\c = *jc\c
                *c\c = 0
                Break
            EndIf
            *jc + SizeOf(Character)
        Wend

        If *c\c
            Break
        EndIf
        *c - SizeOf(Character)
    Next

    ProcedureReturn String$
EndProcedure


Define String$
String$ = "test" + #CRLF$ + #CRLF$ + #CRLF$ + #TAB$ + #FF$ + #VT$ + " "
Debug "|" + RTrimChar(String$) + "|"
String$ = "C:\folder" + "\/\/\/\/"
Debug "|" + RTrimChar(String$, "\/") + "|"
String$ = #CRLF$ + #CRLF$ + #CRLF$ + #TAB$ + #FF$ + #VT$ + " "
Debug "|" + RTrimChar(String$) + "|"

Delete Left

Code: Select all

EnableExplicit

; https://www.purebasic.fr/english/viewtopic.php?t=79183
Procedure.s LTrimChar(String$, TrimChar$ = #CRLF$ + #TAB$ + #FF$ + #VT$ + " ")
	Protected *memChar, *c.Character, *jc.Character

	If Not Asc(String$)
		ProcedureReturn ""
	EndIf

	*c.Character = @String$
	*memChar = @TrimChar$

	While *c\c
		*jc.Character = *memChar

		While *jc\c
			If *c\c = *jc\c
				*c\c = 0
				Break
			EndIf
			*jc + SizeOf(Character)
		Wend

		If *c\c
			String$ = PeekS(*c)
			Break
		EndIf
		*c + SizeOf(Character)
	Wend

	ProcedureReturn String$
EndProcedure


Define String$
String$ = #CRLF$ + #CRLF$ + #CRLF$ + #TAB$ + #FF$ + #VT$ + " " + "test"
Debug "|" + LTrimChar(String$) + "|"
String$ = "\/\/\/\/" + "C:\folder"
Debug "|" + LTrimChar(String$, "\/") + "|"
String$ = #CRLF$ + #CRLF$ + #CRLF$ + #TAB$ + #FF$ + #VT$ + " "
Debug "|" + LTrimChar(String$) + "|"
Last edited by AZJIO on Mon Oct 17, 2022 9:09 am, edited 4 times in total.
Quin
Enthusiast
Enthusiast
Posts: 282
Joined: Thu Mar 31, 2022 7:03 pm
Location: United States
Contact:

Re: RTrimWS

Post by Quin »

Wow, had I known about this a week ago I would've saved myself quite some typing :D. Thanks so much for this, I'm definitely going to implement it into my programs! :)
PB v5.40/6.10, Windows 10 64-bit.
16-core AMD Ryzen 9 5950X, 128 GB DDR5.
BarryG
Addict
Addict
Posts: 3292
Joined: Thu Apr 18, 2019 8:17 am

Re: RTrimWS

Post by BarryG »

Don't forget to test for Chr(160) as well - that's whitespace.
AZJIO
Addict
Addict
Posts: 1316
Joined: Sun May 14, 2017 1:48 am

Re: RTrimWS

Post by AZJIO »

BarryG wrote: Thu May 19, 2022 10:17 pm Don't forget to test for Chr(160) as well - that's whitespace.
This space is treated as a letter. It is not used to wrap words to the next line.
In HTML, this is pseudocode " " used to create a wide margin.
BarryG
Addict
Addict
Posts: 3292
Joined: Thu Apr 18, 2019 8:17 am

Re: RTrimWS

Post by BarryG »

I thought the idea was to remove whitespace at the end of a line? So if the line ends with Chr(160), shouldn't that be removed?
User avatar
NicTheQuick
Addict
Addict
Posts: 1224
Joined: Sun Jun 22, 2003 7:43 pm
Location: Germany, Saarbrücken
Contact:

Re: RTrimWS

Post by NicTheQuick »

Here is a list of space separators: https://www.compart.com/en/unicode/category/Zs
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.
AZJIO
Addict
Addict
Posts: 1316
Joined: Sun May 14, 2017 1:48 am

Re: RTrimWS

Post by AZJIO »

BarryG
Everyone has their own idea of ​​what is a whitespace character that needs to be removed. Therefore, we leave the opportunity to independently determine this parameter. In AutoIt3, character 160 is not truncated.
BarryG
Addict
Addict
Posts: 3292
Joined: Thu Apr 18, 2019 8:17 am

Re: RTrimWS

Post by BarryG »

AZJIO wrote: Fri May 20, 2022 2:56 pmEveryone has their own idea of ​​what is a whitespace character that needs to be removed.
I wasn't trying to start an argument; I was just pointing out that it wasn't considered when perhaps it should've been. To me, since it isn't visible to the user, it shouldn't be included when trimming non-visible characters from the start or end of a string.
Post Reply