BufferClass
- Namespace
- Ancestors
-
-
Data
Object
-
A buffer for building U::Strings. Buffers should be used when you want to
create U::Strings step-wise, for example, when joining them together, or
reading some input. Create a new buffer with #initialize, optionally
specifying an initial size. Then, #append (or #<<
) content to it. You
can also #append_formatted content. You can check the #length
(#size), #bytesize, and #width of the buffer, which can be useful if
you want to limit how much content you want to generate. Once all content
has been appended, a buffer can be converted to a U::String via #to_u or
#to_u! depending on whether you want to let the buffer keep its content or
not. You can also convert it to a String with #to_s.
- Example: Benchmarking U::String#+ Versus U::Buffer#append/U::Buffer#to_u!
require 'benchmark' require 'u-1.0' Benchmark.bm do |x| x.report do a = ''.u 100000.times do a = a + 'a' end end x.report do b = U::Buffer.new 100000.times do b.append 'a' end a = b.to_u! end end # ⇒ # user system total real # 3.560000 0.650000 4.210000 ( 4.726064) # 0.060000 0.000000 0.060000 ( 0.057134)
Constructor
initialize(size#to_int = 128
)#⚙
Sets up a new buffer of size bytes.
Instance Methods
append(*partsU::Buffer, Fixnum, Bignum, U::String, #to_str)self#⚙
Append each p in parts, append q to the receiver, where q = p#to_s, if p is a U::Buffer, q = p#chr, if p is a Fixnum or Bignum, q = p#to_str, if p is a U::String or responds to #to_str.
- RaisesRangeError
-
If a p is a Fixnum or Bignum and ¬p#chr#valid?
<<
(*partsU::Buffer, Fixnum, Bignum, U::String, #to_str)self#⚙
This is an alias for #append.
append_format(formatU::String, #to_str, *values)self#⚙
Appends the result of format#%(values) to the receiver.
to_uU::String#⚙
Returns a UTF-8-encoded string of the receiver’s content.
to_u!U::String#⚙
Returns the UTF-8-encoded string of the receiver’s content after clearing it from the receiver.
to_sString#⚙
Returns a UTF-8-encoded string of the receiver’s content.
lengthInteger#⚙
Returns the number of characters in the receiver.
sizeInteger#⚙
This is an alias for #length.
bytesizeInteger#⚙
Returns the number of bytes required to represent the receiver.
widthInteger#⚙
Returns the width of the receiver. The width is defined as the sum of the number of “cells” on a terminal or similar cell-based display that the characters in the string will require.
Characters that are U::String#wide? have a width of 2. Characters that are U::String#zero_width? have a width of 0. Other characters have a width of 1.
==
(otherU::Buffer)Boolean#⚙
Returns true if the receiver’s class and content equal those of other.
eql?(otherU::Buffer)Boolean#⚙
This is an alias for #==
.
hashFixnum#⚙
Returns the hash value of the receiver’s content.