From 34aa67b1e818625609030425510c3c4471097968 Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Mon, 5 Apr 2021 08:02:37 +0300 Subject: [PATCH] ci: fix compilation and formatting of sqlite example in docs.md --- doc/docs.md | 18 +++++++++--------- vlib/builtin/string.v | 4 ++-- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/doc/docs.md b/doc/docs.md index f2d468c197..30fd138d1a 100644 --- a/doc/docs.md +++ b/doc/docs.md @@ -3306,26 +3306,26 @@ struct C.sqlite3 { struct C.sqlite3_stmt { } -type FnSqlite3Callback = fn (voidptr, int, &charptr, &charptr) int +type FnSqlite3Callback = fn (voidptr, int, &&char, &&char) int -fn C.sqlite3_open(charptr, &&C.sqlite3) int +fn C.sqlite3_open(&char, &&C.sqlite3) int fn C.sqlite3_close(&C.sqlite3) int fn C.sqlite3_column_int(stmt &C.sqlite3_stmt, n int) int // ... you can also just define the type of parameter and leave out the C. prefix -fn C.sqlite3_prepare_v2(&C.sqlite3, charptr, int, &&C.sqlite3_stmt, &charptr) int +fn C.sqlite3_prepare_v2(&C.sqlite3, &char, int, &&C.sqlite3_stmt, &&char) int fn C.sqlite3_step(&C.sqlite3_stmt) fn C.sqlite3_finalize(&C.sqlite3_stmt) -fn C.sqlite3_exec(db &C.sqlite3, sql charptr, cb FnSqlite3Callback, cb_arg voidptr, emsg &charptr) int +fn C.sqlite3_exec(db &C.sqlite3, sql &char, cb FnSqlite3Callback, cb_arg voidptr, emsg &&char) int fn C.sqlite3_free(voidptr) -fn my_callback(arg voidptr, howmany int, cvalues &charptr, cnames &charptr) int { +fn my_callback(arg voidptr, howmany int, cvalues &&char, cnames &&char) int { unsafe { for i in 0 .. howmany { print('| ${cstring_to_vstring(cnames[i])}: ${cstring_to_vstring(cvalues[i]):20} ') @@ -3344,17 +3344,17 @@ fn main() { stmt := &C.sqlite3_stmt(0) // NB: you can also use the `.str` field of a V string, // to get its C style zero terminated representation - C.sqlite3_prepare_v2(db, query.str, -1, &stmt, 0) + C.sqlite3_prepare_v2(db, &char(query.str), -1, &stmt, 0) C.sqlite3_step(stmt) nr_users := C.sqlite3_column_int(stmt, 0) C.sqlite3_finalize(stmt) println('There are $nr_users users in the database.') // - error_msg := charptr(0) + error_msg := &char(0) query_all_users := 'select * from users' - rc := C.sqlite3_exec(db, query_all_users.str, my_callback, 7, &error_msg) + rc := C.sqlite3_exec(db, &char(query_all_users.str), my_callback, voidptr(7), &error_msg) if rc != C.SQLITE_OK { - eprintln(cstring_to_vstring(error_msg)) + eprintln(unsafe { cstring_to_vstring(error_msg) }) C.sqlite3_free(error_msg) } C.sqlite3_close(db) diff --git a/vlib/builtin/string.v b/vlib/builtin/string.v index cc54075bdc..c8a6b66b84 100644 --- a/vlib/builtin/string.v +++ b/vlib/builtin/string.v @@ -270,8 +270,8 @@ pub fn (s string) cstr() byteptr { */ // cstring_to_vstring creates a copy of cstr and turns it into a v string. [unsafe] -pub fn cstring_to_vstring(cstr &byte) string { - return unsafe { tos_clone(cstr) } +pub fn cstring_to_vstring(cstr &char) string { + return unsafe { tos_clone(&byte(cstr)) } } // replace_once replaces the first occurence of `rep` with the string passed in `with`.