I want to get the first #include statement from a .cpp file using Python regex as fast as possible.
For example,
/* Copyright:
This file is
protected
#include <bad.h>
*/
// Include files:
#undef A_MACRO
#include <stddef.h> // defines NULL
#include "logger.h"
// Global static pointer used to ensure a single instance of the class.
Logger* Logger::m_pInstance = NULL;
should return #include <stddef.h>
I know one way is to remove all comments and then get the first line from the remaining texts. But this seems not to be fast enough since it has to go through the whole file. If I only need the first #include statement, is there any efficient way I can do it using Python regex?
[Update 1] Several folks mentioned it's not a good solution to use regex. I understand this is not a typical use case of regex. But is there a better way to get rid of the leading comments than regex? Any suggestion would be appreciated.
[Update 2] Thanks for the answers. But seems there is no one I am satisfied yet. My requirements are straightforward: (1) avoid going through the whole file to get the first line. (2) Need to handle the leading comments correctly.
<bad.h>#includewrapped in#if 0/#endif? What about one wrapped in#ifdef linux/#endif? What about#define foo <stdio.h>/#include foo?