strings.subr revision 244675
1238438Sdteskeif [ ! "$_STRINGS_SUBR" ]; then _STRINGS_SUBR=1
2238438Sdteske#
3238438Sdteske# Copyright (c) 2006-2012 Devin Teske
4238438Sdteske# All Rights Reserved.
5238438Sdteske#
6238438Sdteske# Redistribution and use in source and binary forms, with or without
7238438Sdteske# modification, are permitted provided that the following conditions
8238438Sdteske# are met:
9238438Sdteske# 1. Redistributions of source code must retain the above copyright
10238438Sdteske#    notice, this list of conditions and the following disclaimer.
11238438Sdteske# 2. Redistributions in binary form must reproduce the above copyright
12238438Sdteske#    notice, this list of conditions and the following disclaimer in the
13238438Sdteske#    documentation and/or other materials provided with the distribution.
14238438Sdteske#
15238438Sdteske# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16238438Sdteske# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING BUT NOT LIMITED TO, THE
17238438Sdteske# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18238438Sdteske# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19238438Sdteske# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20238438Sdteske# DAMAGES (INLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21238438Sdteske# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22238438Sdteske# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23238438Sdteske# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24238438Sdteske# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25238438Sdteske# SUCH DAMAGE.
26238438Sdteske#
27238438Sdteske# $FreeBSD: head/usr.sbin/bsdconfig/share/strings.subr 244675 2012-12-25 10:47:45Z dteske $
28238438Sdteske
29238438Sdteske# f_substr "$string" $start [ $length ]
30238438Sdteske#
31238438Sdteske# Simple wrapper to awk(1)'s `substr' function.
32238438Sdteske#
33238438Sdteskef_substr()
34238438Sdteske{
35238438Sdteske	local string="$1" start="${2:-0}" len="${3:-0}"
36238438Sdteske	echo "$string" | awk "{ print substr(\$0, $start, $len) }"
37238438Sdteske}
38238438Sdteske
39238438Sdteske# f_longest_line_length
40238438Sdteske#
41238438Sdteske# Simple wrapper to an awk(1) script to print the length of the longest line of
42238438Sdteske# input (read from stdin). Supports the newline escape-sequence `\n' for
43238438Sdteske# splitting a single line into multiple lines.
44238438Sdteske#
45238438Sdteskef_longest_line_length_awk='
46238438SdteskeBEGIN { longest = 0 }
47238438Sdteske{
48238438Sdteske	if (split($0, lines, /\\n/) > 1)
49238438Sdteske	{
50238438Sdteske		for (n in lines)
51238438Sdteske		{
52238438Sdteske			len = length(lines[n])
53238438Sdteske			longest = ( len > longest ? len : longest )
54238438Sdteske		}
55238438Sdteske	}
56238438Sdteske	else
57238438Sdteske	{
58238438Sdteske		len = length($0)
59238438Sdteske		longest = ( len > longest ? len : longest )
60238438Sdteske	}
61238438Sdteske}
62238438SdteskeEND { print longest }
63238438Sdteske'
64238438Sdteskef_longest_line_length()
65238438Sdteske{
66238438Sdteske	awk "$f_longest_line_length_awk"
67238438Sdteske}
68238438Sdteske
69238438Sdteske# f_number_of_lines
70238438Sdteske#
71238438Sdteske# Simple wrapper to an awk(1) script to print the number of lines read from
72238438Sdteske# stdin. Supports newline escape-sequence `\n' for splitting a single line into
73238438Sdteske# multiple lines.
74238438Sdteske#
75238438Sdteskef_number_of_lines_awk='
76238438SdteskeBEGIN { num_lines = 0 }
77238438Sdteske{
78241700Sdteske	num_lines += split(" "$0, unused, /\\n/)
79238438Sdteske}
80238438SdteskeEND { print num_lines }
81238438Sdteske'
82238438Sdteskef_number_of_lines()
83238438Sdteske{
84238438Sdteske	awk "$f_number_of_lines_awk"
85238438Sdteske}
86238438Sdteske
87238438Sdteske# f_isinteger $arg
88238438Sdteske#
89238438Sdteske# Returns true if argument is a positive/negative whole integer.
90238438Sdteske#
91238438Sdteskef_isinteger()
92238438Sdteske{
93238438Sdteske	local arg="$1"
94238438Sdteske
95238438Sdteske	# Prevent division-by-zero
96238438Sdteske	[ "$arg" = "0" ] && return $SUCCESS
97238438Sdteske
98238438Sdteske	# Attempt to perform arithmetic divison (an operation which will exit
99238438Sdteske	# with error unless arg is a valid positive/negative whole integer).
100238438Sdteske	#
101240783Sdteske	( : $((0/$arg)) ) > /dev/null 2>&1
102238438Sdteske}
103238438Sdteske
104244675Sdteskef_dprintf "%s: Successfully loaded." strings.subr
105244675Sdteske
106238438Sdteskefi # ! $_STRINGS_SUBR
107