Page 1 of 1

A trim function

Posted: 21 Oct 2011 13:57
by colargol
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

Re: A trim function

Posted: 21 Oct 2011 15:13
by dbenham
(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

Re: A trim function

Posted: 21 Oct 2011 16:04
by colargol
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

Re: A trim function

Posted: 21 Oct 2011 17:39
by Ed Dyreen
'
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'

Re: A trim function

Posted: 22 Oct 2011 01:22
by colargol
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

Re: A trim function

Posted: 22 Oct 2011 15:36
by Ed Dyreen
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]