Verified Commit 47580733 authored by Yorick Peterse's avatar Yorick Peterse 🌴

Implement Length for StringBuffer

parent a3d9ab9e
Pipeline #41361404 passed with stages
in 15 minutes and 30 seconds
#! Efficient concatenating of `String` objects.
import std::conversion::ToString
import std::length::Length
## A buffer for efficiently concatenating `String` objects together.
##
......@@ -35,7 +36,7 @@ import std::conversion::ToString
## This would only allocate four `String` objects: three for the `String`
## literals, and one for the resulting `String` returned by
## `StringBuffer.to_string`.
object StringBuffer impl ToString {
object StringBuffer impl ToString, Length {
def init(strings: Array!(String) = []) {
let @strings = strings
}
......@@ -72,4 +73,8 @@ object StringBuffer impl ToString {
def to_string -> String {
_INKOC.string_concat_multiple(@strings)
}
def length -> Integer {
@strings.length
}
}
......@@ -23,3 +23,13 @@ test.group('std::string_buffer::StringBuffer.to_string') do (g) {
assert.equal(buffer.to_string, 'foobarbaz')
}
}
test.group('std::string_buffer::StringBuffer.length') do (g) {
g.test('Obtaining the length of an empty StringBuffer') {
assert.equal(StringBuffer.new.length, 0)
}
g.test('Obtaining the length of a non-empty StringBuffer') {
assert.equal(StringBuffer.new(['foo']).length, 1)
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment