Skip to content

Commit e15c30c

Browse files
committed
refactor: replace string replacers with config regexes
MermaidID and KeyFileSlug used multi-pair strings.NewReplacer with inline character mappings. Replaced with centralized regexes: - regex.MermaidUnsafe: matches [/.\-] for Mermaid node IDs - regex.SlugUnsafe: matches [/.] for journal slug generation One regex call replaces 3-4 token references and a NewReplacer allocation. Reduces overtokenization for agents reading the code. Signed-off-by: Jose Alekhinne <jose@ctx.ist>
1 parent c7499d4 commit e15c30c

3 files changed

Lines changed: 21 additions & 10 deletions

File tree

internal/cli/dep/core/format.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"strings"
1414

1515
"github.com/ActiveMemory/ctx/internal/config/dep"
16+
"github.com/ActiveMemory/ctx/internal/config/regex"
1617
"github.com/ActiveMemory/ctx/internal/config/token"
1718
"github.com/ActiveMemory/ctx/internal/io"
1819
)
@@ -25,12 +26,9 @@ import (
2526
// Returns:
2627
// - string: Safe Mermaid node identifier
2728
func MermaidID(pkg string) string {
28-
r := strings.NewReplacer(
29-
token.Slash, token.Underscore,
30-
token.Dot, token.Underscore,
31-
token.Dash, token.Underscore,
29+
return regex.MermaidUnsafe.ReplaceAllString(
30+
pkg, token.Underscore,
3231
)
33-
return r.Replace(pkg)
3432
}
3533

3634
// RenderMermaid produces a Mermaid graph TD definition.

internal/cli/journal/core/format/fmt.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"github.com/ActiveMemory/ctx/internal/config/embed/text"
1515
"github.com/ActiveMemory/ctx/internal/config/file"
1616
cfgFmt "github.com/ActiveMemory/ctx/internal/config/format"
17+
"github.com/ActiveMemory/ctx/internal/config/regex"
1718
"github.com/ActiveMemory/ctx/internal/config/token"
1819
)
1920

@@ -46,10 +47,12 @@ func Size(bytes int64) string {
4647
// Returns:
4748
// - string: Safe slug (e.g., "internal_config_x_go")
4849
func KeyFileSlug(path string) string {
49-
slug := strings.ReplaceAll(path, token.Slash, token.Underscore)
50-
slug = strings.ReplaceAll(slug, token.Dot, token.Underscore)
51-
slug = strings.ReplaceAll(slug, token.GlobStar, token.GlobReplace)
52-
return slug
50+
slug := regex.SlugUnsafe.ReplaceAllString(
51+
path, token.Underscore,
52+
)
53+
return strings.ReplaceAll(
54+
slug, token.GlobStar, token.GlobReplace,
55+
)
5356
}
5457

5558
// SessionLink formats a Markdown list item linking to a page with a

internal/config/regex/file.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,15 @@ var FileNameChar = regexp.MustCompile(`[^a-zA-Z0-9-]+`)
1313

1414
// UUID matches a UUID (v4) anywhere in a string.
1515
var UUID = regexp.MustCompile(
16-
`[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}`,
16+
`[0-9a-f]{8}-[0-9a-f]{4}-` +
17+
`[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}`,
1718
)
19+
20+
// MermaidUnsafe matches characters that are invalid in
21+
// Mermaid node identifiers (slash, dot, hyphen).
22+
var MermaidUnsafe = regexp.MustCompile(`[/.\-]`)
23+
24+
// SlugUnsafe matches characters replaced during slug
25+
// generation (slash, dot). Glob star (*) is handled
26+
// separately as it maps to a different replacement.
27+
var SlugUnsafe = regexp.MustCompile(`[/.]`)

0 commit comments

Comments
 (0)