1
1
# shellcheck shell=bash
2
2
3
- # str.compare() {
4
- # unset REPLY; REPLY=
5
- # local str1="$1"
6
- # local str2="$2"
7
-
8
- # # TODO: set locale?
9
- # # TODO: shopt -s nocaseglob, etc.
10
- # if [[ $str1 > "$str2" ]]; then
11
- # REPLY=1
12
- # elif [[ $str2 < "$str1" ]]; then
13
- # REPLY=-1
14
- # else
15
- # REPLY=0
16
- # fi
17
- # }
3
+ str.compare () {
4
+ unset REPLY; REPLY=
5
+ local str1=$1
6
+ local str2=$2
7
+
8
+ local old_collate=" $LC_COLLATE " # TODO
9
+ LC_COLLATE=' C'
10
+
11
+ if [[ $str1 > " $str2 " ]]; then
12
+ REPLY=1
13
+ elif [[ $str1 < " $str2 " ]]; then
14
+ REPLY=-1
15
+ else
16
+ REPLY=0
17
+ fi
18
+
19
+ LC_COLLATE=" $old_collate "
20
+ }
18
21
19
22
str.contains () {
20
- local str=" $1 "
21
- local substr=" $2 "
23
+ local str=$1
24
+ local substr=$2
22
25
23
26
if [[ $str == * " $substr " * ]]; then
24
27
return 0
@@ -29,8 +32,8 @@ str.contains() {
29
32
30
33
str.count () {
31
34
unset REPLY; REPLY=
32
- local str=" $1 "
33
- local substr=" $2 "
35
+ local str=$1
36
+ local substr=$2
34
37
35
38
# local s="${str//"$substr"}"
36
39
# REPLY="$(((${#str} - ${#s}) / ${#substr}))"
@@ -46,15 +49,15 @@ str.count() {
46
49
47
50
str.fields () {
48
51
unset REPLY; REPLY=
49
- local str=" $1 "
52
+ local str=$1
50
53
51
54
# TODO: fix hack
52
55
REPLY=($str )
53
56
}
54
57
55
58
str.has_prefix () {
56
- local str=" $1 "
57
- local prefix=" $2 "
59
+ local str=$1
60
+ local prefix=$2
58
61
59
62
if [[ $str == " $prefix " * ]]; then
60
63
return 0
@@ -64,8 +67,8 @@ str.has_prefix() {
64
67
}
65
68
66
69
str.has_suffix () {
67
- local str=" $1 "
68
- local prefix=" $2 "
70
+ local str=$1
71
+ local prefix=$2
69
72
70
73
if [[ " $str " == * " $prefix " ]]; then
71
74
return 0
@@ -75,8 +78,8 @@ str.has_suffix() {
75
78
}
76
79
77
80
str.index () {
78
- local str=" $1 "
79
- local substr=" $2 "
81
+ local str=$1
82
+ local substr=$2
80
83
81
84
local rest=" ${str#* $substr } "
82
85
if (( ${# rest} == ${# str} )) ; then
@@ -88,8 +91,8 @@ str.index() {
88
91
89
92
str.join () {
90
93
unset REPLY; REPLY=
91
- local arr_name=" $1 "
92
- local sep=" $2 "
94
+ local arr_name=$1
95
+ local sep=$2
93
96
94
97
local -n __arr=" $arr_name "
95
98
local i= item=
@@ -104,8 +107,8 @@ str.join() {
104
107
105
108
str.last_index () {
106
109
unset REPLY; REPLY=
107
- local str=" $1 "
108
- local substr=" $2 "
110
+ local str=$1
111
+ local substr=$2
109
112
110
113
local rest=" ${str##* $substr } "
111
114
if (( ${# rest} == ${# str} )) ; then
@@ -114,3 +117,43 @@ str.last_index() {
114
117
REPLY=$(( ${# str} - ${# rest} - ${# substr} ))
115
118
fi
116
119
}
120
+
121
+ str.repeat () {
122
+ unset REPLY; REPLY=
123
+ local str=$1
124
+ local -i count=$2
125
+
126
+ local i=
127
+ for (( i= 0 ; i< count; ++ i)) ; do
128
+ REPLY+=" $str "
129
+ done ; unset i
130
+ }
131
+
132
+ # TODO: behavior differes from Go (will replace overlapping strings)
133
+ str.replace () {
134
+ unset REPLY; REPLY=
135
+ local str=$1
136
+ local old=$2
137
+ local new=$3
138
+ local -i count=$4
139
+
140
+ if (( count < 0 )) ; then
141
+ REPLY=" ${str// " $old " / " $new " } "
142
+ else
143
+ local i=
144
+ for (( i= 0 ; i< count; ++ i)) ; do
145
+ REPLY=" ${str/ " $old " / " $new " } "
146
+ done ; unset i
147
+ fi
148
+
149
+ }
150
+
151
+ # TODO: behavior differes from Go (will replace overlapping strings)
152
+ str.replace_all () {
153
+ unset REPLY; REPLY=
154
+ local str=$1
155
+ local old=$2
156
+ local new=$3
157
+
158
+ REPLY=" ${str// " $old " / " $new " } "
159
+ }
0 commit comments