Opcode
#struct OpcodeOne step of the edit script: what happened to a[a_start:a_stop] compared with b[b_start:b_stop]. The range of the second input is empty for a delete, and the range of the first input is empty for an insert.
| Name | Type | Description |
|---|---|---|
tag | int = 0 | One of Equal, Replace, Delete and Insert. |
a_start | int = 0 | Index of the first line of the first input this step covers. |
a_stop | int = 0 | Index after the last line of the first input this step covers. |
b_start | int = 0 | Index of the first line of the second input this step covers. |
b_stop | int = 0 | Index after the last line of the second input this step covers. |
import text.diff
import str
func name(tag: int): str:
switch tag:
diff.Equal:
ret "equal"
end
diff.Replace:
ret "replace"
end
diff.Delete:
ret "delete"
end
else:
ret "insert"
end
end
end
func main:
a := diff.Lines("one\ntwo\nthree\nfour\n")
b := diff.Lines("one\n2\nthree\nfour\nfive\n")
defer a.Free()
defer b.Free()
ops := diff.Opcodes(a, b)
defer ops.Free()
each op in ops:
println name(op.tag),
"a[" + str.FromInt(op.a_start as i64) + ":" + str.FromInt(op.a_stop as i64) + "]",
"b[" + str.FromInt(op.b_start as i64) + ":" + str.FromInt(op.b_stop as i64) + "]"
end
groups := diff.GroupedOpcodes(a, b, 0)
println "hunks:", groups.Len()
each g in groups:
g.Free()
end
groups.Free()
endequal a[0:1] b[0:1] replace a[1:2] b[1:2] equal a[2:4] b[2:4] insert a[4:4] b[4:5] hunks: 2