From 818892daaf4c5f96227081c12ef65d370df8f011 Mon Sep 17 00:00:00 2001 From: Hank Shen Date: Sat, 14 Sep 2024 12:21:21 +0800 Subject: [PATCH] update --- regex.go | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++ regex_test.go | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 131 insertions(+) diff --git a/regex.go b/regex.go index 7d033d8..1d999f8 100644 --- a/regex.go +++ b/regex.go @@ -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 + } +} diff --git a/regex_test.go b/regex_test.go index f983a61..ae70513 100644 --- a/regex_test.go +++ b/regex_test.go @@ -1,6 +1,7 @@ package com import ( + "regexp" "testing" "github.com/stretchr/testify/assert" @@ -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 := ` + {{Include "header"}} +
+ {{Include "sidebar"}} +
+
+
+` + 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 := ` + {P} +
+ {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) +} + +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) +}