我一直在编写一个代码,在C语言的'#include'语句的末尾插入换行符('\ n')。
离) [BEFORE]
#include <stdio.h> #include <stdlib.h>
[后]
#include <stdio.h>
#include <stdlib.h>
所以我为此做了一个功能。可变的'raw'代表原始数据的字符串缓冲区,而idx是一个索引变量,它通过字符串变量'raw'移动。
def insert(raw, idx, new): # inserts string(new) to the original string(raw). Location of insertion can be specified by variable idx.
return raw[:idx] + new + raw[idx:]
使用上面的函数,我也写了这样的代码:
includeFlag1 = False # be ready to detect '<' of the include statement.
includeFlag2 = False # Turned on when the '<' is detected. Turned off when the include statement ends.
elif raw[idx:idx+8] == '#include' : # include statement is detected.
includeFlag1 = True
elif raw[idx] == '<' and includeFlag1 == True:
includeFlag2 = True
elif raw[idx] == '>' and includeFlag1 == True:
raw = insert(raw, idx+1, '\n') # adds the new-line charcter at the end of the include statement.
includeFlag2 = False # the include statement has just ended.
includeFlag1 = False
idx = idx + 1
真正的问题是,当我运行上面的代码时,新行字符会覆盖'#'字符,这是'#include&lt;&gt;'的起点。声明。 有关更多信息,请查看下面的调试日志。 日志记录如下: raw [idx] [icdFlag] includeFlag1 includeFlag2
=============================================== ====================
# [icdFlag] False False
i [icdFlag] True False
n [icdFlag] True False
c [icdFlag] True False
l [icdFlag] True False
u [icdFlag] True False
d [icdFlag] True False
e [icdFlag] True False
[icdFlag] True False
< [icdFlag] True False
s [icdFlag] True True
t [icdFlag] True True
d [icdFlag] True True
i [icdFlag] True True
o [icdFlag] True True
. [icdFlag] True True
h [icdFlag] True True
> [icdFlag] True True
[icdFlag] False False
i [icdFlag] False False
n [icdFlag] False False
c [icdFlag] False False
l [icdFlag] False False
u [icdFlag] False False
d [icdFlag] False False
e [icdFlag] False False
[icdFlag] False False
< [icdFlag] False False
s [icdFlag] False False
t [icdFlag] False False
d [icdFlag] False False
l [icdFlag] False False
i [icdFlag] False False
b [icdFlag] False False
. [icdFlag] False False
h [icdFlag] False False
> [icdFlag] False False
# [icdFlag] False False
i [icdFlag] True False
n [icdFlag] True False
c [icdFlag] True False
l [icdFlag] True False
u [icdFlag] True False
d [icdFlag] True False
e [icdFlag] True False
[icdFlag] True False
< [icdFlag] True False
s [icdFlag] True True
t [icdFlag] True True
r [icdFlag] True True
i [icdFlag] True True
n [icdFlag] True True
g [icdFlag] True True
. [icdFlag] True True
h [icdFlag] True True
> [icdFlag] True True
[icdFlag] False False
i [icdFlag] False False
n [icdFlag] False False
c [icdFlag] False False
l [icdFlag] False False
u [icdFlag] False False
d [icdFlag] False False
e [icdFlag] False False
[icdFlag] False False
< [icdFlag] False False
t [icdFlag] False False
i [icdFlag] False False
m [icdFlag] False False
e [icdFlag] False False
. [icdFlag] False False
h [icdFlag] False False
> [icdFlag] False False
v [icdFlag] False False
o [icdFlag] False False
i [icdFlag] False False
d [icdFlag] False False
[icdFlag] False False
g [icdFlag] False False
e [icdFlag] False False
t [icdFlag] False False
_ [icdFlag] False False
u [icdFlag] False False
s [icdFlag] False False
e [icdFlag] False False
r [icdFlag] False False
_ [icdFlag] False False
i [icdFlag] False False
n [icdFlag] False False
f [icdFlag] False False
o [icdFlag] False False
( [icdFlag] False False
c [icdFlag] False False
h [icdFlag] False False
a [icdFlag] False False
r [icdFlag] False False
[icdFlag] False False
当然,我已经检查过原始C源代码是否没有错过'#'字符,但事实并非如此。我想知道为什么会发生这种事情。
答案 0 :(得分:1)
我可能只是把它分成几行并用“包含”分开。
processedLines = [] # put into an array
lines = raw.split('\n')
for line in lines:
if "#include" in line:
includes = line.split("#include ") # "#include <foo.h> #include <bar.h>" becomes ["", "<foo.h>", "<bar.h>"]
for include in includes[1:] # skip the blank at the beginning
processedLines.append("#include " + include)
else
processedLines.append(line)
答案 1 :(得分:0)
也许这个?
#!/usr/bin/env python
my_code = """
# Other stuff
#include <stdio.h> #include <stdlib.h> #include <other.h>
# Other stuff
"""
import os
import re
for line in my_code.splitlines():
include_list = re.findall(r"include <\w+\.h>", line)
if include_list:
print(os.linesep.join(include_list))
else:
print(line)