Skip to main content
added 410 characters in body
Source Link
Pimgd
  • 22.3k
  • 5
  • 66
  • 144

You don't handle pre-processor related things such as \.

Consider the case where

int x = 0;
//setting \
x to 0

The backslash at the end of a line should "escape" the newline character that follows. But because you skip all characters until \n, you ignore the escaping and would return the file as

int x = 0;
x to 0

Which would no longer compile.

Of course, if we were really evil, we would do something like this:

File 1

#define WEIRD_COMMENT //comment \\

File 2

#include <file1.h>

WEIRD_COMMENT
//this is a weird comment
int main(void)
{
 ...

And your code would again break things, by removing the //this is a weird comment line, causing the WEIRD_COMMENT inclusion to turn int main into a comment... At this point I worry about the feasibility of your solution - there's no way for you to strip this sort of comments properly, I think.

Then there's also trigraphs, which are sequences like ??/, which converts to \. Take a look on wikipedia for a full view of the madness that is trigraphs and digraphs.

You don't handle pre-processor related things such as \.

Consider the case where

int x = 0;
//setting \
x to 0

The backslash at the end of a line should "escape" the newline character that follows. But because you skip all characters until \n, you ignore the escaping and would return the file as

int x = 0;
x to 0

Which would no longer compile.

You don't handle pre-processor related things such as \.

Consider the case where

int x = 0;
//setting \
x to 0

The backslash at the end of a line should "escape" the newline character that follows. But because you skip all characters until \n, you ignore the escaping and would return the file as

int x = 0;
x to 0

Which would no longer compile.

Of course, if we were really evil, we would do something like this:

File 1

#define WEIRD_COMMENT //comment \\

File 2

#include <file1.h>

WEIRD_COMMENT
//this is a weird comment
int main(void)
{
 ...

And your code would again break things, by removing the //this is a weird comment line, causing the WEIRD_COMMENT inclusion to turn int main into a comment... At this point I worry about the feasibility of your solution - there's no way for you to strip this sort of comments properly, I think.

Then there's also trigraphs, which are sequences like ??/, which converts to \. Take a look on wikipedia for a full view of the madness that is trigraphs and digraphs.

Source Link
Pimgd
  • 22.3k
  • 5
  • 66
  • 144

You don't handle pre-processor related things such as \.

Consider the case where

int x = 0;
//setting \
x to 0

The backslash at the end of a line should "escape" the newline character that follows. But because you skip all characters until \n, you ignore the escaping and would return the file as

int x = 0;
x to 0

Which would no longer compile.