From 8368feab2657bc3fcae899c1b7f4716fb62fad65 Mon Sep 17 00:00:00 2001 From: Alejandro Colomar Date: Tue, 23 May 2023 16:29:34 +0200 Subject: bash_aliases: Increase robustness of sed_rm_ccomments() Thanks to perl(1), we can use a non-greedy match to remove several C89-style comments in the same line. And by splitting (and slightly modifying) the other sed(1) call, we can handle multi-line comments that start in the same line that the previous one ends. Now the only remaining issue is nested comments, but that's rare, so let's ignore it for now. $ cat com.c /* let's see */ int foo/*how about here?*/; // meh int bar; /* Let's see how this behaves */ int baz; /* like this? like that! */ int qwe; // asdasd $ sed_rm_ccomments --- scripts/bash_aliases | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/scripts/bash_aliases b/scripts/bash_aliases index 5c0458402..e461707c8 100644 --- a/scripts/bash_aliases +++ b/scripts/bash_aliases @@ -23,14 +23,15 @@ EX_USAGE=64; # C # sed_rm_ccomments() removes C comments. -# It can't handle multiple comments in a single line correctly, -# nor mixed or embedded //... and /*...*/ comments. +# It can't handle mixed //... and /*...*/ comments. # Use as a filter (see man_lsfunc() in this file). sed_rm_ccomments() { - sed 's%/\*.*\*/%%' \ - |sed -E '\%/\*%,\%\*/%{\%(\*/|/\*)%!d; s%/\*.*%%; s%.*\*/%%;}' \ + perl -p -e 's%/\*.*?\*/%%g' \ + |sed -E '\%/\*%, \%\*/% {\%(\*/|/\*)%!d}' \ + |sed -E '\%/\*% {s%/\*.*%%; n; s%.*\*/%%;}' \ + |sed -E '\%/\*% {s%/\*.*%%; n; s%.*\*/%%;}' \ |sed 's%//.*%%'; } -- cgit v1.2.3