[pypy-svn] r27189 - in pypy/dist/pypy/rpython/ootypesystem: . test
antocuni at codespeak.net
antocuni at codespeak.net
Sat May 13 20:19:09 CEST 2006
Author: antocuni
Date: Sat May 13 20:19:00 2006
New Revision: 27189
Modified:
pypy/dist/pypy/rpython/ootypesystem/ootype.py
pypy/dist/pypy/rpython/ootypesystem/test/test_oostring.py
Log:
Added a new type to ootype: StringBuilder, used for dynamically
building strings.
Modified: pypy/dist/pypy/rpython/ootypesystem/ootype.py
==============================================================================
--- pypy/dist/pypy/rpython/ootypesystem/ootype.py (original)
+++ pypy/dist/pypy/rpython/ootypesystem/ootype.py Sat May 13 20:19:00 2006
@@ -294,6 +294,29 @@
def _specialize(self, generic_types):
return self
+# WARNING: the name 'StringBuilder' is rebound at the end of file
+class StringBuilder(BuiltinADTType):
+ def __init__(self):
+ self._null = _null_string_builder(self)
+ self._GENERIC_METHODS = frozendict({
+ "ll_allocate": Meth([Signed], Void),
+ "ll_append_char": Meth([Char], Void),
+ "ll_append": Meth([String], Void),
+ "ll_build": Meth([], String),
+ })
+ self._setup_methods({})
+
+ def _defl(self):
+ return self._null
+
+ def _example(self):
+ return self._defl()
+
+ def _get_interp_class(self):
+ return _string_builder
+
+ def _specialize(self, generic_types):
+ return self
class List(BuiltinADTType):
# placeholders for types
@@ -768,7 +791,6 @@
return object.__getattribute__(self, name)
-
class _string(_builtin_type):
def __init__(self, STRING, value = ''):
@@ -816,6 +838,31 @@
def __init__(self, STRING):
self.__dict__["_TYPE"] = STRING
+class _string_builder(_builtin_type):
+ def __init__(self, STRING_BUILDER):
+ self._TYPE = STRING_BUILDER
+ self._buf = []
+
+ def ll_allocate(self, n):
+ assert isinstance(n, int)
+ # do nothing
+
+ def ll_append_char(self, ch):
+ assert isinstance(ch, str) and len(ch) == 1
+ self._buf.append(ch)
+
+ def ll_append(self, s):
+ assert isinstance(s, _string)
+ self._buf.append(s._str)
+
+ def ll_build(self):
+ return make_string(''.join(self._buf))
+
+class _null_string_builder(_null_mixin(_string_builder), _string_builder):
+ def __init__(self, STRING_BUILDER):
+ self.__dict__["_TYPE"] = STRING_BUILDER
+
+
class _list(_builtin_type):
def __init__(self, LIST):
self._TYPE = LIST
@@ -1073,4 +1120,5 @@
return DICT._is_initialized()
String = String()
+StringBuilder = StringBuilder()
ROOT = Instance('Root', None, _is_root=True)
Modified: pypy/dist/pypy/rpython/ootypesystem/test/test_oostring.py
==============================================================================
--- pypy/dist/pypy/rpython/ootypesystem/test/test_oostring.py (original)
+++ pypy/dist/pypy/rpython/ootypesystem/test/test_oostring.py Sat May 13 20:19:00 2006
@@ -18,3 +18,10 @@
assert isinstance(res, ootype._list)
assert res.ll_getitem_fast(0)._str == 'foo'
assert res.ll_getitem_fast(1)._str == 'bar'
+
+def test_string_builder():
+ b = ootype.new(ootype.StringBuilder)
+ b.ll_append_char('a')
+ b.ll_append(ootype.make_string('bcd'))
+ res = b.ll_build()
+ assert res._str == 'abcd'
More information about the Pypy-commit
mailing list