给定一个C++具有许多功能定义的文件,如何使用Python获取特定函数的启动和结束索引?

发布于 2025-02-14 00:58:55 字数 967 浏览 5 评论 0原文

目的是使用python对整个函数void test_func评论 。

file = open(path_of_file)
data = file.readlines()
for item in data:
    if item.find('void test_func') != -1:
        point = data.index(item)
        data.insert(point, '/*')
        stack = []
        for i in data[point+1:]:
            if i.find('{') != -1:
               stack.append('{')
            elif i.find('}') != -1:
               stack.pop()
            if len(stack) == 0:
               point1= data.index(i)
               data.insert(point1+1,'*/')

使用find()方法,我可以在迭代行时找到该功能的起始索引。我试图使用平衡的括号方法来达到函数的末尾,因此,当我的堆栈为空时,我将到达test_func的末尾。

在此示例中,这不起作用:

void test_func(arguments,\
arguments)
{ 

它在行之后插入*/

/*void test_func(arguments,\
*/arguments)
{

The purpose is to comment the entire function void test_func with many nested {} using Python.

file = open(path_of_file)
data = file.readlines()
for item in data:
    if item.find('void test_func') != -1:
        point = data.index(item)
        data.insert(point, '/*')
        stack = []
        for i in data[point+1:]:
            if i.find('{') != -1:
               stack.append('{')
            elif i.find('}') != -1:
               stack.pop()
            if len(stack) == 0:
               point1= data.index(i)
               data.insert(point1+1,'*/')

Using the find() method I can find the starting index of the function while iterating over the lines. I was trying to use the balanced parenthesis method, to reach the end of the function, So when my stack is empty I will reach the end of the test_func.

This is not working in this example:

void test_func(arguments,\
arguments)
{ 

It is inserting */ just after the line:

/*void test_func(arguments,\
*/arguments)
{

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

ゞ记忆︶ㄣ 2025-02-21 00:58:55

假设“} \ n”行仅发生在函数的末尾,并且总是在函数结束时发生,则需要类似的东西:

in_func = False
for line in file.readlines():
    line = line.strip('\r\n') # remove line breaks since print will add them
    if in_func:
        print("// " + line)
        if line == "}":
            in_func = False
    elif line.startswith('void test_func('): # paren required to not match test_func2
        print("// " + line)
        in_func = True
    else:
        print(line)

Assuming that a "}\n" line only happens at the end of a function and always happens at the end of a function, you want something like:

in_func = False
for line in file.readlines():
    line = line.strip('\r\n') # remove line breaks since print will add them
    if in_func:
        print("// " + line)
        if line == "}":
            in_func = False
    elif line.startswith('void test_func('): # paren required to not match test_func2
        print("// " + line)
        in_func = True
    else:
        print(line)
朮生 2025-02-21 00:58:55

假设两个/对和{/}对配对,您可以查找平衡参数列表的,然后}平衡了身体的第一个{

我们要确保<代码> void还捕获了Test_func(){} 等,因此我将支票删除到本地功能中,因此我们开始在同一条线上查看下一对字符。

file = open(path_of_file)
data = file.readlines()

start = None
params = None
body = None
open = 0
close = 0

for index, line in enumerate(data):

    def balanced(o, c):
        open += line.count(o)
        close += line.count(c)
        return open > 0 and open = close
    
    def checkBody():
        if balanced('{', '}'):
            body = index
    
    def checkParams():
        if balanced('(', ')'):
            params = index
            open = 0
            close = 0
            checkBody()
    
    def checkStart():
        if line.find('void test_func') != -1:
            start = index
            checkParams()

    if start is None:
        checkStart()
    elif params is None:
        checkParams()
    elif body is None:
        checkBody()

if start is not None and body is not None:
    data.insert(start, '/*')
    data.insert(body + 1, '*/')

Assuming that both ( / ) pairs and { / } pairs are balanced, you can look for the ) that balances the parameter list's ( and then the } that balances the body's first {.

We want to ensure that void test_func() {} etc is also caught, so I've pulled out the checks into local functions, so we start looking on the same line for the next pair of characters.

file = open(path_of_file)
data = file.readlines()

start = None
params = None
body = None
open = 0
close = 0

for index, line in enumerate(data):

    def balanced(o, c):
        open += line.count(o)
        close += line.count(c)
        return open > 0 and open = close
    
    def checkBody():
        if balanced('{', '}'):
            body = index
    
    def checkParams():
        if balanced('(', ')'):
            params = index
            open = 0
            close = 0
            checkBody()
    
    def checkStart():
        if line.find('void test_func') != -1:
            start = index
            checkParams()

    if start is None:
        checkStart()
    elif params is None:
        checkParams()
    elif body is None:
        checkBody()

if start is not None and body is not None:
    data.insert(start, '/*')
    data.insert(body + 1, '*/')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文