Skip to content

Commit

Permalink
Merge pull request #100 from Khronos31/encode
Browse files Browse the repository at this point in the history
unko.encodeを少し最適化
  • Loading branch information
greymd authored Apr 11, 2022
2 parents 93c23d8 + dd71d87 commit 3e7fed5
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 31 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ Commands
| unko.king | Build your king shift tower. |
| unko.fizzbuzz | No need to implement FizzBuzz. |
| unko.ping | Ping the 💩 domain. |
| unko.encode | Encode/Decode data and print to standard output. |
| unko.date | TBD |
| unko.awk | TBD |
| unko.xargs | TBD |
Expand Down
68 changes: 37 additions & 31 deletions bin/unko.encode
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
#!/bin/bash

UNKO_OCT=(う ん こ ウ ン コ 💩 👑)
if [[ "$LANG" = zh* ]] || [[ "$LANG" = en* ]]; then
export TEXTDOMAINDIR="/usr/share/locale-langpack/${LANG%%.*}/LC_MESSAGES"
else
export TEXTDOMAINDIR="/usr/share/locale-langpack/${LANG%%_*}/LC_MESSAGES"
fi
export TEXTDOMAIN=libc

UNKO_OCT="うんこウンコ💩👑"
usage() {
echo "Usage: $0 [OPTION] [FILE]"
echo "Unko encode or decode FILE, or standard input to standard output."
Expand All @@ -11,38 +17,30 @@ usage() {
}

encode() {
od -b -v -An "$1" |
tr -d ' \n' |
sed "
s/0/${UNKO_OCT[0x0]}/g;
s/1/${UNKO_OCT[0x1]}/g;
s/2/${UNKO_OCT[0x2]}/g;
s/3/${UNKO_OCT[0x3]}/g;
s/4/${UNKO_OCT[0x4]}/g;
s/5/${UNKO_OCT[0x5]}/g;
s/6/${UNKO_OCT[0x6]}/g;
s/7/${UNKO_OCT[0x7]}/g"
od -b -v -An |
sed -e "y/01234567/$UNKO_OCT/" |
tr -d ' \n'
echo
}

decode() {
sed "s/[^うんこウンコ💩👑]//g" "$1" |
tr -d '\n' |
sed "
s/${UNKO_OCT[0x0]}/0/g;
s/${UNKO_OCT[0x1]}/1/g;
s/${UNKO_OCT[0x2]}/2/g;
s/${UNKO_OCT[0x3]}/3/g;
s/${UNKO_OCT[0x4]}/4/g;
s/${UNKO_OCT[0x5]}/5/g;
s/${UNKO_OCT[0x6]}/6/g;
s/${UNKO_OCT[0x7]}/7/g" |
sed 's/.../0& /g' |
xargs printf %02x |
xxd -p -r
printf '%b' "$(
sed "s/[^$UNKO_OCT]//g" |
sed \
-e "s/${UNKO_OCT:0:1}/0/g" \
-e "s/${UNKO_OCT:1:1}/1/g" \
-e "s/${UNKO_OCT:2:1}/2/g" \
-e "s/${UNKO_OCT:3:1}/3/g" \
-e "s/${UNKO_OCT:4:1}/4/g" \
-e "s/${UNKO_OCT:5:1}/5/g" \
-e "s/${UNKO_OCT:6:1}/6/g" \
-e "s/${UNKO_OCT:7:1}/7/g" |
tr -d '\n' |
sed 's/.../\\&/g'
)"
}

MODE="ENCODE"
EXEC_COMMAND="encode"
FILE="-"

for arg in "$@"; do
Expand All @@ -51,15 +49,23 @@ for arg in "$@"; do
usage
exit 0
;;
"-d" | "--decode") MODE="DECODE" ;;
"-d" | "--decode") EXEC_COMMAND="decode" ;;
*) FILE="$arg" ;;
esac
done

