Comment detail

コメントの削除 (Nested Flatten)
C++で書いてみました。
が、C++を書いた気にならない...

ちなみに、#if 0 ~ #endif は「コメント」ではないと思っているので対応していません。
 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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include <iostream>
#include <fstream>

/* g++ -o decomment decomment.cpp */

// decomment
void decomment(
    std::ostream& os,
    std::istream& is
    )
{
  bool blc, olc; /* in block/one-line comment */
  bool str, chr; /* in string/char literal */

  blc=olc=str=chr=false;

  char c,pc='\0';
  while ( is.get(c) ) {
    // escape
    if ( c == '\\' ) {
      if ( !olc && !blc ) {
        if ( pc == '/' )
          os.put('/');
        os.put(c);
      }
      is.get(c);
      if ( !olc && !blc )
        os.put(c);
    }
    // end of one-line comment
    else if ( olc && (c == '\n' || c == '\r') ) {
      olc = false;
      os.put(c);
      pc = '\0';
      continue;
    }
    // end of block comment
    else if ( blc && pc == '*' && c == '/' ) {
      blc = false;
      pc = '\0';
      continue;
    }
    // start of one-line comment
    else if ( pc == '/' && c == '/' && !str && !chr && !blc ) {
      olc = true;
      pc = '\0';
      continue;
    }
    // start of block comment
    else if ( pc == '/' && c == '*' && !str && !chr && !olc ) {
      blc = true;
      pc = '\0';
    }
    // start/end of char literal
    else if ( !olc && !blc && c == '\'' ) {
      chr = !chr;
      if ( pc == '/' )
        os.put('/');
      os.put(c);
    }
    // start/end of string literal
    else if ( !olc && !blc && c == '\"' ) {
      str = !str;
      if ( pc == '/' )
        os.put('/');
      os.put(c);
    }
    else {
      if ( pc == '/' && !olc && !blc )
        os.put('/');
      if ( c != '/' && !olc && !blc )
        os.put(c);
    }
    pc = c;
  }
}

namespace {
  const char* test = "//\
                      " "/*" "hoge" "*/"
                      /* / * /* // */ "hige";
}
int main(int c, char** v)
{
  (void)test;
  if ( c < 2 ) {
    std::cout << "usage: " << v[0] << " <C++ source file>\n";
    return 0;
  }

  std::ifstream ifs(v[1]);
  if ( !ifs ) {
    std::cerr << "failed to open " << v[1] << "\n";
    return 1;
  }
  decomment(std::cout, ifs);
  return 0;
}

Index

Feed

Other

Link

Pathtraq

loading...