Skip to content

Commit

Permalink
* main [1.10.1] : быстрый метод в текст и бечмарки
Browse files Browse the repository at this point in the history
  • Loading branch information
SUNsung committed May 23, 2024
1 parent b113828 commit e9cf891
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 2 deletions.
14 changes: 14 additions & 0 deletions 0_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,3 +220,17 @@ func TestTextTransform(t *testing.T) {
}
})
}

func BenchmarkText(b *testing.B) {
htmlStr := strings.NewReader(strings.Repeat("<html><body><h1>Hello, World!</h1><p>This is a test.</p></body></html>", 1000))
for i := 0; i < b.N; i++ {
Text(htmlStr)
}
}

func BenchmarkTextFast(b *testing.B) {
htmlStr := strings.NewReader(strings.Repeat("<html><body><h1>Hello, World!</h1><p>This is a test.</p></body></html>", 1000))
for i := 0; i < b.N; i++ {
TextFast(htmlStr)
}
}
2 changes: 1 addition & 1 deletion _run/minor-ver
Original file line number Diff line number Diff line change
@@ -1 +1 @@
9
10
2 changes: 1 addition & 1 deletion const.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package htmlValidator

const GlobalVersion string = "1.8.1"
const GlobalVersion string = "1.9.1"
const GlobalDateUpdate string = "05-23-2024"
const GlobalName string = "htmlValidator"
23 changes: 23 additions & 0 deletions func.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package htmlValidator

import (
"io"
"strings"
)

/* Приведение html-текста к "стандартному" виду */
Expand Down Expand Up @@ -31,3 +32,25 @@ func Text(htmlText io.Reader) string {
transformObj := TextTransform()
return transformObj.Transform(htmlText)
}

/* Возрашвет только текст и БЫСТРО */
func TextFast(htmlText io.Reader) string {
buf := new(strings.Builder)
io.Copy(buf, htmlText)

input := buf.String()
output := make([]rune, 0, len(input))
inTag := false

for _, char := range input {
if char == '<' {
inTag = true
} else if char == '>' {
inTag = false
} else if !inTag {
output = append(output, char)
}
}

return string(output)
}

0 comments on commit e9cf891

Please sign in to comment.