Jump to content

Removing un-necessary white space...


CyberSlug
 Share

Recommended Posts

Anyone have a function that removes unnecessary space

- you can assume that the line does not end in the underscore continutation charcter

- space inside of literal strings and comments must remain intact

- watch out for complex boolean expressions with parentheses and operations like AND

- if line contains an in-line comment, I'd prefer keeping the space between that comment and the code

Example:

$var = 'If ( Not $foo Or $bar And (1 <= 2) ) Then MsgBox ( 4096, "blah;blah", " ") ;example'
$spaceRemoved = _RemoveSpace($var)
Exit
Func _RemoveSpace($str)
  Local $output
  ;.... magic goes here
   Return $output
EndFunc

The result should look like:

If (Not $foo Or $bar And (1<=2)) Then MsgBox(4096,"blah;blah"," ") ;example
Use Mozilla | Take a look at My Disorganized AutoIt stuff | Very very old: AutoBuilder 11 Jan 2005 prototype I need to update my sig!
Link to comment
Share on other sites

Well, it's pretty simple, but it works:

Func _RemoveSpace($str)
  Local $output
  $strsplit = StringSplit($str, '"')
  If $strsplit[0] = 1 Then $strsplit = StringSplit($str, "'")
  For $I = 1 To $strsplit[0]
    If Mod($I, 2) = 1 Then
      $strsplit[$I] = StringReplace($strsplit[$I], " )", ")")
      $strsplit[$I] = StringReplace($strsplit[$I], "( ", "(")
      $strsplit[$I] = StringReplace($strsplit[$I], ", ", ",")
      $strsplit[$I] = StringReplace($strsplit[$I], "< ", "<")
      $strsplit[$I] = StringReplace($strsplit[$I], " <", "<")
      $strsplit[$I] = StringReplace($strsplit[$I], "= ", "=")
      $strsplit[$I] = StringReplace($strsplit[$I], " =", "=")
      $strsplit[$I] = StringReplace($strsplit[$I], "> ", ">")
      $strsplit[$I] = StringReplace($strsplit[$I], " >", ">")
      $strsplit[$I] = StringReplace($strsplit[$I], "  ", " ")
      $output = $output & $strsplit[$I]
    Else
      $output = $output & '"' & $strsplit[$I] & '"'
    EndIf
  Next
  Return $output
EndFunc

The only unwanted spaces remaining are the ones between function name and "("

I hope i was helpful. :ph34r:

Greetings,

ZeD

Link to comment
Share on other sites

Thanks for post--it gives me some ideas.

Comments on your code: If two or three spaces are around operators, they aren't removed. Also the spaces between function names and "(" are ones I really need to be removed.

Use Mozilla | Take a look at My Disorganized AutoIt stuff | Very very old: AutoBuilder 11 Jan 2005 prototype I need to update my sig!
Link to comment
Share on other sites

CS, doesn't Tidy do this?  I have never used Tidy myself, but I seem to remember that removing extraneous whitespace was something it did a little bit of.  I could be wrong, however.

<{POST_SNAPBACK}>

:ph34r::(:lol: you dont use tidy!!!! can we get jDeB in here

Roger! You son of a big pile o' Monkey Nuts.

Link to comment
Share on other sites

Perhaps I should approach this from a different angle as the space removal is secondary to my main goal: Anyone have pre-built routines that would make converting old GUI syntax to new GUI syntax really easy :ph34r:

Tidy, in fact, does remove extraneous whitespace. It never occurred to me to run tidy *before* the text processing! Funny thing it that I suggest running it after...

I need to do more preliminary planning before I resume scripting!

Use Mozilla | Take a look at My Disorganized AutoIt stuff | Very very old: AutoBuilder 11 Jan 2005 prototype I need to update my sig!
Link to comment
Share on other sites

Spending the time cleanly organizing your code is the best way I have seen to do any thing. I have always tabbed my code. I have also always organized it as I went along. I havent tried any of your recommended coding programs. I have heard alot about them. I just havent done anything with them yet. Notepad suffices my needs to this point. I had actually never heard of tidy :-P

Oh well thats my input... btw how does everyone like my icon?

JS

AutoIt Links

File-String Hash Plugin Updated! 04-02-2008 Plugins have been discontinued. I just found out.

ComputerGetInfo UDF's Updated! 11-23-2006

External Links

Vortex Revolutions Engineer / Inventor (Web, Desktop, and Mobile Applications, Hardware Gizmos, Consulting, and more)

Link to comment
Share on other sites

  • Developers

Perhaps I should approach this from a different angle as the space removal is  secondary to my main goal:  Anyone have pre-built routines that would make converting old GUI syntax to new GUI syntax really easy :ph34r:

Tidy, in fact, does remove extraneous whitespace.  It never occurred to me to run tidy *before* the text processing!  Funny thing it that I suggest running it after...

I need to do more preliminary planning before I resume scripting!

<{POST_SNAPBACK}>

Funny to be away for a day and see discussions like this.. :(

I would have suggested to run tidy as well...

I don't use Tidy because I format my code as I write it, not after-the-fact.  SciTE's auto-indent goes a long way into simplifying this as well.

<{POST_SNAPBACK}>

Are you also capable of doing also proper cases for all variables ?

Tidy does that as well since a couple of weeks.... i find that function very handy...

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

Funny to be away for a day and see discussions like this.. ;)

I would have suggested to run tidy as well... 

Are you also capable of doing also proper cases for all variables ?

Tidy does that as well since a couple of weeks....  i find that function very handy...

<{POST_SNAPBACK}>

The proper cases thing would definitely be handy. To make sure my code was complete and perfect :). Now you have given me a reason to use it thank you! :)

JS

AutoIt Links

File-String Hash Plugin Updated! 04-02-2008 Plugins have been discontinued. I just found out.

ComputerGetInfo UDF's Updated! 11-23-2006

External Links

Vortex Revolutions Engineer / Inventor (Web, Desktop, and Mobile Applications, Hardware Gizmos, Consulting, and more)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...