简体   繁体   English

在C ++代码上删除空格并使用astyle添加换行符

[英]Remove spaces and add newlines with astyle on c++ code

I inherited a massive amount of code formatted as follows: 我继承了大量格式如下的代码:

void <whitespace> Foo::bar <whitespace> ( ) <whitespace> // short documentation
{
  // code
}
void Foo::bar()
{
  // code
}

with no empty line at the end of files and sometimes missing newline between functions. 文件末尾没有空行,有时函数之间缺少换行符。 I've successfully used the following options with astyle to remove most style errors, but I can't find any reference or documentation on how to fix these issues. 我已成功将以下选项与astyle一起使用,以消除大多数样式错误,但是找不到有关如何解决这些问题的任何参考或文档。

--style=ansi -t3 -N -j -k1 -z2 -n -r -H -U -p -q -w -Y -L -S

I've used a simple python script in the past to make sure that there are a newline at the end, and I guess that I could use something like it again together with regex to remove whitespace and add newlines, but since I use astyle now it would be great if it could do it, so... suggestions? 过去,我使用一个简单的python脚本来确保末尾有换行符,我想我可以再次将类似的东西与regex一起使用以删除空格并添加换行符,但是由于我现在使用astyle如果能够做到,那就太好了,所以...建议?

I don't know what platform you're using, but if you're on any type of NIX-based platform, you have access to a lot of text-editing tools such as sed , meaning you don't have to roll your own code to do it. 我不知道您使用的平台是什么,但是如果您使用任何类型的基于NIX的平台,您都可以使用许多文本编辑工具,例如sed ,这意味着您不必滚动自己的代码来做到这一点。

I'm not great with regex, but the general syntax for sed search and replace on the command line is this: 我对regex不太满意,但是在命令行上进行sed搜索和替换的常规语法是:

sed -i -e 's/expr1/expr2/g' filename

This will replace "expr1" with "expr2". 这会将“ expr1”替换为“ expr2”。 It works with spaces and regex, so you can condense all multiple spaces into one space (or none, if you don't type anything for expr2). 它可以与空格和正则表达式一起使用,因此您可以将所有多个空格压缩为一个空格(如果没有为expr2输入任何内容,则可以不压缩任何空格)。

sed may have what you need to deal with your newlines, but unfortunately, I'm not that skilled with its language. sed可能具有处理换行符所需的功能,但不幸的是,我对它的语言并不了解。

Good luck, though. 祝你好运。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM