strings.subr revision 240783
1169691Skanif [ ! "$_STRINGS_SUBR" ]; then _STRINGS_SUBR=1
2169691Skan#
3169691Skan# Copyright (c) 2006-2012 Devin Teske
4169691Skan# All Rights Reserved.
5169691Skan#
6169691Skan# Redistribution and use in source and binary forms, with or without
7169691Skan# modification, are permitted provided that the following conditions
8169691Skan# are met:
9169691Skan# 1. Redistributions of source code must retain the above copyright
10169691Skan#    notice, this list of conditions and the following disclaimer.
11169691Skan# 2. Redistributions in binary form must reproduce the above copyright
12169691Skan#    notice, this list of conditions and the following disclaimer in the
13169691Skan#    documentation and/or other materials provided with the distribution.
14169691Skan#
15169691Skan# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16169691Skan# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING BUT NOT LIMITED TO, THE
17169691Skan# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18169691Skan# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19169691Skan# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20169691Skan# DAMAGES (INLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21169691Skan# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22233749Stheraven# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23169691Skan# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24169691Skan# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25169691Skan# SUCH DAMAGE.
26169691Skan#
27169691Skan# $FreeBSD: head/usr.sbin/bsdconfig/share/strings.subr 240783 2012-09-21 19:03:25Z dteske $
28169691Skan
29169691Skan# f_substr "$string" $start [ $length ]
30169691Skan#
31169691Skan# Simple wrapper to awk(1)'s `substr' function.
32169691Skan#
33169691Skanf_substr()
34169691Skan{
35169691Skan	local string="$1" start="${2:-0}" len="${3:-0}"
36169691Skan	echo "$string" | awk "{ print substr(\$0, $start, $len) }"
37169691Skan}
38169691Skan
39169691Skan# f_longest_line_length
40169691Skan#
41169691Skan# Simple wrapper to an awk(1) script to print the length of the longest line of
42169691Skan# input (read from stdin). Supports the newline escape-sequence `\n' for
43169691Skan# splitting a single line into multiple lines.
44169691Skan#
45169691Skanf_longest_line_length_awk='
46169691SkanBEGIN { longest = 0 }
47169691Skan{
48169691Skan	if (split($0, lines, /\\n/) > 1)
49169691Skan	{
50169691Skan		for (n in lines)
51169691Skan		{
52169691Skan			len = length(lines[n])
53169691Skan			longest = ( len > longest ? len : longest )
54169691Skan		}
55169691Skan	}
56169691Skan	else
57169691Skan	{
58169691Skan		len = length($0)
59169691Skan		longest = ( len > longest ? len : longest )
60169691Skan	}
61169691Skan}
62169691SkanEND { print longest }
63169691Skan'
64169691Skanf_longest_line_length()
65169691Skan{
66169691Skan	awk "$f_longest_line_length_awk"
67169691Skan}
68169691Skan
69169691Skan# f_number_of_lines
70169691Skan#
71169691Skan# Simple wrapper to an awk(1) script to print the number of lines read from
72169691Skan# stdin. Supports newline escape-sequence `\n' for splitting a single line into
73169691Skan# multiple lines.
74169691Skan#
75169691Skanf_number_of_lines_awk='
76169691SkanBEGIN { num_lines = 0 }
77169691Skan{
78169691Skan	num_lines += split($0, unused, /\\n/)
79169691Skan}
80169691SkanEND { print num_lines }
81169691Skan'
82169691Skanf_number_of_lines()
83169691Skan{
84169691Skan	awk "$f_number_of_lines_awk"
85169691Skan}
86169691Skan
87169691Skan# f_isinteger $arg
88169691Skan#
89169691Skan# Returns true if argument is a positive/negative whole integer.
90169691Skan#
91169691Skanf_isinteger()
92169691Skan{
93169691Skan	local arg="$1"
94169691Skan
95169691Skan	# Prevent division-by-zero
96169691Skan	[ "$arg" = "0" ] && return $SUCCESS
97169691Skan
98169691Skan	# Attempt to perform arithmetic divison (an operation which will exit
99169691Skan	# with error unless arg is a valid positive/negative whole integer).
100169691Skan	#
101169691Skan	( : $((0/$arg)) ) > /dev/null 2>&1
102169691Skan}
103169691Skan
104169691Skanfi # ! $_STRINGS_SUBR
105169691Skan