-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsplit.go
More file actions
99 lines (95 loc) · 2.79 KB
/
split.go
File metadata and controls
99 lines (95 loc) · 2.79 KB
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
package fmt
// Split divides a string by a separator and returns a slice of substrings.
// Usage: Convert("Hello World").Split() => []string{"Hello", "World"}
// Usage with separator: Convert("Hello;World").Split(";") => []string{"Hello", "World"}
// If no separator is provided, splits by whitespace (similar to strings.Fields).
// Uses the Conv work buffer for memory efficiency. The global Split function is deprecated; always use Convert(...).Split(...).
func (c *Conv) Split(separator ...string) []string {
src := c.GetString(BuffOut)
return c.splitStr(src, separator...)
}
// splitStr is a reusable internal method for splitting a string by a separator (empty = by character, default whitespace).
func (c *Conv) splitStr(src string, separator ...string) []string {
var sep string
if len(separator) == 0 {
// Whitespace split: mimic strings.Fields
out := make([]string, 0, len(src)/2+1)
fieldStart := -1
for i, r := range src {
isSpace := r == ' ' || r == '\t' || r == '\n' || r == '\r'
if !isSpace {
if fieldStart == -1 {
fieldStart = i
}
} else {
if fieldStart != -1 {
out = append(out, src[fieldStart:i])
fieldStart = -1
}
}
}
if fieldStart != -1 {
out = append(out, src[fieldStart:])
}
return out
} else {
sep = separator[0]
}
// Special case: split by character (empty separator)
if len(sep) == 0 {
if len(src) == 0 {
return []string{}
}
out := make([]string, 0, len(src))
for _, ch := range src {
// OPTIMIZED: Direct string conversion without buffer operations
out = append(out, string(ch))
}
return out
}
// Handle string shorter than 3 chars (legacy behavior)
if len(src) < 3 {
return []string{src}
}
// If src is empty, return [""] (legacy behavior)
if len(src) == 0 {
return []string{""}
}
// Use splitByDelimiterWithBuffer for all splits
var out []string
first := true
orig := src
for {
before, after, found := c.splitByDelimiterWithBuffer(src, sep)
out = append(out, before)
if !found {
// Legacy: if separator not found at all, return original string as single element
if first && len(out) == 1 && out[0] == orig {
return []string{orig}
}
break
}
src = after
first = false
}
return out
}
// splitByDelimiterWithBuffer splits a string by the first occurrence of the delimiter.
// Returns the parts (before and after the delimiter). If not found, found=false.
// OPTIMIZED: Direct substring operations without buffer usage
func (c *Conv) splitByDelimiterWithBuffer(s, delim string) (before, after string, found bool) {
di := -1
for i := 0; i <= len(s)-len(delim); i++ {
if s[i:i+len(delim)] == delim {
di = i
break
}
}
if di < 0 {
return s, "", false
}
// OPTIMIZED: Direct substring without buffer operations
before = s[:di]
after = s[di+len(delim):]
return before, after, true
}