/pypy/rpython/ootypesystem/rbuilder.py

https://github.com/alemacgo/pypy · Python · 57 lines · 43 code · 13 blank · 1 comment · 4 complexity · ebdc9237b1c7446256a277d694f9894c MD5 · raw file

  1. from pypy.rpython.rbuilder import AbstractStringBuilderRepr
  2. from pypy.rpython.ootypesystem import ootype
  3. from pypy.rpython.ootypesystem.rstr import string_repr, char_repr,\
  4. unicode_repr, unichar_repr
  5. MAX = 16*1024*1024
  6. class BaseBuilderRepr(AbstractStringBuilderRepr):
  7. def empty(self):
  8. return ootype.null(self.lowleveltype)
  9. @classmethod
  10. def ll_new(cls, init_size):
  11. if init_size < 0 or init_size > MAX:
  12. init_size = MAX
  13. return ootype.new(cls.lowleveltype)
  14. @staticmethod
  15. def ll_append_char(builder, char):
  16. builder.ll_append_char(char)
  17. @staticmethod
  18. def ll_append(builder, string):
  19. builder.ll_append(string)
  20. @staticmethod
  21. def ll_append_slice(builder, string, start, end):
  22. # XXX not sure how to optimize it
  23. for i in range(start, end):
  24. builder.ll_append_char(string.ll_stritem_nonneg(i))
  25. @staticmethod
  26. def ll_append_multiple_char(builder, char, times):
  27. for i in range(times):
  28. builder.ll_append_char(char)
  29. @staticmethod
  30. def ll_build(builder):
  31. return builder.ll_build()
  32. @staticmethod
  33. def ll_is_true(builder):
  34. return bool(builder)
  35. class StringBuilderRepr(BaseBuilderRepr):
  36. lowleveltype = ootype.StringBuilder
  37. string_repr = string_repr
  38. char_repr = char_repr
  39. class UnicodeBuilderRepr(BaseBuilderRepr):
  40. lowleveltype = ootype.UnicodeBuilder
  41. string_repr = unicode_repr
  42. char_repr = unichar_repr
  43. stringbuilder_repr = StringBuilderRepr()
  44. unicodebuilder_repr = UnicodeBuilderRepr()