vlib: add [unsafe] tag to more functions: tos, string_from_wide, strings.Builder: write_bytes, free (#8766)
parent
4bdbb0cfa8
commit
4a0367a63c
|
@ -292,8 +292,10 @@ fn (vd VDoc) gen_html(d doc.Doc) string {
|
||||||
}
|
}
|
||||||
modules_toc_str := modules_toc.str()
|
modules_toc_str := modules_toc.str()
|
||||||
symbols_toc_str := symbols_toc.str()
|
symbols_toc_str := symbols_toc.str()
|
||||||
|
unsafe {
|
||||||
modules_toc.free()
|
modules_toc.free()
|
||||||
symbols_toc.free()
|
symbols_toc.free()
|
||||||
|
}
|
||||||
return html_content.replace('{{ title }}', d.head.name).replace('{{ head_name }}',
|
return html_content.replace('{{ title }}', d.head.name).replace('{{ head_name }}',
|
||||||
header_name).replace('{{ version }}', version).replace('{{ light_icon }}', vd.assets['light_icon']).replace('{{ dark_icon }}',
|
header_name).replace('{{ version }}', version).replace('{{ light_icon }}', vd.assets['light_icon']).replace('{{ dark_icon }}',
|
||||||
vd.assets['dark_icon']).replace('{{ menu_icon }}', vd.assets['menu_icon']).replace('{{ head_assets }}',
|
vd.assets['dark_icon']).replace('{{ menu_icon }}', vd.assets['menu_icon']).replace('{{ head_assets }}',
|
||||||
|
@ -471,7 +473,7 @@ fn doc_node_html(dn doc.DocNode, link string, head bool, include_examples bool,
|
||||||
dnw.writeln('</section>')
|
dnw.writeln('</section>')
|
||||||
dnw_str := dnw.str()
|
dnw_str := dnw.str()
|
||||||
defer {
|
defer {
|
||||||
dnw.free()
|
unsafe { dnw.free() }
|
||||||
}
|
}
|
||||||
return dnw_str
|
return dnw_str
|
||||||
}
|
}
|
||||||
|
|
|
@ -142,11 +142,11 @@ fn get_reg_value(reg_env_key voidptr, key string) ?string {
|
||||||
$if windows {
|
$if windows {
|
||||||
// query the value (shortcut the sizing step)
|
// query the value (shortcut the sizing step)
|
||||||
reg_value_size := 4095 // this is the max length (not for the registry, but for the system %PATH%)
|
reg_value_size := 4095 // this is the max length (not for the registry, but for the system %PATH%)
|
||||||
mut reg_value := &u16(malloc(reg_value_size))
|
mut reg_value := unsafe { &u16(malloc(reg_value_size)) }
|
||||||
if C.RegQueryValueEx(reg_env_key, key.to_wide(), 0, 0, reg_value, ®_value_size) != 0 {
|
if C.RegQueryValueEx(reg_env_key, key.to_wide(), 0, 0, reg_value, ®_value_size) != 0 {
|
||||||
return error('Unable to get registry value for "$key", try rerunning as an Administrator')
|
return error('Unable to get registry value for "$key", try rerunning as an Administrator')
|
||||||
}
|
}
|
||||||
return string_from_wide(reg_value)
|
return unsafe { string_from_wide(reg_value) }
|
||||||
}
|
}
|
||||||
return error('not on windows')
|
return error('not on windows')
|
||||||
}
|
}
|
||||||
|
|
|
@ -142,14 +142,14 @@ fn print_backtrace_skipping_top_frames_msvc(skipframes int) bool {
|
||||||
nframe := frames - i - 1
|
nframe := frames - i - 1
|
||||||
mut lineinfo := ''
|
mut lineinfo := ''
|
||||||
if C.SymGetLineFromAddr64(handle, frame_addr, &offset, &sline64) == 1 {
|
if C.SymGetLineFromAddr64(handle, frame_addr, &offset, &sline64) == 1 {
|
||||||
file_name := tos3(sline64.f_file_name)
|
file_name := unsafe { tos3(sline64.f_file_name) }
|
||||||
lnumber := sline64.f_line_number
|
lnumber := sline64.f_line_number
|
||||||
lineinfo = '$file_name:$lnumber'
|
lineinfo = '$file_name:$lnumber'
|
||||||
} else {
|
} else {
|
||||||
addr:
|
addr:
|
||||||
lineinfo = '?? : address = 0x${(&frame_addr):x}'
|
lineinfo = '?? : address = 0x${(&frame_addr):x}'
|
||||||
}
|
}
|
||||||
sfunc := tos3(fname)
|
sfunc := unsafe { tos3(fname) }
|
||||||
eprintln('${nframe:-2d}: ${sfunc:-25s} $lineinfo')
|
eprintln('${nframe:-2d}: ${sfunc:-25s} $lineinfo')
|
||||||
} else {
|
} else {
|
||||||
// https://docs.microsoft.com/en-us/windows/win32/debug/system-error-codes
|
// https://docs.microsoft.com/en-us/windows/win32/debug/system-error-codes
|
||||||
|
|
|
@ -51,6 +51,6 @@ pub fn (b []byte) bytestr() string {
|
||||||
fn bytes2string(b []byte) string {
|
fn bytes2string(b []byte) string {
|
||||||
mut copy := b.clone()
|
mut copy := b.clone()
|
||||||
copy << `\0`
|
copy << `\0`
|
||||||
res := tos(copy.data, copy.len-1)
|
res := unsafe { tos(copy.data, copy.len-1) }
|
||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
|
|
|
@ -73,7 +73,7 @@ pub fn vstrlen(s byteptr) int {
|
||||||
|
|
||||||
// tos converts a C string to a V string.
|
// tos converts a C string to a V string.
|
||||||
// String data is reused, not copied.
|
// String data is reused, not copied.
|
||||||
//[unsafe]
|
[unsafe]
|
||||||
pub fn tos(s byteptr, len int) string {
|
pub fn tos(s byteptr, len int) string {
|
||||||
// This should never happen.
|
// This should never happen.
|
||||||
if s == 0 {
|
if s == 0 {
|
||||||
|
@ -86,12 +86,14 @@ pub fn tos(s byteptr, len int) string {
|
||||||
}
|
}
|
||||||
|
|
||||||
// tos_clone returns a copy of `s`.
|
// tos_clone returns a copy of `s`.
|
||||||
|
[unsafe]
|
||||||
pub fn tos_clone(s byteptr) string {
|
pub fn tos_clone(s byteptr) string {
|
||||||
return tos2(s).clone()
|
return unsafe { tos2(s) }.clone()
|
||||||
}
|
}
|
||||||
|
|
||||||
// tos2 does the same as `tos`, but also calculates the length. Called by `string(bytes)` casts.
|
// tos2 does the same as `tos`, but also calculates the length. Called by `string(bytes)` casts.
|
||||||
// Used only internally.
|
// Used only internally.
|
||||||
|
[unsafe]
|
||||||
pub fn tos2(s byteptr) string {
|
pub fn tos2(s byteptr) string {
|
||||||
if s == 0 {
|
if s == 0 {
|
||||||
panic('tos2: nil string')
|
panic('tos2: nil string')
|
||||||
|
@ -103,6 +105,7 @@ pub fn tos2(s byteptr) string {
|
||||||
}
|
}
|
||||||
|
|
||||||
// tos3 does the same as `tos2`, but for char*, to avoid warnings.
|
// tos3 does the same as `tos2`, but for char*, to avoid warnings.
|
||||||
|
[unsafe]
|
||||||
pub fn tos3(s charptr) string {
|
pub fn tos3(s charptr) string {
|
||||||
if s == 0 {
|
if s == 0 {
|
||||||
panic('tos3: nil string')
|
panic('tos3: nil string')
|
||||||
|
@ -114,19 +117,21 @@ pub fn tos3(s charptr) string {
|
||||||
}
|
}
|
||||||
|
|
||||||
// tos4 does the same as `tos2`, but returns an empty string on nil ptr.
|
// tos4 does the same as `tos2`, but returns an empty string on nil ptr.
|
||||||
|
[unsafe]
|
||||||
pub fn tos4(s byteptr) string {
|
pub fn tos4(s byteptr) string {
|
||||||
if s == 0 {
|
if s == 0 {
|
||||||
return ''
|
return ''
|
||||||
}
|
}
|
||||||
return tos2(s)
|
return unsafe { tos2(s) }
|
||||||
}
|
}
|
||||||
|
|
||||||
// tos5 does the same as `tos4`, but for char*, to avoid warnings.
|
// tos5 does the same as `tos4`, but for char*, to avoid warnings.
|
||||||
|
[unsafe]
|
||||||
pub fn tos5(s charptr) string {
|
pub fn tos5(s charptr) string {
|
||||||
if s == 0 {
|
if s == 0 {
|
||||||
return ''
|
return ''
|
||||||
}
|
}
|
||||||
return tos3(s)
|
return unsafe { tos3(s) }
|
||||||
}
|
}
|
||||||
|
|
||||||
[deprecated]
|
[deprecated]
|
||||||
|
@ -207,7 +212,7 @@ pub fn (s string) cstr() byteptr {
|
||||||
// cstring_to_vstring creates a copy of cstr and turns it into a v string.
|
// cstring_to_vstring creates a copy of cstr and turns it into a v string.
|
||||||
[unsafe]
|
[unsafe]
|
||||||
pub fn cstring_to_vstring(cstr byteptr) string {
|
pub fn cstring_to_vstring(cstr byteptr) string {
|
||||||
return tos_clone(cstr)
|
return unsafe { tos_clone(cstr) }
|
||||||
}
|
}
|
||||||
|
|
||||||
// replace_once replaces the first occurence of `rep` with the string passed in `with`.
|
// replace_once replaces the first occurence of `rep` with the string passed in `with`.
|
||||||
|
|
|
@ -21,6 +21,7 @@ pub fn (_str string) to_wide() &u16 {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[unsafe]
|
||||||
pub fn string_from_wide(_wstr &u16) string {
|
pub fn string_from_wide(_wstr &u16) string {
|
||||||
$if windows {
|
$if windows {
|
||||||
unsafe {
|
unsafe {
|
||||||
|
@ -32,6 +33,7 @@ pub fn string_from_wide(_wstr &u16) string {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[unsafe]
|
||||||
pub fn string_from_wide2(_wstr &u16, len int) string {
|
pub fn string_from_wide2(_wstr &u16, len int) string {
|
||||||
$if windows {
|
$if windows {
|
||||||
unsafe {
|
unsafe {
|
||||||
|
|
|
@ -249,7 +249,7 @@ fn gg_cleanup_fn(user_data voidptr) {
|
||||||
|
|
||||||
fn gg_fail_fn(msg charptr, user_data voidptr) {
|
fn gg_fail_fn(msg charptr, user_data voidptr) {
|
||||||
mut g := unsafe { &Context(user_data) }
|
mut g := unsafe { &Context(user_data) }
|
||||||
vmsg := tos3(msg)
|
vmsg := unsafe { tos3(msg) }
|
||||||
if g.config.fail_fn != voidptr(0) {
|
if g.config.fail_fn != voidptr(0) {
|
||||||
g.config.fail_fn(vmsg, g.config.user_data)
|
g.config.fail_fn(vmsg, g.config.user_data)
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -108,7 +108,7 @@ fn decode_string(root &C.cJSON) string {
|
||||||
}
|
}
|
||||||
// println('decode string valuestring="$root.valuestring"')
|
// println('decode string valuestring="$root.valuestring"')
|
||||||
// return tos(root.valuestring, _strlen(root.valuestring))
|
// return tos(root.valuestring, _strlen(root.valuestring))
|
||||||
return tos_clone(root.valuestring) // , _strlen(root.valuestring))
|
return unsafe { tos_clone(root.valuestring) } // , _strlen(root.valuestring))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn C.cJSON_IsTrue(voidptr) bool
|
fn C.cJSON_IsTrue(voidptr) bool
|
||||||
|
|
|
@ -45,17 +45,17 @@ fn (req &Request) ssl_do(port int, method Method, host_name string, path string)
|
||||||
mut readcounter := 0
|
mut readcounter := 0
|
||||||
for {
|
for {
|
||||||
readcounter++
|
readcounter++
|
||||||
len := C.BIO_read(web, buff, bufsize)
|
len := unsafe { C.BIO_read(web, buff, bufsize) }
|
||||||
if len <= 0 {
|
if len <= 0 {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
$if debug_http ? {
|
$if debug_http ? {
|
||||||
eprintln('ssl_do, read ${readcounter:4d} | len: $len')
|
eprintln('ssl_do, read ${readcounter:4d} | len: $len')
|
||||||
eprintln('-'.repeat(20))
|
eprintln('-'.repeat(20))
|
||||||
eprintln(tos(buff, len))
|
eprintln(unsafe { tos(buff, len) })
|
||||||
eprintln('-'.repeat(20))
|
eprintln('-'.repeat(20))
|
||||||
}
|
}
|
||||||
content.write_bytes(buff, len)
|
unsafe { content.write_bytes(buff, len) }
|
||||||
}
|
}
|
||||||
if web != 0 {
|
if web != 0 {
|
||||||
C.BIO_free_all(web)
|
C.BIO_free_all(web)
|
||||||
|
|
|
@ -35,7 +35,7 @@ pub fn (mut con TcpConn) read_line() string {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
bufbp := byteptr(buf)
|
bufbp := byteptr(buf)
|
||||||
line = tos_clone(bufbp)
|
line = unsafe { tos_clone(bufbp) }
|
||||||
if eol_idx > 0 {
|
if eol_idx > 0 {
|
||||||
// At this point, we are sure that recv returned valid data,
|
// At this point, we are sure that recv returned valid data,
|
||||||
// that contains *at least* one line.
|
// that contains *at least* one line.
|
||||||
|
|
|
@ -72,7 +72,7 @@ pub fn environ() map[string]string {
|
||||||
mut estrings := C.GetEnvironmentStringsW()
|
mut estrings := C.GetEnvironmentStringsW()
|
||||||
mut eline := ''
|
mut eline := ''
|
||||||
for c := estrings; *c != 0; {
|
for c := estrings; *c != 0; {
|
||||||
eline = string_from_wide(c)
|
eline = unsafe { string_from_wide(c) }
|
||||||
eq_index := eline.index_byte(`=`)
|
eq_index := eline.index_byte(`=`)
|
||||||
if eq_index > 0 {
|
if eq_index > 0 {
|
||||||
res[eline[0..eq_index]] = eline[eq_index + 1..]
|
res[eline[0..eq_index]] = eline[eq_index + 1..]
|
||||||
|
|
|
@ -319,7 +319,7 @@ pub fn posix_get_error_msg(code int) string {
|
||||||
if ptr_text == 0 {
|
if ptr_text == 0 {
|
||||||
return ''
|
return ''
|
||||||
}
|
}
|
||||||
return tos3(ptr_text)
|
return unsafe { tos3(ptr_text) }
|
||||||
}
|
}
|
||||||
|
|
||||||
// vpclose will close a file pointer opened with `vpopen`.
|
// vpclose will close a file pointer opened with `vpopen`.
|
||||||
|
@ -463,7 +463,7 @@ pub fn rmdir(path string) ? {
|
||||||
// print_c_errno will print the current value of `C.errno`.
|
// print_c_errno will print the current value of `C.errno`.
|
||||||
fn print_c_errno() {
|
fn print_c_errno() {
|
||||||
e := C.errno
|
e := C.errno
|
||||||
se := tos_clone(byteptr(C.strerror(C.errno)))
|
se := unsafe { tos_clone(byteptr(C.strerror(C.errno))) }
|
||||||
println('errno=$e err=$se')
|
println('errno=$e err=$se')
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -498,12 +498,12 @@ pub fn get_raw_line() string {
|
||||||
} $else {
|
} $else {
|
||||||
max := size_t(0)
|
max := size_t(0)
|
||||||
mut buf := charptr(0)
|
mut buf := charptr(0)
|
||||||
nr_chars := C.getline(&buf, &max, C.stdin)
|
nr_chars := unsafe { C.getline(&buf, &max, C.stdin) }
|
||||||
// defer { unsafe{ free(buf) } }
|
// defer { unsafe{ free(buf) } }
|
||||||
if nr_chars == 0 || nr_chars == -1 {
|
if nr_chars == 0 || nr_chars == -1 {
|
||||||
return ''
|
return ''
|
||||||
}
|
}
|
||||||
return tos3(buf)
|
return unsafe { tos3(buf) }
|
||||||
// res := tos_clone(buf)
|
// res := tos_clone(buf)
|
||||||
// return res
|
// return res
|
||||||
}
|
}
|
||||||
|
@ -610,7 +610,7 @@ pub fn executable() string {
|
||||||
// https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-getfinalpathnamebyhandlew
|
// https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-getfinalpathnamebyhandlew
|
||||||
final_len := C.GetFinalPathNameByHandleW(file, final_path, size, 0)
|
final_len := C.GetFinalPathNameByHandleW(file, final_path, size, 0)
|
||||||
if final_len < size {
|
if final_len < size {
|
||||||
ret := string_from_wide2(final_path, final_len)
|
ret := unsafe { string_from_wide2(final_path, final_len) }
|
||||||
// remove '\\?\' from beginning (see link above)
|
// remove '\\?\' from beginning (see link above)
|
||||||
return ret[4..]
|
return ret[4..]
|
||||||
} else {
|
} else {
|
||||||
|
@ -619,7 +619,7 @@ pub fn executable() string {
|
||||||
}
|
}
|
||||||
C.CloseHandle(file)
|
C.CloseHandle(file)
|
||||||
}
|
}
|
||||||
return string_from_wide2(result, len)
|
return unsafe { string_from_wide2(result, len) }
|
||||||
}
|
}
|
||||||
$if macos {
|
$if macos {
|
||||||
mut result := vcalloc(max_path_len)
|
mut result := vcalloc(max_path_len)
|
||||||
|
|
|
@ -82,7 +82,7 @@ fn init_os_args(argc int, argv &&byte) []string {
|
||||||
|
|
||||||
pub fn ls(path string) ?[]string {
|
pub fn ls(path string) ?[]string {
|
||||||
mut res := []string{}
|
mut res := []string{}
|
||||||
dir := C.opendir(charptr(path.str))
|
dir := unsafe { C.opendir(charptr(path.str)) }
|
||||||
if isnil(dir) {
|
if isnil(dir) {
|
||||||
return error('ls() couldnt open dir "$path"')
|
return error('ls() couldnt open dir "$path"')
|
||||||
}
|
}
|
||||||
|
@ -99,9 +99,9 @@ pub fn ls(path string) ?[]string {
|
||||||
|| (bptr[0] == `.` && bptr[1] == `.` && bptr[2] == 0) {
|
|| (bptr[0] == `.` && bptr[1] == `.` && bptr[2] == 0) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
}
|
|
||||||
res << tos_clone(bptr)
|
res << tos_clone(bptr)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
C.closedir(dir)
|
C.closedir(dir)
|
||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
|
|
|
@ -79,7 +79,7 @@ mut:
|
||||||
fn init_os_args_wide(argc int, argv &byteptr) []string {
|
fn init_os_args_wide(argc int, argv &byteptr) []string {
|
||||||
mut args_ := []string{}
|
mut args_ := []string{}
|
||||||
for i in 0 .. argc {
|
for i in 0 .. argc {
|
||||||
args_ << string_from_wide(unsafe { &u16(argv[i]) })
|
args_ << unsafe { string_from_wide(&u16(argv[i])) }
|
||||||
}
|
}
|
||||||
return args_
|
return args_
|
||||||
}
|
}
|
||||||
|
@ -102,12 +102,12 @@ pub fn ls(path string) ?[]string {
|
||||||
// NOTE:TODO: once we have a way to convert utf16 wide character to utf8
|
// NOTE:TODO: once we have a way to convert utf16 wide character to utf8
|
||||||
// we should use FindFirstFileW and FindNextFileW
|
// we should use FindFirstFileW and FindNextFileW
|
||||||
h_find_files := C.FindFirstFile(path_files.to_wide(), voidptr(&find_file_data))
|
h_find_files := C.FindFirstFile(path_files.to_wide(), voidptr(&find_file_data))
|
||||||
first_filename := string_from_wide(&u16(find_file_data.c_file_name))
|
first_filename := unsafe { string_from_wide(&u16(find_file_data.c_file_name)) }
|
||||||
if first_filename != '.' && first_filename != '..' {
|
if first_filename != '.' && first_filename != '..' {
|
||||||
dir_files << first_filename
|
dir_files << first_filename
|
||||||
}
|
}
|
||||||
for C.FindNextFile(h_find_files, voidptr(&find_file_data)) > 0 {
|
for C.FindNextFile(h_find_files, voidptr(&find_file_data)) > 0 {
|
||||||
filename := string_from_wide(&u16(find_file_data.c_file_name))
|
filename := unsafe { string_from_wide(&u16(find_file_data.c_file_name)) }
|
||||||
if filename != '.' && filename != '..' {
|
if filename != '.' && filename != '..' {
|
||||||
dir_files << filename.clone()
|
dir_files << filename.clone()
|
||||||
}
|
}
|
||||||
|
@ -218,7 +218,7 @@ pub fn get_error_msg(code int) string {
|
||||||
if ptr_text == 0 { // compare with null
|
if ptr_text == 0 { // compare with null
|
||||||
return ''
|
return ''
|
||||||
}
|
}
|
||||||
return string_from_wide(ptr_text)
|
return unsafe { string_from_wide(ptr_text) }
|
||||||
}
|
}
|
||||||
|
|
||||||
// exec starts the specified command, waits for it to complete, and returns its output.
|
// exec starts the specified command, waits for it to complete, and returns its output.
|
||||||
|
@ -273,15 +273,17 @@ pub fn exec(cmd string) ?Result {
|
||||||
mut bytes_read := u32(0)
|
mut bytes_read := u32(0)
|
||||||
mut read_data := strings.new_builder(1024)
|
mut read_data := strings.new_builder(1024)
|
||||||
for {
|
for {
|
||||||
readfile_result := C.ReadFile(child_stdout_read, buf, 1000, voidptr(&bytes_read),
|
mut result := false
|
||||||
0)
|
unsafe {
|
||||||
|
result = C.ReadFile(child_stdout_read, buf, 1000, voidptr(&bytes_read), 0)
|
||||||
read_data.write_bytes(buf, int(bytes_read))
|
read_data.write_bytes(buf, int(bytes_read))
|
||||||
if readfile_result == false || int(bytes_read) == 0 {
|
}
|
||||||
|
if result == false || int(bytes_read) == 0 {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
soutput := read_data.str().trim_space()
|
soutput := read_data.str().trim_space()
|
||||||
read_data.free()
|
unsafe { read_data.free() }
|
||||||
exit_code := u32(0)
|
exit_code := u32(0)
|
||||||
C.WaitForSingleObject(proc_info.h_process, C.INFINITE)
|
C.WaitForSingleObject(proc_info.h_process, C.INFINITE)
|
||||||
C.GetExitCodeProcess(proc_info.h_process, voidptr(&exit_code))
|
C.GetExitCodeProcess(proc_info.h_process, voidptr(&exit_code))
|
||||||
|
|
|
@ -170,7 +170,7 @@ fn rw_callback(loop &C.picoev_loop, fd int, events int, cb_arg voidptr) {
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
r += idx
|
r += idx
|
||||||
mut s := tos(buf, r)
|
mut s := unsafe { tos(buf, r) }
|
||||||
mut out := p.out
|
mut out := p.out
|
||||||
unsafe {
|
unsafe {
|
||||||
out += fd * max_write
|
out += fd * max_write
|
||||||
|
|
|
@ -28,8 +28,10 @@ pub fn (mut r Request) parse_request(s string, max_headers int) int {
|
||||||
0
|
0
|
||||||
)
|
)
|
||||||
if pret > 0 {
|
if pret > 0 {
|
||||||
|
unsafe {
|
||||||
r.method = tos(r.method.str, int(method_len))
|
r.method = tos(r.method.str, int(method_len))
|
||||||
r.path = tos(r.path.str, int(path_len))
|
r.path = tos(r.path.str, int(path_len))
|
||||||
|
}
|
||||||
r.num_headers = num_headers
|
r.num_headers = num_headers
|
||||||
}
|
}
|
||||||
return pret
|
return pret
|
||||||
|
@ -46,9 +48,11 @@ pub fn (mut r Request) parse_request_path(s string) int {
|
||||||
&r.path, &path_len
|
&r.path, &path_len
|
||||||
)
|
)
|
||||||
if pret > 0 {
|
if pret > 0 {
|
||||||
|
unsafe {
|
||||||
r.method = tos(r.method.str, int(method_len))
|
r.method = tos(r.method.str, int(method_len))
|
||||||
r.path = tos(r.path.str, int(path_len))
|
r.path = tos(r.path.str, int(path_len))
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return pret
|
return pret
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -63,8 +67,10 @@ pub fn (mut r Request) parse_request_path_pipeline(s string) int {
|
||||||
&r.path, &path_len
|
&r.path, &path_len
|
||||||
)
|
)
|
||||||
if pret > 0 {
|
if pret > 0 {
|
||||||
|
unsafe {
|
||||||
r.method = tos(r.method.str, int(method_len))
|
r.method = tos(r.method.str, int(method_len))
|
||||||
r.path = tos(r.path.str, int(path_len))
|
r.path = tos(r.path.str, int(path_len))
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return pret
|
return pret
|
||||||
}
|
}
|
||||||
|
|
|
@ -530,7 +530,7 @@ fn (re RE) get_char_class(pc int) string {
|
||||||
buf_ptr[i] = byte(0)
|
buf_ptr[i] = byte(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
return tos_clone( buf_ptr )
|
return unsafe { tos_clone( buf_ptr ) }
|
||||||
}
|
}
|
||||||
|
|
||||||
fn (re RE) check_char_class(pc int, ch rune) bool {
|
fn (re RE) check_char_class(pc int, ch rune) bool {
|
||||||
|
|
|
@ -123,7 +123,7 @@ pub fn (db DB) q_string(query string) string {
|
||||||
stmt := &C.sqlite3_stmt(0)
|
stmt := &C.sqlite3_stmt(0)
|
||||||
C.sqlite3_prepare_v2(db.conn, query.str, -1, &stmt, 0)
|
C.sqlite3_prepare_v2(db.conn, query.str, -1, &stmt, 0)
|
||||||
C.sqlite3_step(stmt)
|
C.sqlite3_step(stmt)
|
||||||
res := tos_clone(C.sqlite3_column_text(stmt, 0))
|
res := unsafe { tos_clone(C.sqlite3_column_text(stmt, 0)) }
|
||||||
C.sqlite3_finalize(stmt)
|
C.sqlite3_finalize(stmt)
|
||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
|
@ -145,7 +145,7 @@ pub fn (db DB) exec(query string) ([]Row, int) {
|
||||||
}
|
}
|
||||||
mut row := Row{}
|
mut row := Row{}
|
||||||
for i in 0 .. nr_cols {
|
for i in 0 .. nr_cols {
|
||||||
val := tos_clone(C.sqlite3_column_text(stmt, i))
|
val := unsafe { tos_clone(C.sqlite3_column_text(stmt, i)) }
|
||||||
row.vals << val
|
row.vals << val
|
||||||
}
|
}
|
||||||
rows << row
|
rows << row
|
||||||
|
|
|
@ -183,7 +183,7 @@ pub fn f64_to_str_lnd(f f64, dec_digit int) string {
|
||||||
}
|
}
|
||||||
res[r_i] = 0
|
res[r_i] = 0
|
||||||
//println("result: [${tos(&res[0],r_i)}]")
|
//println("result: [${tos(&res[0],r_i)}]")
|
||||||
return tos(res.data, r_i)
|
return unsafe { tos(res.data, r_i) }
|
||||||
} else {
|
} else {
|
||||||
if dec_digit > 0 {
|
if dec_digit > 0 {
|
||||||
mut c := 0
|
mut c := 0
|
||||||
|
@ -194,7 +194,7 @@ pub fn f64_to_str_lnd(f f64, dec_digit int) string {
|
||||||
}
|
}
|
||||||
res[r_i] = 0
|
res[r_i] = 0
|
||||||
}
|
}
|
||||||
return tos(res.data, r_i)
|
return unsafe { tos(res.data, r_i) }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -336,5 +336,5 @@ pub fn f64_to_str_l(f f64) string {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
res[r_i] = 0
|
res[r_i] = 0
|
||||||
return tos(res.data,r_i)
|
return unsafe { tos(res.data,r_i) }
|
||||||
}
|
}
|
||||||
|
|
|
@ -27,6 +27,7 @@ pub fn new_builder(initial_size int) Builder {
|
||||||
}
|
}
|
||||||
|
|
||||||
// write_bytes appends `bytes` to the accumulated buffer
|
// write_bytes appends `bytes` to the accumulated buffer
|
||||||
|
[unsafe]
|
||||||
pub fn (mut b Builder) write_bytes(bytes byteptr, howmany int) {
|
pub fn (mut b Builder) write_bytes(bytes byteptr, howmany int) {
|
||||||
b.buf.push_many(bytes, howmany)
|
b.buf.push_many(bytes, howmany)
|
||||||
b.len += howmany
|
b.len += howmany
|
||||||
|
@ -61,8 +62,7 @@ pub fn (mut b Builder) go_back(n int) {
|
||||||
fn bytes2string(b []byte) string {
|
fn bytes2string(b []byte) string {
|
||||||
mut copy := b.clone()
|
mut copy := b.clone()
|
||||||
copy << byte(`\0`)
|
copy << byte(`\0`)
|
||||||
res := tos(copy.data, copy.len - 1)
|
return unsafe { tos(copy.data, copy.len - 1) }
|
||||||
return res
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// cut_last cuts the last `n` bytes from the buffer and returns them
|
// cut_last cuts the last `n` bytes from the buffer and returns them
|
||||||
|
@ -137,6 +137,7 @@ pub fn (mut b Builder) str() string {
|
||||||
}
|
}
|
||||||
|
|
||||||
// free - manually free the contents of the buffer
|
// free - manually free the contents of the buffer
|
||||||
|
[unsafe]
|
||||||
pub fn (mut b Builder) free() {
|
pub fn (mut b Builder) free() {
|
||||||
unsafe { free(b.buf.data) }
|
unsafe { free(b.buf.data) }
|
||||||
// b.buf = []byte{cap: b.initial_size}
|
// b.buf = []byte{cap: b.initial_size}
|
||||||
|
|
|
@ -29,9 +29,11 @@ pub const (
|
||||||
pub fn vhash() string {
|
pub fn vhash() string {
|
||||||
mut buf := [50]byte{}
|
mut buf := [50]byte{}
|
||||||
buf[0] = 0
|
buf[0] = 0
|
||||||
unsafe { C.snprintf(charptr(buf), 50, '%s', C.V_COMMIT_HASH) }
|
unsafe {
|
||||||
|
C.snprintf(charptr(buf), 50, '%s', C.V_COMMIT_HASH)
|
||||||
return tos_clone(buf)
|
return tos_clone(buf)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn full_hash() string {
|
pub fn full_hash() string {
|
||||||
build_hash := vhash()
|
build_hash := vhash()
|
||||||
|
@ -95,9 +97,11 @@ pub fn githash(should_get_from_filesystem bool) string {
|
||||||
}
|
}
|
||||||
mut buf := [50]byte{}
|
mut buf := [50]byte{}
|
||||||
buf[0] = 0
|
buf[0] = 0
|
||||||
unsafe { C.snprintf(charptr(buf), 50, '%s', C.V_CURRENT_COMMIT_HASH) }
|
unsafe {
|
||||||
|
C.snprintf(charptr(buf), 50, '%s', C.V_CURRENT_COMMIT_HASH)
|
||||||
return tos_clone(buf)
|
return tos_clone(buf)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
pub fn set_vroot_folder(vroot_path string) {
|
pub fn set_vroot_folder(vroot_path string) {
|
||||||
|
|
|
@ -96,7 +96,7 @@ pub fn (mut ctx Context) send_response_to_client(mimetype string, res string) bo
|
||||||
ctx.done = true
|
ctx.done = true
|
||||||
mut sb := strings.new_builder(1024)
|
mut sb := strings.new_builder(1024)
|
||||||
defer {
|
defer {
|
||||||
sb.free()
|
unsafe { sb.free() }
|
||||||
}
|
}
|
||||||
sb.write('HTTP/1.1 $ctx.status')
|
sb.write('HTTP/1.1 $ctx.status')
|
||||||
sb.write('\r\nContent-Type: $mimetype')
|
sb.write('\r\nContent-Type: $mimetype')
|
||||||
|
|
|
@ -10,7 +10,7 @@ fn (mut ws Client) handshake() ? {
|
||||||
seckey := base64.encode(nonce)
|
seckey := base64.encode(nonce)
|
||||||
mut sb := strings.new_builder(1024)
|
mut sb := strings.new_builder(1024)
|
||||||
defer {
|
defer {
|
||||||
sb.free()
|
unsafe { sb.free() }
|
||||||
}
|
}
|
||||||
sb.write('GET ')
|
sb.write('GET ')
|
||||||
sb.write(ws.uri.resource)
|
sb.write(ws.uri.resource)
|
||||||
|
|
|
@ -35,7 +35,7 @@ fn create_key_challenge_response(seckey string) ?string {
|
||||||
sha1buf := seckey + guid
|
sha1buf := seckey + guid
|
||||||
shabytes := sha1buf.bytes()
|
shabytes := sha1buf.bytes()
|
||||||
hash := sha1.sum(shabytes)
|
hash := sha1.sum(shabytes)
|
||||||
b64 := base64.encode(tos(hash.data, hash.len))
|
b64 := base64.encode(unsafe { tos(hash.data, hash.len) })
|
||||||
unsafe {
|
unsafe {
|
||||||
hash.free()
|
hash.free()
|
||||||
shabytes.free()
|
shabytes.free()
|
||||||
|
@ -50,5 +50,5 @@ fn get_nonce(nonce_size int) string {
|
||||||
for i in 0 .. nonce_size {
|
for i in 0 .. nonce_size {
|
||||||
nonce[i] = alphanum[rand.intn(alphanum.len)]
|
nonce[i] = alphanum[rand.intn(alphanum.len)]
|
||||||
}
|
}
|
||||||
return tos(nonce.data, nonce.len).clone()
|
return unsafe { tos(nonce.data, nonce.len) }.clone()
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue