mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
bin2v: make output compliant to fmt -verify (#6763)
This commit is contained in:
parent
3e5871ffb3
commit
2258ab17a4
@ -35,10 +35,9 @@ fn (context Context) header() string {
|
|||||||
}
|
}
|
||||||
soptions := options.join(' ')
|
soptions := options.join(' ')
|
||||||
header_s += '// File generated by:\n'
|
header_s += '// File generated by:\n'
|
||||||
header_s += '// v bin2v $allfiles $soptions\n'
|
header_s += '// v bin2v $allfiles $soptions\n'
|
||||||
header_s += '// Please, do not edit this file.\n'
|
header_s += '// Please, do not edit this file.\n'
|
||||||
header_s += '// Your changes may be overwritten.\n'
|
header_s += '// Your changes may be overwritten.\n'
|
||||||
header_s += '\n'
|
|
||||||
header_s += 'const (\n'
|
header_s += 'const (\n'
|
||||||
return header_s
|
return header_s
|
||||||
}
|
}
|
||||||
@ -47,28 +46,51 @@ fn (context Context) footer() string {
|
|||||||
return ')\n'
|
return ')\n'
|
||||||
}
|
}
|
||||||
|
|
||||||
fn (context Context) file2v(file string) string {
|
fn (context Context) file2v(bname string, fbytes []byte, bn_max int) string {
|
||||||
mut sb := strings.new_builder(1000)
|
mut sb := strings.new_builder(1000)
|
||||||
|
bn_diff_len := bn_max - bname.len
|
||||||
|
sb.write('\t${bname}_len' + ' '.repeat(bn_diff_len - 4) + ' = $fbytes.len\n')
|
||||||
|
mut last_len := sb.len
|
||||||
|
fbyte := fbytes[0]
|
||||||
|
sb.write('\t$bname' + ' '.repeat(bn_diff_len) + ' = [byte($fbyte), ')
|
||||||
|
for i := 1; i < fbytes.len; i++ {
|
||||||
|
b := int(fbytes[i]).str()
|
||||||
|
sb_diff_len := sb.len - last_len
|
||||||
|
if i < 30 && sb_diff_len > 86 {
|
||||||
|
sb.write('$b,\n\t\t')
|
||||||
|
last_len = sb.len
|
||||||
|
} else if sb_diff_len > 88 && 92 - sb_diff_len < b.len {
|
||||||
|
sb.write('$b,\n\t\t')
|
||||||
|
last_len = sb.len
|
||||||
|
} else if i == fbytes.len - 1 {
|
||||||
|
sb.write(b)
|
||||||
|
} else {
|
||||||
|
sb.write('$b, ')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
sb.write(']!!\n')
|
||||||
|
return sb.str()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn (context Context) bname_and_bytes(file string) ?(string, []byte) {
|
||||||
fname := os.file_name(file)
|
fname := os.file_name(file)
|
||||||
fname_no_dots := fname.replace('.', '_')
|
fname_no_dots := fname.replace('.', '_')
|
||||||
byte_name := '$context.prefix$fname_no_dots'.to_lower()
|
byte_name := '$context.prefix$fname_no_dots'.to_lower()
|
||||||
fbytes := os.read_bytes(file) or {
|
fbytes := os.read_bytes(file) or {
|
||||||
eprintln('Error: $err')
|
return error('Error: $err')
|
||||||
return ''
|
|
||||||
}
|
}
|
||||||
fbyte := fbytes[0]
|
return byte_name, fbytes
|
||||||
sb.write(' ${byte_name}_len = $fbytes.len\n')
|
}
|
||||||
sb.write(' $byte_name = [ byte($fbyte), \n ')
|
|
||||||
for i := 1; i < fbytes.len; i++ {
|
fn (context Context) max_bname_len(bnames []string) int {
|
||||||
b := int(fbytes[i]).str()
|
mut max := 0
|
||||||
sb.write('${b:4s}, ')
|
for n in bnames {
|
||||||
if 0 == i % 16 {
|
if n.len > max {
|
||||||
sb.write('\n ')
|
max = n.len
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
sb.write('\n]!!\n')
|
// Add 4 to max due to "_len" suffix
|
||||||
sb.write('\n')
|
return max + 4
|
||||||
return sb.str()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
@ -99,19 +121,28 @@ fn main() {
|
|||||||
if !context.write_file.ends_with('.v') {
|
if !context.write_file.ends_with('.v') {
|
||||||
context.write_file += '.v'
|
context.write_file += '.v'
|
||||||
}
|
}
|
||||||
|
mut file_byte_map := map[string][]byte{}
|
||||||
|
for file in real_files {
|
||||||
|
bname, fbytes := context.bname_and_bytes(file) or {
|
||||||
|
eprintln(err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
file_byte_map[bname] = fbytes
|
||||||
|
}
|
||||||
|
max_bname := context.max_bname_len(file_byte_map.keys())
|
||||||
if context.write_file.len > 0 {
|
if context.write_file.len > 0 {
|
||||||
mut out_file := os.create(context.write_file) or {
|
mut out_file := os.create(context.write_file) or {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
out_file.write(context.header())
|
out_file.write(context.header())
|
||||||
for file in real_files {
|
for bname, fbytes in file_byte_map {
|
||||||
out_file.write(context.file2v(file))
|
out_file.write(context.file2v(bname, fbytes, max_bname))
|
||||||
}
|
}
|
||||||
out_file.write(context.footer())
|
out_file.write(context.footer())
|
||||||
} else {
|
} else {
|
||||||
println(context.header())
|
println(context.header())
|
||||||
for file in real_files {
|
for bname, fbytes in file_byte_map {
|
||||||
println(context.file2v(file))
|
println(context.file2v(bname, fbytes, max_bname))
|
||||||
}
|
}
|
||||||
println(context.footer())
|
println(context.footer())
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user