-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathString
136 lines (117 loc) · 2.99 KB
/
String
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#!/bin/bash (source)
# //////////////////////////////////////////////////
# STRING OPERATIONS
# //////////////////////////////////////////////////
function Is_Empty() { [[ -z "$*" ]] ;}
function Has_Substring() {
alt=${1/$2/}
[[ "$alt" != "$1" ]]
}
function File_Contains_Substring() {
var=$(<"$1")
Has_Substring "$var" "$2"
}
# example: Starts_With "oper" "operations"
function Starts_With() {
l=${#1}
[[ "${2:0:l}" = "$1" ]]
}
#detsch, 23/08/2004
# example: Ends_With "tions" "operations"
function Ends_With() {
l2=${#2}
l3=$[ ${#2} - ${#1} ]
[[ "${2:l3:l2}" = "$1" ]]
}
function Has_Uppercase() {
expr index "$1" "ABCDEFGHIJKLMNOPQRSTUVWXYZ" &> /dev/null
}
function Capitalize() {
python3 -c "
import sys,string
word = sys.argv[1]
def breakWord(word):
for ch in ['-', '_']:
pos = word.find(ch)
if pos != -1:
return [word[:pos], ch] + breakWord(word[pos+1:])
return [word]
parts = breakWord(word)
for part in parts:
sys.stdout.write(str.capitalize(part))
" "$1"
}
# Split_String
# splits a string into an array and places the result in the varable specified
# if <token> is ommitted whitespace is used
# Usage:
# eval $(Split_String <name of variable> <string> [<token>])
function Split_String {
string_name="$1"
string="$2"
token="$3"
if [ -z "$token" ]
then
token=" "
string=$(echo "${string}" | sed -r 's/\s+/ /g')
fi
echo -n "${string_name}=("
while index=$(expr index "${string}" "${token}")
[ $index -gt 0 ]
do
echo -n "\"${string:0:((index-1))}\" "
string="${string:index}"
done
echo -n "\"${string}\""
echo ")"
}
#detsch, 23/08/2004
# shell/sed/awk/cut implementation is welcome, but don't forget
# negative values at "$3"
#function Get_Token() {
# python -c "
#import sys,string
#sys.stdout.write(string.split(sys.argv[1], sys.argv[2])[int(sys.argv[3])])
#" "$1" "$2" "$3"
#}
# splits $1 by $2, returns entry $3 (may be negative)
function Get_Token() {
local i=0 tokens=() len=${#2}
local start rest=$1
while [ "$rest" ]
do
start=${rest%%$2*}
if [ $i -eq $3 ]
then
echo -n $start
return 0
fi
tokens[$i]=$start
rest=${rest:${#start}}
if [ "$rest" ]
then
rest=${rest:$len}
let i++
fi
done
[ $3 -lt 0 ] && echo -n ${tokens[$(( $i+$3+1 ))]}
}
# writes each string passed to it, but with the last character removed.
function Chop() {
echo -n "$(echo "$*" | sed -re "s/(.*)./\1/g")"
}
# Returns the arguments passed to it converted to lower case
function Downcase() {
echo "$*" | tr '[:upper:]' '[:lower:]'
}
function Uppercase() {
echo "$*" | tr '[:lower:]' '[:upper:]'
}
function String_Revision() {
Parameters "$@" versionandrevision
echo $versionandrevision | sed -nr 's,.*-(r[0-9]+(p[0-9]+)?)$,\1,p'
}
function String_Version() {
Parameters "$@" versionandrevision
echo $versionandrevision | sed -r 's/-r[0-9]+(p[0-9]+)?$//g'
}