if [ "$MODE" = "DECODE" ]; then
decode "$FILE"
if [ "$FILE" = - ]; then
"$EXEC_COMMAND"
else
encode "$FILE"
if [ -f "$FILE" ]; then
"$EXEC_COMMAND" < "$FILE"
elif [ -d "$FILE" ]; then
printf '%s: %s: %s\n' "$0" "$FILE" $"Is a directory" >&2
exit 1
elif ! [ -e "$FILE" ]; then
printf '%s: %s: %s\n' "$0" "$FILE" $"No such file or directory" >&2
exit 1
fi
fi

exit 0
1 change: 1 addition & 0 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ RUN echo -e $'\n\
ENV LANG ja_JP.UTF-8
RUN apt update -yqq \
&& apt install -y --no-install-recommends \
language-pack-ja-base \
toilet \
figlet \
bc \
Expand Down
80 changes: 80 additions & 0 deletions test/unko.encode-test.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#!/usr/bin/env bats
source functions.sh

readonly TARGET_COMMAND="../bin/unko.encode"

@test "-h でヘルプを出力する" {
run "$TARGET_COMMAND" -h
echo "$output"
[ "$status" -eq 0 ]
[[ "${lines[0]}" =~ ^Usage:.* ]]
coverage "$TARGET_COMMAND" -h
}

@test "--help でヘルプを出力する" {
run "$TARGET_COMMAND" --help
echo "$output"
[ "$status" -eq 0 ]
[[ "${lines[0]}" =~ ^Usage:.* ]]
coverage "$TARGET_COMMAND" --help
}

@test '引数なしのときは標準入力をエンコード' {
run "$TARGET_COMMAND" <<< うんこ
echo "$output"
[ "$status" -eq 0 ]
[ "${lines[0]}" = "ウンウこうんこう💩ウンウこうこここウウンウこうんここウうんこ" ]
coverage "$TARGET_COMMAND" <<< うんこ
}

@test '-d で標準入力をデコード {
run "$TARGET_COMMAND" -d <<< ウンウこうんこう💩ウンウこうこここウウンウこうんここウうんこ
echo "$output"
[ "$status" -eq 0 ]
[ "${lines[0]}" = "うんこ" ]
coverage "$TARGET_COMMAND" -d <<< ウンウこうんこう💩ウンウこうこここウウンウこうんここウうんこ
}
@test '--decode で標準入力をデコード {
run "$TARGET_COMMAND" --decode <<< ウンウこうんこう💩ウンウこうこここウウンウこうんここウうんこ
echo "$output"
[ "$status" -eq 0 ]
[ "${lines[0]}" = "うんこ" ]
coverage "$TARGET_COMMAND" --decode <<< ウンウこうんこう💩ウンウこうこここウウンウこうんここウうんこ
}

@test 'ファイルが存在しない場合エラー' {
export LANG=ja_JP.UTF-8
run "$TARGET_COMMAND" うんこ
echo "$output"
[ "$status" -eq 1 ]
[[ "${lines[0]}" =~ .*そのようなファイルやディレクトリはありません ]]
coverage LANG=ja_JP.UTF-8 "$TARGET_COMMAND" うんこ
}

@test 'ディレクトリの場合エラー' {
export LANG=ja_JP.UTF-8
run "$TARGET_COMMAND" .
echo "$output"
[ "$status" -eq 1 ]
[[ "${lines[0]}" =~ .*ディレクトリです ]]
coverage LANG=ja_JP.UTF-8 "$TARGET_COMMAND" .
}

@test 'ファイルが存在しない場合エラー(英語版)' {
export LANG=en_US.UTF-8
run "$TARGET_COMMAND" うんこ
echo "$output"
[ "$status" -eq 1 ]
[[ "${lines[0]}" =~ .*"No such file or directory" ]]
coverage LANG=en_US.UTF-8 "$TARGET_COMMAND" うんこ
}

@test 'ディレクトリの場合エラー(英語版)' {
export LANG=en_US.UTF-8
run "$TARGET_COMMAND" .
echo "$output"
[ "$status" -eq 1 ]
[[ "${lines[0]}" =~ .*"Is a directory" ]]
coverage LANG=en_US.UTF-8 "$TARGET_COMMAND" .
}

0 comments on commit 3e7fed5

Please sign in to comment.