-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparts.go
More file actions
299 lines (265 loc) · 8.79 KB
/
Copy pathparts.go
File metadata and controls
299 lines (265 loc) · 8.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
package strings2
import (
"strings"
)
// Part represents a grouped sequence of SubParts.
type Part interface {
String() string
SubParts() []SubPart
}
type BasePart struct {
Subs []SubPart
}
func (p BasePart) String() string {
var sb strings.Builder
for _, s := range p.Subs {
sb.WriteRune(s.Rune())
}
return sb.String()
}
func (p BasePart) SubParts() []SubPart { return p.Subs }
// Concrete Part types
type WordPart struct{ BasePart }
type SeparatorPart struct{ BasePart }
// Partitioner defines a function that groups SubParts into Parts.
type Partitioner func([]SubPart) []Part
// SubPartsToParts converts SubParts to Parts using the provided Partitioner.
func SubPartsToParts(subs []SubPart, partitioner Partitioner) []Part {
if partitioner == nil {
return []Part{&WordPart{BasePart{Subs: subs}}}
}
return partitioner(subs)
}
// Common Partitioners
func overridePartitionerConfig(pcfg *PartitionerConfig, opts ...any) {
for _, opt := range opts {
switch o := opt.(type) {
case NumberMode:
pcfg.NumberMode = o
case ParserEmitEmpty:
pcfg.EmitEmpty = bool(o)
case ParserNonAlphanumericAsDelimiter:
pcfg.NonAlphanumericAsDelimiter = bool(o)
case ParserDelimiterDetector:
pcfg.DelimiterDetector = o.Detector
}
}
}
// SnakeCasePartitioner splits on underscore '_'.
// Deprecated: Does not support dynamic configuration via options (e.g. EmitEmpty). Use WithSnakeCasePartitioner instead.
func SnakeCasePartitioner(subs []SubPart) []Part {
return NewPartitioner(PartitionerConfig{
Delimiters: map[rune]bool{'_': true},
SplitCamel: true,
})(subs)
}
// KebabCasePartitioner splits on hyphen '-'.
// Deprecated: Does not support dynamic configuration via options (e.g. EmitEmpty). Use WithKebabCasePartitioner instead.
func KebabCasePartitioner(subs []SubPart) []Part {
return NewPartitioner(PartitionerConfig{
Delimiters: map[rune]bool{'-': true},
SplitCamel: true,
})(subs)
}
// SplitByDelimiter is a helper to split SubParts by a specific rune delimiter.
// Deprecated: Does not support dynamic configuration via options (e.g. EmitEmpty). Use WithSplitByDelimiter instead.
func SplitByDelimiter(subs []SubPart, delim rune) []Part {
return NewPartitioner(PartitionerConfig{
Delimiters: map[rune]bool{delim: true},
})(subs)
}
// CamelCasePartitioner splits on case transitions.
// Deprecated: Does not support dynamic configuration via options (e.g. EmitEmpty). Use WithCamelCasePartitioner instead.
func CamelCasePartitioner(subs []SubPart) []Part {
return NewPartitioner(PartitionerConfig{
SplitCamel: true,
})(subs)
}
// WithSnakeCasePartitioner creates a ParserOption that splits on underscore '_'.
func WithSnakeCasePartitioner(opts ...any) ParserOption {
return funcParserOption(func(cfg *ParserConfig) {
if cfg.Partitioner != nil {
return
}
pcfg := PartitionerConfig{
Delimiters: map[rune]bool{'_': true},
SplitCamel: true,
NumberMode: cfg.NumberMode,
EmitEmpty: cfg.EmitEmpty,
NonAlphanumericAsDelimiter: cfg.NonAlphanumericAsDelimiter,
}
overridePartitionerConfig(&pcfg, opts...)
cfg.Partitioner = NewPartitioner(pcfg)
})
}
// WithKebabCasePartitioner creates a ParserOption that splits on hyphen '-'.
func WithKebabCasePartitioner(opts ...any) ParserOption {
return funcParserOption(func(cfg *ParserConfig) {
if cfg.Partitioner != nil {
return
}
pcfg := PartitionerConfig{
Delimiters: map[rune]bool{'-': true},
SplitCamel: true,
NumberMode: cfg.NumberMode,
EmitEmpty: cfg.EmitEmpty,
NonAlphanumericAsDelimiter: cfg.NonAlphanumericAsDelimiter,
}
overridePartitionerConfig(&pcfg, opts...)
cfg.Partitioner = NewPartitioner(pcfg)
})
}
// WithSplitByDelimiter creates a ParserOption that splits on a specific rune delimiter.
func WithSplitByDelimiter(delim rune, opts ...any) ParserOption {
return funcParserOption(func(cfg *ParserConfig) {
if cfg.Partitioner != nil {
return
}
pcfg := PartitionerConfig{
Delimiters: map[rune]bool{delim: true},
SplitCamel: true,
NumberMode: cfg.NumberMode,
EmitEmpty: cfg.EmitEmpty,
NonAlphanumericAsDelimiter: cfg.NonAlphanumericAsDelimiter,
}
overridePartitionerConfig(&pcfg, opts...)
cfg.Partitioner = NewPartitioner(pcfg)
})
}
// WithCamelCasePartitioner creates a ParserOption that splits on case transitions.
func WithCamelCasePartitioner(opts ...any) ParserOption {
return funcParserOption(func(cfg *ParserConfig) {
if cfg.Partitioner != nil {
return
}
pcfg := PartitionerConfig{
SplitCamel: true,
NumberMode: cfg.NumberMode,
EmitEmpty: cfg.EmitEmpty,
NonAlphanumericAsDelimiter: cfg.NonAlphanumericAsDelimiter,
}
overridePartitionerConfig(&pcfg, opts...)
cfg.Partitioner = NewPartitioner(pcfg)
})
}
// DelimiterDetector detects if a delimiter starts at the given index in subs.
// It returns the length of the delimiter in subparts (runes), or 0 if no delimiter is found.
type DelimiterDetector func(subs []SubPart, index int) (length int)
type PartitionerConfig struct {
Ignore string
Delimiters map[rune]bool
DelimiterDetector DelimiterDetector
SplitCamel bool
NonAlphanumericAsDelimiter bool // If true, treats any non-alphanumeric character as a delimiter
NumberMode NumberMode
PreserveSep bool // If true, delimiters are returned as SeparatorPart instead of discarded
EmitEmpty bool // If true, emits empty WordParts for leading, consecutive, or trailing delimiters
}
// NewPartitioner creates a partitioner with specific configuration.
func NewPartitioner(cfg PartitionerConfig) Partitioner {
return func(subs []SubPart) []Part {
var parts []Part
var current []SubPart
for i := 0; i < len(subs); {
s := subs[i]
delimLen := 0
if cfg.Ignore != "" && strings.ContainsRune(cfg.Ignore, s.Rune()) {
// if ignored, it's never a delimiter
} else {
if cfg.DelimiterDetector != nil {
delimLen = cfg.DelimiterDetector(subs, i)
if delimLen < 0 {
delimLen = 0
}
if delimLen > len(subs)-i {
delimLen = len(subs) - i
}
}
if delimLen == 0 && cfg.Delimiters != nil && cfg.Delimiters[s.Rune()] {
delimLen = 1
}
if delimLen == 0 && cfg.NonAlphanumericAsDelimiter && !s.IsLetter() && !s.IsDigit() {
delimLen = 1
}
}
// Check if current rune(s) is a delimiter
if delimLen > 0 {
if len(current) > 0 {
parts = append(parts, &WordPart{BasePart{Subs: current}})
current = nil
} else if cfg.EmitEmpty {
parts = append(parts, &WordPart{BasePart{Subs: nil}})
}
if cfg.PreserveSep {
parts = append(parts, &SeparatorPart{BasePart{Subs: subs[i : i+delimLen]}})
}
i += delimLen
continue
}
// Transition check
isSplit := false
if (cfg.SplitCamel || cfg.NumberMode != NumberModeNone) && len(current) > 0 {
prev := current[len(current)-1]
// Note: if prev was delimiter, current is empty or started anew.
// We rely on current being non-empty to check transitions within a word chunk.
if cfg.SplitCamel {
isPrevLower := prev.IsLower()
isPrevUpper := prev.IsUpper()
isCurrUpper := s.IsUpper()
if cfg.NumberMode == NumberModeTreatAsLowercase {
if prev.IsDigit() {
isPrevLower = true
}
}
// lower -> Upper
if isPrevLower && isCurrUpper {
isSplit = true
}
// Upper -> Upper -> lower (PDFLoader split at L)
if i+1 < len(subs) {
next := subs[i+1]
isNextLower := next.IsLower()
if cfg.NumberMode == NumberModeTreatAsLowercase && next.IsDigit() {
isNextLower = true
}
if isPrevUpper && isCurrUpper && isNextLower {
isSplit = true
}
}
// MergeRecursive specific rule: digit -> Upper triggers a split, similar to lower -> Upper
if cfg.NumberMode == NumberModeMergeWithWord {
if prev.IsDigit() && isCurrUpper {
isSplit = true
}
}
}
if cfg.NumberMode == NumberModeSplitAlways {
// Letter -> Digit -> Split.
// Digit -> Letter -> Split.
if prev.IsLetter() && s.IsDigit() {
isSplit = true
}
if prev.IsDigit() && s.IsLetter() {
isSplit = true
}
}
}
if isSplit {
if len(current) > 0 {
parts = append(parts, &WordPart{BasePart{Subs: current}})
current = nil
}
}
current = append(current, s)
i++
}
if len(current) > 0 {
parts = append(parts, &WordPart{BasePart{Subs: current}})
} else if cfg.EmitEmpty && len(subs) > 0 {
// If we ended with a delimiter, current is empty.
// Emit an empty part for the trailing delimiter if EmitEmpty is true.
parts = append(parts, &WordPart{BasePart{Subs: nil}})
}
return parts
}
}