paul@390 | 1 | #!/usr/bin/env python |
paul@390 | 2 | |
paul@390 | 3 | """ |
paul@390 | 4 | Unicode objects. |
paul@390 | 5 | |
paul@390 | 6 | Copyright (C) 2015, 2016 Paul Boddie <paul@boddie.org.uk> |
paul@390 | 7 | |
paul@390 | 8 | This program is free software; you can redistribute it and/or modify it under |
paul@390 | 9 | the terms of the GNU General Public License as published by the Free Software |
paul@390 | 10 | Foundation; either version 3 of the License, or (at your option) any later |
paul@390 | 11 | version. |
paul@390 | 12 | |
paul@390 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@390 | 14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@390 | 15 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@390 | 16 | details. |
paul@390 | 17 | |
paul@390 | 18 | You should have received a copy of the GNU General Public License along with |
paul@390 | 19 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@390 | 20 | """ |
paul@390 | 21 | |
paul@390 | 22 | from __builtins__.str import basestring |
paul@390 | 23 | from posix.iconv import Converter |
paul@403 | 24 | from native import str_add, unicode_len, isinstance as _isinstance |
paul@390 | 25 | |
paul@390 | 26 | class utf8string(basestring): |
paul@390 | 27 | |
paul@390 | 28 | "A character string representation based on UTF-8." |
paul@390 | 29 | |
paul@396 | 30 | def __init__(self, other=None, encoding=None): |
paul@396 | 31 | |
paul@396 | 32 | """ |
paul@396 | 33 | Initialise the string, perhaps from 'other', with any original |
paul@396 | 34 | 'encoding' indicated. |
paul@396 | 35 | """ |
paul@396 | 36 | |
paul@396 | 37 | get_using(basestring.__init__, self)(other) |
paul@396 | 38 | self.encoding = encoding |
paul@403 | 39 | self.length = None |
paul@396 | 40 | |
paul@396 | 41 | def _binary_op(self, op, other): |
paul@396 | 42 | |
paul@396 | 43 | "Perform 'op' on this object and 'other' if appropriate." |
paul@396 | 44 | |
paul@396 | 45 | # Reject non-strings. |
paul@396 | 46 | |
paul@396 | 47 | if not _isinstance(other, basestring): |
paul@396 | 48 | return NotImplemented |
paul@396 | 49 | |
paul@396 | 50 | # Combining text with bytes. |
paul@396 | 51 | |
paul@396 | 52 | elif not _isinstance(other, utf8string): |
paul@396 | 53 | s = self.encode() |
paul@396 | 54 | return op(s.__data__, other.__data__) |
paul@396 | 55 | |
paul@396 | 56 | # Otherwise, perform the operation on the operands' data. |
paul@396 | 57 | |
paul@396 | 58 | else: |
paul@396 | 59 | return op(self.__data__, other.__data__) |
paul@390 | 60 | |
paul@396 | 61 | def _binary_op_rev(self, op, other): |
paul@396 | 62 | |
paul@396 | 63 | "Perform 'op' on 'other' and this object if appropriate." |
paul@396 | 64 | |
paul@396 | 65 | # Reject non-strings. |
paul@396 | 66 | |
paul@396 | 67 | if not _isinstance(other, basestring): |
paul@396 | 68 | return NotImplemented |
paul@396 | 69 | |
paul@396 | 70 | # Combining text with bytes. |
paul@396 | 71 | |
paul@396 | 72 | elif not _isinstance(other, utf8string): |
paul@396 | 73 | s = self.encode() |
paul@396 | 74 | return op(other.__data__, s.__data__) |
paul@396 | 75 | |
paul@396 | 76 | # Otherwise, perform the operation on the operands' data. |
paul@396 | 77 | |
paul@396 | 78 | else: |
paul@396 | 79 | return op(other.__data__, self.__data__) |
paul@396 | 80 | |
paul@398 | 81 | def _convert(self, result, other): |
paul@398 | 82 | |
paul@398 | 83 | "Convert 'result' to a Unicode object if 'other' already is." |
paul@398 | 84 | |
paul@398 | 85 | if _isinstance(other, utf8string): |
paul@398 | 86 | return utf8string(result, self.encoding) |
paul@398 | 87 | else: |
paul@398 | 88 | return result |
paul@398 | 89 | |
paul@398 | 90 | def __iadd__(self, other): |
paul@398 | 91 | |
paul@398 | 92 | "Return a string combining this string with 'other'." |
paul@398 | 93 | |
paul@398 | 94 | return self._convert(self._binary_op(str_add, other), other) |
paul@398 | 95 | |
paul@398 | 96 | __add__ = __iadd__ |
paul@398 | 97 | |
paul@398 | 98 | def __radd__(self, other): |
paul@398 | 99 | |
paul@398 | 100 | "Return a string combining this string with 'other'." |
paul@398 | 101 | |
paul@398 | 102 | return self._convert(self._binary_op_rev(str_add, other), other) |
paul@398 | 103 | |
paul@403 | 104 | def __len__(self): |
paul@403 | 105 | |
paul@403 | 106 | "Return the length of this string in characters." |
paul@403 | 107 | |
paul@403 | 108 | if self.length is None: |
paul@403 | 109 | self.length = unicode_len(self.__data__) |
paul@403 | 110 | |
paul@403 | 111 | return self.length |
paul@403 | 112 | |
paul@396 | 113 | def encode(self, encoding=None): |
paul@396 | 114 | |
paul@396 | 115 | """ |
paul@396 | 116 | Encode the string to the given 'encoding' or any original encoding if |
paul@396 | 117 | omitted. |
paul@396 | 118 | """ |
paul@396 | 119 | |
paul@396 | 120 | encoding = encoding or self.encoding |
paul@396 | 121 | if not encoding: |
paul@396 | 122 | return self |
paul@390 | 123 | |
paul@390 | 124 | from_utf8 = Converter("UTF-8", encoding) |
paul@392 | 125 | |
paul@390 | 126 | try: |
paul@390 | 127 | from_utf8.feed(self) |
paul@390 | 128 | return str(from_utf8) |
paul@392 | 129 | |
paul@390 | 130 | finally: |
paul@390 | 131 | from_utf8.close() |
paul@390 | 132 | |
paul@409 | 133 | def join(self, l): |
paul@409 | 134 | |
paul@409 | 135 | "Join the elements in 'l' with this string." |
paul@409 | 136 | |
paul@409 | 137 | # Empty strings just cause the list elements to be concatenated. |
paul@409 | 138 | |
paul@409 | 139 | nonempty = self.__bool__() |
paul@409 | 140 | |
paul@409 | 141 | # Non-empty strings join the elements together in a buffer. |
paul@409 | 142 | |
paul@409 | 143 | b = buffer() |
paul@409 | 144 | first = True |
paul@409 | 145 | encoding = self.encoding |
paul@409 | 146 | |
paul@409 | 147 | for s in l: |
paul@409 | 148 | if first: |
paul@409 | 149 | first = False |
paul@409 | 150 | elif nonempty: |
paul@409 | 151 | b.append(self) |
paul@409 | 152 | |
paul@409 | 153 | if _isinstance(s, utf8string): |
paul@409 | 154 | encoding = None |
paul@409 | 155 | |
paul@409 | 156 | b.append(s) |
paul@409 | 157 | |
paul@409 | 158 | s = str(b) |
paul@409 | 159 | if encoding: |
paul@409 | 160 | s = utf8string(s) |
paul@409 | 161 | s.encoding = encoding |
paul@409 | 162 | return s |
paul@409 | 163 | |
paul@390 | 164 | def unicode(s, encoding): |
paul@390 | 165 | |
paul@390 | 166 | "Convert 's' to a Unicode object, interpreting 's' as using 'encoding'." |
paul@390 | 167 | |
paul@390 | 168 | if isinstance(s, utf8string): |
paul@390 | 169 | return s |
paul@390 | 170 | |
paul@390 | 171 | # Obtain a string representation. |
paul@390 | 172 | |
paul@390 | 173 | s = s.__str__() |
paul@390 | 174 | |
paul@390 | 175 | # Convert the string to UTF-8. |
paul@390 | 176 | |
paul@390 | 177 | to_utf8 = Converter(encoding, "UTF-8") |
paul@392 | 178 | |
paul@390 | 179 | try: |
paul@390 | 180 | to_utf8.feed(s) |
paul@396 | 181 | return utf8string(str(to_utf8), encoding) |
paul@392 | 182 | |
paul@390 | 183 | finally: |
paul@390 | 184 | to_utf8.close() |
paul@390 | 185 | |
paul@390 | 186 | # vim: tabstop=4 expandtab shiftwidth=4 |