-
Notifications
You must be signed in to change notification settings - Fork 0
/
canonicalize_yang_decimal
executable file
·64 lines (61 loc) · 2.19 KB
/
canonicalize_yang_decimal
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#! /bin/sed -Ef
# canonicalize_yang_decimal - canonicalize YANG decimal literals
# Copyright (C) 2019 Erik Auerswald <[email protected]>
#
# 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/>.
# Turn decimal number literals into the canonical format for YANG decimal64
# numbers as defined in RFC 7950, section 9.3.2. "Canonical Form:"
#
# The canonical form of a positive decimal64 value does not include the
# sign "+". The decimal point is required. Leading and trailing zeros
# are prohibited, subject to the rule that there MUST be at least one
# digit before and after the decimal point. The value zero is
# represented as "0.0".
#
# Please note that the script does not check if the decimal number is
# actually representable as a YANG decimal64.
#
# This script canonicalizes every number literal in the input stream,
# number literals can be adjunct to non-numbers, e.g., text.
# add missing decimal point to integers
: adp
s/(^|[^-+.[:digit:]])([-+[:digit:]][[:digit:]]*)([^-+.[:digit:]]|$)/\1\2.0\3/
t adp
# add missing zero before decimal point
s/(^|[^[:digit:]])\./\10./g
# add missing zero after decimal point
s/\.([^[:digit:]]|$)/.0\1/g
# remove leading plus sign
s/\+([[:digit:]]+\.[[:digit:]]+)/\1/g
# canonicalize the number zero
## squeeze leading zeros
: czlz
s/(^|[^[:digit:]])-?0+0\./\10./
t czlz
## squeeze trailing zeros
: cztz
s/\.00+([^[:digit:]]|$)/.0\1/
t cztz
## negative zero == positive zero
: nnz
s/-0\.0([^[[:digit:]]|$)/0.0\1/
t nnz
# remove leading zeros
: rlz
s/(^|[^[:digit:]])0+([1-9][[:digit:]]*\.)/\1\2/
t rlz
# remove trailing zeros
: rtz
s/(\.[[:digit:]]*[1-9])0+([^[:digit:]]|$)/\1\2/
t rtz