Skip to content

Commit

Permalink
rename @buffer.Buffer to @buffer.T (moonbitlang#1210)
Browse files Browse the repository at this point in the history
  • Loading branch information
Yoorkin authored Nov 12, 2024
1 parent 537c0e1 commit 0a0ba68
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 20 deletions.
34 changes: 17 additions & 17 deletions buffer/buffer.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@
/// buf.write_char("t")
/// println(buf.to_string()) // output: Test
/// ```
struct Buffer {
struct T {
mut bytes : Bytes
mut len : Int
initial_bytes : Bytes
}

///|
/// 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 {
Expand All @@ -53,55 +53,55 @@ 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
}

///|
/// 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)
}

///|
/// 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 }
}

///|
/// 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)
Expand All @@ -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
Expand All @@ -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
Expand All @@ -136,32 +136,32 @@ 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
}

///|
/// 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())
}
6 changes: 3 additions & 3 deletions buffer/buffer.mbti
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down

0 comments on commit 0a0ba68

Please sign in to comment.