#@IgnoreInspection BashAddShebang # String functionality (Koalephant Shell Script Library) # Version: 2.0.0 # Copyright: 2014-2017, Koalephant Co., Ltd # Author: Stephen Reay , Koalephant Packaging Team # Get a line from a multiline variable # # Input: # $1 - the line number to fetch # $2...n - all remaining arguments are treated as the text to search # # Output: # The text on the specified line # proc k_string_get_line { var lineNo = '', delim = '' if test $Argc -eq 0 { k_log_err "k_string_get_line requires least 1 argument" return 1 } set delim = $[printf '\n ] set lineNo = $1 shift echo @Argv | cut -d $(delim% ) -f $(lineNo) } # Right Pad a string to the given length # # Input: # $1 - the length to pad to # $n - all remaining arguments are treated as the text to pad # Output: # The string, with padding applied proc k_string_pad_right { var length = '', text = '' if test $Argc -eq 0 { k_log_err "k_string_pad_right requires least 1 argument" return 1 } set length = $1 shift printf "%-$(length)s" "$ifsjoin(Argv)" } # Left Pad a string to the given length # # Input: # $1 - the length to pad to # $n - all remaining arguments are treated as the text to pad # PAD_CHAR - if set, the character to pad with (defaults to space) # Output: # The string, with padding applied proc k_string_pad_left { var length = '', text = '' if test $Argc -eq 0 { k_log_err "k_string_pad_left requires least 1 argument" exit 1 } set length = $1 shift printf "%+$(length)s" "$ifsjoin(Argv)" } # Convert a string to lower case # # Input: # $1...n - all arguments are treated as the text to convert # # Output: # The string, converted to lower case proc k_string_lower { printf "%s" "$ifsjoin(Argv)" | tr '[:upper:]' '[:lower:]' } # Convert a string to upper case # # Input: # $1...n - all arguments are treated as the text to convert # # Output: # The string, converted to upper case proc k_string_upper { printf "%s" "$ifsjoin(Argv)" | tr '[:lower:]' '[:upper:]' } # Test if a string contains the given substring # # Input: # $1 - the substring to check for # $2...n - all arguments are treated as the string to check in # # Return: # 0 if substring is found, 1 if not proc k_string_contains { var substring = '', string = '' if test $Argc -lt 2 { k_log_err "k_string_contains requires least 2 arguments" exit 1 } set substring = $(1) shift set string = "$ifsjoin(Argv)" if test $(#string) -gt 0 && test $(string%*${substring}*) != $(string) { return 0 } return 1 } # Test if a string starts with the given substring # # Input: # $1 - the substring to check for # $2...n - all arguments are treated as the string to check in # # Return: # 0 if the string starts with substring, 1 if not proc k_string_starts_with { var substring = '', string = '' if test $Argc -lt 2 { k_log_err "k_string_starts_with requires least 2 arguments" exit 1 } set substring = $(1) shift set string = "$ifsjoin(Argv)" if test $(#string) -gt 0 && test $(string%%${substring}*) = "" { return 0 } return 1 } # Test if a string ends with the given substring # # Input: # $1 - the substring to check for # $2...n - all arguments are treated as the string to check in # # Return: # 0 if the string ends with substring, 1 if not proc k_string_ends_with { var substring = '', string = '' if test $Argc -lt 2 { k_log_err "k_string_ends_with requires least 2 arguments" exit 1 } set substring = $(1) shift set string = "$ifsjoin(Argv)" if test $(#string) -gt 0 && test $(string##*${substring}) = "" { return 0 } return 1 } # Remove a substring from the start of a string # # Input: # $1 - the substring to remove # $2...n - all arguments are treated as the string to operate on # # Output: # The string with substring removed from the start proc k_string_remove_start { var substring = '', string = '', greedy = 'false' if test $[k_bool_parse $(REMOVE_GREEDY:-)] = true { set greedy = 'true' } if test $Argc -lt 2 { k_log_err "k_string_remove_start requires least 2 arguments" exit 1 } set substring = $(1) shift set string = "$ifsjoin(Argv)" if test $(greedy) = true { printf "%s" $(string##${substring}) } else { printf "%s" $(string#${substring}) } } # Remove a substring from the end of a string # # Input: # $1 - the substring to remove # $2...n - all arguments are treated as the string to operate on # REMOVE_GREEDY - set to `true` to match the longest pattern possible # # Output: # The string with substring removed from the end proc k_string_remove_end { var substring = '', string = '', greedy = 'false' if test $[k_bool_parse $(REMOVE_GREEDY:-)] = true { set greedy = 'true' } if test $Argc -lt 2 { k_log_err "k_string_remove_start requires least 2 arguments" exit 1 } set substring = $(1) shift set string = "$ifsjoin(Argv)" if test $(greedy) = true { printf "%s" $(string%%${substring}) } else { printf "%s" $(string%${substring}) } } # Join strings with an optional separator # # Input: # $1 - the separator string # $2...n - the strings to join # # Output: # the strings joined by the separator # # Example: # k_string_join "-" this is a nice day # "this-is-a-nice-day" proc k_string_join { var separator = $(1-) shift k_string_remove_end $(separator) $[printf "%s$(separator)" @Argv] } # Remove leading and trailing whitespace from strings # # Input: # $1...n - the strings to join # # Output: # the strings with leading/trailing whitespace removed proc k_string_trim { printf "%s" "$ifsjoin(Argv)" | sed -e "s/^[[:space:]]*//g; s/[[:space:]]*$//g;" } # Check if the given keyword is in the list of valid keywords, or error # # Input: # $1 - the input to check # $2...n - the list of valid keywords # # Output: # the keyword if valid # # Return: # 0 if keyword is valid, 1 if not proc k_string_keyword_error { var input = $(1) shift while test $Argc -gt 0 { if test $(input) = $(1) { printf $1 return 0 } shift } return 1 } # Get the selected keyword from a list, or default # # Input: # $1 - the input to check # $2...n - the list of valid keywords. The last value is used as the default if none match # # Output: # the valid keyword or default proc k_string_keyword { var value = '' if set value = $[k_string_keyword_error @Argv] { echo $(value) } else { shift $shExpr('$#-1') echo $1 } }