FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
gotests/testdata/string_utils.go at develop · cweill/gotests · GitHub
cweill
/
gotests
Public
Notifications
You must be signed in to change notification settings
Fork
351
Star
5.3k
Code
Issues
33
Pull requests
6
Discussions
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Discussions
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
gotests
/
testdata
/
string_utils.go
Copy path
More file actions
More file actions
Latest commit
History
History
History
114 lines (91 loc) · 2.15 KB
Breadcrumbs
gotests
/
testdata
/
string_utils.go
Copy path
File metadata and controls
114 lines (91 loc) · 2.15 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
package
testdata
import
(
"errors"
"strings"
)
// TrimAndLower trims whitespace and converts to lowercase
func
TrimAndLower
(
s
string
)
string
{
return
strings
.
ToLower
(
strings
.
TrimSpace
(
s
))
}
// Join joins strings with a separator, handling empty inputs
func
Join
(
parts
...
string
)
string
{
if
len
(
parts
)
==
0
{
return
""
}
// Filter out empty strings
nonEmpty
:=
make
([]
string
,
0
,
len
(
parts
))
for
_
,
part
:=
range
parts
{
if
part
!=
""
{
nonEmpty
=
append
(
nonEmpty
,
part
)
}
}
if
len
(
nonEmpty
)
==
0
{
return
""
}
return
strings
.
Join
(
nonEmpty
,
" "
)
}
// ParseKeyValue parses "key=value" pairs from input
func
ParseKeyValue
(
input
string
) (
map
[
string
]
string
,
error
) {
if
input
==
""
{
return
nil
,
errors
.
New
(
"input cannot be empty"
)
}
result
:=
make
(
map
[
string
]
string
)
pairs
:=
strings
.
Split
(
input
,
","
)
for
_
,
pair
:=
range
pairs
{
pair
=
strings
.
TrimSpace
(
pair
)
if
pair
==
""
{
continue
}
parts
:=
strings
.
SplitN
(
pair
,
"="
,
2
)
if
len
(
parts
)
!=
2
{
return
nil
,
errors
.
New
(
"invalid format: expected key=value"
)
}
key
:=
strings
.
TrimSpace
(
parts
[
0
])
value
:=
strings
.
TrimSpace
(
parts
[
1
])
if
key
==
""
{
return
nil
,
errors
.
New
(
"key cannot be empty"
)
}
result
[
key
]
=
value
}
if
len
(
result
)
==
0
{
return
nil
,
errors
.
New
(
"no valid key-value pairs found"
)
}
return
result
,
nil
}
// Reverse reverses a string
func
Reverse
(
s
string
)
string
{
runes
:=
[]
rune
(
s
)
for
i
,
j
:=
0
,
len
(
runes
)
-
1
;
i
<
j
;
i
,
j
=
i
+
1
,
j
-
1
{
runes
[
i
],
runes
[
j
]
=
runes
[
j
],
runes
[
i
]
}
return
string
(
runes
)
}
// ContainsAny checks if a string contains any of the substrings
func
ContainsAny
(
s
string
,
substrings
[]
string
)
bool
{
if
s
==
""
||
len
(
substrings
)
==
0
{
return
false
}
for
_
,
substr
:=
range
substrings
{
if
substr
!=
""
&&
strings
.
Contains
(
s
,
substr
) {
return
true
}
}
return
false
}
// TruncateWithEllipsis truncates a string to maxLen with ellipsis
func
TruncateWithEllipsis
(
s
string
,
maxLen
int
)
string
{
if
maxLen
<=
0
{
return
""
}
if
maxLen
<=
3
{
// Too short for ellipsis
if
len
(
s
)
<=
maxLen
{
return
s
}
return
s
[:
maxLen
]
}
if
len
(
s
)
<=
maxLen
{
return
s
}
return
s
[:
maxLen
-
3
]
+
"..."
}
Back
|
FazBrowse Home
|
New Git URL