Skip to content

Commit a777370

Browse files
author
Andy C
committed
[osh refactor] Introduce word_.MakePiece()
There is now one usage that doesn't use one of the word_.*Piece* constructors, which makes it stand out. That's the one that takes the IS_SUBST flag into account, for test operations.
1 parent db6c88f commit a777370

File tree

2 files changed

+18
-10
lines changed

2 files changed

+18
-10
lines changed

osh/word_.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,14 @@
3030
_ = log
3131

3232

33+
def MakePiece(s, quoted):
34+
# type: (str, bool) -> Piece
35+
"""
36+
For $x versus "$x", etc.
37+
"""
38+
return Piece(s, quoted, not quoted)
39+
40+
3341
def PieceQuoted(s):
3442
# type: (str) -> Piece
3543
"""

osh/word_eval.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -214,11 +214,11 @@ def _ValueToPartValue(val, quoted, part_loc):
214214
if case(value_e.Undef):
215215
# This happens in the case of ${undef+foo}. We skipped _ProcessUndef,
216216
# but we have to append to the empty string.
217-
return Piece('', quoted, not quoted)
217+
return word_.MakePiece('', quoted)
218218

219219
elif case(value_e.Str):
220220
val = cast(value.Str, UP_val)
221-
return Piece(val.s, quoted, not quoted)
221+
return word_.MakePiece(val.s, quoted)
222222

223223
elif case(value_e.InternalStringArray):
224224
val = cast(value.InternalStringArray, UP_val)
@@ -239,7 +239,7 @@ def _ValueToPartValue(val, quoted, part_loc):
239239
elif case(value_e.Null, value_e.Bool, value_e.Int, value_e.Float,
240240
value_e.Eggex, value_e.List):
241241
s = val_ops.Stringify(val, loc.WordPart(part_loc), 'Word eval ')
242-
return Piece(s, quoted, not quoted)
242+
return word_.MakePiece(s, quoted)
243243

244244
else:
245245
raise error.TypeErr(val, "Can't substitute into word",
@@ -302,7 +302,7 @@ def _MakeWordFrames(part_vals):
302302
continue # ignore undefined array entries
303303

304304
# Arrays parts are not quoted for $* and $@
305-
piece = Piece(s, p.quoted, not p.quoted)
305+
piece = word_.MakePiece(s, p.quoted)
306306
if is_first:
307307
current.append(piece)
308308
is_first = False
@@ -1571,7 +1571,7 @@ def _EvalBracedVarSub(self, part, part_vals, quoted):
15711571
if quoted and nullary_op.id == Id.VOp3_Star:
15721572
sep = self.splitter.GetJoinChar()
15731573
part_vals.append(
1574-
Piece(sep.join(names), quoted, not quoted))
1574+
word_.MakePiece(sep.join(names), quoted))
15751575
else:
15761576
part_vals.append(part_value.Array(names, quoted))
15771577
return # EARLY RETURN
@@ -1602,7 +1602,7 @@ def _EvalBracedVarSub(self, part, part_vals, quoted):
16021602
val = self._ProcessUndef(val, part.name_tok, vsub_state)
16031603

16041604
n = self._Count(val, part.name_tok)
1605-
part_vals.append(Piece(str(n), quoted, not quoted))
1605+
part_vals.append(word_.MakePiece(str(n), quoted))
16061606
return # EARLY EXIT: nothing else can come after length
16071607

16081608
elif part.prefix_op.id == Id.VSub_Bang:
@@ -1908,7 +1908,7 @@ def _EvalWordPart(self, part, part_vals, flags):
19081908
elif case(word_part_e.ArithSub):
19091909
part = cast(word_part.ArithSub, UP_part)
19101910
num = self.arith_ev.EvalToBigInt(part.anode)
1911-
v = Piece(mops.ToStr(num), quoted, not quoted)
1911+
v = word_.MakePiece(mops.ToStr(num), quoted)
19121912
part_vals.append(v)
19131913

19141914
elif case(word_part_e.ExtGlob):
@@ -1957,7 +1957,7 @@ def _EvalRhsWordToParts(self, w, part_vals, eval_flags=0):
19571957
UP_w = w
19581958
with tagswitch(w) as case:
19591959
if case(rhs_word_e.Empty):
1960-
part_vals.append(Piece('', quoted, not quoted))
1960+
part_vals.append(word_.MakePiece('', quoted))
19611961

19621962
elif case(rhs_word_e.Compound):
19631963
w = cast(CompoundWord, UP_w)
@@ -2607,7 +2607,7 @@ def _EvalCommandSub(self, cs_part, quoted):
26072607
#strs = self.splitter.SplitForWordEval(stdout_str)
26082608
return part_value.Array(strs, True)
26092609
else:
2610-
return Piece(stdout_str, quoted, not quoted)
2610+
return word_.MakePiece(stdout_str, quoted)
26112611

26122612
def _EvalProcessSub(self, cs_part):
26132613
# type: (CommandSub) -> Piece
@@ -2652,7 +2652,7 @@ def _EvalCommandSub(self, cs_part, quoted):
26522652
if cs_part.left_token.id == Id.Left_AtParen:
26532653
return part_value.Array([_DUMMY], quoted)
26542654
else:
2655-
return Piece(_DUMMY, quoted, not quoted)
2655+
return word_.MakePiece(_DUMMY, quoted)
26562656

26572657
def _EvalProcessSub(self, cs_part):
26582658
# type: (CommandSub) -> Piece

0 commit comments

Comments
 (0)