英文:
How to replace all include guards by #pragma once in whole repository in VSCode?
问题
在我们的项目中,我们使用类似以下的包含保护:
#ifndef _SINGLETON_H_
#define _SINGLETON_H_
#endif // #ifndef _SINGLETON_H_
最后一行可以不带注释等,所以文件可以匹配类似这样的内容:
^.*#ifndef\s+[A-Z_]+\s+#define\s+[A-Z_]+(.*\n)*.*#endif.*$
我想要用以下方式替换它:
#pragma once
如何实现这个目标呢?可能是要匹配包含保护的内容,将内容存储在包含保护内,使用#pragma前缀内容,并替换包含保护的内容,对吗?我在存储(或不修改)包含保护内的内容方面有困难。
英文:
In our project, we use include guards like:
..
#ifndef _SINGLETON_H_
#define _SINGLETON_H_
..
#endif // #ifndef _SINGLETON_H_
..
The last line can be without comment etc. so the files match something like:
^.*#ifndef\s+[A-Z_]+\s+#define\s+[A-Z_]+(.*\n)*.*#endif.*$
.
I want to replace it with:
..
#pragma once
..
..
using Replace All
functionality.
How to achieve that?
Probably somehow match the include guard with content, store the content within the include guard, prefix the content with pragma and replace the include guard with content, right?
I am struggling with storing (or not modifying) the content within include guard.
答案1
得分: 0
- 搜索:
^#ifndef\s+[A-Z_].*\s*#define\s+[A-Z_].*((.*\n)+.*)#endif.*$
替换:#pragma once$1
- 点击“全部替换”。
英文:
So I made it work myself (at least for our project with 4687 headers replaced):
- Search:
^#ifndef\s+[A-Z_].*\s*#define\s+[A-Z_].*((.*\n)+.*)#endif.*$
Replace:#pragma once$1
- Hit
Replace All
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论