lib/core: Added `add_char` method to `Bytes`
authorLucas Bajolet <r4pass@hotmail.com>
Fri, 27 Nov 2015 16:06:29 +0000 (11:06 -0500)
committerLucas Bajolet <r4pass@hotmail.com>
Fri, 27 Nov 2015 20:34:13 +0000 (15:34 -0500)
Signed-off-by: Lucas Bajolet <r4pass@hotmail.com>

lib/core/bytes.nit

index 94d3d27..1efda33 100644 (file)
@@ -155,6 +155,21 @@ class Bytes
                length += 1
        end
 
+       # Adds the UTF-8 representation of `c` to `self`
+       #
+       #     var b = new Bytes.empty
+       #     b.add_char('A')
+       #     b.add_char('キ')
+       #     assert b.hexdigest == "41E382AD"
+       fun add_char(c: Char) do
+               if persisted then regen
+               var cln = c.u8char_len
+               var ln = length
+               enlarge(ln + cln)
+               items.set_char_at(length, c)
+               length += cln
+       end
+
        #     var b = new Bytes.empty
        #     b.append([104u8, 101u8, 108u8, 108u8, 111u8])
        #     assert b.to_s == "hello"