Page 1 of 1

RTrimWS

Posted: Wed May 18, 2022 9:43 am
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$) + "|"

Re: RTrimWS

Posted: Thu May 19, 2022 3:45 pm
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! :)

Re: RTrimWS

Posted: Thu May 19, 2022 10:17 pm
by BarryG
Don't forget to test for Chr(160) as well - that's whitespace.

Re: RTrimWS

Posted: Fri May 20, 2022 8:29 am
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.

Re: RTrimWS

Posted: Fri May 20, 2022 12:08 pm
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?

Re: RTrimWS

Posted: Fri May 20, 2022 12:57 pm
by NicTheQuick
Here is a list of space separators: https://www.compart.com/en/unicode/category/Zs

Re: RTrimWS

Posted: Fri May 20, 2022 2:56 pm
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.

Re: RTrimWS

Posted: Sat May 21, 2022 1:01 am
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.