os: use arr.trim() in read_bytes, instead of a arr[0..x].clone()

pull/10394/head
Delyan Angelov 2021-06-08 16:52:20 +03:00
parent 40ac7d4892
commit d820f2da6f
No known key found for this signature in database
GPG Key ID: 66886C0F12D595ED
1 changed files with 11 additions and 12 deletions

View File

@ -86,9 +86,8 @@ pub fn read_bytes(path string) ?[]byte {
return error('fread failed') return error('fread failed')
} }
C.fclose(fp) C.fclose(fp)
fres := res[0..nr_read_elements * fsize].clone() res.trim(nr_read_elements * fsize)
unsafe { res.free() } return res
return fres
} }
// read_file reads the file in `path` and returns the contents. // read_file reads the file in `path` and returns the contents.
@ -156,6 +155,10 @@ pub fn truncate(path string, len u64) ? {
} }
} }
fn eprintln_unknown_file_size() {
eprintln('os.file_size() Cannot determine file-size: ' + posix_get_error_msg(C.errno))
}
// file_size returns the size of the file located in `path`. // file_size returns the size of the file located in `path`.
// If an error occurs it returns 0. // If an error occurs it returns 0.
// Note that use of this on symbolic links on Windows returns always 0. // Note that use of this on symbolic links on Windows returns always 0.
@ -166,15 +169,13 @@ pub fn file_size(path string) u64 {
$if windows { $if windows {
mut swin := C.__stat64{} mut swin := C.__stat64{}
if C._wstat64(&char(path.to_wide()), voidptr(&swin)) != 0 { if C._wstat64(&char(path.to_wide()), voidptr(&swin)) != 0 {
eprintln('os.file_size() Cannot determine file-size: ' + eprintln_unknown_file_size()
posix_get_error_msg(C.errno))
return 0 return 0
} }
return swin.st_size return swin.st_size
} $else { } $else {
if C.stat(&char(path.str), &s) != 0 { if C.stat(&char(path.str), &s) != 0 {
eprintln('os.file_size() Cannot determine file-size: ' + eprintln_unknown_file_size()
posix_get_error_msg(C.errno))
return 0 return 0
} }
return u64(s.st_size) return u64(s.st_size)
@ -186,15 +187,13 @@ pub fn file_size(path string) u64 {
} }
$if windows { $if windows {
if C._wstat(path.to_wide(), voidptr(&s)) != 0 { if C._wstat(path.to_wide(), voidptr(&s)) != 0 {
eprintln('os.file_size() Cannot determine file-size: ' + eprintln_unknown_file_size()
posix_get_error_msg(C.errno))
return 0 return 0
} }
return u64(s.st_size) return u64(s.st_size)
} $else { } $else {
if C.stat(&char(path.str), &s) != 0 { if C.stat(&char(path.str), &s) != 0 {
eprintln('os.file_size() Cannot determine file-size: ' + eprintln_unknown_file_size()
posix_get_error_msg(C.errno))
return 0 return 0
} }
return u64(s.st_size) return u64(s.st_size)