Advertisement
Guest User

Untitled

a guest
Jul 19th, 2019
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.33 KB | None | 0 0
  1. @classmethod
  2. def apply(cls, dest, diff):
  3. """
  4. Apply the given patch string diff to the given string dest
  5. :param dest: Destination string
  6. :param diff: Patch string
  7. :return:
  8. """
  9. if diff is None:
  10. return
  11. if not isinstance(dest, MutableString):
  12. raise ValueError
  13. if not dest:
  14. return
  15.  
  16. pos = len(dest) - 1
  17. try:
  18. for i in range(int(len(diff) / 2)):
  19. cmd = diff[2 * i]
  20. param = diff[2 * i + 1]
  21. par_num = ord(param) - ord('a') + 1
  22. if cmd == '-':
  23. pos -= (par_num - 1)
  24. elif cmd == 'R':
  25. cls.__check_index(dest, pos)
  26. dest[pos] = param
  27. elif cmd == 'D':
  28. o = pos
  29. pos -= (par_num - 1)
  30. cls.__check_index(dest, pos)
  31. dest[pos:o + 1] = ''
  32. elif cmd == 'I':
  33. pos += 1
  34. cls.__check_offset(dest, pos)
  35. dest.insert(pos, param)
  36. pos -= 1
  37. except IndexError:
  38. # swallow, same thing happens in original Java version
  39. pass
  40.  
  41. @classmethod
  42. def __check_index(cls, s, index):
  43. if index < 0 or index >= len(s):
  44. raise IndexError
  45.  
  46. @classmethod
  47. def __check_offset(cls, s, offset):
  48. if offset < 0 or offset > len(s):
  49. raise IndexError
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement