Class: Packcr::Buffer
- Inherits:
-
Object
- Object
- Packcr::Buffer
- Defined in:
- lib/packcr/buffer.rb
Instance Method Summary collapse
- #[](index) ⇒ Object
- #[]=(pos, ch) ⇒ Object
- #add(ch) ⇒ Object
- #add_pos(offset) ⇒ Object
- #count_characters(s, e) ⇒ Object
-
#initialize ⇒ Buffer
constructor
A new instance of Buffer.
- #len ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize ⇒ Buffer
Returns a new instance of Buffer.
4 5 6 |
# File 'lib/packcr/buffer.rb', line 4 def initialize @buf = +"".b end |
Instance Method Details
#[](index) ⇒ Object
12 13 14 |
# File 'lib/packcr/buffer.rb', line 12 def [](index) @buf[index].ord end |
#[]=(pos, ch) ⇒ Object
39 40 41 |
# File 'lib/packcr/buffer.rb', line 39 def []=(pos, ch) @buf[pos] = ch end |
#add(ch) ⇒ Object
31 32 33 |
# File 'lib/packcr/buffer.rb', line 31 def add(ch) @buf.concat(ch) end |
#add_pos(offset) ⇒ Object
43 44 45 |
# File 'lib/packcr/buffer.rb', line 43 def add_pos(offset) @buf[0, offset] = "" end |
#count_characters(s, e) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/packcr/buffer.rb', line 16 def count_characters(s, e) # UTF-8 multibyte character support but without checking UTF-8 validity n = 0 i = s while i < e c = self[i] if c == 0 break end n += 1 i += (c < 0x80) ? 1 : ((c & 0xe0) == 0xc0) ? 2 : ((c & 0xf0) == 0xe0) ? 3 : ((c & 0xf8) == 0xf0) ? 4 : 1 end return n end |
#len ⇒ Object
8 9 10 |
# File 'lib/packcr/buffer.rb', line 8 def len @buf.length end |
#to_s ⇒ Object
35 36 37 |
# File 'lib/packcr/buffer.rb', line 35 def to_s @buf end |