mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
builtin: x.vstring() instead of string(x) (#6102)
This commit is contained in:
@@ -120,6 +120,23 @@ pub fn tos_lit(s charptr) string {
|
||||
}
|
||||
}
|
||||
|
||||
// byteptr.vstring() - converts a C style string to a V string. NB: the string data is reused, NOT copied.
|
||||
[unsafe]
|
||||
pub fn (bp byteptr) vstring() string {
|
||||
return string{
|
||||
str: bp
|
||||
len: unsafe {C.strlen(bp)}
|
||||
}
|
||||
}
|
||||
|
||||
// byteptr.vstring_with_len() - converts a C style string to a V string. NB: the string data is reused, NOT copied.
|
||||
[unsafe]
|
||||
pub fn (bp byteptr) vstring_with_len(len int) string {
|
||||
return string{
|
||||
str: bp
|
||||
len: len
|
||||
}
|
||||
}
|
||||
|
||||
// string.clone_static returns an independent copy of a given array
|
||||
// It should be used only in -autofree generated code.
|
||||
@@ -1429,9 +1446,10 @@ pub fn (s string) repeat(count int) string {
|
||||
}
|
||||
}
|
||||
unsafe {
|
||||
ret[s.len * count] = 0
|
||||
new_len := s.len * count
|
||||
ret[new_len] = 0
|
||||
return ret.vstring_with_len(new_len)
|
||||
}
|
||||
return string(ret)
|
||||
}
|
||||
|
||||
pub fn (s string) fields() []string {
|
||||
@@ -1463,7 +1481,7 @@ pub fn (s string) filter(func fn(b byte) bool) string {
|
||||
}
|
||||
unsafe {
|
||||
buf[new_len] = 0
|
||||
return string(buf, new_len)
|
||||
return buf.vstring_with_len(new_len)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1524,6 +1542,6 @@ pub fn (s string) strip_margin_custom(del byte) string {
|
||||
}
|
||||
unsafe {
|
||||
ret[count] = 0
|
||||
return ret.vstring_with_len(count)
|
||||
}
|
||||
return string(ret)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user