mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
strings: add Builder.cut_to (#10042)
This commit is contained in:
parent
b34b56ee4e
commit
2d2b4f79cc
@ -44,11 +44,6 @@ pub fn (b []byte) clone() []byte {
|
|||||||
|
|
||||||
// TODO remove this once runes are implemented
|
// TODO remove this once runes are implemented
|
||||||
pub fn (b []byte) bytestr() string {
|
pub fn (b []byte) bytestr() string {
|
||||||
return bytes2string(b)
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO copy pasted from builder.v
|
|
||||||
fn bytes2string(b []byte) string {
|
|
||||||
unsafe {
|
unsafe {
|
||||||
buf := malloc(b.len + 1)
|
buf := malloc(b.len + 1)
|
||||||
C.memcpy(buf, b.data, b.len)
|
C.memcpy(buf, b.data, b.len)
|
||||||
|
@ -72,28 +72,24 @@ pub fn (mut b Builder) go_back(n int) {
|
|||||||
b.len -= n
|
b.len -= n
|
||||||
}
|
}
|
||||||
|
|
||||||
fn bytes2string(b []byte) string {
|
|
||||||
mut copy := b.clone()
|
|
||||||
copy << byte(0)
|
|
||||||
return unsafe { tos(copy.data, copy.len - 1) }
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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
|
||||||
pub fn (mut b Builder) cut_last(n int) string {
|
pub fn (mut b Builder) cut_last(n int) string {
|
||||||
res := bytes2string(b.buf[b.len - n..])
|
res := b.buf[b.len - n..].bytestr()
|
||||||
b.buf.trim(b.buf.len - n)
|
b.buf.trim(b.buf.len - n)
|
||||||
b.len -= n
|
b.len -= n
|
||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
// cut_to cuts the string after `pos` and returns it.
|
||||||
|
// if `pos` is superior to builder length, returns an empty string
|
||||||
|
// and cancel further operations
|
||||||
pub fn (mut b Builder) cut_to(pos int) string {
|
pub fn (mut b Builder) cut_to(pos int) string {
|
||||||
res := bytes2string( b.buf[pos..] )
|
if pos > b.len {
|
||||||
b.buf.trim(pos)
|
return ''
|
||||||
b.len = pos
|
}
|
||||||
return res
|
return b.cut_last(b.buf.len - pos)
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
// go_back_to resets the buffer to the given position `pos`
|
// go_back_to resets the buffer to the given position `pos`
|
||||||
// NB: pos should be < than the existing buffer length.
|
// NB: pos should be < than the existing buffer length.
|
||||||
pub fn (mut b Builder) go_back_to(pos int) {
|
pub fn (mut b Builder) go_back_to(pos int) {
|
||||||
@ -119,7 +115,7 @@ pub fn (b &Builder) last_n(n int) string {
|
|||||||
if n > b.len {
|
if n > b.len {
|
||||||
return ''
|
return ''
|
||||||
}
|
}
|
||||||
return bytes2string(b.buf[b.len - n..])
|
return b.buf[b.len - n..].bytestr()
|
||||||
}
|
}
|
||||||
|
|
||||||
// buf == 'hello world'
|
// buf == 'hello world'
|
||||||
@ -128,7 +124,7 @@ pub fn (b &Builder) after(n int) string {
|
|||||||
if n >= b.len {
|
if n >= b.len {
|
||||||
return ''
|
return ''
|
||||||
}
|
}
|
||||||
return bytes2string(b.buf[n..])
|
return b.buf[n..].bytestr()
|
||||||
}
|
}
|
||||||
|
|
||||||
// str returns a copy of all of the accumulated buffer content.
|
// str returns a copy of all of the accumulated buffer content.
|
||||||
|
@ -82,3 +82,15 @@ fn test_strings_builder_reuse() {
|
|||||||
sb.write_string('hello')
|
sb.write_string('hello')
|
||||||
assert sb.str() == 'hello'
|
assert sb.str() == 'hello'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn test_cut_to() {
|
||||||
|
mut sb := strings.new_builder(16)
|
||||||
|
sb.write_string('hello')
|
||||||
|
assert sb.cut_to(3) == 'lo'
|
||||||
|
assert sb.len == 3
|
||||||
|
assert sb.cut_to(3) == ''
|
||||||
|
assert sb.len == 3
|
||||||
|
assert sb.cut_to(0) == 'hel'
|
||||||
|
assert sb.cut_to(32) == ''
|
||||||
|
assert sb.len == 0
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user