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@396 | 24 | from native import str_add, 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@396 | 39 | |
paul@396 | 40 | def _binary_op(self, op, other): |
paul@396 | 41 | |
paul@396 | 42 | "Perform 'op' on this object and 'other' if appropriate." |
paul@396 | 43 | |
paul@396 | 44 | # Reject non-strings. |
paul@396 | 45 | |
paul@396 | 46 | if not _isinstance(other, basestring): |
paul@396 | 47 | return NotImplemented |
paul@396 | 48 | |
paul@396 | 49 | # Combining text with bytes. |
paul@396 | 50 | |
paul@396 | 51 | elif not _isinstance(other, utf8string): |
paul@396 | 52 | s = self.encode() |
paul@396 | 53 | return op(s.__data__, other.__data__) |
paul@396 | 54 | |
paul@396 | 55 | # Otherwise, perform the operation on the operands' data. |
paul@396 | 56 | |
paul@396 | 57 | else: |
paul@396 | 58 | return op(self.__data__, other.__data__) |
paul@390 | 59 | |
paul@396 | 60 | def _binary_op_rev(self, op, other): |
paul@396 | 61 | |
paul@396 | 62 | "Perform 'op' on 'other' and this object if appropriate." |
paul@396 | 63 | |
paul@396 | 64 | # Reject non-strings. |
paul@396 | 65 | |
paul@396 | 66 | if not _isinstance(other, basestring): |
paul@396 | 67 | return NotImplemented |
paul@396 | 68 | |
paul@396 | 69 | # Combining text with bytes. |
paul@396 | 70 | |
paul@396 | 71 | elif not _isinstance(other, utf8string): |
paul@396 | 72 | s = self.encode() |
paul@396 | 73 | return op(other.__data__, s.__data__) |
paul@396 | 74 | |
paul@396 | 75 | # Otherwise, perform the operation on the operands' data. |
paul@396 | 76 | |
paul@396 | 77 | else: |
paul@396 | 78 | return op(other.__data__, self.__data__) |
paul@396 | 79 | |
paul@396 | 80 | def encode(self, encoding=None): |
paul@396 | 81 | |
paul@396 | 82 | """ |
paul@396 | 83 | Encode the string to the given 'encoding' or any original encoding if |
paul@396 | 84 | omitted. |
paul@396 | 85 | """ |
paul@396 | 86 | |
paul@396 | 87 | encoding = encoding or self.encoding |
paul@396 | 88 | if not encoding: |
paul@396 | 89 | return self |
paul@390 | 90 | |
paul@390 | 91 | from_utf8 = Converter("UTF-8", encoding) |
paul@392 | 92 | |
paul@390 | 93 | try: |
paul@390 | 94 | from_utf8.feed(self) |
paul@390 | 95 | return str(from_utf8) |
paul@392 | 96 | |
paul@390 | 97 | finally: |
paul@390 | 98 | from_utf8.close() |
paul@390 | 99 | |
paul@390 | 100 | def unicode(s, encoding): |
paul@390 | 101 | |
paul@390 | 102 | "Convert 's' to a Unicode object, interpreting 's' as using 'encoding'." |
paul@390 | 103 | |
paul@390 | 104 | if isinstance(s, utf8string): |
paul@390 | 105 | return s |
paul@390 | 106 | |
paul@390 | 107 | # Obtain a string representation. |
paul@390 | 108 | |
paul@390 | 109 | s = s.__str__() |
paul@390 | 110 | |
paul@390 | 111 | # Convert the string to UTF-8. |
paul@390 | 112 | |
paul@390 | 113 | to_utf8 = Converter(encoding, "UTF-8") |
paul@392 | 114 | |
paul@390 | 115 | try: |
paul@390 | 116 | to_utf8.feed(s) |
paul@396 | 117 | return utf8string(str(to_utf8), encoding) |
paul@392 | 118 | |
paul@390 | 119 | finally: |
paul@390 | 120 | to_utf8.close() |
paul@390 | 121 | |
paul@390 | 122 | # vim: tabstop=4 expandtab shiftwidth=4 |