{"id":215,"date":"2007-05-12T17:08:19","date_gmt":"2007-05-12T15:08:19","guid":{"rendered":" http:\/\/www.nsonic.de\/blog\/2007\/05\/strip-comments-from-string\/"},"modified":"2007-05-12T17:08:20","modified_gmt":"2007-05-12T15:08:20","slug":"strip-comments-from-string","status":"publish","type":"post","link":"https:\/\/www.nsonic.de\/blog\/2007\/05\/strip-comments-from-string\/","title":{"rendered":"Strip Comments from String"},"content":{"rendered":"<p>[code lang=&#8221;delphi&#8221;]type<br \/>\n  STRIP_STATES = ( SS_NORMA, SS_BRACE, SS_PAREN, SS_LITER );<\/p>\n<p>function SCFS( const S : string; var SS : STRIP_STATES ): string;<br \/>\n\/\/ strip comments from string<br \/>\nvar<br \/>\n  I : integer;<br \/>\nbegin<br \/>\n  Result := &#8221;;<br \/>\n  I := 1;<br \/>\n  while I <= Length( S ) do begin\n    case SS of\n      SS_NORMA :\n        case S[ I ] of\n          '{' : if ( I < Length( S ) ) and ( S[ I + 1 ] = '$' )\n                  then Result := Result + S[ I ]\n                  else SS := SS_BRACE;\n          '(' : if ( I < Length( S ) ) and ( S[ I + 1 ] = '*' )\n                  then begin\n                    if ( I < Length( S ) - 1 ) and ( S[ I + 2 ] = '$' )\n                      then Result := Result + S[ I ]\n                      else begin SS := SS_PAREN; Inc( I ); end;\n                  end else Result := Result + S[ I ];\n          '\/' : if ( I < Length( S ) ) and ( S[ I + 1 ] = '\/' )\n                  then Exit\n                  else Result := Result + S[ I ];\n          '''' : begin SS := SS_LITER; Result := Result + S[ I ]; end;\n          else Result := Result + S[ I ];\n        end;\n      SS_BRACE :\n        case S[ I ] of\n          '}' : SS := SS_NORMA;\n        end;\n      SS_PAREN :\n        case S[ I ] of\n          '*' : if ( I < Length( S ) ) and ( S[ I + 1 ] = ')' )\n                  then begin SS := SS_NORMA; Inc( I ); end;\n        end;\n      SS_LITER :\n        case S[ I ] of\n          '''' : begin SS := SS_NORMA; Result := Result + S[ I ]; end;\n          else Result := Result + S[ I ];\n        end;\n    end; \/\/ case\nSS of\n    Inc( I );\n  end; \/\/ while I <= Length( S ) do begin\nend; \/\/ SCFS\n\nprocedure STRIP_COMMENTS( slIN, slOUT : TStringList;\n  EMPTY_LINES : boolean = false; \/\/ True - to preserve empty lines\n  LEFT_TRIM : boolean = false; \/\/ True - to strip indentation\n  RIGHT_TRIM : boolean = true ); \/\/ False - to preserve right white spaces\n\/\/ strips Object Pascal's comments\n\/\/ slIN - input, slOUT - output\nvar\n  I : integer;\n  STRIP_STATE : STRIP_STATES;\n  S : string;\nbegin\n  STRIP_STATE := SS_NORMA;\n  for I := 0 to slIN.Count - 1 do begin\n    S := SCFS( slIN[ I ], STRIP_STATE );\n    if LEFT_TRIM then S := TrimLeft( S );\n    if RIGHT_TRIM then S := TrimRight( S );\n    if EMPTY_LINES or ( Length( Trim( S )) > 0 ) then slOUT.Append( S );<br \/>\n  end;<br \/>\nend;                                         \/\/ STRIP_COMMENTS<br \/>\n[\/code]<\/p>\n<p>[tags]Delphi, Strings[\/tags]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>[code lang=&#8221;delphi&#8221;]type STRIP_STATES = ( SS_NORMA, SS_BRACE, SS_PAREN, SS_LITER ); function SCFS( const S : string; var SS : STRIP_STATES ): string; \/\/&hellip;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":""},"categories":[11],"tags":[75,123],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p71Tml-3t","_links":{"self":[{"href":"https:\/\/www.nsonic.de\/blog\/wp-json\/wp\/v2\/posts\/215"}],"collection":[{"href":"https:\/\/www.nsonic.de\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.nsonic.de\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.nsonic.de\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.nsonic.de\/blog\/wp-json\/wp\/v2\/comments?post=215"}],"version-history":[{"count":0,"href":"https:\/\/www.nsonic.de\/blog\/wp-json\/wp\/v2\/posts\/215\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.nsonic.de\/blog\/wp-json\/wp\/v2\/media?parent=215"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.nsonic.de\/blog\/wp-json\/wp\/v2\/categories?post=215"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.nsonic.de\/blog\/wp-json\/wp\/v2\/tags?post=215"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}