Lichen

lib/__builtins__/buffer.py

938:799711337453
5 months ago Paul Boddie Renamed the string class to str, replacing the str function with the new_str function, this being invoked specially by the string instantiation function. As with the Unicode type renaming, a more general instantiation mechanism might permit the new_str function to be part of the functionality of the str or basestring classes.
     1 #!/usr/bin/env python     2      3 """     4 Buffer object.     5      6 Copyright (C) 2015, 2016, 2017 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:    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 ValueError(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.__data__)    56         elif isinstance(s, str):    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.__data__)    66     67     def __repr__(self):    68     69         "Return a program representation."    70     71         return buffer(["buffer([", repr(str(self)), "])"])    72     73 # vim: tabstop=4 expandtab shiftwidth=4