Skip to content

Commit f10e43a

Browse files
committed
fix(ellipsize): use POSIX-safe slicing via awk
1 parent 8bece69 commit f10e43a

3 files changed

Lines changed: 71 additions & 48 deletions

File tree

README.adoc

Lines changed: 43 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -89,48 +89,48 @@ The public functions are, in alphabetical order:
8989
{url-base}#L246[`dng2jpg`],
9090
{url-base}#L252[`echo`],
9191
{url-base}#L274[`ellipsize`],
92-
{url-base}#L292[`file_exists`],
93-
{url-base}#L301[`gitlog`],
94-
{url-base}#L325[`grbt`],
95-
{url-base}#L337[`handle_pipefails`],
96-
{url-base}#L343[`heic2jpg`],
97-
{url-base}#L356[`inside`],
98-
{url-base}#L363[`isempty`],
99-
{url-base}#L387[`isfunc`],
100-
{url-base}#L400[`isnumber`],
101-
{url-base}#L408[`isreadable`],
102-
{url-base}#L417[`isroot`],
103-
{url-base}#L429[`issolid`],
104-
{url-base}#L463[`iswritable`],
105-
{url-base}#L477[`log`],
106-
{url-base}#L486[`loge`],
107-
{url-base}#L494[`logw`],
108-
{url-base}#L503[`map_del`],
109-
{url-base}#L523[`map_get`],
110-
{url-base}#L545[`map_put`],
111-
{url-base}#L557[`nmea2gpx`],
112-
{url-base}#L576[`pdf2jpg`],
113-
{url-base}#L581[`pdf2png`],
114-
{url-base}#L600[`prettytable`],
115-
{url-base}#L626[`prettyuptime`],
116-
{url-base}#L649[`realdir`],
117-
{url-base}#L658[`realpath`],
118-
{url-base}#L671[`semver`],
119-
{url-base}#L689[`should_continue`],
120-
{url-base}#L745[`timestamp`],
121-
{url-base}#L765[`tolog`],
122-
{url-base}#L771[`tologe`],
123-
{url-base}#L778[`tolower`],
124-
{url-base}#L796[`totsout`],
125-
{url-base}#L802[`tsout`],
126-
{url-base}#L810[`url_exists`],
127-
{url-base}#L834[`user_exists`],
128-
{url-base}#L850[`validate_cmd`],
129-
{url-base}#L857[`validate_var`],
130-
{url-base}#L864[`var_exists`],
131-
{url-base}#L898[`ver_ge`],
132-
{url-base}#L906[`vid2aud`],
133-
{url-base}#L920[`ytda`].
92+
{url-base}#L300[`file_exists`],
93+
{url-base}#L309[`gitlog`],
94+
{url-base}#L333[`grbt`],
95+
{url-base}#L345[`handle_pipefails`],
96+
{url-base}#L351[`heic2jpg`],
97+
{url-base}#L364[`inside`],
98+
{url-base}#L371[`isempty`],
99+
{url-base}#L395[`isfunc`],
100+
{url-base}#L408[`isnumber`],
101+
{url-base}#L416[`isreadable`],
102+
{url-base}#L425[`isroot`],
103+
{url-base}#L437[`issolid`],
104+
{url-base}#L471[`iswritable`],
105+
{url-base}#L485[`log`],
106+
{url-base}#L494[`loge`],
107+
{url-base}#L502[`logw`],
108+
{url-base}#L511[`map_del`],
109+
{url-base}#L531[`map_get`],
110+
{url-base}#L553[`map_put`],
111+
{url-base}#L565[`nmea2gpx`],
112+
{url-base}#L584[`pdf2jpg`],
113+
{url-base}#L589[`pdf2png`],
114+
{url-base}#L608[`prettytable`],
115+
{url-base}#L634[`prettyuptime`],
116+
{url-base}#L657[`realdir`],
117+
{url-base}#L666[`realpath`],
118+
{url-base}#L679[`semver`],
119+
{url-base}#L697[`should_continue`],
120+
{url-base}#L753[`timestamp`],
121+
{url-base}#L773[`tolog`],
122+
{url-base}#L779[`tologe`],
123+
{url-base}#L786[`tolower`],
124+
{url-base}#L804[`totsout`],
125+
{url-base}#L810[`tsout`],
126+
{url-base}#L818[`url_exists`],
127+
{url-base}#L842[`user_exists`],
128+
{url-base}#L858[`validate_cmd`],
129+
{url-base}#L865[`validate_var`],
130+
{url-base}#L872[`var_exists`],
131+
{url-base}#L906[`ver_ge`],
132+
{url-base}#L914[`vid2aud`],
133+
{url-base}#L928[`ytda`].
134134

135135
Global variables have the `BASE_` prefix and can be used by clients.
136136
Clients should place temporary files under `$BASE_WIP`.
@@ -205,7 +205,7 @@ eval "$(
205205
log I\'m using the shellbase.
206206
----
207207

208-
{url-base}#L600[`prettytable`] example:
208+
{url-base}#L608[`prettytable`] example:
209209

210210
[,sh]
211211
----

app/test-ellipsize-no

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/bin/sh
2+
# vi:et lbr noet sw=2 ts=2 tw=79 wrap
3+
# SPDX-FileCopyrightText: 2026 David Rabkin
4+
# SPDX-License-Identifier: 0BSD
5+
# shellcheck disable=SC1090,SC1091 # File not following.
6+
. "$(CDPATH='' cd -- "$(dirname -- "$0")" && pwd -P)"/../lib/base.sh
7+
ellipsize abc qwe

lib/base.sh

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ BASE_RC_CON_NO=14
4747
BASE_RC_CON_TO=13
4848
BASE_RC_DIE_NO=10
4949
BASE_SHOULD_CON=false
50-
BASE_VERSION=0.9.20260404
50+
BASE_VERSION=0.9.20260407
5151

5252
# Removes any file besides mp3, m4a, flac in the current directory.
5353
# Removes empty directories.
@@ -272,7 +272,16 @@ echo() {
272272
# The origin of the idea:
273273
# https://github.com/yegor256/ellipsized
274274
ellipsize() {
275-
local beg max="$2" str="$1" end
275+
local beg end max="$2" str="$1"
276+
[ $# -eq 2 ] || {
277+
loge ellipsize: expected 2 arguments, got "$#".
278+
return $BASE_RC_ARG_NO
279+
}
280+
isnumber "$max" || {
281+
loge ellipsize: max is not numeric: "$max".
282+
return $BASE_RC_ARG_NO
283+
}
284+
cmd_exists awk || return $?
276285
[ "${#str}" -le "$max" ] && {
277286
printf %s "$str"
278287
return
@@ -281,9 +290,16 @@ ellipsize() {
281290
end=$beg
282291
[ $((beg + end + 3)) -lt "$max" ] && beg=$((beg + 1))
283292
[ "$beg" -gt "$max" ] && beg=$max
284-
285-
# shellcheck disable=SC3057
286-
printf %s "${str:0:beg}...${str:$((${#str} - end))}"
293+
awk \
294+
-v b="$beg" \
295+
-v e="$end" \
296+
-v s="$str" \
297+
'BEGIN {
298+
n = length(s)
299+
p = (b > 0) ? substr(s, 1, b) : ""
300+
q = (e > 0) ? substr(s, n - e + 1) : ""
301+
printf "%s...%s", p, q
302+
}'
287303
}
288304

289305
# Verifies the existence of all files. Iterates through the arguments,

0 commit comments

Comments
 (0)