comparison configmix/config.py @ 515:3387a9d5fb12

Performance: compute the length of the constant input strinc only once and compute the new rest earlier and use the computed value also in comparisons
author Franz Glasner <fzglas.hg@dom66.de>
date Sun, 19 Dec 2021 14:10:28 +0100
parents 599e8ade567c
children ad1e630ba736
comparison
equal deleted inserted replaced
514:d803321d927b 515:3387a9d5fb12
744 def expand_variable(self, s): 744 def expand_variable(self, s):
745 """Expand variables in the single string `s`""" 745 """Expand variables in the single string `s`"""
746 start = s.find(_STARTTOK, 0) 746 start = s.find(_STARTTOK, 0)
747 if start < 0: 747 if start < 0:
748 return s 748 return s
749 len_s = len(s)
749 res = [] 750 res = []
750 res_append = res.append 751 res_append = res.append
751 rest = 0 752 rest = 0
752 while start != -1: 753 while start != -1:
753 res_append(s[rest:start]) 754 res_append(s[rest:start])
771 warnings.warn("Cannot expand variable %r in string " 772 warnings.warn("Cannot expand variable %r in string "
772 "%r" % (varname, s, ), 773 "%r" % (varname, s, ),
773 UserWarning, 774 UserWarning,
774 stacklevel=1) 775 stacklevel=1)
775 raise 776 raise
777 rest = end + 2
776 # 778 #
777 # Dont apply and type conversions to the variable value if 779 # Dont apply and type conversions to the variable value if
778 # the whole `s` is just one expansion 780 # the whole `s` is just one expansion
779 # 781 #
780 if (start == 0) and (end + 2 == len(s)): 782 if (start == 0) and (rest == len_s):
781 return varvalue 783 return varvalue
782 if not varvalue: 784 if not varvalue:
783 # None and/or empty str are handled equally here 785 # None and/or empty str are handled equally here
784 pass 786 pass
785 else: 787 else:
786 res_append(str_and_u(varvalue)) 788 res_append(str_and_u(varvalue))
787 # don't re-evaluate because `self.getvar_s()` expands already 789 # don't re-evaluate because `self.getvar_s()` expands already
788 rest = end + 2
789 start = s.find(_STARTTOK, rest) 790 start = s.find(_STARTTOK, rest)
790 res_append(s[rest:]) 791 res_append(s[rest:])
791 return _EMPTY_STR.join(res) 792 return _EMPTY_STR.join(res)
792 793
793 def _apply_filters(self, filters, value): 794 def _apply_filters(self, filters, value):