1 |
commit: fba4880f560a03e52857af53c722b6b2138da732 |
2 |
Author: Zac Medico <zmedico <AT> gentoo <DOT> org> |
3 |
AuthorDate: Sat Mar 31 23:19:04 2012 +0000 |
4 |
Commit: Zac Medico <zmedico <AT> gentoo <DOT> org> |
5 |
CommitDate: Sat Mar 31 23:19:04 2012 +0000 |
6 |
URL: http://git.overlays.gentoo.org/gitweb/?p=proj/portage.git;a=commit;h=fba4880f |
7 |
|
8 |
varexpand: optimize access to current char |
9 |
|
10 |
--- |
11 |
pym/portage/util/__init__.py | 47 +++++++++++++++++++++++++----------------- |
12 |
1 files changed, 28 insertions(+), 19 deletions(-) |
13 |
|
14 |
diff --git a/pym/portage/util/__init__.py b/pym/portage/util/__init__.py |
15 |
index 3e7187c..d6ac46c 100644 |
16 |
--- a/pym/portage/util/__init__.py |
17 |
+++ b/pym/portage/util/__init__.py |
18 |
@@ -660,14 +660,15 @@ def varexpand(mystring, mydict=None, error_leader=None): |
19 |
This would be a good bunch of code to port to C. |
20 |
""" |
21 |
numvars=0 |
22 |
- mystring=" "+mystring |
23 |
#in single, double quotes |
24 |
insing=0 |
25 |
indoub=0 |
26 |
- pos=1 |
27 |
+ pos = 0 |
28 |
+ length = len(mystring) |
29 |
newstring = [] |
30 |
- while (pos<len(mystring)): |
31 |
- if (mystring[pos]=="'") and (mystring[pos-1]!="\\"): |
32 |
+ while pos < length: |
33 |
+ current = mystring[pos] |
34 |
+ if current == "'": |
35 |
if (indoub): |
36 |
newstring.append("'") |
37 |
else: |
38 |
@@ -675,7 +676,7 @@ def varexpand(mystring, mydict=None, error_leader=None): |
39 |
insing=not insing |
40 |
pos=pos+1 |
41 |
continue |
42 |
- elif (mystring[pos]=='"') and (mystring[pos-1]!="\\"): |
43 |
+ elif current == '"': |
44 |
if (insing): |
45 |
newstring.append('"') |
46 |
else: |
47 |
@@ -685,11 +686,11 @@ def varexpand(mystring, mydict=None, error_leader=None): |
48 |
continue |
49 |
if (not insing): |
50 |
#expansion time |
51 |
- if (mystring[pos]=="\n"): |
52 |
+ if current == "\n": |
53 |
#convert newlines to spaces |
54 |
newstring.append(" ") |
55 |
- pos=pos+1 |
56 |
- elif (mystring[pos]=="\\"): |
57 |
+ pos += 1 |
58 |
+ elif current == "\\": |
59 |
# For backslash expansion, this function used to behave like |
60 |
# echo -e, but that's not needed for our purposes. We want to |
61 |
# behave like bash does when expanding a variable assignment |
62 |
@@ -699,19 +700,27 @@ def varexpand(mystring, mydict=None, error_leader=None): |
63 |
# escaped quotes here, since getconfig() uses shlex |
64 |
# to handle that earlier. |
65 |
if (pos+1>=len(mystring)): |
66 |
- newstring.append(mystring[pos]) |
67 |
+ newstring.append(current) |
68 |
break |
69 |
else: |
70 |
- a = mystring[pos + 1] |
71 |
- pos = pos + 2 |
72 |
- if a in ("\\", "$"): |
73 |
- newstring.append(a) |
74 |
- elif a == "\n": |
75 |
+ current = mystring[pos + 1] |
76 |
+ pos += 2 |
77 |
+ if current == "$": |
78 |
+ newstring.append(current) |
79 |
+ elif current == "\\": |
80 |
+ newstring.append(current) |
81 |
+ # BUG: This spot appears buggy, but it's intended to |
82 |
+ # be bug-for-bug compatible with existing behavior. |
83 |
+ if pos < length and \ |
84 |
+ mystring[pos] in ("'", '"', "$"): |
85 |
+ newstring.append(mystring[pos]) |
86 |
+ pos += 1 |
87 |
+ elif current == "\n": |
88 |
pass |
89 |
else: |
90 |
newstring.append(mystring[pos - 2:pos]) |
91 |
continue |
92 |
- elif (mystring[pos]=="$") and (mystring[pos-1]!="\\"): |
93 |
+ elif current == "$": |
94 |
pos=pos+1 |
95 |
if mystring[pos]=="{": |
96 |
pos=pos+1 |
97 |
@@ -754,11 +763,11 @@ def varexpand(mystring, mydict=None, error_leader=None): |
98 |
if myvarname in mydict: |
99 |
newstring.append(mydict[myvarname]) |
100 |
else: |
101 |
- newstring.append(mystring[pos]) |
102 |
- pos=pos+1 |
103 |
+ newstring.append(current) |
104 |
+ pos += 1 |
105 |
else: |
106 |
- newstring.append(mystring[pos]) |
107 |
- pos=pos+1 |
108 |
+ newstring.append(current) |
109 |
+ pos += 1 |
110 |
|
111 |
return "".join(newstring) |