1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

tools: check formatting of more modules with v test-cleancode, colorize v vet output

This commit is contained in:
Delyan Angelov
2021-03-24 12:39:09 +02:00
parent 9e48826bcb
commit 9b78d7d21d
13 changed files with 128 additions and 67 deletions

View File

@@ -1,6 +1,7 @@
module gg
#include "@VROOT/vlib/gg/gg_darwin.m"
fn C.gg_get_screen_size() Size
fn C.darwin_draw_string(x int, y int, s string, cfg voidptr)

View File

@@ -28,7 +28,9 @@ pub fn (x Vec4) str() string {
// create a Vec4 function passing x,y,z as parameteres. w is set to 1
pub fn vec3(x f32, y f32, z f32) Vec4 {
return m4.Vec4{e:[x, y, z, 1]!}
return Vec4{
e: [x, y, z, 1]!
}
}
// Remove all the raw zeros
@@ -47,12 +49,14 @@ pub fn (a Vec4) clean() Vec4 {
// Set all elements to value
pub fn (mut x Vec4) copy(value f32) {
x.e = [ value, value, value, value, ]!
x.e = [value, value, value, value]!
}
// Scale the vector using a scalar
pub fn (x Vec4) mul_scalar(value f32) Vec4 {
return Vec4{ e: [ x.e[0] * value, x.e[1] * value, x.e[2] * value, x.e[3] * value, ]! }
return Vec4{
e: [x.e[0] * value, x.e[1] * value, x.e[2] * value, x.e[3] * value]!
}
}
// Reciprocal of the vector

View File

@@ -261,7 +261,9 @@ pub fn system_font_path() string {
mut fonts := ['Ubuntu-R.ttf', 'Arial.ttf', 'LiberationSans-Regular.ttf', 'NotoSans-Regular.ttf',
'FreeSans.ttf', 'DejaVuSans.ttf']
$if macos {
fonts = ['/System/Library/Fonts/SFNS.ttf', '/System/Library/Fonts/SFNSText.ttf', '/Library/Fonts/Arial.ttf']
fonts = ['/System/Library/Fonts/SFNS.ttf', '/System/Library/Fonts/SFNSText.ttf',
'/Library/Fonts/Arial.ttf',
]
for font in fonts {
if os.is_file(font) {
return font
@@ -269,8 +271,10 @@ pub fn system_font_path() string {
}
}
$if android {
xml_files := ['/system/etc/system_fonts.xml', '/system/etc/fonts.xml', '/etc/system_fonts.xml',
'/etc/fonts.xml', '/data/fonts/fonts.xml', '/etc/fallback_fonts.xml']
xml_files := ['/system/etc/system_fonts.xml', '/system/etc/fonts.xml',
'/etc/system_fonts.xml', '/etc/fonts.xml', '/data/fonts/fonts.xml',
'/etc/fallback_fonts.xml',
]
font_locations := ['/system/fonts', '/data/fonts']
for xml_file in xml_files {
if os.is_file(xml_file) && os.is_readable(xml_file) {

View File

@@ -25,7 +25,7 @@ const (
// ReadAllConfig allows options to be passed for the behaviour
// of read_all
pub struct ReadAllConfig {
reader Reader
reader Reader
read_to_end_of_stream bool
}
@@ -35,18 +35,16 @@ pub fn read_all(config ReadAllConfig) ?[]byte {
r := config.reader
read_till_eof := config.read_to_end_of_stream
mut b := []byte{len: read_all_len}
mut b := []byte{len: io.read_all_len}
mut read := 0
for {
new_read := r.read(mut b[read..]) or {
break
}
new_read := r.read(mut b[read..]) or { break }
read += new_read
if !read_till_eof && read == 0 {
break
}
if b.len == read {
unsafe { b.grow_len(read_all_grow_len) }
unsafe { b.grow_len(io.read_all_grow_len) }
}
}
return b[..read]
@@ -55,18 +53,16 @@ pub fn read_all(config ReadAllConfig) ?[]byte {
// read_any reads any available bytes from a reader
// (until the reader returns a read of 0 length)
pub fn read_any(r Reader) ?[]byte {
mut b := []byte{len: read_all_len}
mut b := []byte{len: io.read_all_len}
mut read := 0
for {
new_read := r.read(mut b[read..]) or {
break
}
new_read := r.read(mut b[read..]) or { break }
read += new_read
if new_read == 0 {
break
}
if b.len == read {
unsafe { b.grow_len(read_all_grow_len) }
unsafe { b.grow_len(io.read_all_grow_len) }
}
}
return b[..read]

View File

@@ -4,7 +4,7 @@ struct Buf {
pub:
bytes []byte
mut:
i int
i int
}
fn (mut b Buf) read(mut buf []byte) ?int {
@@ -39,7 +39,7 @@ fn test_read_all_huge() {
}
struct StringReader {
text string
text string
mut:
place int
}
@@ -58,17 +58,15 @@ const (
)
fn test_stringreader() {
text := '12345\n'.repeat(newline_count)
text := '12345\n'.repeat(io.newline_count)
mut s := StringReader{
text: text
}
mut r := new_buffered_reader({
reader: make_reader(s)
})
mut r := new_buffered_reader(reader: make_reader(s))
for i := 0; true; i++ {
if _ := r.read_line() {
} else {
assert i == newline_count
assert i == io.newline_count
break
}
}
@@ -85,24 +83,22 @@ fn test_stringreader() {
}
fn test_stringreader2() {
text := '12345\r\n'.repeat(newline_count)
text := '12345\r\n'.repeat(io.newline_count)
mut s := StringReader{
text: text
}
mut r := new_buffered_reader({
reader: make_reader(s)
})
mut r := new_buffered_reader(reader: make_reader(s))
for i := 0; true; i++ {
if _ := r.read_line() {
} else {
assert i == newline_count
assert i == io.newline_count
break
}
}
if _ := r.read_line() {
assert false
}
leftover := read_all(reader: io.make_reader(r)) or {
leftover := read_all(reader: make_reader(r)) or {
assert false
panic('bad')
}
@@ -116,9 +112,7 @@ fn test_leftover() {
mut s := StringReader{
text: text
}
mut r := new_buffered_reader({
reader: make_reader(s)
})
mut r := new_buffered_reader(reader: make_reader(s))
_ := r.read_line() or {
assert false
panic('bad')

View File

@@ -6,8 +6,10 @@ module runtime
//$if linux {
fn C.sysconf(name int) i64
//}
//$if windows {
fn C.GetCurrentProcessorNumber() u32
//}

View File

@@ -22,24 +22,32 @@ pub fn nr_jobs() int {
// is_32bit returns true if the current executable is running on a 32 bit system.
pub fn is_32bit() bool {
$if x32 { return true }
$if x32 {
return true
}
return false
}
// is_64bit returns true if the current executable is running on a 64 bit system.
pub fn is_64bit() bool {
$if x64 { return true }
$if x64 {
return true
}
return false
}
// is_little_endian returns true if the current executable is running on a little-endian system.
pub fn is_little_endian() bool {
$if little_endian { return true }
$if little_endian {
return true
}
return false
}
// is_big_endian returns true if the current executable is running on a big-endian system.
pub fn is_big_endian() bool {
$if big_endian { return true }
$if big_endian {
return true
}
return false
}

View File

@@ -10,30 +10,30 @@ fn test_nr_jobs() {
assert nr_jobs > 0
}
fn test_is_32bit(){
fn test_is_32bit() {
x := runtime.is_32bit().str()
assert x == 'true' || x == 'false'
}
fn test_is_64bit(){
fn test_is_64bit() {
x := runtime.is_64bit().str()
assert x == 'true' || x == 'false'
}
fn test_is_little_endian(){
fn test_is_little_endian() {
x := runtime.is_little_endian().str()
assert x == 'true' || x == 'false'
}
fn test_is_big_endian(){
fn test_is_big_endian() {
x := runtime.is_big_endian().str()
assert x == 'true' || x == 'false'
}
fn test_is_big_endian_different_than_is_little_endian(){
fn test_is_big_endian_different_than_is_little_endian() {
assert runtime.is_big_endian() != runtime.is_little_endian()
}
fn test_is_32bit_different_than_is_64bit(){
fn test_is_32bit_different_than_is_64bit() {
assert runtime.is_32bit() != runtime.is_64bit()
}

View File

@@ -4,8 +4,9 @@ import os
[typedef]
struct C.SYSTEM_INFO {
dwNumberOfProcessors u32
dwNumberOfProcessors u32
}
fn C.GetSystemInfo(&C.SYSTEM_INFO)
// nr_cpus returns the number of virtual CPU cores found on the system.

View File

@@ -556,3 +556,25 @@ pub fn should_bundle_module(mod string) bool {
return mod in util.bundle_modules
|| (mod.contains('.') && mod.all_before('.') in util.bundle_modules)
}
// find_all_v_files - given a list of files/folders, finds all .v/.vsh files
// if some of the files/folders on the list does not exist, or a file is not
// a .v or .vsh file, returns an error instead.
pub fn find_all_v_files(roots []string) ?[]string {
mut files := []string{}
for file in roots {
if os.is_dir(file) {
files << os.walk_ext(file, '.v')
files << os.walk_ext(file, '.vsh')
continue
}
if !file.ends_with('.v') && !file.ends_with('.vv') && !file.ends_with('.vsh') {
return error('v fmt can only be used on .v files.\nOffending file: "$file"')
}
if !os.exists(file) {
return error('"$file" does not exist')
}
files << file
}
return files
}