From aef95721a43490dbbb77c13a2e4deba834bbf90c Mon Sep 17 00:00:00 2001 From: David 'Epper' Marshall Date: Sun, 8 May 2022 00:15:42 -0400 Subject: [PATCH] strings: builder: add clear (#14328) --- vlib/strings/builder.c.v | 8 +++----- vlib/strings/builder.js.v | 7 +++---- vlib/strings/builder_test.js.v | 3 +++ vlib/strings/builder_test.v | 2 ++ 4 files changed, 11 insertions(+), 9 deletions(-) diff --git a/vlib/strings/builder.c.v b/vlib/strings/builder.c.v index 70b262f348..490b94d770 100644 --- a/vlib/strings/builder.c.v +++ b/vlib/strings/builder.c.v @@ -50,11 +50,9 @@ pub fn (mut b Builder) write_runes(runes []rune) { } } -// write_b appends a single `data` byte to the accumulated buffer -[deprecated: 'Use write_u8() instead'] -[deprecated_after: '2022-02-11'] -pub fn (mut b Builder) write_b(data u8) { - b << data +// clear clears the buffer contents +pub fn (mut b Builder) clear() { + b = []u8{cap: b.cap} } // write_u8 appends a single `data` byte to the accumulated buffer diff --git a/vlib/strings/builder.js.v b/vlib/strings/builder.js.v index 3340582589..108abb0227 100644 --- a/vlib/strings/builder.js.v +++ b/vlib/strings/builder.js.v @@ -18,13 +18,12 @@ pub fn new_builder(initial_size int) Builder { return []u8{cap: initial_size} } -[deprecated: 'Use write_u8() instead'] -pub fn (mut b Builder) write_b(data u8) { +pub fn (mut b Builder) write_byte(data byte) { b << data } -pub fn (mut b Builder) write_byte(data byte) { - b << data +pub fn (mut b Builder) clear() { + b = []u8{cap: b.cap} } pub fn (mut b Builder) write_u8(data u8) { diff --git a/vlib/strings/builder_test.js.v b/vlib/strings/builder_test.js.v index 70701a7f86..dc25577e44 100644 --- a/vlib/strings/builder_test.js.v +++ b/vlib/strings/builder_test.js.v @@ -39,6 +39,9 @@ fn test_sb() { assert last_2 == '56' final_sb := sb.str() assert final_sb == '1234' + + sb.clear() + assert sb.str() == '' //} } diff --git a/vlib/strings/builder_test.v b/vlib/strings/builder_test.v index 69d86b188f..14614f3f8c 100644 --- a/vlib/strings/builder_test.v +++ b/vlib/strings/builder_test.v @@ -40,6 +40,8 @@ fn test_sb() { final_sb := sb.str() assert final_sb == '1234' //} + sb.clear() + assert sb.str() == '' } fn test_big_sb() {