Skip to content

Commit 87e7e9e

Browse files
committed
modernize: replace SplitN(..., 2) -> Cut(...)
1 parent 7baa663 commit 87e7e9e

3 files changed

Lines changed: 25 additions & 22 deletions

File tree

conf/files.go

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func replaceDIR(path, flags string) string {
2828
return strings.ReplaceAll(flags, "$DIR", filepath.Dir(path))
2929
}
3030

31-
// getNameAndFlags returns model name and llama-server flags
31+
// getNameAndFlags returns model name and llama-server flags.
3232
func getNameAndFlags(root, path string) (name, flags_ string) {
3333
truncated, flags := extractFlags(path)
3434
name = beautifyModelName(root, truncated)
@@ -48,6 +48,7 @@ func beautifyModelName(root, truncated string) string {
4848
name = strings.TrimSuffix(name, "_")
4949
name = strings.TrimSuffix(name, "-GGUF")
5050
name = strings.Replace(name, "-GGUF_", ":", 1)
51+
name = strings.Replace(name, "-GGUF:", ":", 1)
5152

5253
return name
5354
}
@@ -96,6 +97,7 @@ func nameWithSlash(root, truncated, name string) string {
9697
return string(out)
9798
case !unicode.IsLower(char):
9899
return nameWithDir(root, truncated, name)
100+
default:
99101
}
100102
}
101103
return nameWithDir(root, truncated, name)
@@ -116,22 +118,22 @@ func nameWithGGUF(name string) string {
116118
return ""
117119
}
118120

119-
// expected split:
120-
// begin[0] = ggml-org unsloth
121-
// begin[1] = gpt-oss-120b Devstral-2-123B-Instruct-2512
122-
begin := strings.SplitN(name[:pos], "_", 2)
123-
if len(begin) != 2 {
121+
// Expected Cut:
122+
// grp = ggml-org unsloth
123+
// model = gpt-oss-120b Devstral-2-123B-Instruct-2512
124+
grp, model, ok := strings.Cut(name[:pos], "_")
125+
if !ok {
124126
return ""
125127
}
126128

127129
// search for the duplicated model name
128130
after := name[pos+len(gguf):]
129-
pos = strings.Index(after, begin[1])
131+
pos = strings.Index(after, model)
130132
if pos < 0 {
131133
return ""
132134
}
133135

134-
name = begin[0] + "/" + begin[1]
136+
name = grp + "/" + model
135137
if pos > 1 {
136138
quants := after[:pos-1]
137139
name += ":" + quants
@@ -163,6 +165,7 @@ func nameWithDir(root, truncated, name string) string {
163165
dash = i // number of letters before the dash
164166
case !unicode.IsLower(char):
165167
return name
168+
default:
166169
}
167170
}
168171
if dash > 0 {
@@ -212,10 +215,10 @@ func extractFlags(path string) (truncated, flags_ string) {
212215

213216
// Slice after the first '&' to avoid an empty first element.
214217
for f := range strings.SplitSeq(truncated[pos+1:], "&") {
215-
kv := strings.SplitN(f, "=", 2)
216-
if len(kv) > 0 {
217-
kv[0] = "-" + kv[0]
218-
flags = append(flags, kv...)
218+
key, value, ok := strings.Cut(f, "=")
219+
if ok {
220+
key = "-" + key
221+
flags = append(flags, key, value)
219222
}
220223
}
221224

conf/info.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,11 @@ func (cfg *Cfg) refineModelInfo(name string) {
8282
// after the first space, the arguments
8383
pos := strings.Index(cfg.Swap.Models[name].Cmd, " ")
8484
if pos > 1 {
85-
// split the arguments at -m: -first -args -m path/to/file.gguf
86-
args := strings.SplitN(cfg.Swap.Models[name].Cmd[pos:], " -m ", 2)
87-
mi.Flags = args[0]
88-
if len(args) > 1 {
89-
mi.Path = args[1]
85+
// split the arguments at -m: -arg1 -arg2 -m path/to/file.gguf
86+
flags, path, ok := strings.Cut(cfg.Swap.Models[name].Cmd[pos:], " -m ")
87+
mi.Flags = flags
88+
if ok {
89+
mi.Path = path
9090
mi.Error = "file absent but configured in llama-swap.yml"
9191
}
9292
} else {

conf/read.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -158,12 +158,12 @@ func (cfg *Cfg) parseExtraModels(extra string) {
158158

159159
for pair := range strings.SplitSeq(extra, "|||") {
160160
// split model=flags
161-
mf := strings.SplitN(pair, "=", 2)
162-
model := strings.TrimSpace(mf[0])
161+
model, flags, ok := strings.Cut(pair, "=")
162+
model = strings.TrimSpace(model)
163+
model = strings.Replace(model, "-GGUF", "", 1)
163164
cfg.ExtraModels[model] = ""
164-
if len(mf) > 1 {
165-
flags := strings.TrimSpace(mf[1])
166-
cfg.ExtraModels[model] = flags
165+
if ok {
166+
cfg.ExtraModels[model] = strings.TrimSpace(flags)
167167
}
168168
// if DefaultModel unset => use the first ExtraModels
169169
if cfg.DefaultModel == "" {

0 commit comments

Comments
 (0)