Tech Tip: Utility to Remove all comments from a 4D method text
PRODUCT: 4D | VERSION: 14 R5 | PLATFORM: Mac & Win
Published On: March 7, 2016
If you are doing any kind of source code repository, from home grown to commercial, there may come when you would like to compare versions of a method without the clutter and noise of comments. You just want to see what were the changes in code, not in the comments.
The utility below will take a text copy of a 4D method and place a copy of the text of the Pasteboard with all comments removed. It uses RegEx (Regular Expressions) to find the patterns for comments, texts that begin with // and end with carriage return or text segments that begin with /* and end with */.
The utility can be used with languages other than 4D, such as JavaScript, as long as it uses the same commenting patterns.
If (True) If (False) Begin SQL /* Name: RemoveComments Path: RemoveComments Purpose: Remove all comments from a 4D method, Javascript, and any code that uses the commenting patterns of // or /*...*/ */ End SQL End if C_TEXT($MethodName_T) //===================== Declare Variables ================================== //method_parameters_declarations //-------------------------------------------------------------------------- //method_wide_constants_declarations //-------------------------------------------------------------------------- //local_variable_declarations C_TEXT($Code_T;$Pattern_T) C_LONGINT($Start_L;$Found_L;$Length_L) C_TIME($Ref_H) End if //====================== Initialize and Setup ================================ $Ref_H:=Open document("";"TEXT";Get pathname) //======================== Method Actions ================================== If (OK=1) $Code_T:=Document to text(Document) $Pattern_T:="((?:\\/\\*(?:[^*]|(?:\\*+[^*\\/]))*\\*+\\/)|(?:\\/\\/.*))" $Start_L:=1 While (Match regex($Pattern_T;$Code_T;$Start_L;$Found_L;$Length_L)) $Code_T:=Delete string($Code_T;$Found_L;$Length_L) $Start_L:=$Found_L End while // Clean up the consecutive empty lines that may have been left behind // $Code_T:=Replace string($Code_T;" \r \r";"\r") $Code_T:=Replace string($Code_T;" \r";"\r") $Code_T:=Replace string($Code_T;" \r";"\r") $Code_T:=Replace string($Code_T;"\r\r";"\r") $Code_T:=Replace string($Code_T;"\r\r\r";"\r") SET TEXT TO PASTEBOARD($Code_T) End if //======================== Clean up and Exit ================================= |