TextEncoder¶
from acron.core import TextEncoder
- class TextEncoder¶
Bases:
This class can be used to convert text between multiple representations, e.g. UTF-8 to UTF-16. You may use it as a static class object, passing the encoding each time, or you may create an instance and use that object, which will record the current encoding and retain the current string.
This class is also a base class of
TextNode, which inherits this functionality.Inheritance diagram
digraph inheritancea7cc361c4c { bgcolor=transparent; rankdir=BT; size="8.0, 12.0"; "DTOOL_SUPER_BASE" [fillcolor=white,fontname="Vera Sans, DejaVu Sans, Liberation Sans, Arial, Helvetica, sans",fontsize=11,height=0.25,shape=box,style=""]; "TextEncoder" [URL="acron.core.TextEncoder.html#acron.core.TextEncoder",fillcolor=white,fontname="Vera Sans, DejaVu Sans, Liberation Sans, Arial, Helvetica, sans",fontsize=11,height=0.25,shape=box,style="",target="_top",tooltip="/**"]; "DTOOL_SUPER_BASE" -> "TextEncoder" [arrowsize=0.75,style=""]; }-
enum Encoding¶
-
enumerator E_iso8859 = 0¶
-
enumerator E_utf8 = 1¶
-
enumerator E_utf16be = 2¶
-
enumerator E_cp437 = 3¶
-
enumerator E_unicode = 2¶
Deprecated alias for E_utf16be
-
enumerator E_iso8859 = 0¶
- __init__()¶
- append_text()¶
Appends the indicates string to the end of the stored text.
- append_unicode_char()¶
Appends a single character to the end of the stored text. This may be a wide character, up to 16 bits in Unicode.
- append_wtext()¶
Appends the indicates string to the end of the stored wide-character text.
- clear_text()¶
Removes the text from the
TextEncoder.
- decode_text()¶
Returns the given wstring decoded to a single-byte string, via the current encoding system.
Returns the given wstring decoded to a single-byte string, via the given encoding system.
- property default_encoding Encoding¶
Specifies the default encoding to be used for all subsequently created
TextEncoderobjects. Seeset_encoding().
- static encode_wchar()¶
Encodes a single Unicode character into a one-, two-, three-, or four-byte string, according to the given encoding system.
- encode_wtext()¶
Encodes a wide-text string into a single-char string, according to the current encoding.
Encodes a wide-text string into a single-char string, according to the given encoding.
- static get_default_encoding()¶
Specifies the default encoding to be used for all subsequently created
TextEncoderobjects. Seeset_encoding().
- get_encoded_char()¶
Returns the nth char of the stored text, as a one-, two-, or three-byte encoded string.
Returns the nth char of the stored text, as a one-, two-, or three-byte encoded string.
- get_encoding()¶
Returns the encoding by which the string set via
set_text()is to be interpreted. Seeset_encoding().
- get_num_chars()¶
Returns the number of characters in the stored text. This is a count of wide characters, after the string has been decoded according to
set_encoding().
- get_text()¶
Returns the current text, as encoded via the current encoding system.
Returns the current text, as encoded via the indicated encoding system.
- get_text_as_ascii()¶
Returns the text associated with the node, converted as nearly as possible to a fully-ASCII representation. This means replacing accented letters with their unaccented ASCII equivalents.
It is possible that some characters in the string cannot be converted to ASCII. (The string may involve symbols like the copyright symbol, for instance, or it might involve letters in some other alphabet such as Greek or Cyrillic, or even Latin letters like thorn or eth that are not part of the ASCII character set.) In this case, as much of the string as possible will be converted to ASCII, and the nonconvertible characters will remain encoded in the encoding specified by
set_encoding().
- get_unicode_char()¶
Returns the Unicode value of the nth character in the stored text. This may be a wide character (greater than 255), after the string has been decoded according to
set_encoding().
- get_wtext()¶
Returns the text associated with the
TextEncoder, as a wide-character string.
- get_wtext_as_ascii()¶
Returns the text associated with the node, converted as nearly as possible to a fully-ASCII representation. This means replacing accented letters with their unaccented ASCII equivalents.
It is possible that some characters in the string cannot be converted to ASCII. (The string may involve symbols like the copyright symbol, for instance, or it might involve letters in some other alphabet such as Greek or Cyrillic, or even Latin letters like thorn or eth that are not part of the ASCII character set.) In this case, as much of the string as possible will be converted to ASCII, and the nonconvertible characters will remain in their original form.
- has_text()¶
- is_wtext()¶
Returns true if any of the characters in the string returned by
get_wtext()are out of the range of an ASCII character (and, therefore,get_wtext()should be called in preference toget_text()).
- static lower()¶
Converts the string to lowercase, assuming the string is encoded in the default encoding.
Converts the string to lowercase, assuming the string is encoded in the indicated encoding.
- make_lower()¶
Adjusts the text stored within the encoder to all lowercase letters (preserving accent marks correctly).
- make_upper()¶
Adjusts the text stored within the encoder to all uppercase letters (preserving accent marks correctly).
- static reencode_text()¶
Given the indicated text string, which is assumed to be encoded via the encoding “from”, decodes it and then reencodes it into the encoding “to”, and returns the newly encoded string. This does not change or affect any properties on the
TextEncoderitself.
- static set_default_encoding()¶
Specifies the default encoding to be used for all subsequently created
TextEncoderobjects. Seeset_encoding().
- set_encoding()¶
Specifies how the string set via
set_text()is to be interpreted. The default, E_iso8859, means a standard string with one-byte characters (i.e. ASCII). Other encodings are possible to take advantage of character sets with more than 256 characters.This affects only future calls to
set_text(); it does not change text that was set previously.
- set_text()¶
Changes the text that is stored in the encoder. The text should be encoded according to the method indicated by
set_encoding(). Subsequent calls toget_text()will return this same string, whileget_wtext()will return the decoded version of the string.The two-parameter version of set_text() accepts an explicit encoding; the text is immediately decoded and stored as a wide-character string. Subsequent calls to
get_text()will return the same text re-encoded using whichever encoding is specified byset_encoding().
- set_unicode_char()¶
Sets the Unicode value of the nth character in the stored text. This may be a wide character (greater than 255), after the string has been decoded according to
set_encoding().
- set_wtext()¶
Direct support for wide-character strings. Now publishable with the new wstring support in interrogate.
Changes the text that is stored in the encoder. Subsequent calls to
get_wtext()will return this same string, whileget_text()will return the encoded version of the string.
- property text¶
- Getter
Returns the current text, as encoded via the current encoding system.
Returns the current text, as encoded via the indicated encoding system.
- Setter
Changes the text that is stored in the encoder. The text should be encoded according to the method indicated by
set_encoding(). Subsequent calls toget_text()will return this same string, whileget_wtext()will return the decoded version of the string.The two-parameter version of
set_text()accepts an explicit encoding; the text is immediately decoded and stored as a wide-character string. Subsequent calls toget_text()will return the same text re-encoded using whichever encoding is specified byset_encoding().
- static unicode_isalpha()¶
Returns true if the indicated character is an alphabetic letter, false otherwise. This is akin to ctype’s isalpha(), extended to Unicode.
- static unicode_isdigit()¶
Returns true if the indicated character is a numeric digit, false otherwise. This is akin to ctype’s isdigit(), extended to Unicode.
- static unicode_islower()¶
Returns true if the indicated character is a lowercase letter, false otherwise. This is akin to ctype’s islower(), extended to Unicode.
- static unicode_ispunct()¶
Returns true if the indicated character is a punctuation mark, false otherwise. This is akin to ctype’s ispunct(), extended to Unicode.
- static unicode_isspace()¶
Returns true if the indicated character is a whitespace letter, false otherwise. This is akin to ctype’s isspace(), extended to Unicode.
- static unicode_isupper()¶
Returns true if the indicated character is an uppercase letter, false otherwise. This is akin to ctype’s isupper(), extended to Unicode.
- static unicode_tolower()¶
Returns the uppercase equivalent of the given Unicode character. This is akin to ctype’s tolower(), extended to Unicode.
- static unicode_toupper()¶
Returns the uppercase equivalent of the given Unicode character. This is akin to ctype’s toupper(), extended to Unicode.
- static upper()¶
Converts the string to uppercase, assuming the string is encoded in the default encoding.
Converts the string to uppercase, assuming the string is encoded in the indicated encoding.
-
enum Encoding¶