def string_append(args): if jit.isconstant(len(args)): return string_append_fastpath(args) if not args: return W_String.fromascii("") builder = StringBuilder(len(args)) unibuilder = None ascii_idx = 0 try: for ascii_idx in range(len(args)): arg = args[ascii_idx] if not isinstance(arg, W_String): raise SchemeException("string-append: expected a string") builder.append(arg.as_str_ascii()) except ValueError: unibuilder = UnicodeBuilder(len(args)) unibuilder.append(unicode(builder.build())) builder = None for i in range(ascii_idx, len(args)): arg = args[i] if not isinstance(arg, W_String): raise SchemeException("string-append: expected a string") unibuilder.append(arg.as_unicode()) if unibuilder is None: assert builder is not None return W_String.fromascii(builder.build()) else: assert unibuilder is not None return W_String.fromunicode(unibuilder.build())
def string_append(args): if not args: return W_String.fromascii("") builder = StringBuilder() unibuilder = None ascii_idx = 0 try: for ascii_idx in range(len(args)): arg = args[ascii_idx] if not isinstance(arg, W_String): raise SchemeException("string-append: expected a string") builder.append(arg.as_str_ascii()) except ValueError: unibuilder = UnicodeBuilder() unibuilder.append(unicode(builder.build())) builder = None for i in range(ascii_idx, len(args)): arg = args[i] if not isinstance(arg, W_String): raise SchemeException("string-append: expected a string") unibuilder.append(arg.as_unicode()) if unibuilder is None: assert builder is not None return W_String.fromascii(builder.build()) else: assert unibuilder is not None return W_String.fromunicode(unibuilder.build())
def num2str(a, radix): from rpython.rlib.rbigint import BASE8, BASE16 if radix.value == 10: return W_String.fromascii(a.tostring()) else: if isinstance(a, values.W_Fixnum): if radix.value == 16: res = hex(a.value) if a.value >= 0: res = res[2:] else: res = "-" + res[3:] return W_String.fromascii(res) #elif radix.value == 8: # return W_String.fromascii(oct(a.value)) # elif radix.value == 2: # return W_String.fromascii(bin(a.value)) else: raise SchemeException("number->string: radix unsupported") elif isinstance(a, values.W_Bignum): if radix.value == 16: return W_String.fromascii(a.value.format(BASE16)) elif radix.value == 8: return W_String.fromascii(a.value.format(BASE8)) elif radix.value == 2: return W_String.fromascii(a.value.format("01")) else: raise SchemeException("number->string: radix unsupported") elif isinstance(a, values.W_Flonum): raise SchemeException("number->string: flonum only supports radix 10") else: assert 0 # not reached
def num2str(a, radix): from rpython.rlib.rbigint import BASE8, BASE16 if radix.value == 10: return W_String.fromascii(a.tostring()) else: if isinstance(a, values.W_Fixnum): if radix.value == 16: res = hex(a.value) if a.value >= 0: res = res[2:] else: res = "-" + res[3:] return W_String.fromascii(res) #elif radix.value == 8: # return W_String.fromascii(oct(a.value)) # elif radix.value == 2: # return W_String.fromascii(bin(a.value)) else: raise SchemeException("number->string: radix unsupported") elif isinstance(a, values.W_Bignum): if radix.value == 16: return W_String.fromascii(a.value.format(BASE16)) elif radix.value == 8: return W_String.fromascii(a.value.format(BASE8)) elif radix.value == 2: return W_String.fromascii(a.value.format("01")) else: raise SchemeException("number->string: radix unsupported") elif isinstance(a, values.W_Flonum): raise SchemeException( "number->string: flonum only supports radix 10") else: assert 0 # not reached
def string_append_fastpath(args): try: joined = "".join([a.as_str_ascii() for a in args]) result = W_String.fromascii(joined) except ValueError: joined = u"".join([a.as_unicode() for a in args]) result = W_String.fromunicode(joined) return result
def string_append_fastpath(args): try: joined = "".join([a.as_str_ascii() for a in args]) result = W_String.fromascii(joined) except ValueError: joined = u"".join([a.as_unicode() for a in args]) result = W_String.fromunicode(joined) return result
def string_append(args): if not args: return W_String.fromascii("") builder = StringBuilder() unibuilder = None for a in args: if not isinstance(a, W_String): raise SchemeException("string-append: expected a string") if unibuilder is None: try: builder.append(a.as_str_ascii()) continue except ValueError: unibuilder = UnicodeBuilder() unibuilder.append(unicode(builder.build())) unibuilder.append(a.as_unicode()) if unibuilder is None: return W_String.fromascii(builder.build()) else: return W_String.fromunicode(unibuilder.build())
def string_append(args): if not args: return W_String.fromascii("") builder = StringBuilder() unibuilder = None for a in args: if not isinstance(a, W_String): raise SchemeException("string-append: expected a string") if unibuilder is None: try: builder.append(a.as_str_ascii()) continue except ValueError: unibuilder = UnicodeBuilder() unibuilder.append(unicode(builder.build())) unibuilder.append(a.as_unicode()) if unibuilder is None: return W_String.fromascii(builder.build()) else: return W_String.fromunicode(unibuilder.build())
def string(args): if len(args) == 0: return W_String.fromascii("") assert len(args) > 0 builder = UnicodeBuilder() # XXX could do one less copy in the ascii case for char in args: if not isinstance(char, values.W_Character): raise SchemeException("string: expected a character") builder.append(char.value) return W_String.fromunicode(builder.build())
def string(args): if len(args) == 0: return W_String.fromascii("") assert len(args) > 0 builder = UnicodeBuilder() # XXX could do one less copy in the ascii case for char in args: if not isinstance(char, values.W_Character): raise SchemeException("string: expected a character") builder.append(char.value) return W_String.fromunicode(builder.build())
def list_to_string(w_list): if not w_list.is_proper_list(): raise SchemeException("list->string: expected proper list") if not isinstance(w_list, values.W_Cons): return W_String.fromascii("") builder = UnicodeBuilder() while isinstance(w_list, values.W_Cons): char, w_list = w_list.car(), w_list.cdr() if not isinstance(char, values.W_Character): raise SchemeException("list->string: expected list of characters") builder.append(char.value) return W_String.fromunicode(builder.build())
def list_to_string(w_list): if not w_list.is_proper_list(): raise SchemeException("list->string: expected proper list") if not isinstance(w_list, values.W_Cons): return W_String.fromascii("") builder = UnicodeBuilder() while isinstance(w_list, values.W_Cons): char, w_list = w_list.car(), w_list.cdr() if not isinstance(char, values.W_Character): raise SchemeException("list->string: expected list of characters") builder.append(char.value) return W_String.fromunicode(builder.build())
def make_string(k, char): if char is None: char = u'\0' else: char = char.value c = ord(char) if k.value < 0: raise SchemeException("make-string: around negative") if c < 128: char = chr(c) return W_String.fromascii(char * k.value) else: char = unichr(c) return W_String.fromunicode(char * k.value)
def make_string(k, char): if char is None: char = u'\0' else: char = char.value c = ord(char) if k.value < 0: raise SchemeException("make-string: around negative") if c < 128: char = chr(c) return W_String.fromascii(char * k.value) else: char = unichr(c) return W_String.fromunicode(char * k.value)
def symbol_to_string_impl(v, immutable=False): asciivalue = v.asciivalue() if asciivalue is not None: return W_String.fromascii(asciivalue, immutable) return W_String.fromunicode(v.unicodevalue(), immutable)
def symbol_to_string(v): if v.asciivalue is not None: return W_String.fromascii(v.asciivalue) return W_String.fromunicode(v.unicodevalue)
def bytes_to_string_latin(str, err, start, end): # XXX Not a valid implementation return W_String.fromascii("")
def symbol_to_string_impl(v): asciivalue = v.asciivalue() if asciivalue is not None: return W_String.fromascii(asciivalue) return W_String.fromunicode(v.unicodevalue())
def bytes_to_string_latin(str, err, start, end): # XXX Not a valid implementation return W_String.fromascii("")
def symbol_to_string_impl(v): asciivalue = v.asciivalue() if asciivalue is not None: return W_String.fromascii(asciivalue) return W_String.fromunicode(v.unicodevalue())