From 9aa1335fd28af92b8d15c84bf4da48b76d8b6f85 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Elsd=C3=B6rfer?= Date: Tue, 30 Aug 2016 17:13:05 +0200 Subject: [PATCH] Improve codepage selection logic. --- src/escpos/magicencode.py | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/src/escpos/magicencode.py b/src/escpos/magicencode.py index 8cfecf9..3d62dab 100644 --- a/src/escpos/magicencode.py +++ b/src/escpos/magicencode.py @@ -55,7 +55,7 @@ class Encoder(object): canonical encoding name; and also validate that the encoding is supported. - TOOD: Support encoding aliases. + TODO: Support encoding aliases: pc437 instead of cp437. """ encoding = CodePages.get_encoding(encoding) if not encoding in self.codepages: @@ -78,6 +78,14 @@ class Encoder(object): return True + def __encoding_sort_func(self, item): + key, index = item + return ( + key in self.used_encodings, + index + ) + + def find_suitable_encoding(self, char): """The order of our search is a specific one: @@ -93,17 +101,12 @@ class Encoder(object): is missing or incomplete, we might increase our change that the code page we pick for this character is actually supported. + """ + sorted_encodings = sorted( + self.codepages.items(), + key=self.__encoding_sort_func) - # TODO actually do speed up the search - """ - """ - - remove the ones not supported - - order by used first, then others - - do not use a cache, because encode already is so fast - """ - sorted_encodings = self.codepages.keys() - - for encoding in sorted_encodings: + for encoding, _ in sorted_encodings: if self.can_encode(encoding, char): # This encoding worked; at it to the set of used ones. self.used_encodings.add(encoding)