Advertisement
nna42799

Untitled

Sep 25th, 2024
18
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.75 KB | None | 0 0
  1.  
  2. debug = False
  3.  
  4.  
  5. class DiffData:
  6.     def __init__(self,start_line ,origin_count, patched_count):
  7.         self.start_line = start_line
  8.         self.origin_count = origin_count
  9.         self.patched_count = patched_count
  10.         self.patched_contents = []
  11.    
  12.     def __repr__(self):
  13.         return f"Diff: start:{self.start_line} prev: {self.origin_count} line(s) --> patched: {self.patched_count} line(s)"
  14.    
  15.     def apply_to_file_lines(self, file_lines, offset=0):
  16.         # first part of original file
  17.         new_file_lines = file_lines[0 : self.start_line-1+offset]
  18.         # diff patched part
  19.         new_file_lines.extend(self.patched_contents)
  20.         # second part of the original file
  21.         new_file_lines += file_lines[self.start_line-1+offset + self.origin_count : ]
  22.  
  23.         return new_file_lines
  24.  
  25.  
  26.  
  27. def input_diff_block(start_line):
  28.     meta_info_str_list = start_line.split(' ')
  29.  
  30.     diff_start_info_str = meta_info_str_list[1].split(',')
  31.     diff_end_info_str = meta_info_str_list[2].split(',')
  32.  
  33.     diff_start_line = int(diff_start_info_str[0][1:])
  34.     original_line_count = int(diff_start_info_str[1])
  35.     patched_line_count = int(diff_end_info_str[1])
  36.  
  37.     # create diff data
  38.     diff_data = DiffData(start_line = diff_start_line, origin_count=original_line_count, patched_count=patched_line_count)
  39.     if debug:
  40.         print(diff_data)
  41.    
  42.     origin_received = 0
  43.     patched_received = 0
  44.  
  45.     while origin_received < diff_data.origin_count or patched_received < diff_data.patched_count:
  46.         if debug:
  47.             print(f'Expecting more diff info: {origin_received} < {diff_data.origin_count} or {patched_received} < {diff_data.patched_count}')
  48.  
  49.         new_line = input()
  50.         new_line_type = new_line[0]
  51.         new_line_content = new_line[1:]
  52.  
  53.         if new_line_type!='+' and new_line_type !=' ' and new_line_type!='#':
  54.             origin_received += 1
  55.         elif new_line_type == ' ':
  56.             origin_received += 1
  57.             patched_received += 1
  58.             diff_data.patched_contents.append(new_line_content)
  59.         elif new_line_type == '+':
  60.             patched_received += 1
  61.             diff_data.patched_contents.append(new_line_content)
  62.         else:
  63.             pass
  64.    
  65.     return diff_data
  66.  
  67.  
  68.  
  69. def input_file_and_diff():
  70.     """
  71.    Currently only support input one diff block
  72.    """
  73.     file_lines = []
  74.     diff_datas = []
  75.  
  76.     file_total_lines = int(input())
  77.  
  78.     input_file_lines = 0
  79.  
  80.     while True:
  81.         try:
  82.             current_line = input()
  83.             # maybe some comment of diff
  84.             if current_line.startswith("#"):
  85.                 continue
  86.         except:
  87.             # expect raise eof when input end
  88.             break
  89.        
  90.         # meets a new line of diff block
  91.         if current_line.startswith('@@'):
  92.             diff_datas.append(input_diff_block(current_line))
  93.             continue
  94.        
  95.         # just a new file lines
  96.         input_file_lines += 1
  97.         if input_file_lines > file_total_lines:
  98.             continue
  99.         file_lines.append(current_line)
  100.    
  101.     return file_lines, diff_datas
  102.  
  103.  
  104. def main():
  105.     file_lines, diff_datas = input_file_and_diff()
  106.  
  107.     if debug:
  108.         print('Input Statistics')
  109.         print(f'file_lines:')
  110.         for i in file_lines:
  111.             print(i)
  112.         print('diff_data')
  113.         print(diff_datas)
  114.  
  115.     # sort diff data by start point
  116.     diff_datas.sort(key=lambda diff_data: diff_data.start_line)
  117.  
  118.     # apply diff to file lines
  119.     offset = 0
  120.     for diff_data in diff_datas:
  121.         file_lines = diff_data.apply_to_file_lines(file_lines, offset=offset)
  122.         offset += (diff_data.patched_count - diff_data.origin_count)
  123.  
  124.     for i in file_lines:
  125.         print(i)
  126.  
  127. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement