Comment detail

/*コメント*/を取り除く (Nested Flatten)

'/' と '/' をビットパターンで調べてみました。

 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
#include <stdio.h>
#include <stdint.h>
#include <string.h>

#define BEGIN_COMMENT   (('/'<<8)+'*')
#define END_COMMENT     (('*'<<8)+'/')

char *remove_comment_c(char *s)
{
  int in_comment = 0;
  uint32_t next_match = BEGIN_COMMENT, cs = 0;
  char *retval, *buf;

  retval = s;
  buf = s;
  while (*s != 0) {
    cs <<= 8;
    cs |= *s;
    s++;
    if ((cs & INT16_MAX) == next_match) {
      in_comment = !in_comment;
      next_match = in_comment ? END_COMMENT : BEGIN_COMMENT;
      if (in_comment)
        *buf++ = (cs & (INT8_MAX<<16)) >> 16;
      cs = 0;
    } else if (!in_comment && cs >= (1<<16)) {
      *buf++ = (cs & (INT8_MAX<<16)) >> 16;
    }
  }
  if (!in_comment) {
    if (cs >= 256)
      *buf++ = (cs & (INT8_MAX<<8)) >> 8;
    *buf++ = cs & INT8_MAX;
  }
  *buf = 0;

  return retval;
}
あー、* が消えてしまった。'/*' と '*/' をビットパターンで、ということです。

Index

Feed

Other

Link

Pathtraq

loading...