Skip to content
This repository was archived by the owner on May 20, 2024. It is now read-only.

Commit d91e8b1

Browse files
CodingMarkusCodingMarkus
CodingMarkus
authored and
CodingMarkus
committed
Improve stack variable naming
1 parent a424df2 commit d91e8b1

File tree

1 file changed

+34
-46
lines changed

1 file changed

+34
-46
lines changed

lib/psst/basic/stack.inc.sh

Lines changed: 34 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,6 @@ INCLUDE_SEEN_PSST="${INCLUDE_SEEN_PSST-} _stack.inc.sh_"
2222
# newValue: Value to push on stack.
2323
# stackName: Name of the stack (no spaces, only letters and underscore).
2424
#
25-
# RETURNS
26-
# 0: Success.
27-
# 1: Value could not be pushed to stack.
28-
#
2925
# SAMPLE
3026
# stack_push_psst "$value" "someStackName"
3127
#
@@ -42,25 +38,19 @@ stack_push_psst()
4238
#stackName=$2
4339

4440
# shellcheck disable=SC2034 # It is used but only indirect by eval
45-
_stackCountName_psst="${2}__stackCount_psst"
41+
_countName_stack_psst="${2}__count_stack_psst"
42+
eval "_count_stack_psst=\${$_countName_stack_psst-0}"
4643

47-
eval "_stackCount_psst=\${$_stackCountName_psst-}"
48-
if [ -z "$_stackCount_psst" ]
49-
then
50-
_stackCount_psst=0
51-
fi
52-
53-
_stackItemName_psst="${2}__$_stackCount_psst"
54-
_stackItemName_psst="${_stackItemName_psst}__stackItem_psst"
55-
_stackCount_psst=$(( _stackCount_psst + 1 ))
44+
_itemName_stack_psst="${2}__${_count_stack_psst}_item_stack_psst"
45+
_count_stack_psst=$(( _count_stack_psst + 1 ))
5646

57-
eval "$_stackItemName_psst=\"\$1\""
58-
eval "$_stackCountName_psst=$_stackCount_psst"
47+
eval "$_itemName_stack_psst=\"\$1\""
48+
eval "$_countName_stack_psst=$_count_stack_psst"
5949

6050
unset _stackName_psst
61-
unset _stackCountName_psst
62-
unset _stackCount_psst
63-
unset _stackItemName_psst
51+
unset _countName_stack_psst
52+
unset _count_stack_psst
53+
unset _itemName_stack_psst
6454
}
6555

6656

@@ -80,7 +70,7 @@ stack_push_psst()
8070
#
8171
# RETURNS
8272
# 0: Success.
83-
# 1: No stack with that name has been found.
73+
# 2: No stack with that name has been found.
8474
#
8575
# SAMPLE
8676
# if stack_pop_psst "someStackName" myResultVar
@@ -102,35 +92,33 @@ stack_pop_psst()
10292
# resultVarName=$2
10393

10494
# shellcheck disable=SC2034 # It is used but only indirect by eval
105-
_stackCountName_psst="${1}__stackCount_psst"
106-
eval "_stackCount_psst=\${$_stackCountName_psst-}"
95+
_countName_stack_psst="${1}__count_stack_psst"
96+
eval "_count_stack_psst=\${$_countName_stack_psst-}"
10797

108-
if [ -z "$_stackCount_psst" ]
98+
if [ -z "$_count_stack_psst" ]
10999
then
110100
# Clean up and report error
111-
unset _stackCountName_psst
112-
unset _stackCount_psst
113-
return 1
101+
unset _countName_stack_psst
102+
unset _count_stack_psst
103+
return 2
114104
fi
115105

116-
_stackCount_psst=$(( _stackCount_psst - 1 ))
117-
118-
_stackItemName_psst="${1}__$_stackCount_psst"
119-
_stackItemName_psst="${_stackItemName_psst}__stackItem_psst"
106+
_count_stack_psst=$(( _count_stack_psst - 1 ))
107+
_itemName_stack_psst="${1}__${_count_stack_psst}_item_stack_psst"
120108

121-
eval "$2=\"\$$_stackItemName_psst\""
122-
unset "$_stackItemName_psst"
109+
eval "$2=\"\$$_itemName_stack_psst\""
110+
unset "$_itemName_stack_psst"
123111

124-
if [ $_stackCount_psst -eq 0 ]
112+
if [ $_count_stack_psst -eq 0 ]
125113
then
126-
unset "$_stackCountName_psst"
114+
unset "$_countName_stack_psst"
127115
else
128-
eval "$_stackCountName_psst=$_stackCount_psst"
116+
eval "$_countName_stack_psst=$_count_stack_psst"
129117
fi
130118

131-
unset _stackCountName_psst
132-
unset _stackCount_psst
133-
unset _stackItemName_psst
119+
unset _countName_stack_psst
120+
unset _count_stack_psst
121+
unset _itemName_stack_psst
134122
}
135123

136124

@@ -147,7 +135,7 @@ stack_pop_psst()
147135
#
148136
# RETURNS
149137
# 0: Stack does exist.
150-
# 1: Stack does not exist.
138+
# 2: Stack does not exist.
151139
#
152140
# SAMPLE
153141
# if stack_exists_psst "someStackName"
@@ -161,18 +149,18 @@ stack_exists_psst()
161149
# variables in the main shell. Thus we need to be careful to not conflict
162150
# when defining local variables.
163151

164-
_stackCountName_psst="${1}__stackCount_psst"
165-
eval "_stackCount_psst=\${$_stackCountName_psst-}"
166-
unset _stackCountName_psst
152+
_countName_stack_psst="${1}__count_stack_psst"
153+
eval "_count_stack_psst=\${$_countName_stack_psst-}"
154+
unset _countName_stack_psst
167155

168156
#stackName=$1
169157

170-
if [ -n "${_stackCount_psst-}" ]
158+
if [ -n "${_count_stack_psst-}" ]
171159
then
172-
unset _stackCount_psst
160+
unset _count_stack_psst
173161
return 0
174162
fi
175163

176-
unset _stackCount_psst
177-
return 1
164+
unset _count_stack_psst
165+
return 2
178166
}

0 commit comments

Comments
 (0)