Skip to content

Commit

Permalink
New plugin datatype/number.
Browse files Browse the repository at this point in the history
Additional number conversion&handling methods.
  • Loading branch information
Roberto Reale committed Nov 16, 2016
1 parent de585d8 commit a0df0c4
Showing 1 changed file with 123 additions and 0 deletions.
123 changes: 123 additions & 0 deletions plugins/datatype/number
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
#!/bin/bash


################################################################################
#
# | | | |
# |---.,---.,---.|---.| ,---.|--- ,---.
# | |,---|`---.| || |---'| `---.
# `---'`---^`---'` '`---'`---'`---'`---'
#
#
# Bashlets -- A modular extensible toolbox for Bash
#
# Copyright (c) 2014-6 Roberto Reale
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#
################################################################################


#@method
function bashlet_datatype_number_to_roman()
{
########################################################################
#
# Code borrowed from:
#
# roman - convert number (1-9999) to Roman numeral
# cf. https://gist.github.com/ldante86/7816b0750b80df06527a9799a40b4b02
#
# Copyright (C) 2014 Luciano D. Cecere <ldante86@aol.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
#########################################################################

declare -a base=(I II III IV V VI VII VIII IX)
declare -a tens=(X XX XXX XL L LX LXX LXXX XC)
declare -a hundreds=(C CC CCC CD D DC DCC DCCC CM)
declare -a thousands=(M MM MMM ^IV ^V ^VI ^VII ^VIII ^IX)

local input

case "$1" in
[1-9]|[1-9][0-9]|[1-9][0-9][0-9][0-9]|[1-9][0-9][0-9])
input="$1" ;;
*)
# TODO echo "Out of range [1-9999]"
return 1
esac

local subst_0=${input:0:1}
local subst_1=${input:1:1}
local subst_2=${input:2:1}
local subst_3=${input:3:1}

local base_0=${base[subst_0-1]}
local base_1=${base[subst_1-1]}
local base_2=${base[subst_2-1]}
local base_3=${base[subst_3-1]}
local tens_0=${tens[subst_0-1]}
local tens_1=${tens[subst_1-1]}
local tens_2=${tens[subst_2-1]}
local hundreds_0=${hundreds[subst_0-1]}
local hundreds_1=${hundreds[subst_1-1]}
local thousands_0=${thousands[subst_0-1]}

case ${#input} in
1) echo $base_0
;;
2) [ $subst_1 -eq 0 ] && echo $tens_0
[ $subst_1 -gt 0 ] && echo $tens_0$base_1
;;
3) [ $subst_1 -eq 0 -a $subst_2 -eq 0 ] && echo $hundreds_0
[ $subst_1 -gt 0 -a $subst_2 -eq 0 ] && echo $hundreds_0$tens_1
[ $subst_1 -eq 0 -a $subst_2 -gt 0 ] && echo $hundreds_0$base_2
[ $subst_1 -gt 0 -a $subst_2 -gt 0 ] && echo $hundreds_0$tens_1$base_2
;;
4) [ $subst_1 -eq 0 -a $subst_2 -eq 0 -a $subst_3 -eq 0 ] && echo $thousands_0
[ $subst_1 -eq 0 -a $subst_2 -eq 0 -a $subst_3 -gt 0 ] && echo $thousands_0$base_3
[ $subst_1 -eq 0 -a $subst_2 -gt 0 -a $subst_3 -eq 0 ] && echo $thousands_0$tens_2
[ $subst_1 -eq 0 -a $subst_2 -gt 0 -a $subst_3 -gt 0 ] && echo $thousands_0$tens_2$base_3
[ $subst_1 -gt 0 -a $subst_2 -eq 0 -a $subst_3 -eq 0 ] && echo $thousands_0$hundreds_1
[ $subst_1 -gt 0 -a $subst_2 -eq 0 -a $subst_3 -gt 0 ] && echo $thousands_0$hundreds_1$base_3
[ $subst_1 -gt 0 -a $subst_2 -gt 0 -a $subst_3 -eq 0 ] && echo $thousands_0$hundreds_1$tens_2
[ $subst_1 -gt 0 -a $subst_2 -gt 0 -a $subst_3 -gt 0 ] && echo $thousands_0$hundreds_1$tens_2$base_3
esac
}

# Local variables:
# mode: shell-script
# sh-basic-offset: 4
# sh-indent-comment: t
# indent-tabs-mode: nil
# End:
# ex: ts=4 sw=4 et filetype=sh

0 comments on commit a0df0c4

Please sign in to comment.