Deleted Added
full compact
strings.subr (244675) strings.subr (247280)
1if [ ! "$_STRINGS_SUBR" ]; then _STRINGS_SUBR=1
2#
1if [ ! "$_STRINGS_SUBR" ]; then _STRINGS_SUBR=1
2#
3# Copyright (c) 2006-2012 Devin Teske
3# Copyright (c) 2006-2013 Devin Teske
4# All Rights Reserved.
5#
6# Redistribution and use in source and binary forms, with or without
7# modification, are permitted provided that the following conditions
8# are met:
9# 1. Redistributions of source code must retain the above copyright
10# notice, this list of conditions and the following disclaimer.
11# 2. Redistributions in binary form must reproduce the above copyright

--- 7 unchanged lines hidden (view full) ---

19# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20# DAMAGES (INLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25# SUCH DAMAGE.
26#
4# All Rights Reserved.
5#
6# Redistribution and use in source and binary forms, with or without
7# modification, are permitted provided that the following conditions
8# are met:
9# 1. Redistributions of source code must retain the above copyright
10# notice, this list of conditions and the following disclaimer.
11# 2. Redistributions in binary form must reproduce the above copyright

--- 7 unchanged lines hidden (view full) ---

19# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20# DAMAGES (INLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25# SUCH DAMAGE.
26#
27# $FreeBSD: head/usr.sbin/bsdconfig/share/strings.subr 244675 2012-12-25 10:47:45Z dteske $
27# $FreeBSD: head/usr.sbin/bsdconfig/share/strings.subr 247280 2013-02-25 19:55:32Z dteske $
28
29# f_substr "$string" $start [ $length ]
30#
31# Simple wrapper to awk(1)'s `substr' function.
32#
33f_substr()
34{
35 local string="$1" start="${2:-0}" len="${3:-0}"

--- 60 unchanged lines hidden (view full) ---

96 [ "$arg" = "0" ] && return $SUCCESS
97
98 # Attempt to perform arithmetic divison (an operation which will exit
99 # with error unless arg is a valid positive/negative whole integer).
100 #
101 ( : $((0/$arg)) ) > /dev/null 2>&1
102}
103
28
29# f_substr "$string" $start [ $length ]
30#
31# Simple wrapper to awk(1)'s `substr' function.
32#
33f_substr()
34{
35 local string="$1" start="${2:-0}" len="${3:-0}"

--- 60 unchanged lines hidden (view full) ---

96 [ "$arg" = "0" ] && return $SUCCESS
97
98 # Attempt to perform arithmetic divison (an operation which will exit
99 # with error unless arg is a valid positive/negative whole integer).
100 #
101 ( : $((0/$arg)) ) > /dev/null 2>&1
102}
103
104# f_uriencode [$text]
105#
106# Encode $text for the purpose of embedding safely into a URL. Non-alphanumeric
107# characters are converted to `%XX' sequence where XX represents the hexa-
108# decimal ordinal of the non-alphanumeric character. If $text is missing, data
109# is instead read from standard input.
110#
111f_uriencode_awk='
112BEGIN {
113 output = ""
114 for (n = 0; n < 256; n++) pack[sprintf("%c", n)] = sprintf("%%%02x", n)
115}
116{
117 sline = ""
118 slen = length($0)
119 for (n = 1; n <= slen; n++) {
120 char = substr($0, n, 1)
121 if ( char !~ /^[[:alnum:]_]$/ ) char = pack[char]
122 sline = sline char
123 }
124 output = output ( output ? "%0a" : "" ) sline
125}
126END { print output }
127'
128f_uriencode()
129{
130 if [ $# -gt 0 ]; then
131 echo "$1" | awk "$f_uriencode_awk"
132 else
133 awk "$f_uriencode_awk"
134 fi
135}
136
137# f_uridecode [$text]
138#
139# Decode $text from a URI. Encoded characters are converted from their `%XX'
140# sequence into original unencoded ASCII sequences. If $text is missing, data
141# is instead read from standard input.
142#
143f_uridecode_awk='
144BEGIN { for (n = 0; n < 256; n++) chr[n] = sprintf("%c", n) }
145{
146 sline = ""
147 slen = length($0)
148 for (n = 1; n <= slen; n++)
149 {
150 seq = substr($0, n, 3)
151 if ( seq ~ /^%[[:xdigit:]][[:xdigit:]]$/ ) {
152 hex = substr(seq, 2, 2)
153 sline = sline chr[sprintf("%u", "0x"hex)]
154 n += 2
155 } else
156 sline = sline substr(seq, 1, 1)
157 }
158 print sline
159}
160'
161f_uridecode()
162{
163 if [ $# -gt 0 ]; then
164 echo "$1" | awk "$f_uridecode_awk"
165 else
166 awk "$f_uridecode_awk"
167 fi
168}
169
104f_dprintf "%s: Successfully loaded." strings.subr
105
106fi # ! $_STRINGS_SUBR
170f_dprintf "%s: Successfully loaded." strings.subr
171
172fi # ! $_STRINGS_SUBR