Lichen

lib/__builtins__/buffer.py

221:a7684997ef7a
2016-11-23 Paul Boddie Treat buffer initialisation arguments as append inputs. Test mixing of object types in list serialisation.
     1 #!/usr/bin/env python     2      3 """     4 Buffer object.     5      6 Copyright (C) 2015, 2016 Paul Boddie <paul@boddie.org.uk>     7      8 This program is free software; you can redistribute it and/or modify it under     9 the terms of the GNU General Public License as published by the Free Software    10 Foundation; either version 3 of the License, or (at your option) any later    11 version.    12     13 This program is distributed in the hope that it will be useful, but WITHOUT    14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS    15 FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more    16 details.    17     18 You should have received a copy of the GNU General Public License along with    19 this program.  If not, see <http://www.gnu.org/licenses/>.    20 """    21     22 from native import _list_init, _list_append, _list_concat, _buffer_str    23     24 class buffer(object):    25     26     "A buffer, used to build strings."    27     28     def __init__(self, args=None, size=0):    29     30         "Initialise a buffer from the given 'args' or the given 'size'."    31     32         if args is not None:    33             n = len(args)    34         elif isinstance(size, int):    35             n = size    36         else:    37             raise TypeError(size)    38     39         self.__data__ = _list_init(n)    40     41         # Append all arguments to the buffer.    42     43         if args:    44             for arg in args:    45                 self.append(arg)    46     47     def append(self, s):    48     49         """    50         Append 's' to the buffer, concatenating buffers and adding other objects    51         in string form.    52         """    53     54         if isinstance(s, buffer):    55             _list_concat(self, s)    56         elif isinstance(s, string):    57             _list_append(self, s)    58         else:    59             _list_append(self, str(s))    60     61     def __str__(self):    62     63         "Return a string representation."    64     65         return _buffer_str(self)    66     67 # vim: tabstop=4 expandtab shiftwidth=4