Created
September 5, 2021 14:54
-
-
Save honmaple/b6bd0a32dca04ca6616e6944656e3616 to your computer and use it in GitHub Desktop.
golang多行匹配与逐行匹配性能
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"regexp" | |
"strings" | |
"testing" | |
) | |
var template = ` | |
%s | |
#+begin_src bash | |
cccc | |
bvv | |
%s | |
#+end_src | |
%s | |
` | |
var text = fmt.Sprintf(template, | |
strings.Repeat("vvvvvvvvvvvvasdasdasdads\n", 100), | |
strings.Repeat("vvvvvvvvvvvvasdasdasdads\n", 10), | |
strings.Repeat("vvvvvvvvvvvvasdasdasdads\n", 100), | |
) | |
var ( | |
beginBlockRegexp = regexp.MustCompile(`(?i)^(\s*)#\+BEGIN_(\w+)(.*)`) | |
endBlockRegexp = regexp.MustCompile(`(?i)^(\s*)#\+END_(\w+)`) | |
) | |
var ( | |
blockRegexp = regexp.MustCompile(`(?ism:^(\s*)#\+BEGIN_(\w+)(.+?)\n(.+?)^(\s*)#\+END_(\w+))`) | |
) | |
type Block struct { | |
Type string | |
Parameters []string | |
} | |
func parseSplit(lines []string) (*Block, int) { | |
match := beginBlockRegexp.FindStringSubmatch(lines[0]) | |
if match == nil { | |
return nil, 0 | |
} | |
blockType := strings.ToUpper(match[2]) | |
end := len(lines) | |
num := 1 | |
for num < end { | |
if m := endBlockRegexp.FindStringSubmatch(lines[num]); m != nil && strings.ToUpper(m[2]) == blockType { | |
return &Block{ | |
Type: blockType, | |
Parameters: strings.Split(strings.TrimSpace(match[3]), " "), | |
}, num + 1 | |
} | |
num++ | |
} | |
return nil, 0 | |
} | |
func parseRegex(lines string) (*Block, int) { | |
match := blockRegexp.FindStringSubmatch(lines) | |
if match == nil { | |
return nil, 0 | |
} | |
blockType := strings.ToUpper(match[2]) | |
b := &Block{ | |
Type: blockType, | |
Parameters: strings.Split(strings.TrimSpace(match[3]), " "), | |
} | |
return b, 0 | |
} | |
func BenchmarkSplit(b *testing.B) { | |
count := 0 | |
lines := strings.Split(text, "\n") | |
for i := 0; i < b.N; i++ { | |
index := 0 | |
end := len(lines) | |
for index < end { | |
if m, n := parseSplit(lines[index:]); m != nil { | |
index = index + n | |
if count == 0 { | |
b.Log(m) | |
} | |
count++ | |
continue | |
} | |
index++ | |
} | |
} | |
b.Log(count) | |
} | |
func BenchmarkRegex(b *testing.B) { | |
count := 0 | |
for i := 0; i < b.N; i++ { | |
if m, _ := parseRegex(text); m != nil { | |
if count == 0 { | |
b.Log(m) | |
} | |
count++ | |
} | |
} | |
b.Log(count) | |
} |
Author
honmaple
commented
Sep 5, 2021
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment