vlib,tools: add an arrays.diff module, implement a simple platform independent tool v diff file1.txt file2.txt using it (#24428)

This commit is contained in:
kbkpbot 2025-05-08 16:09:36 +08:00 committed by GitHub
parent 1070378a46
commit 3291372c57
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 505 additions and 0 deletions

41
examples/diff.v Normal file
View file

@ -0,0 +1,41 @@
module main
import arrays.diff
import os
// diff_files generate diff for two files.
fn diff_files(src_file string, dst_file string) !string {
src := os.read_lines(src_file)!
dst := os.read_lines(dst_file)!
mut ctx := diff.diff(src, dst)
return ctx.generate_patch(colorful: true, block_header: true)
}
fn main() {
f1 := "Module{
name: 'Foo'
description: 'Awesome V module.'
version: '0.0.0'
dependencies: []
}
"
f2 := "Module{
name: 'foo'
description: 'Awesome V module.'
version: '0.1.0'
license: 'MIT'
dependencies: []
}
"
p1 := 'diff_f1.txt'
p2 := 'diff_f2.txt'
os.write_file(p1, f1)!
os.write_file(p2, f2)!
str := diff_files(p1, p2)!
println(str)
os.rm(p1)!
os.rm(p2)!
}