Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
admpub committed Sep 14, 2024
1 parent 8830330 commit 818892d
Show file tree
Hide file tree
Showing 2 changed files with 131 additions and 0 deletions.
66 changes: 66 additions & 0 deletions regex.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,3 +140,69 @@ func FindChineseWords(text string, n ...int) []string {
}
return result
}

// ReplaceByMatchedIndex 通过 FindAllStringSubmatchIndex 的值来替换
// matches = FindAllStringSubmatchIndex
// var replaced string
// replacer := ReplaceByMatchedIndex(content, matches, &replaced)
// for k, v := range matches {
// var fullmatch string
// replacer(k, v, `newContent`)
// }
func ReplaceByMatchedIndex(content string, matches [][]int, replaced *string) func(k int, v []int, newInnerStr ...string) {
endK := len(matches) - 1
var lastEndIdx int
return func(k int, v []int, newInnerStr ...string) {
if len(newInnerStr) > 0 {
if k == 0 {
*replaced = content[0:v[0]] + newInnerStr[0]
if k == endK {
*replaced += content[v[1]:]
}
} else if k == endK {
*replaced += content[lastEndIdx:v[0]] + newInnerStr[0] + content[v[1]:]
} else {
*replaced += content[lastEndIdx:v[0]] + newInnerStr[0]
}
} else {
if k == 0 {
if k == endK {
*replaced = content
} else {
*replaced = content[0:v[1]]
}
} else if k == endK {
*replaced += content[lastEndIdx:]
} else {
*replaced += content[lastEndIdx:v[1]]
}
}
lastEndIdx = v[1]
}
}

// GetMatchedByIndex 通过 FindAllStringSubmatchIndex 的值获取匹配结果
// matches = FindAllStringSubmatchIndex
// for _, match := range matches {
// var fullmatch string
// GetMatchedByIndex(contet, match, &fullmatch)
// }
func GetMatchedByIndex(content string, v []int, recv ...*string) {
recvNum := len(recv)
matchIdx := 0
matchNum := len(v)
matchEdx := matchNum - 1
for idx := 0; idx < recvNum; idx++ {
if matchIdx > matchEdx {
return
}
if recv[idx] != nil && v[matchIdx] > -1 {
endIdx := matchIdx + 1
if endIdx >= matchNum {
return
}
*(recv[idx]) = content[v[matchIdx]:v[endIdx]]
}
matchIdx += 2
}
}
65 changes: 65 additions & 0 deletions regex_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com

import (
"regexp"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -14,3 +15,67 @@ func TestIsFloat(t *testing.T) {
assert.True(t, IsFloat(`-0.1`))
assert.Equal(t, -0.1, Float64(`-0.1`))
}

func TestReplaceByMatchedIndex(t *testing.T) {
content := `<body>
{{Include "header"}}
<div id="wrapper">
{{Include "sidebar"}}
<div class="body">
</div>
</div>
</body>`
matches := regexp.MustCompile(`\{\{Include "([^"]+)"\}\}`).FindAllStringSubmatchIndex(content, -1)
var replaced string
fn := ReplaceByMatchedIndex(content, matches, &replaced)
for k, v := range matches {
var tmplFile, passObject string
GetMatchedByIndex(content, v, nil, &tmplFile, &passObject)
if k == 0 {
assert.Equal(t, `header`, tmplFile)
assert.Equal(t, ``, passObject)
} else {
assert.Equal(t, `sidebar`, tmplFile)
assert.Equal(t, ``, passObject)
}
fn(k, v, `{P}`)
}
expected := `<body>
{P}
<div id="wrapper">
{P}
<div class="body">
</div>
</div>
</body>`
assert.Equal(t, expected, replaced)
var replaced2 string
fn2 := ReplaceByMatchedIndex(content, matches, &replaced2)
for k, v := range matches {
fn2(k, v)
}
assert.Equal(t, content, replaced2)
}

func TestReplaceByMatchedIndex2(t *testing.T) {
content := `{{Include "sub"}}`
matches := regexp.MustCompile(`\{\{Include "([^"]+)"\}\}`).FindAllStringSubmatchIndex(content, -1)
Dump(matches)
var replaced string
fn := ReplaceByMatchedIndex(content, matches, &replaced)
for k, v := range matches {
var tmplFile, passObject string
GetMatchedByIndex(content, v, nil, &tmplFile, &passObject)
assert.Equal(t, `sub`, tmplFile)
assert.Equal(t, ``, passObject)
fn(k, v, `{P}`)
}
expected := `{P}`
assert.Equal(t, expected, replaced)
var replaced2 string
fn2 := ReplaceByMatchedIndex(content, matches, &replaced2)
for k, v := range matches {
fn2(k, v)
}
assert.Equal(t, content, replaced2)
}

0 comments on commit 818892d

Please sign in to comment.