Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
F
fzf
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to JiHu GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
KMSCAKKSCFKA AKFACAMADCAS
fzf
Commits
28810c17
Unverified
Commit
28810c17
authored
7 years ago
by
Junegunn Choi
Browse files
Options
Downloads
Patches
Plain Diff
Optimize ANSI code scanner
This change gives 5x speed improvement
parent
a9e64efe
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/ansi.go
+47
-13
47 additions, 13 deletions
src/ansi.go
with
47 additions
and
13 deletions
src/ansi.go
+
47
−
13
View file @
28810c17
...
...
@@ -44,7 +44,21 @@ func init() {
*/
// The following regular expression will include not all but most of the
// frequently used ANSI sequences
ansiRegex
=
regexp
.
MustCompile
(
"
\x1b
[
\\
[()][0-9;]*[a-zA-Z@]|
\x1b
.|[
\x0e\x0f
]|.
\x08
"
)
ansiRegex
=
regexp
.
MustCompile
(
"(?:
\x1b
[
\\
[()][0-9;]*[a-zA-Z@]|
\x1b
.|[
\x0e\x0f
]|.
\x08
)"
)
}
func
findAnsiStart
(
str
string
)
int
{
idx
:=
0
for
;
idx
<
len
(
str
);
idx
++
{
b
:=
str
[
idx
]
if
b
==
0x1b
||
b
==
0x0e
||
b
==
0x0f
{
return
idx
}
if
b
==
0x08
&&
idx
>
0
{
return
idx
-
1
}
}
return
idx
}
func
extractColor
(
str
string
,
state
*
ansiState
,
proc
func
(
string
,
*
ansiState
)
bool
)
(
string
,
*
[]
ansiOffset
,
*
ansiState
)
{
...
...
@@ -55,41 +69,61 @@ func extractColor(str string, state *ansiState, proc func(string, *ansiState) bo
offsets
=
append
(
offsets
,
ansiOffset
{[
2
]
int32
{
0
,
0
},
*
state
})
}
idx
:=
0
for
_
,
offset
:=
range
ansiRegex
.
FindAllStringIndex
(
str
,
-
1
)
{
prev
:=
str
[
idx
:
offset
[
0
]]
output
.
WriteString
(
prev
)
prevIdx
:=
0
runeCount
:=
0
for
idx
:=
0
;
idx
<
len
(
str
);
{
idx
+=
findAnsiStart
(
str
[
idx
:
])
// No sign of ANSI code
if
idx
==
len
(
str
)
{
break
}
// Make sure that we found an ANSI code
offset
:=
ansiRegex
.
FindStringIndex
(
str
[
idx
:
])
if
offset
==
nil
{
idx
++
continue
}
offset
[
0
]
+=
idx
offset
[
1
]
+=
idx
idx
=
offset
[
1
]
// Check if we should continue
prev
:=
str
[
prevIdx
:
offset
[
0
]]
if
proc
!=
nil
&&
!
proc
(
prev
,
state
)
{
return
""
,
nil
,
nil
}
newState
:=
interpretCode
(
str
[
offset
[
0
]
:
offset
[
1
]],
state
)
prevIdx
=
offset
[
1
]
runeCount
+=
utf8
.
RuneCountInString
(
prev
)
output
.
WriteString
(
prev
)
newState
:=
interpretCode
(
str
[
offset
[
0
]
:
offset
[
1
]],
state
)
if
!
newState
.
equals
(
state
)
{
if
state
!=
nil
{
// Update last offset
(
&
offsets
[
len
(
offsets
)
-
1
])
.
offset
[
1
]
=
int32
(
utf8
.
R
uneCount
(
output
.
Bytes
())
)
(
&
offsets
[
len
(
offsets
)
-
1
])
.
offset
[
1
]
=
int32
(
r
uneCount
)
}
if
newState
.
colored
()
{
// Append new offset
state
=
newState
newLen
:=
int32
(
utf8
.
RuneCount
(
output
.
Bytes
()))
offsets
=
append
(
offsets
,
ansiOffset
{[
2
]
int32
{
newLen
,
newLen
},
*
state
})
offsets
=
append
(
offsets
,
ansiOffset
{[
2
]
int32
{
int32
(
runeCount
),
int32
(
runeCount
)},
*
state
})
}
else
{
// Discard state
state
=
nil
}
}
idx
=
offset
[
1
]
}
rest
:=
str
[
i
dx
:
]
rest
:=
str
[
prevI
dx
:
]
if
len
(
rest
)
>
0
{
output
.
WriteString
(
rest
)
if
state
!=
nil
{
// Update last offset
(
&
offsets
[
len
(
offsets
)
-
1
])
.
offset
[
1
]
=
int32
(
utf8
.
RuneCount
(
output
.
Bytes
()))
runeCount
+=
utf8
.
RuneCountInString
(
rest
)
(
&
offsets
[
len
(
offsets
)
-
1
])
.
offset
[
1
]
=
int32
(
runeCount
)
}
}
if
proc
!=
nil
{
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment