A trim function

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
colargol
Posts: 49
Joined: 28 Sep 2011 13:23
Location: france

A trim function

#1 Post by colargol » 21 Oct 2011 13:57

edit:forget this post, there is quite the same function here, sorry I didn't know!


Hi
As I'm using many trim in my batch, I was looking for a good trim function and I tried this:

Code: Select all

setlocal ENABLEDELAYEDEXPANSION

set "str=    some string    "
call:trim str

Code: Select all

:trim
call:trim2 %~1 %%%~1%%
goto:eof

:trim2
set tmp_s=%*
for /f "tokens=1* delims= " %%a in ("!tmp_s!") do (set %%a=%%b)
goto:eof

It seems it works and may be faster than usual trim function, but I'm not sure, what do you think? can this be improved?
And I'm still not very familiar with setlocal command, I don't really undestand where they should be placed in functions :oops:
Thx
Last edited by colargol on 21 Oct 2011 16:30, edited 2 times in total.

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: A trim function

#2 Post by dbenham » 21 Oct 2011 15:13

(I deleted my initial response)

How are your TRIM and TRIM2 functions an improvement over the existing DOS TIPS functions TrimSpaces and TrimSpaces2 :?:

Dave Benham

colargol
Posts: 49
Joined: 28 Sep 2011 13:23
Location: france

Re: A trim function

#3 Post by colargol » 21 Oct 2011 16:04

No! :shock:
I never saw this code before, I can ensure you :?

I thought I found something interresting :?

I was using this trim until now

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: A trim function

#4 Post by Ed Dyreen » 21 Oct 2011 17:39

'
Are my TRIM functions an improvement over the existing DOS TIPS functions ?
They accept exact trim or trim by occurence:

Code: Select all

%_forX% ( '$FullPathFile, "\"' ) %@TrimLeft%
%_forX% ( '$FullPathFile, 3' ) %@TrimLeft%

%_forX% ( '$FullPathFile, "\"' ) %@TrimRight%
%_forX% ( '$FullPathFile, 3' ) %@TrimRight%
code snippet of @TrimLeft:

Code: Select all

set ^"%$Defines%=!@Udelim! ( %$n1c%

   !_forTS! ( "%$Defines%" ) !@SetLocal! %$n1c%
   !_forL!  ( '"#Var,""%%~a""", "$Val,""%%~a"",#", "#strORint,""%%~b"""' ) !@Param! %$n1c%
   !_necho! ^^^!$Trace^^^!: '%%~a' %$n1c%

   if defined $Val if %%~b NEQ +%%~b ( %$n1c%
      set "$Val=^!$Val:*%%~b=^!" %$n1c%
   ) else set "$Val=^!$Val:~0,-%%~b^!" %$n1c%
   set "%%~a=^!$Val^!" %$n1c%
   !_necho!  %%~a: '^^^!$Val^^^!' %$n1c%

   !_forL! ( '^^^!$Return^^^!, %%~a' ) !@EndLocal! ^&!@nError! %$n1c%
)"
code snippet of @TrimRight:

Code: Select all

set ^"%$Defines%=!@Udelim! ( %$n1c%

   !_forTS! ( "%$Defines%" ) !@SetLocal! %$n1c%
   !_forL!  ( '"#Var,""%%~a""", "$Val,""%%~a"",#", "#strORint,""%%~b"""' ) !@Param! %$n1c%
   !_necho! ^^^!$Trace^^^!: '%%~a' %$n1c%

   if defined $Val if %%~b NEQ +%%~b ( %$n1c%
      set "$Last=^!$Val:%%~b=" "^!" %$n1c%
      set  $Last="^!$Last^!" %$n1c%
      !_forTS! ( ^^^!$Last^^^! ) do set "$Last=%%~^!" %$n1c%
      set  "$Last=%%~b^!$Last^!###" %$n1c%
      set "$Val=^!$Val^!###" %$n1c%
      for %%? in ( %$n1c%

         "^!$Last^!" %$n1c%
      ) do set "$Val=^!$Val:%%~?=^!" %$n1c%
   ) else  set "$Val=^!$Val:~0,-%%~b^!" %$n1c%
   set "%%~a=^!$Val^!" %$n1c%
   !_necho!  %%~a: '^^^!$Val^^^!' %$n1c%

   !_forL! ( '^^^!$Return^^^!, %%~a' ) !@EndLocal! ^&!@nError! %$n1c%
)"
CPU3G; loadtime: '+-50ms', exectime; '+-50ms', executions: '+-20p/sec'

colargol
Posts: 49
Joined: 28 Sep 2011 13:23
Location: france

Re: A trim function

#5 Post by colargol » 22 Oct 2011 01:22

Ed Dyreen wrote:Are my TRIM functions an improvement over the existing DOS TIPS functions ?
Maybe, yes... I don't understand macros :o


Finally, I use this, but of course it has some limitations

Code: Select all

:trim
set var_trim=%~1
call:trim2 %%%~1%%
goto:eof

:trim2
set %var_trim%=%*
goto:eof

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: A trim function

#6 Post by Ed Dyreen » 22 Oct 2011 15:36

colargol wrote:Maybe, yes... I don't understand macros :o
Yes, it's very difficult

Code: Select all

set "@macro=echo. My GOD, I just wrote a macro ^!"
%@macro%
That DOSTips function works, but it can be much faster and robust and flexible !
This is an outer trim, it trims ANY string :roll: :

Code: Select all

%_forX% ( '$var' ) %@Trim%
%_forX% ( '$var, "This Works"' ) %@Trim%
code snippet of @Trim:

Code: Select all

!_forQ! ( %$n1c%

   "^!$str^!" %$n1c%

) do    if defined $val ( %$n1c%
   if /i ["%%~?"] neq [" "] set "$val=^!$val: =###^!" %$n1c%

   set   "$?=^!$val:%%~?= ^!" %$n1c%
   set "$val=" %$n1c%
   !_forT! ( %$n1c%
      ^^^!$?^^^! %$n1c%
   ) do    set "$val=^!$val^!%%~?%%~^!" %$n1c%

   if defined $val ( %$n1c%
      set "$val=^!$val:*%%~?=^!" %$n1c%
      if /i ["%%~?"] neq [" "] set "$val=^!$val:###= ^!" %$n1c%
   ) %$n1c%
) %$n1c%
set "%%~a=^!$val^!" %$n1c%
console snippet:

Code: Select all

 >> Pre Define @Trim, outer trim
 << Post Define @Trim, outer trim [OK]

 @Trim: '$var'
  $val: '   C:\This Works\This Works.TMP   '
  $str: ' '
  $var: 'C:\This Works\This Works.TMP'
 [ok:0]

Post Reply