Freitag, 30. November 2007

BATCH_ Trim String

My first solution I came up with involved substring replacement including a helper string added before and after the real string. This works as long as that helper string does not appear anywhere in the original string.
:: Trims whitespace at beginning and end of a string.
:: PARAM
:: 1 string to trim
:: RETURN String trimmed string
:TRIM
SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
SET HLPSTR=#####%RANDOM%#####
SET STR="!HLPSTR!%~1!HLPSTR!"& SET STR=!STR:~1,-1!
:TRIM_INNER_LOOP
IF "!STR:%HLPSTR% =%HLPSTR%!" NEQ "!STR!" (
    SET STR="!STR:%HLPSTR% =%HLPSTR%!"& SET STR=!STR:~1,-1!
    GOTO :TRIM_INNER_LOOP
) ELSE IF "!STR: %HLPSTR%=%HLPSTR%!" NEQ "!STR!" (
    SET STR="!STR: %HLPSTR%=%HLPSTR%!"& SET STR=!STR:~1,-1!
    GOTO :TRIM_INNER_LOOP
)
SET STR="!STR:%HLPSTR%=!"& SET STR=!STR:~1,-1!
ENDLOCAL & SET RESULT="%STR%"& SET RESULT=!RESULT:~1,-1!
GOTO :EOF
So after thinking about that for a while (~10 seconds), it came to my mind that I do have a way to retrieve the first or the last character of a string. So this solution followed:
:: Trims whitespace at beginning and end of a string.
:: PARAM
:: 1 string to trim
:: RETURN String trimmed string
:TRIM
SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
SET STR="%~1"& SET STR=!STR:~1,-1!
:TRIM_INNER_LOOP
IF "!STR!" NEQ "" (
    IF "!STR:~0,1!" == " " (
        SET STR=!STR:~1!
    ) ELSE IF "!STR:0,1!" == "	" (
        SET STR=!STR:~1!
    ) ELSE IF "!STR:~-1!" == " " (
        SET STR=!STR:~0,-1!
    ) ELSE IF "!STR:~-1!" == "	" (
        SET STR=!STR:~0,-1!
    ) ELSE (
        GOTO :TRIM_INNER_LOOP_END
    )
    GOTO :TRIM_INNER_LOOP
)
:TRIM_INNER_LOOP_END
ENDLOCAL & SET RESULT="%STR%"& SET RESULT=!RESULT:~1,-1!
GOTO :EOF
What you cannot see: I compare against space and then against tab. But I disliked the horrible IF-ELSE comparison stuff and ended with replacement check against a whitespace-string:
:: Trims whitespace at beginning and end of a string.
:: PARAM
:: 1 string to trim
:: RETURN String trimmed string
:TRIM
SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
SET STR="%~1"& SET STR=!STR:~1,-1!
:: whitespace is space and tab
SET WHITESPACE= 	
:TRIM_INNER_LOOP
IF "!STR!" NEQ "" (
    IF "!WHITESPACE:%STR:~0,1%=!" NEQ "!WHITESPACE!" (
        SET STR=!STR:~1!
        GOTO :TRIM_INNER_LOOP
    ) ELSE IF "!WHITESPACE:%STR:~-1%=!" NEQ "!WHITESPACE!" (
        SET STR=!STR:~0,-1!
        GOTO :TRIM_INNER_LOOP
    )
)
ENDLOCAL & SET RESULT="%STR%"& SET RESULT=!RESULT:~1,-1!
GOTO :EOF
TheOutcaste (Gast) - 21. Aug, 02:33

Small bug

There's a small bug in the routine. You must have enabled Delayed Expansion before calling the routine, or RESULT gets set to !RESULT:~1,-1! (the actual text, not the expanded variable).
This is because the Endlocal statement reverts Delayed Expansion to the state is was in before the SetLocal statement that enabled it in the TRIM routine. If it was disabled before calling TRIM, the delayed expansion of !RESULT:~1,-1! doesn't happen, and that is seen as a text string, not a variable.

Reflog

Informationstechnische Howtos, Hinweise und Merkwürdiges

Batchlib v1.0 2008-03-29

Aktuelle Beiträge

HOWTO_ O2 DSL Surf &...
Der O2 DSL Surf & Phone-Router ist für die...
cypressor - 12. Feb, 19:57
Uptweak Windows XP Home...
There are a lot of annoying limitations in Windows...
cypressor - 9. Okt, 19:30
BATCHLIB_ Batchlib package...
Download Batchlib package v1.0 (5 KB zip file) What...
cypressor - 29. Mrz, 19:10
BATCHLIB_ Batchlib library...
The batchlib library string.cmd is part of the batchlib...
cypressor - 29. Mrz, 18:10

Homepage Ticker

Links

Status

Online seit 4202 Tagen
Zuletzt aktualisiert: 28. Jun, 11:32
RSS XML 1.0 Button-Get-Firefox

batch
batchlib
howto
tech
video
Profil
Abmelden
Weblog abonnieren