Skip to content

Commit

Permalink
Performance slightly improved + benchmark data updated
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeyco committed Jan 9, 2018
1 parent 25987d0 commit 6e300ca
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 22 deletions.
8 changes: 4 additions & 4 deletions _example/03-benchmarks-with-others/README.md
Expand Up @@ -71,9 +71,9 @@ $ go test -bench=. -benchmem
## Results
Intel Core i5 (3470), 8Gb DDR3 memory, Windows 10 results:
```
BenchmarkSimpletable-4 20000 84929 ns/op 39266 B/op 983 allocs/op
BenchmarkTermtables-4 20000 86327 ns/op 27792 B/op 1193 allocs/op
BenchmarkUITable-4 5000 240229 ns/op 39571 B/op 1082 allocs/op
BenchmarkSimpletable-4 10000 186476 ns/op 45651 B/op 1330 allocs/op
BenchmarkTermtables-4 10000 130461 ns/op 27632 B/op 1164 allocs/op
BenchmarkUITable-4 10000 229150 ns/op 39425 B/op 1073 allocs/op
PASS
ok github.com/alexeyco/simpletable/_example/03-benchmarks-with-others/bench 6.436s
ok github.com/alexeyco/simpletable/_example/03-benchmarks-with-others/bench 5.559s
```
35 changes: 17 additions & 18 deletions content.go
Expand Up @@ -13,27 +13,14 @@ var stripAnsiEscapeRegexp = regexp.MustCompile(`(\x9B|\x1B\[)[0-?]*[ -/]*[@-~]`)

// content is a cell content
type content struct {
c []string // content lines (trimmed)
w int // meta content width
}

// width returns maximum content lines width
func (c *content) maxLinewidth() int {
w := 0

for _, r := range c.c {
l := utf8.RuneCountInString(stripAnsiEscape(r))
if l > w {
w = l
}
}

return w
c []string // content lines (trimmed)
mw int // maximal content width (f.e, for multilines columns)
w int // meta content width
}

// width returns content width
func (c *content) width() int {
m := c.maxLinewidth()
m := c.mw
if m > c.w {
return m
}
Expand Down Expand Up @@ -104,17 +91,29 @@ func (c *content) line(l string, a int) string {
// newContent returns new content object
func newContent(s string) *content {
c := strings.Split(s, "\n")
mw := 0

for i, v := range c {
c[i] = strings.TrimSpace(v)
w := realLength(c[i])

if w > mw {
mw = w
}
}

return &content{
c: c,
c: c,
mw: mw,
}
}

// stripAnsiEscape returns string without ANSI escape sequences (colors etc)
func stripAnsiEscape(s string) string {
return stripAnsiEscapeRegexp.ReplaceAllString(s, "")
}

// realWidth returns real string length (without ANSI escape sequences)
func realLength(s string) int {
return utf8.RuneCountInString(stripAnsiEscape(s))
}
13 changes: 13 additions & 0 deletions content_test.go
@@ -1,6 +1,7 @@
package simpletable

import (
"fmt"
"testing"
)

Expand Down Expand Up @@ -82,6 +83,18 @@ func TestContent_StripAnsiEscape(t *testing.T) {
}
}

func TestContent_RealLength(t *testing.T) {
colorDefault := "\x1b[39m"
colorRed := "\x1b[91m"

plainString := "I am string"
colorizedString := fmt.Sprintf("%s%s%s", colorRed, plainString, colorDefault)

if realLength(plainString) != realLength(colorizedString) {
t.Error("Wrong real string length calculation")
}
}

func testContentEqual(o, s []string) bool {
for i, v := range o {
if s[i] != v {
Expand Down

0 comments on commit 6e300ca

Please sign in to comment.