#!/usr/bin/env bash

set -o nounset -o noglob +o braceexpand
shopt -s lastpipe
export LC_ALL='C.UTF-8'

tab_spaces=8

sed_expr='s/[[:blank:]]+$//'

test=$'  \tLine with tabs\t why?\t  '

repeat="${1}"

for (( i = 0; i < repeat; i++ )); do
  printf '%s\n' "${test}"
done > tab-input.txt

printf '%s' "Standard:"
time {
  sed --binary --regexp-extended --expression="${sed_expr}" < tab-input.txt |
    expand --tabs="${tab_spaces}" > /dev/null
}

printf '%s' "Line-buffered:"
time {
  stdbuf --output=L -- \
      sed --binary --regexp-extended --expression="${sed_expr}" < tab-input.txt |
    stdbuf --output=L -- \
        expand --tabs="${tab_spaces}" > /dev/null
}

printf '%s' "Unbuffered:"
time {
  stdbuf --output=0 -- \
      sed --binary --regexp-extended --expression="${sed_expr}" < tab-input.txt |
    stdbuf --output=0 -- \
        expand --tabs="${tab_spaces}" > /dev/null
}
