Skip to content

Commit f1bccf8

Browse files
committed
feat: Add more function implementations
1 parent 6180e1e commit f1bccf8

File tree

2 files changed

+74
-31
lines changed

2 files changed

+74
-31
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
String manipulation functions for Bash. Modeled from Go's [strings](https://pkg.go.dev/strings) module
44

5-
Everything between `strings.Contains()` and `strings.LastIndex()` have been implemented
5+
Everything between `strings.Compare()` and `strings.ReplaceAll()` have been implemented
66

77
## Installation
88

pkg/lib/public/bash-str.sh

Lines changed: 73 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,27 @@
11
# shellcheck shell=bash
22

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+
}
1821

1922
str.contains() {
20-
local str="$1"
21-
local substr="$2"
23+
local str=$1
24+
local substr=$2
2225

2326
if [[ $str == *"$substr"* ]]; then
2427
return 0
@@ -29,8 +32,8 @@ str.contains() {
2932

3033
str.count() {
3134
unset REPLY; REPLY=
32-
local str="$1"
33-
local substr="$2"
35+
local str=$1
36+
local substr=$2
3437

3538
# local s="${str//"$substr"}"
3639
# REPLY="$(((${#str} - ${#s}) / ${#substr}))"
@@ -46,15 +49,15 @@ str.count() {
4649

4750
str.fields() {
4851
unset REPLY; REPLY=
49-
local str="$1"
52+
local str=$1
5053

5154
# TODO: fix hack
5255
REPLY=($str)
5356
}
5457

5558
str.has_prefix() {
56-
local str="$1"
57-
local prefix="$2"
59+
local str=$1
60+
local prefix=$2
5861

5962
if [[ $str == "$prefix"* ]]; then
6063
return 0
@@ -64,8 +67,8 @@ str.has_prefix() {
6467
}
6568

6669
str.has_suffix() {
67-
local str="$1"
68-
local prefix="$2"
70+
local str=$1
71+
local prefix=$2
6972

7073
if [[ "$str" == *"$prefix" ]]; then
7174
return 0
@@ -75,8 +78,8 @@ str.has_suffix() {
7578
}
7679

7780
str.index() {
78-
local str="$1"
79-
local substr="$2"
81+
local str=$1
82+
local substr=$2
8083

8184
local rest="${str#*$substr}"
8285
if ((${#rest} == ${#str})); then
@@ -88,8 +91,8 @@ str.index() {
8891

8992
str.join() {
9093
unset REPLY; REPLY=
91-
local arr_name="$1"
92-
local sep="$2"
94+
local arr_name=$1
95+
local sep=$2
9396

9497
local -n __arr="$arr_name"
9598
local i= item=
@@ -104,8 +107,8 @@ str.join() {
104107

105108
str.last_index() {
106109
unset REPLY; REPLY=
107-
local str="$1"
108-
local substr="$2"
110+
local str=$1
111+
local substr=$2
109112

110113
local rest="${str##*$substr}"
111114
if ((${#rest} == ${#str})); then
@@ -114,3 +117,43 @@ str.last_index() {
114117
REPLY=$(( ${#str} - ${#rest} - ${#substr} ))
115118
fi
116119
}
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

Comments
 (0)