@mbozzi
Ooooh, it was because of the indentation. Tricky, didn't realize there couldn't be spaces in between the R and quotes. Thanks!
@OP
Sorry if it seems like we're being difficult, but mbozzi made me realize this is actually a bit more difficult to do correctly.
Not only do you need to find where /** and */ delimiters are, you also need to make sure you're not in a string, or (in C++11) a raw string. This requires having context of the current state of the document.
For instance, if you have
1 2 3 4 5 6
|
/** \
\
\
*/
int i = 0; \
|
You can't tell if you're actually in a comment or not unless you have more context.
It could look like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
// Example program
#include <iostream>
#include <string>
int main()
{
// Not a comment:
std::string text =
"\
/** \
\
\
*/ int i = 0; \
";
// Is a comment:
/** \
\
\
*/ int i = 0; \
}
|
You also have to make sure you aren't already in a comment:
e.g.
/* /* */ int main() {}
comments don't nest.
single line comment overrides the multi-line start
Anyway, if we ignore the difficult parts for now, you need to iterate through every character until you find a "/". If you find a "/", see if the two next characters are both"*". If this is true, then you need to set a state variable to say that we're currently in a comment.
Once you're in a comment, keep iterating until you find a "*" followed by a "/".
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
|
#include <iostream>
#include <string>
#include <fstream>
/**
Test comment
*/
int function() {return 42;}
/**
We're inside a documentation comment or whatever it's called.
Yes, this will break with strings that look like comments, among a plethora of other gotchas.
It's just an example.
*/
int main()
{
std::ifstream fin("main.cpp");
char ch;
while (fin.get(ch))
{
if ((ch == '/') &&
fin.get(ch) && (ch == '*') &&
fin.get(ch) && (ch == '*'))
{
std::cout << "Entered comment...\n";
}
if ((ch == '*') && fin.get(ch) && (ch == '/'))
{
std::cout << "Leaving comment...\n";
}
}
}
|
D:\test>main
Entered comment...
Leaving comment...
Entered comment...
Leaving comment... |