mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
C string literals (c'str'); bare builtin module; bare println()
This commit is contained in:
28
vlib/builtin/bare/builtin_bare.v
Normal file
28
vlib/builtin/bare/builtin_bare.v
Normal file
@ -0,0 +1,28 @@
|
||||
module builtin
|
||||
|
||||
pub fn syscall5(number, arg1, arg2, arg3, arg4, arg5 voidptr) voidptr
|
||||
|
||||
// TODO no pub => error
|
||||
pub fn write(fd int, data voidptr, nbytes int) int {
|
||||
return syscall5(
|
||||
1, // SYS_write
|
||||
fd,
|
||||
data,
|
||||
nbytes,
|
||||
0, // ignored
|
||||
0 // ignored
|
||||
)
|
||||
}
|
||||
|
||||
pub fn println(s string) {
|
||||
write(1, s.str, s.len)
|
||||
}
|
||||
|
||||
pub fn panic(s string) {
|
||||
write(1, s.str, s.len)
|
||||
}
|
||||
|
||||
pub fn malloc(n int) voidptr {
|
||||
return syscall5(0,0,0,0,0,0)
|
||||
|
||||
}
|
66
vlib/builtin/bare/string_bare.v
Normal file
66
vlib/builtin/bare/string_bare.v
Normal file
@ -0,0 +1,66 @@
|
||||
module builtin
|
||||
|
||||
pub struct string {
|
||||
pub:
|
||||
str byteptr
|
||||
len int
|
||||
}
|
||||
|
||||
pub fn strlen(s byteptr) int {
|
||||
mut i := 0
|
||||
for i = 0; s[i] != 0; i++ {
|
||||
|
||||
}
|
||||
return i
|
||||
}
|
||||
|
||||
pub fn tos(s byteptr, len int) string {
|
||||
if s == 0 {
|
||||
panic('tos(): nil string')
|
||||
}
|
||||
return string {
|
||||
str: s
|
||||
len: len
|
||||
}
|
||||
}
|
||||
|
||||
pub fn tos_clone(s byteptr) string {
|
||||
if s == 0 {
|
||||
panic('tos: nil string')
|
||||
}
|
||||
return tos2(s).clone()
|
||||
}
|
||||
|
||||
// Same as `tos`, but calculates the length. Called by `string(bytes)` casts.
|
||||
// Used only internally.
|
||||
pub fn tos2(s byteptr) string {
|
||||
if s == 0 {
|
||||
panic('tos2: nil string')
|
||||
}
|
||||
return string {
|
||||
str: s
|
||||
len: strlen(s)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn tos3(s *C.char) string {
|
||||
if s == 0 {
|
||||
panic('tos3: nil string')
|
||||
}
|
||||
return string {
|
||||
str: byteptr(s)
|
||||
len: strlen(byteptr(s))
|
||||
}
|
||||
}
|
||||
|
||||
pub fn (a string) clone() string {
|
||||
mut b := string {
|
||||
len: a.len
|
||||
str: malloc(a.len + 1)
|
||||
}
|
||||
for i := 0; i < a.len; i++ {
|
||||
b[i] = a[i]
|
||||
}
|
||||
b[a.len] = `\0`
|
||||
return b
|
||||
}
|
Reference in New Issue
Block a user