压缩多行 C 函数原型
我有 C 函数原型(某些 Windows api 头文件),看起来像:(
int
foo
(
int
a
,
int
*
b
)
;
它们似乎没有编码约定)
,我试图以编程方式将其转换为表单的单行原型(或接近它的东西)
int foo(int a, int * b);
:研究过像 ctags 这样的程序( ctags 多行 C 函数原型)并进入 uncrustify 中的各种设置(http://uncrustify.sourceforge.net/ )但是我在这两个方面都没有取得任何进展。 (任何见解都很棒,或者也许我错过的 385 个 uncrustify 选项之一可以满足我的需求)。
以编程方式,我试图寻找表示函数原型的独特标记,以便我可以编写一个脚本,将代码格式化为我喜欢的格式。
如果不使用词法分析器和解析器,这似乎很快就会变得非常复杂;有什么建议吗?
I have C function prototypes (certain windows api header files) that look like:
int
foo
(
int
a
,
int
*
b
)
;
(they seem to have no coding convention)
which I am trying to programmatically turn into a one-line prototype of the form (or something close to it):
int foo(int a, int * b);
I have looked into programs like ctags ( ctags multi-line C function prototypes ) and into various settings in uncrustify ( http://uncrustify.sourceforge.net/ ) however I haven't been able to make any headway in either. (any insight would be great, or perhaps one of the 385 uncrustify options that I missed does what I want).
Programmatically, I am trying to look for unique markers that signify a function prototype so that I can write a script that will format the code to my liking.
Without using a lexer and a parser this seems like it could get very convoluted very quickly; any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
通过
indent -kr
或astyle --style=kr
运行它们run them through
indent -kr
orastyle --style=kr
使用vim解决方案?
将标记放在 int 上并执行
11J
Solution using vim?
put marker on int and do
11J
第一个命令 - 在管道之前 - 将所有换行符替换为空格,下一个命令将在每个分号之后放回一个换行符。
当然,只有当文件中除了这些原型之外没有其他内容时,这才有效。如果您想保留其他一些内容,您可以使用 vim 的视觉选择和两个替换命令:
选择您想要加入的区域,然后
选择加入的行并
The first command - before the pipe - will replace all new-lines to spaces, and the next will put a new-line back after every semicolon.
Of course this will only work if there are nothing else but these prototypes in the file. If there are some other stuff that you want to keep as they are, you can use vim's visual selection and two substitution commands:
Select the region you want to join, than
Select the joined line and
使用 vi 的另一个解决方案:
进行正则表达式搜索,删除所有换行符。然后将结果弄乱并进行另一个正则表达式搜索,将每个
;
替换为; \n\n
。这应该会给你留下一个原型列表,每个原型之间跳过一行。由于我们标记原型的结束而不是开始,并且所有原型都以相同的方式结束,因此我们不必担心无法识别特殊情况。Another solution using vi:
do a regex search removing all newlines. Then take the resulting mess and do another regex search replacing each
;
with; \n\n
. That should leave you with a list of prototypes with a line skipped between each one. Since we're marking the ends of the prototypes instead of the beginnings and all prototypes end the same way, we don't have to worry about not recognizing special cases.