v/vlib/builtin/string_charptr_byteptr_helpers.v
Felipe Pena 9a0166701c
Some checks are pending
Graphics CI / gg-regressions (push) Waiting to run
vlib modules CI / build-module-docs (push) Waiting to run
native backend CI / native-backend-ubuntu (push) Waiting to run
native backend CI / native-backend-windows (push) Waiting to run
Shy and PV CI / v-compiles-puzzle-vibes (push) Waiting to run
Sanitized CI / sanitize-undefined-gcc (push) Waiting to run
Sanitized CI / sanitize-undefined-clang (push) Waiting to run
Sanitized CI / tests-sanitize-address-clang (push) Waiting to run
Sanitized CI / sanitize-address-msvc (push) Waiting to run
Sanitized CI / sanitize-address-gcc (push) Waiting to run
Sanitized CI / sanitize-memory-clang (push) Waiting to run
sdl CI / v-compiles-sdl-examples (push) Waiting to run
Time CI / time-linux (push) Waiting to run
Time CI / time-macos (push) Waiting to run
Time CI / time-windows (push) Waiting to run
toml CI / toml-module-pass-external-test-suites (push) Waiting to run
Tools CI / tools-linux (clang) (push) Waiting to run
Tools CI / tools-linux (gcc) (push) Waiting to run
Tools CI / tools-linux (tcc) (push) Waiting to run
Tools CI / tools-macos (clang) (push) Waiting to run
Tools CI / tools-windows (gcc) (push) Waiting to run
Tools CI / tools-windows (msvc) (push) Waiting to run
Tools CI / tools-windows (tcc) (push) Waiting to run
Tools CI / tools-docker-ubuntu-musl (push) Waiting to run
vab CI / vab-compiles-v-examples (push) Waiting to run
vab CI / v-compiles-os-android (push) Waiting to run
wasm backend CI / wasm-backend (ubuntu-22.04) (push) Waiting to run
wasm backend CI / wasm-backend (windows-2022) (push) Waiting to run
cgen: add @[reused] attribute to mark methods, reusing the receiver memory on return (needed for autofree) (fix #25221) (#25235)
2025-09-05 07:44:41 +03:00

104 lines
2.8 KiB
V

module builtin
// Note: this file will be removed soon
// byteptr.vbytes() - makes a V []u8 structure from a C style memory buffer. Note: the data is reused, NOT copied!
@[reused; unsafe]
pub fn (data byteptr) vbytes(len int) []u8 {
return unsafe { voidptr(data).vbytes(len) }
}
// vstring converts a C style string to a V string. Note: the string data is reused, NOT copied.
// strings returned from this function will be normal V strings beside that (i.e. they would be
// freed by V's -autofree mechanism, when they are no longer used).
@[reused; unsafe]
pub fn (bp byteptr) vstring() string {
return string{
str: bp
len: unsafe { vstrlen(bp) }
}
}
// vstring_with_len converts a C style string to a V string.
// Note: the string data is reused, NOT copied.
@[reused; unsafe]
pub fn (bp byteptr) vstring_with_len(len int) string {
return string{
str: bp
len: len
is_lit: 0
}
}
// vstring converts C char* to V string.
// Note: the string data is reused, NOT copied.
@[reused; unsafe]
pub fn (cp charptr) vstring() string {
return string{
str: byteptr(cp)
len: unsafe { vstrlen_char(cp) }
is_lit: 0
}
}
// vstring_with_len converts C char* to V string.
// Note: the string data is reused, NOT copied.
@[reused; unsafe]
pub fn (cp charptr) vstring_with_len(len int) string {
return string{
str: byteptr(cp)
len: len
is_lit: 0
}
}
// vstring_literal converts a C style string to a V string.
// Note: the string data is reused, NOT copied.
// NB2: unlike vstring, vstring_literal will mark the string
// as a literal, so it will not be freed by autofree.
// This is suitable for readonly strings, C string literals etc,
// that can be read by the V program, but that should not be
// managed by it, for example `os.args` is implemented using it.
@[reused; unsafe]
pub fn (bp byteptr) vstring_literal() string {
return string{
str: bp
len: unsafe { vstrlen(bp) }
is_lit: 1
}
}
// vstring_with_len converts a C style string to a V string.
// Note: the string data is reused, NOT copied.
@[reused; unsafe]
pub fn (bp byteptr) vstring_literal_with_len(len int) string {
return string{
str: bp
len: len
is_lit: 1
}
}
// vstring_literal converts C char* to V string.
// See also vstring_literal defined on byteptr for more details.
// Note: the string data is reused, NOT copied.
@[reused; unsafe]
pub fn (cp charptr) vstring_literal() string {
return string{
str: byteptr(cp)
len: unsafe { vstrlen_char(cp) }
is_lit: 1
}
}
// vstring_literal_with_len converts C char* to V string.
// See also vstring_literal_with_len defined on byteptr.
// Note: the string data is reused, NOT copied.
@[reused; unsafe]
pub fn (cp charptr) vstring_literal_with_len(len int) string {
return string{
str: byteptr(cp)
len: len
is_lit: 1
}
}