• Kirill Smelkov's avatar
    golang_str: bstr/ustr encode/decode · 023907ee
    Kirill Smelkov authored
    So far we've overridden almost all string methods, that bstr/ustr
    inherited from bytes and unicode. However 2 of the methods remained
    intact until now: unicode.encode() and bytes.decode(). Let's override
    them too for completeness:
    
    - we want ustr.encode() to follow signature of unicode.encode and for ustr.encode('utf-8') to return bstr.
    - for consistency we also want ustr.encode() to return the same type
      irregardless of which encoding/errors pair is used in the arguments.
    - => ustr.encode() always returns bstr.
    - we want bstr.decode() to follow signature of bytes.decode and for bstr.decode('utf-8') to return ustr.
    - for consistency we also want bstr.decode() to return the same type
      irregardless of which encoding/errors pair is used in the arguments.
    - -> bstr.decode() always returns ustr.
    
    So  ustr.encode() -> bstr  and  bstr.decode() -> ustr.
    
    Let's implement this carrying out encoding/decoding process internally
    similarly to regular bytes and unicode and wrapping the result into
    corresponding pygolang type at the end.
    023907ee
golang_str_test.py 94.6 KB