Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add length() and is_empty() to Buffer #1044

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions buffer/buffer.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,16 @@ fn grow_if_necessary(self : Buffer, required : Int) -> Unit {
}
}

/// Return the given buffer's length in bytes.
pub fn length(self : Buffer) -> Int {
self.len
}

/// Return whether the given buffer is empty.
pub fn is_empty(self : Buffer) -> Bool {
self.len == 0
}

/// Return a new String contains the data in buffer.
pub fn to_string(self : Buffer) -> String {
self.bytes.sub_string(0, self.len)
Expand Down
2 changes: 2 additions & 0 deletions buffer/buffer.mbti
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ package moonbitlang/core/buffer
// Types and methods
type Buffer
impl Buffer {
is_empty(Self) -> Bool
length(Self) -> Int
new(~size_hint : Int = ..) -> Self
reset(Self) -> Unit
to_bytes(Self) -> Bytes
Expand Down
14 changes: 14 additions & 0 deletions buffer/buffer_test.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,20 @@
// See the License for the specific language governing permissions and
// limitations under the License.

test "length method" {
let buf = @buffer.new(size_hint=100)
inspect!(buf.length(), content="0")
buf.write_string("Test")
inspect!(buf.length(), content="8")
}

test "is_empty method" {
let buf = @buffer.new(size_hint=100)
inspect!(buf.is_empty(), content="true")
buf.write_string("Test")
inspect!(buf.is_empty(), content="false")
}

test "expect method with matching content" {
let buf = @buffer.new(size_hint=100)
buf.write_string("Test")
Expand Down
Loading