strings.subr revision 240770
185815Sobrienif [ ! "$_STRINGS_SUBR" ]; then _STRINGS_SUBR=1 285815Sobrien# 385815Sobrien# Copyright (c) 2006-2012 Devin Teske 485815Sobrien# All Rights Reserved. 585815Sobrien# 685815Sobrien# Redistribution and use in source and binary forms, with or without 785815Sobrien# modification, are permitted provided that the following conditions 885815Sobrien# are met: 985815Sobrien# 1. Redistributions of source code must retain the above copyright 1085815Sobrien# notice, this list of conditions and the following disclaimer. 1185815Sobrien# 2. Redistributions in binary form must reproduce the above copyright 1285815Sobrien# notice, this list of conditions and the following disclaimer in the 1385815Sobrien# documentation and/or other materials provided with the distribution. 1485815Sobrien# 1585815Sobrien# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 1685815Sobrien# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING BUT NOT LIMITED TO, THE 1785815Sobrien# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1885815Sobrien# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 1985815Sobrien# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2085815Sobrien# DAMAGES (INLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2185815Sobrien# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2285815Sobrien# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2385815Sobrien# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2485815Sobrien# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2585815Sobrien# SUCH DAMAGE. 2685815Sobrien# 2785815Sobrien# $FreeBSD: head/usr.sbin/bsdconfig/share/strings.subr 240770 2012-09-21 01:36:20Z dteske $ 2885815Sobrien 2985815Sobrien# f_substr "$string" $start [ $length ] 3085815Sobrien# 3185815Sobrien# Simple wrapper to awk(1)'s `substr' function. 3285815Sobrien# 3385815Sobrienf_substr() 3485815Sobrien{ 3585815Sobrien local string="$1" start="${2:-0}" len="${3:-0}" 3685815Sobrien echo "$string" | awk "{ print substr(\$0, $start, $len) }" 3785815Sobrien} 3885815Sobrien 3985815Sobrien# f_longest_line_length 4085815Sobrien# 4185815Sobrien# Simple wrapper to an awk(1) script to print the length of the longest line of 4285815Sobrien# input (read from stdin). Supports the newline escape-sequence `\n' for 4385815Sobrien# splitting a single line into multiple lines. 4485815Sobrien# 4585815Sobrienf_longest_line_length_awk=' 4685815SobrienBEGIN { longest = 0 } 4785815Sobrien{ 4885815Sobrien if (split($0, lines, /\\n/) > 1) 4985815Sobrien { 5085815Sobrien for (n in lines) 5185815Sobrien { 5285815Sobrien len = length(lines[n]) 5385815Sobrien longest = ( len > longest ? len : longest ) 5485815Sobrien } 5585815Sobrien } 5685815Sobrien else 5785815Sobrien { 5885815Sobrien len = length($0) 5985815Sobrien longest = ( len > longest ? len : longest ) 6085815Sobrien } 6185815Sobrien} 6285815SobrienEND { print longest } 6385815Sobrien' 6485815Sobrienf_longest_line_length() 6585815Sobrien{ 6685815Sobrien awk "$f_longest_line_length_awk" 6785815Sobrien} 6885815Sobrien 6985815Sobrien# f_number_of_lines 7085815Sobrien# 7185815Sobrien# Simple wrapper to an awk(1) script to print the number of lines read from 7285815Sobrien# stdin. Supports newline escape-sequence `\n' for splitting a single line into 7385815Sobrien# multiple lines. 7485815Sobrien# 7585815Sobrienf_number_of_lines_awk=' 7685815SobrienBEGIN { num_lines = 0 } 7785815Sobrien{ 7885815Sobrien num_lines += split($0, unused, /\\n/) 7985815Sobrien} 8085815SobrienEND { print num_lines } 8185815Sobrien' 8285815Sobrienf_number_of_lines() 8385815Sobrien{ 8485815Sobrien awk "$f_number_of_lines_awk" 8585815Sobrien} 8685815Sobrien 8785815Sobrien# f_isinteger $arg 8885815Sobrien# 8985815Sobrien# Returns true if argument is a positive/negative whole integer. 9085815Sobrien# 9185815Sobrienf_isinteger() 9285815Sobrien{ 9385815Sobrien local arg="$1" 9485815Sobrien 9585815Sobrien # Prevent division-by-zero 9685815Sobrien [ "$arg" = "0" ] && return $SUCCESS 9785815Sobrien 9885815Sobrien # Attempt to perform arithmetic divison (an operation which will exit 9985815Sobrien # with error unless arg is a valid positive/negative whole integer). 10085815Sobrien # 10185815Sobrien ( : $((0/$arg)) ) >&- 2>&- 10285815Sobrien} 10385815Sobrien 10485815Sobrienfi # ! $_STRINGS_SUBR 10585815Sobrien