1if [ ! "$_STRINGS_SUBR" ]; then _STRINGS_SUBR=1 2# 3# Copyright (c) 2006-2012 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 12# notice, this list of conditions and the following disclaimer in the 13# documentation and/or other materials provided with the distribution. 14# 15# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING BUT NOT LIMITED TO, THE 17# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 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$ 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}" 36 echo "$string" | awk "{ print substr(\$0, $start, $len) }" 37} 38 39# f_longest_line_length 40# 41# Simple wrapper to an awk(1) script to print the length of the longest line of 42# input (read from stdin). Supports the newline escape-sequence `\n' for 43# splitting a single line into multiple lines. 44# 45f_longest_line_length_awk=' 46BEGIN { longest = 0 } 47{ 48 if (split($0, lines, /\\n/) > 1) 49 { 50 for (n in lines) 51 { 52 len = length(lines[n]) 53 longest = ( len > longest ? len : longest ) 54 } 55 } 56 else 57 { 58 len = length($0) 59 longest = ( len > longest ? len : longest ) 60 } 61} 62END { print longest } 63' 64f_longest_line_length() 65{ 66 awk "$f_longest_line_length_awk" 67} 68 69# f_number_of_lines 70# 71# Simple wrapper to an awk(1) script to print the number of lines read from 72# stdin. Supports newline escape-sequence `\n' for splitting a single line into 73# multiple lines. 74# 75f_number_of_lines_awk=' 76BEGIN { num_lines = 0 } 77{ 78 num_lines += split(" "$0, unused, /\\n/) 79} 80END { print num_lines } 81' 82f_number_of_lines() 83{ 84 awk "$f_number_of_lines_awk" 85} 86 87# f_isinteger $arg 88# 89# Returns true if argument is a positive/negative whole integer. 90# 91f_isinteger() 92{ 93 local arg="$1" 94 95 # Prevent division-by-zero 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 104f_dprintf "%s: Successfully loaded." strings.subr 105 106fi # ! $_STRINGS_SUBR 107