challenge /*コメント*/を取り除く

与えられた文字列から「/*」と「*/」で挟まれた部分を取り除くコードを書いてください。

なお、「/*」と入力末尾で挟まれた部分も取り除いてください。 つまり、入力が「AAA/*BBB」なら出力は「AAA」です。 また、コメントは入れ子になりません。入力が「AAA/*BBB/*CCC*/DDD*/EEE」のとき、ひとつめの「*/」でコメントが終わるので出力は「AAADDD*/EEE」になります。 「//」や「**」が混ざる場合の挙動は失敗しやすいので要注意です。

Pythonでの実行例は下のようになります:

>>> remove_comment('AAA')
'AAA'
>>> remove_comment('AAA/*BBB*/')
'AAA'
>>> remove_comment('AAA/*BBB')
'AAA'
>>> remove_comment('AAA/*BBB*/CCC')
'AAACCC'
>>> remove_comment('AAA/*BBB/*CCC*/DDD*/EEE')
'AAADDD*/EEE'
>>> remove_comment('AAA/a//*BB*B**/CCC')
'AAA/a/CCC'

このお題は匿名での投稿を参考にして作成しました。 ありがとうございます。

Posted feedbacks - 秀丸マクロ

なんだか汚くなってしまいました...
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
    call RemoveComment "AAA";
    call RemoveComment "AAA/*BBB*/";
    call RemoveComment "AAA/*BBB";
    call RemoveComment "AAA/*BBB*/CCC";
    call RemoveComment "AAA/*BBB/*CCC*/DDD*/EEE";
    call RemoveComment "AAA/a//*BB*B**/CCC";
    endmacro;

RemoveComment:
    while( true ) {
        ##commentStart = strstr( $$1, "/*" );
        if ( ##commentStart == -1 ) {
            break;
        }
        ##commentEndSearch = strstr( rightstr( $$1, strlen( $$1 ) - ##commentStart - strlen( "/*" ) ), "*/" );
        if ( ##commentEndSearch == -1 ) {
            ##commentLen = strlen( $$1 ) - ##commentStart;
        } else {
            ##commentLen = ##commentEndSearch + strlen( "/*" ) + strlen( "*/" );
        }
        $$1 = leftstr( $$1, ##commentStart ) + rightstr( $$1, strlen( $$1 ) - ##commentStart - ##commentLen );
    }
    message $$1;
    return;

開いている文書から取り除く。
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
    disabledraw;
    begingroupundo;
    gofiletop;
    while( true ) {
        if ( gettext2( column, lineno, column + strlen( "/*" ), lineno ) != "/*" ) {
            searchdown "/*";
            if ( !result ) {
                break;
            }
        }
        #commentStartX = x;
        #commentStartY = y;
        movetolineno column + 1 + strlen( "/*" ), lineno;
        if ( gettext2( column, lineno, column + strlen( "*/" ), lineno ) != "*/" ) {
            searchdown "*/";
            if( !result ) {
                gofileend;
            }
        }
        movetolineno column + 1 + strlen( "*/" ), lineno;
        #commentEndX = x;
        #commentEndY = y;
        beginsel;
        moveto #commentStartX, #commentStartY;
        delete;
    }
    endgroupundo;
    enabledraw;

searchdown2 を使えばもっと簡潔にできましたね。
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
    disabledraw;
    begingroupundo;
    gofiletop;
    while( true ) {
        searchdown2 "/*";
        if ( !result ) {
            break;
        }
        #commentStartX = x;
        #commentStartY = y;
        movetolineno column + 1 + strlen( "/*" ), lineno;
        searchdown2 "*/";
        if( !result ) {
            gofileend;
        }
        movetolineno column + 1 + strlen( "*/" ), lineno;
        beginsel;
        moveto #commentStartX, #commentStartY;
        delete;
    }
    endgroupundo;
    enabledraw;

Index

Feed

Other

Link

Pathtraq

loading...