strings.subr revision 241700
150476Speterif [ ! "$_STRINGS_SUBR" ]; then _STRINGS_SUBR=1 221612Swosch# 321612Swosch# Copyright (c) 2006-2012 Devin Teske 421612Swosch# All Rights Reserved. 521612Swosch# 621612Swosch# Redistribution and use in source and binary forms, with or without 721612Swosch# modification, are permitted provided that the following conditions 821612Swosch# are met: 921612Swosch# 1. Redistributions of source code must retain the above copyright 1021612Swosch# notice, this list of conditions and the following disclaimer. 1121612Swosch# 2. Redistributions in binary form must reproduce the above copyright 1221612Swosch# notice, this list of conditions and the following disclaimer in the 1321612Swosch# documentation and/or other materials provided with the distribution. 1430350Swosch# 1530350Swosch# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 1630350Swosch# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING BUT NOT LIMITED TO, THE 1730350Swosch# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1830350Swosch# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 1930046Swosch# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2030350Swosch# DAMAGES (INLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2130046Swosch# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2223546Swosch# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2323546Swosch# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2421612Swosch# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25139761Skrion# SUCH DAMAGE. 2621612Swosch# 2730350Swosch# $FreeBSD: head/usr.sbin/bsdconfig/share/strings.subr 241700 2012-10-18 17:09:45Z dteske $ 2830350Swosch 2930350Swosch# f_substr "$string" $start [ $length ] 3021612Swosch# 3121612Swosch# Simple wrapper to awk(1)'s `substr' function. 3221612Swosch# 3321612Swoschf_substr() 3421612Swosch{ 3521612Swosch local string="$1" start="${2:-0}" len="${3:-0}" 3621612Swosch echo "$string" | awk "{ print substr(\$0, $start, $len) }" 3721612Swosch} 3821612Swosch 3921612Swosch# f_longest_line_length 4021612Swosch# 4156537Sru# Simple wrapper to an awk(1) script to print the length of the longest line of 4256537Sru# input (read from stdin). Supports the newline escape-sequence `\n' for 4321612Swosch# splitting a single line into multiple lines. 4456537Sru# 4556537Sruf_longest_line_length_awk=' 4621612SwoschBEGIN { longest = 0 } 4756537Sru{ 4821612Swosch if (split($0, lines, /\\n/) > 1) 4921612Swosch { 5021612Swosch for (n in lines) 5121612Swosch { 5221612Swosch len = length(lines[n]) 5321612Swosch longest = ( len > longest ? len : longest ) 5421612Swosch } 5521612Swosch } 56139106Sru else 5721612Swosch { 5821612Swosch len = length($0) 5930350Swosch longest = ( len > longest ? len : longest ) 6021612Swosch } 6130350Swosch} 6221612SwoschEND { print longest } 6321612Swosch' 6421612Swoschf_longest_line_length() 6521612Swosch{ 6621612Swosch awk "$f_longest_line_length_awk" 6721612Swosch} 6821612Swosch 695531Sjkh# f_number_of_lines 7094940Sru# 7124704Sbde# Simple wrapper to an awk(1) script to print the number of lines read from 725345Sbde# stdin. Supports newline escape-sequence `\n' for splitting a single line into 7313973Smpp# multiple lines. 7421588Speter# 7521502Sjmacdf_number_of_lines_awk=' 7621502SjmacdBEGIN { num_lines = 0 } 7756537Sru{ 7821502Sjmacd num_lines += split(" "$0, unused, /\\n/) 7923762Speter} 8023762SpeterEND { print num_lines } 8130046Swosch' 8230350Swoschf_number_of_lines() 8330350Swosch{ 8430350Swosch awk "$f_number_of_lines_awk" 8530350Swosch} 865345Sbde 8730350Swosch# f_isinteger $arg 8823763Speter# 8930105Swosch# Returns true if argument is a positive/negative whole integer. 9024380Speter# 9130308Swoschf_isinteger() 9223763Speter{ 9330105Swosch local arg="$1" 9492648Sbde 9530350Swosch # Prevent division-by-zero 96115205Sru [ "$arg" = "0" ] && return $SUCCESS 9792648Sbde 9830350Swosch # Attempt to perform arithmetic divison (an operation which will exit 9930046Swosch # with error unless arg is a valid positive/negative whole integer). 10030105Swosch # 10130105Swosch ( : $((0/$arg)) ) > /dev/null 2>&1 10292648Sbde} 10330350Swosch 104115205Srufi # ! $_STRINGS_SUBR 10592648Sbde