Created
June 8, 2015 11:25
-
-
Save BR0kEN-/a84b18717f8c67ece6f7 to your computer and use it in GitHub Desktop.
Bash implementation of strpos and substr functions from PHP library
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# @param string $1 | |
# Input string. | |
# @param int $2 | |
# Cut an amount of characters from left side of string. | |
# @param int [$3] | |
# Leave an amount of characters in the truncated string. | |
substr() | |
{ | |
local length=${3} | |
if [ -z "${length}" ]; then | |
length=$((${#1} - ${2})) | |
fi | |
local str=${1:${2}:${length}} | |
if [ "${#str}" -eq "${#1}" ]; then | |
echo "${1}" | |
else | |
echo "${str}" | |
fi | |
} | |
# @param string $1 | |
# Input string. | |
# @param string $2 | |
# String that will be searched in input string. | |
# @param int [$3] | |
# Offset of an input string. | |
strpos() | |
{ | |
local str=${1} | |
local offset=${3} | |
if [ -n "${offset}" ]; then | |
str=`substr "${str}" ${offset}` | |
else | |
offset=0 | |
fi | |
str=${str/${2}*/} | |
if [ "${#str}" -eq "${#1}" ]; then | |
return 0 | |
fi | |
echo $((${#str}+${offset})) | |
} | |
str="windows should should die" | |
substr "${str}" 4 2 # ow | |
strpos "${str}" "i" # 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment