Update extract_code_go.py

This commit is contained in:
krahets 2023-02-09 04:54:52 +08:00
parent e8c78f89f0
commit 61a2c32f64

View file

@ -70,11 +70,7 @@ class ExtractCodeBlocksGo(ExtractCodeBlocksJava):
func_cls_label = func_match.group(self.func_pattern_keys.index("class")) func_cls_label = func_match.group(self.func_pattern_keys.index("class"))
func_return = func_match.group(self.func_pattern_keys.index("return")) func_return = func_match.group(self.func_pattern_keys.index("return"))
if classes: def check_func_blong_to_class(label):
# The function should not blong to any class
flag = False
for label in classes:
# Match the target class label
class_label_pattern = re.compile(f".*\*{label}\).*") class_label_pattern = re.compile(f".*\*{label}\).*")
func_return_pattern = re.compile(f".*\*{label}.*") func_return_pattern = re.compile(f".*\*{label}.*")
constructor_pattern = re.compile(f".*new.*") constructor_pattern = re.compile(f".*new.*")
@ -83,6 +79,16 @@ class ExtractCodeBlocksGo(ExtractCodeBlocksJava):
func_return_match = func_return_pattern.match(f"{func_return}") func_return_match = func_return_pattern.match(f"{func_return}")
constructor_match = constructor_pattern.match(func_label) constructor_match = constructor_pattern.match(func_label)
return class_label_match, func_return_match, constructor_match
if classes:
# The function should not blong to any class
flag = False
for label in classes:
# Match the target class label
class_label_match, func_return_match, constructor_match = \
check_func_blong_to_class(label)
if class_label_match is not None or \ if class_label_match is not None or \
func_return_match is not None and constructor_match is not None: func_return_match is not None and constructor_match is not None:
flag = True flag = True
@ -91,17 +97,11 @@ class ExtractCodeBlocksGo(ExtractCodeBlocksJava):
elif class_label: elif class_label:
# Match the target class label # Match the target class label
class_label_pattern = re.compile(f".*\*{class_label}\).*") class_label_match, func_return_match, constructor_match = \
func_return_pattern = re.compile(f".*\*{class_label}.*") check_func_blong_to_class(class_label)
constructor_pattern = re.compile(f".*new.*")
class_label_match = class_label_pattern.match(f"{func_cls_label}") if class_label_match is None and func_return_match is None or \
func_return_match = func_return_pattern.match(f"{func_return}") func_return_match is not None and constructor_match is None:
constructor_match = constructor_pattern.match(func_label)
if class_label_match is None and func_return_match is None:
continue
if func_return_match is not None and constructor_match is None:
continue continue
# Search the block from the header line # Search the block from the header line
@ -177,7 +177,8 @@ class ExtractCodeBlocksGo(ExtractCodeBlocksJava):
for func in funcs.values(): for func in funcs.values():
replace_tabs(func) replace_tabs(func)
for code_path in glob.glob("codes/go/chapter_*/array_hash_map.go"):
ext = ExtractCodeBlocksGo() # for code_path in glob.glob("codes/go/chapter_*/array_hash_map.go"):
res = ext.extract(code_path) # ext = ExtractCodeBlocksGo()
pass # res = ext.extract(code_path)
# pass