如何用它替换功能 [英] how to replace a function by it content

查看:88
本文介绍了如何用它替换功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何替换所有出现的

check_free_set_to_null(&pointer)

if (pointer)
{
    free(pointer)
    printf(pointer have been freed)
}
else
{
    printf(pointer couldnt had been freed)
    return (1)
}

// maybe using some sort of

{1} = check_free_set_to_null(CONTENT)

if ({1})
{
    free({1})
    printf({1} have been freed)
}
else
{
    printf({1} couldnt had been freed)
    return (1)
}

我该怎么做? (请注意,这并不是必须替换的内容,仅是示例)我不希望编译器将其解释为多行,而是希望修改文件.

how could I do that ? (take note thats its not exactly what that have to be replaced, its just an example) Im not looking for the compiler to interpret it as multiple line, im looking for modifying the file.

推荐答案

基本上与您所说的完全一样,除了...

Basically exactly how you said, except...

这是其中一个正确答案可能是您要执行的愚蠢事情的问题之一,但请确实告诉我们您要解决的真正问题是什么,因为这几乎肯定不是正确的方法."

This is one of those questions where the right answer is probably "Here's how to do the stupid thing you're asking to do, but PLEASE REALLY PLEASE tell us what your real problem is, that you're trying to solve, because this almost certainly isn't the right way to do it."

首先,搜索字符串应为(这是针对vim的,因为我看到了标记vim.如果您想使用其他替换正则表达式来实现,请告诉我们.Perl,php,sed,awk, bash命令行,有什么?):

First, the search string should be (this is for vim, because I see the tag vim. If you want to do it using some other replacement regex thing, let us know which. Perl, php, sed, awk, the bash commandline, something?):

:%s/\bcheck_free_set_to_null\(([^()]+)\)/ replacement text goes here /g

假设(可能是错误地)函数内没有花括号-如果是这种情况,那么您将涉足递归正则表达式领域,在这种情况下,您肯定是在做错误的事情

That presumes (possibly incorrectly) that there are never any braces inside the function - if that might be the case, then you are venturing into recursive regular expression territory, and in that case, you are definitely, definitely doing the wrong thing.

您的替换字符串应该是您的方法将包含的任何内容-在您的示例中,它将是:

Your replacement string should be whatever your method will contain - in your example, it'd be:

:%s/\bcheck_free_set_to_null(\([^()]+\))/if (\1)\n{\n    free(\1)\n    printf("freed")\n}\nelse\n{\n    printf("could not free")\n    return (\1)\n}/g

在这里,我们使用\ n作为行尾,因为您使用的是vi,所以您可能使用的是unix,这就是它们的行尾的方式.

Here we're using \n for line endings, because you're using vi, so you're probably on some kind of unix, and that's how they have their line endings.

现在,我们可以在这里改进一件事.缩进.

Now, there's one thing we could improve here. Indentation.

:%s/\(\s+\)\(.*\)\bcheck_free_set_to_null(\([^()]+\))/\1\2if (\3)\n\1{\n\1    free(\3)\n\1    printf("freed")\n\1}\nelse\n{\n\1    printf("could not free")\n\1    return (\3)\n\1}/g

在这里,我将缩进插入到\1中,将行上的所有其他内容插入到\2中,并将方法参数放入\3中.可能会让您更漂亮.

Here I grab the indentation into \1, everything else on the line into \2, and the method parameter into \3. Which might let you make it a bit prettier.

但是再次:您在这里尝试做的任何事情都可能是错误的事情.告诉我们真正的问题是什么!不要这样解决!这样不好!这种方式使人感到疲惫不堪!而且鼬鼠甚至都不会为您吃掉蟑螂!

But again: whatever you're trying to do here is probably the wrong thing to do. Tell us what the REAL problem is! Don't fix it this way! This way is BAD! THIS WAY HAS WEASELS AND ROACHES IN IT! AND THE WEASELS WON'T EVEN EAT THE ROACHES FOR YOU!

这篇关于如何用它替换功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