Permalink
Browse files
Implement slicing of arrays and strings.
Also add a test for slicing strings with utf-8 encoded characters. Only
bash passes this test.
Also, adjust allowed spec test failures.
Motivation: Aboriginal Linux uses slicing.
Loading branch information...
Showing
3 changed files
with
39 additions
and
7 deletions .
+23
−4
core/word_eval.py
+13
−0
spec/var-op-other.test.sh
+3
−3
test/spec.sh
@@ -815,10 +815,29 @@ def _EvalBracedVarSub(self, part, quoted):
raise AssertionError (val.__class__ .__name__ )
elif op.tag == suffix_op_e.Slice:
# Either string slicing or array slicing. However string slicing has
# a unicode problem?
# Or maybe have a different operator for byte slice and char slice.
raise NotImplementedError (op)
# NOTE : The beginning can be negative, but Python handles this. Might
# want to make it explicit.
# TODO : Check out of bounds errors? begin > end?
if op.begin:
begin = self .arith_ev.Eval(op.begin)
else :
begin = 0
if op.length:
length = self .arith_ev.Eval(op.length)
end = begin + length
else :
end = None # Python supports None as the end
if val.tag == value_e.Str: # Slice characters in a string.
# TODO : Need to support unicode? Write spec # tests.
val = runtime.Str(val.s[begin : end])
elif val.tag == value_e.StrArray: # Slice array entries.
val = runtime.StrArray(val.strs[begin : end])
else :
raise AssertionError (val.__class__ .__name__ )
# After applying suffixes, process decay_array here.
if decay_array:
@@ -1,6 +1,8 @@
#! /usr/bin/env bash
#
# Test combination of var ops.
#
# NOTE: There are also slice tests in {array,arith-context}.test.sh.
# ## String length
v=foo
@@ -22,6 +24,7 @@ echo ${#undef}
v=abcde
echo ${# v: 1: 3}
# status: 1
# OK osh status: 2
# N-I dash status: 0
# N-I dash stdout: 5
@@ -128,3 +131,13 @@ echo ${foo: i-3-2 : i + 2}
# stdout: def
# N-I dash status: 2
# N-I dash stdout-json: ""
# ## Slice String with Unicode
# mksh slices by bytes.
foo=' --μ--'
echo ${foo: 1: 3}
# stdout: -μ-
# BUG mksh stdout: -μ
# N-I dash status: 2
# N-I dash stdout-json: ""
@@ -380,7 +380,7 @@ var-op-test() {
}
var-op-other () {
sh-spec spec/var-op-other.test.sh --osh-failures-allowed 5 \
sh-spec spec/var-op-other.test.sh --osh-failures-allowed 2 \
${REF_SHELLS[@]} $OSH " $@ "
}
@@ -422,12 +422,12 @@ errexit() {
# There as many non-POSIX arithmetic contexts.
arith-context () {
sh-spec spec/arith-context.test.sh --osh-failures-allowed 7 \
sh-spec spec/arith-context.test.sh --osh-failures-allowed 3 \
$BASH $MKSH $ZSH $OSH " $@ "
}
array () {
sh-spec spec/array.test.sh --osh-failures-allowed 10 \
sh-spec spec/array.test.sh --osh-failures-allowed 7 \
$BASH $MKSH $OSH " $@ "
}
Toggle all file notes
0 comments on commit
77e8e89