diff --git a/buffer/buffer.mbt b/buffer/buffer.mbt index 43f5d8fe9..b47d27cba 100644 --- a/buffer/buffer.mbt +++ b/buffer/buffer.mbt @@ -28,7 +28,7 @@ /// buf.write_char("t") /// println(buf.to_string()) // output: Test /// ``` -struct Buffer { +struct T { mut bytes : Bytes mut len : Int initial_bytes : Bytes @@ -36,7 +36,7 @@ struct Buffer { ///| /// Expand the buffer size if capacity smaller than required space. -fn grow_if_necessary(self : Buffer, required : Int) -> Unit { +fn grow_if_necessary(self : T, required : Int) -> Unit { // TODO: get rid of mut let mut enough_space = self.bytes.length() if enough_space <= 0 { @@ -53,13 +53,13 @@ fn grow_if_necessary(self : Buffer, required : Int) -> Unit { ///| /// Return the given buffer's length in bytes. -pub fn length(self : Buffer) -> Int { +pub fn length(self : T) -> Int { self.len } ///| /// Return whether the given buffer is empty. -pub fn is_empty(self : Buffer) -> Bool { +pub fn is_empty(self : T) -> Bool { self.len == 0 } @@ -67,7 +67,7 @@ pub fn is_empty(self : Buffer) -> Bool { /// Return a new string contains the data in buffer. /// /// @alert deprecated "Use `Buffer::to_unchecked_string` instead" -pub fn to_string(self : Buffer) -> String { +pub fn to_string(self : T) -> String { self.bytes.to_unchecked_string(offset=0, length=self.len) } @@ -75,13 +75,13 @@ pub fn to_string(self : Buffer) -> String { /// Return a new unchecked string contains the data in buffer. /// Note this function does not validate the encoding of the byte sequence, /// it simply copy the bytes into a new String. -pub fn to_unchecked_string(self : Buffer) -> String { +pub fn to_unchecked_string(self : T) -> String { self.bytes.to_unchecked_string(offset=0, length=self.len) } ///| /// Create a buffer with initial capacity (in bytes). -pub fn Buffer::new(~size_hint : Int = 0) -> Buffer { +pub fn T::new(~size_hint : Int = 0) -> T { let initial = if size_hint < 1 { 1 } else { size_hint } let bytes = Bytes::new(initial) { bytes, len: 0, initial_bytes: bytes } @@ -89,19 +89,19 @@ pub fn Buffer::new(~size_hint : Int = 0) -> Buffer { ///| /// Write a string into buffer. -pub fn write_string(self : Buffer, value : String) -> Unit { +pub fn write_string(self : T, value : String) -> Unit { self.grow_if_necessary(self.len + value.length() * 2) self.bytes.blit_from_string(self.len, value, 0, value.length()) self.len += value.length() * 2 } ///| -pub fn write_object(self : Buffer, value : Show) -> Unit { +pub fn write_object(self : T, value : Show) -> Unit { self.write_string(value.to_string()) } ///| -pub fn write_bytes(self : Buffer, value : Bytes) -> Unit { +pub fn write_bytes(self : T, value : Bytes) -> Unit { let val_len = value.length() self.grow_if_necessary(self.len + val_len) self.bytes.blit(self.len, value, 0, val_len) @@ -111,7 +111,7 @@ pub fn write_bytes(self : Buffer, value : Bytes) -> Unit { ///| /// Write a sub-string into buffer. pub fn write_substring( - self : Buffer, + self : T, value : String, start : Int, len : Int @@ -126,7 +126,7 @@ pub fn write_substring( /// Write a sub-string into buffer. /// @alert deprecated "Use `Buffer::write_substring` instead" pub fn write_sub_string( - self : Buffer, + self : T, value : String, start : Int, len : Int @@ -136,7 +136,7 @@ pub fn write_sub_string( ///| /// Write a char into buffer. -pub fn write_char(self : Buffer, value : Char) -> Unit { +pub fn write_char(self : T, value : Char) -> Unit { self.grow_if_necessary(self.len + 4) let inc = self.bytes.set_utf16_char(self.len, value) self.len += inc @@ -144,24 +144,24 @@ pub fn write_char(self : Buffer, value : Char) -> Unit { ///| /// Write a byte into buffer. -pub fn write_byte(self : Buffer, value : Byte) -> Unit { +pub fn write_byte(self : T, value : Byte) -> Unit { self.grow_if_necessary(self.len + 1) self.bytes[self.len] = value self.len += 1 } ///| -pub fn reset(self : Buffer) -> Unit { +pub fn reset(self : T) -> Unit { self.bytes = self.initial_bytes self.len = 0 } ///| -pub fn to_bytes(self : Buffer) -> Bytes { +pub fn to_bytes(self : T) -> Bytes { Bytes::new(self.len)..blit(0, self.bytes, 0, self.len) } ///| -pub impl Show for Buffer with output(self, logger) { +pub impl Show for T with output(self, logger) { logger.write_string(self.to_unchecked_string()) } diff --git a/buffer/buffer.mbti b/buffer/buffer.mbti index 389211605..54dccd8a5 100644 --- a/buffer/buffer.mbti +++ b/buffer/buffer.mbti @@ -3,8 +3,8 @@ package moonbitlang/core/buffer // Values // Types and methods -type Buffer -impl Buffer { +type T +impl T { is_empty(Self) -> Bool length(Self) -> Int new(~size_hint : Int = ..) -> Self @@ -20,7 +20,7 @@ impl Buffer { write_sub_string(Self, String, Int, Int) -> Unit //deprecated write_substring(Self, String, Int, Int) -> Unit } -impl Show for Buffer +impl Show for T // Type aliases