comparison configmix/config.py @ 484:0259bd521729

Avoid repeaded string concatenations in .expand_variable(): use a list of string parts and proper indexes into the source string
author Franz Glasner <f.glasner@feldmann-mg.com>
date Fri, 17 Dec 2021 15:55:51 +0100
parents 38e4be1882e3
children 008b35666a43
comparison
equal deleted inserted replaced
483:38e4be1882e3 484:0259bd521729
221 substituted. 221 substituted.
222 222
223 """ 223 """
224 224
225 # Speed 225 # Speed
226 _TEXTTYPE = type(u("")) 226 _EMPTY_STR = u("")
227 _TEXTTYPE = type(_EMPTY_STR)
227 _STARTTOK = u(b"{{") 228 _STARTTOK = u(b"{{")
228 _ENDTOK = u(b"}}") 229 _ENDTOK = u(b"}}")
229 _HIER_SEPARATOR = u(b'.') 230 _HIER_SEPARATOR = u(b'.')
230 _NS_SEPARATOR = u(b':') 231 _NS_SEPARATOR = u(b':')
231 _FILTER_SEPARATOR = u(b'|') 232 _FILTER_SEPARATOR = u(b'|')
657 return obj 658 return obj
658 659
659 def expand_variable(self, s): 660 def expand_variable(self, s):
660 """Expand variables in the single string `s`""" 661 """Expand variables in the single string `s`"""
661 start = s.find(self._STARTTOK, 0) 662 start = s.find(self._STARTTOK, 0)
663 if start < 0:
664 return s
665 res = []
666 res_append = res.append
667 rest = 0
662 while start != -1: 668 while start != -1:
669 res_append(s[rest:start])
663 end = s.find(self._ENDTOK, start) 670 end = s.find(self._ENDTOK, start)
664 if end < 0: 671 if end < 0:
665 return s 672 rest = start
673 break
666 varname, filters = self._split_filters(s[start+2:end]) 674 varname, filters = self._split_filters(s[start+2:end])
667 try: 675 try:
668 if NONE_FILTER in filters: 676 if NONE_FILTER in filters:
669 varvalue = self._apply_filters( 677 varvalue = self._apply_filters(
670 filters, self.getvar_s(varname, default=None)) 678 filters, self.getvar_s(varname, default=None))
671 elif EMPTY_FILTER in filters: 679 elif EMPTY_FILTER in filters:
672 varvalue = self._apply_filters( 680 varvalue = self._apply_filters(
673 filters, self.getvar_s(varname, default=u(""))) 681 filters, self.getvar_s(varname,
682 default=self._EMPTY_STR))
674 else: 683 else:
675 varvalue = self._apply_filters( 684 varvalue = self._apply_filters(
676 filters, self.getvar_s(varname)) 685 filters, self.getvar_s(varname))
677 except KeyError: 686 except KeyError:
678 warnings.warn("Cannot expand variable %r in string " 687 warnings.warn("Cannot expand variable %r in string "
684 # Dont apply and type conversions to the variable value if 693 # Dont apply and type conversions to the variable value if
685 # the whole `s` is just one expansion 694 # the whole `s` is just one expansion
686 # 695 #
687 if (start == 0) and (end + 2 == len(s)): 696 if (start == 0) and (end + 2 == len(s)):
688 return varvalue 697 return varvalue
689 if varvalue is None: 698 if not varvalue:
690 varvalue = u("") 699 # None and/or empty str are handled equally here
691 replaced = s[:start] + u(str(varvalue)) 700 pass
692 s = replaced + s[end+2:] 701 else:
702 res_append(u(str(varvalue)))
693 # don't re-evaluate because `self.getvar_s()` expands already 703 # don't re-evaluate because `self.getvar_s()` expands already
694 start = s.find(self._STARTTOK, len(replaced)) 704 rest = end + 2
695 return s 705 start = s.find(self._STARTTOK, rest)
706 res_append(s[rest:])
707 return self._EMPTY_STR.join(res)
696 708
697 def _apply_filters(self, filters, value): 709 def _apply_filters(self, filters, value):
698 for name in filters: 710 for name in filters:
699 try: 711 try:
700 filterfn = lookup_filter(name) 712 filterfn = lookup_filter(name)