| 98 | |
| 99 | |
| 100 | class EncodedString(str): |
| 101 | # unicode string subclass to keep track of the original encoding. |
| 102 | # 'encoding' is None for unicode strings and the source encoding |
| 103 | # otherwise |
| 104 | encoding = None |
| 105 | |
| 106 | def __deepcopy__(self, memo): |
| 107 | return self |
| 108 | |
| 109 | def byteencode(self): |
| 110 | assert self.encoding is not None |
| 111 | return self.encode(self.encoding) |
| 112 | |
| 113 | def utf8encode(self): |
| 114 | assert self.encoding is None |
| 115 | return self.encode("UTF-8") |
| 116 | |
| 117 | @property |
| 118 | def is_unicode(self): |
| 119 | return self.encoding is None |
| 120 | |
| 121 | def contains_surrogates(self): |
| 122 | return string_contains_surrogates(self) |
| 123 | |
| 124 | def as_utf8_string(self): |
| 125 | return bytes_literal(self.utf8encode(), 'utf8') |
| 126 | |
| 127 | def as_c_string_literal(self): |
| 128 | # first encodes the string then produces a c string literal |
| 129 | if self.encoding is None: |
| 130 | s = self.as_utf8_string() |
| 131 | else: |
| 132 | s = bytes_literal(self.byteencode(), self.encoding) |
| 133 | return s.as_c_string_literal() |
| 134 | |
| 135 | |
| 136 | def string_contains_surrogates(ustring): |
no outgoing calls
no test coverage detected