MCPcopy Create free account
hub / github.com/OctopusDeploy/cli / SplitString

Function SplitString

pkg/util/util.go:256–290  ·  view source on GitHub ↗

SplitString splits the input string into components based on delimiter characters. we want to pick up empty entries here; so "::5" and ":pterm:5" should both return THREE components, rather than one or two and we want to allow for multiple different delimeters. neither the builtin golang strings.Spl

(s string, delimiters []rune)

Source from the content-addressed store, hash-verified

254// and we want to allow for multiple different delimeters.
255// neither the builtin golang strings.Split or strings.FieldsFunc support this. Logic borrowed from strings.FieldsFunc with heavy modifications
256func SplitString(s string, delimiters []rune) []string {
257 // pass 1: collect spans; golang strings.FieldsFunc says it's much more efficient this way
258 type span struct {
259 start int
260 end int
261 }
262 spans := make([]span, 0, 3)
263
264 // Find the field start and end indices.
265 start := 0 // we always start the first span at the beginning of the string
266 for idx, ch := range s {
267 if slices.Contains(delimiters, ch) {
268 if start >= 0 { // we found a delimiter and we are already in a span; end the span and start a new one
269 spans = append(spans, span{start, idx})
270 start = idx + 1
271 } else { // we found a delimiter and we are not in a span; start a new span
272 if start < 0 {
273 start = idx
274 }
275 }
276 }
277 }
278
279 // Last field might end at EOF.
280 if start >= 0 {
281 spans = append(spans, span{start, len(s)})
282 }
283
284 // pass 2: create strings from recorded field indices.
285 a := make([]string, len(spans))
286 for i, span := range spans {
287 a[i] = s[span.start:span.end]
288 }
289 return a
290}
291
292// helpful for debugging
293func PrintJSON(obj interface{}) {

Callers 1

Calls

no outgoing calls

Tested by

no test coverage detected