Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
go
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
go
Commits
21805061
Commit
21805061
authored
Mar 26, 2013
by
Robert Griesemer
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
godoc: internal cleanup: remove a TODO
R=golang-dev, r CC=golang-dev
https://golang.org/cl/8005044
parent
3660b532
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
41 additions
and
29 deletions
+41
-29
src/cmd/godoc/format.go
src/cmd/godoc/format.go
+41
-29
No files found.
src/cmd/godoc/format.go
View file @
21805061
...
@@ -23,15 +23,21 @@ import (
...
@@ -23,15 +23,21 @@ import (
// ----------------------------------------------------------------------------
// ----------------------------------------------------------------------------
// Implementation of FormatSelections
// Implementation of FormatSelections
// A Selection is a function returning offset pairs []int{a, b}
// A Segment describes a text segment [start, end).
// describing consecutive non-overlapping text segments [a, b).
// The zero value of a Segment is a ready-to-use empty segment.
// If there are no more segments, a Selection must return nil.
//
//
// TODO It's more efficient to return a pair (a, b int) instead
type
Segment
struct
{
// of creating lots of slices. Need to determine how to
start
,
end
int
// indicate the end of a Selection.
}
func
(
seg
*
Segment
)
isEmpty
()
bool
{
return
seg
.
start
>=
seg
.
end
}
// A Selection is an "iterator" function returning a text segment.
// Repeated calls to a selection return consecutive, non-overlapping,
// non-empty segments, followed by an infinite sequence of empty
// segments. The first empty segment marks the end of the selection.
//
//
type
Selection
func
()
[]
i
nt
type
Selection
func
()
Segme
nt
// A LinkWriter writes some start or end "tag" to w for the text offset offs.
// A LinkWriter writes some start or end "tag" to w for the text offset offs.
// It is called by FormatSelections at the start or end of each link segment.
// It is called by FormatSelections at the start or end of each link segment.
...
@@ -141,17 +147,17 @@ func FormatSelections(w io.Writer, text []byte, lw LinkWriter, links Selection,
...
@@ -141,17 +147,17 @@ func FormatSelections(w io.Writer, text []byte, lw LinkWriter, links Selection,
//
//
type
merger
struct
{
type
merger
struct
{
selections
[]
Selection
selections
[]
Selection
segments
[]
[]
i
nt
// segments[i] is the next segment of selections[i]
segments
[]
Segme
nt
// segments[i] is the next segment of selections[i]
}
}
const
infinity
int
=
2e9
const
infinity
int
=
2e9
func
newMerger
(
selections
[]
Selection
)
*
merger
{
func
newMerger
(
selections
[]
Selection
)
*
merger
{
segments
:=
make
([]
[]
i
nt
,
len
(
selections
))
segments
:=
make
([]
Segme
nt
,
len
(
selections
))
for
i
,
sel
:=
range
selections
{
for
i
,
sel
:=
range
selections
{
segments
[
i
]
=
[]
i
nt
{
infinity
,
infinity
}
segments
[
i
]
=
Segme
nt
{
infinity
,
infinity
}
if
sel
!=
nil
{
if
sel
!=
nil
{
if
seg
:=
sel
();
seg
!=
nil
{
if
seg
:=
sel
();
!
seg
.
isEmpty
()
{
segments
[
i
]
=
seg
segments
[
i
]
=
seg
}
}
}
}
...
@@ -170,12 +176,12 @@ func (m *merger) next() (index, offs int, start bool) {
...
@@ -170,12 +176,12 @@ func (m *merger) next() (index, offs int, start bool) {
index
=
-
1
index
=
-
1
for
i
,
seg
:=
range
m
.
segments
{
for
i
,
seg
:=
range
m
.
segments
{
switch
{
switch
{
case
seg
[
0
]
<
offs
:
case
seg
.
start
<
offs
:
offs
=
seg
[
0
]
offs
=
seg
.
start
index
=
i
index
=
i
start
=
true
start
=
true
case
seg
[
1
]
<
offs
:
case
seg
.
end
<
offs
:
offs
=
seg
[
1
]
offs
=
seg
.
end
index
=
i
index
=
i
start
=
false
start
=
false
}
}
...
@@ -188,18 +194,17 @@ func (m *merger) next() (index, offs int, start bool) {
...
@@ -188,18 +194,17 @@ func (m *merger) next() (index, offs int, start bool) {
// either way it is ok to consume the start offset: set it
// either way it is ok to consume the start offset: set it
// to infinity so it won't be considered in the following
// to infinity so it won't be considered in the following
// next call
// next call
m
.
segments
[
index
]
[
0
]
=
infinity
m
.
segments
[
index
]
.
start
=
infinity
if
start
{
if
start
{
return
return
}
}
// end offset found - consume it
// end offset found - consume it
m
.
segments
[
index
]
[
1
]
=
infinity
m
.
segments
[
index
]
.
end
=
infinity
// advance to the next segment for that selection
// advance to the next segment for that selection
seg
:=
m
.
selections
[
index
]()
seg
:=
m
.
selections
[
index
]()
if
seg
==
nil
{
if
!
seg
.
isEmpty
()
{
return
}
m
.
segments
[
index
]
=
seg
m
.
segments
[
index
]
=
seg
}
return
return
}
}
...
@@ -209,7 +214,7 @@ func (m *merger) next() (index, offs int, start bool) {
...
@@ -209,7 +214,7 @@ func (m *merger) next() (index, offs int, start bool) {
// lineSelection returns the line segments for text as a Selection.
// lineSelection returns the line segments for text as a Selection.
func
lineSelection
(
text
[]
byte
)
Selection
{
func
lineSelection
(
text
[]
byte
)
Selection
{
i
,
j
:=
0
,
0
i
,
j
:=
0
,
0
return
func
()
(
seg
[]
i
nt
)
{
return
func
()
(
seg
Segme
nt
)
{
// find next newline, if any
// find next newline, if any
for
j
<
len
(
text
)
{
for
j
<
len
(
text
)
{
j
++
j
++
...
@@ -219,7 +224,7 @@ func lineSelection(text []byte) Selection {
...
@@ -219,7 +224,7 @@ func lineSelection(text []byte) Selection {
}
}
if
i
<
j
{
if
i
<
j
{
// text[i:j] constitutes a line
// text[i:j] constitutes a line
seg
=
[]
i
nt
{
i
,
j
}
seg
=
Segme
nt
{
i
,
j
}
i
=
j
i
=
j
}
}
return
return
...
@@ -234,7 +239,7 @@ func tokenSelection(src []byte, sel token.Token) Selection {
...
@@ -234,7 +239,7 @@ func tokenSelection(src []byte, sel token.Token) Selection {
fset
:=
token
.
NewFileSet
()
fset
:=
token
.
NewFileSet
()
file
:=
fset
.
AddFile
(
""
,
fset
.
Base
(),
len
(
src
))
file
:=
fset
.
AddFile
(
""
,
fset
.
Base
(),
len
(
src
))
s
.
Init
(
file
,
src
,
nil
,
scanner
.
ScanComments
)
s
.
Init
(
file
,
src
,
nil
,
scanner
.
ScanComments
)
return
func
()
(
seg
[]
i
nt
)
{
return
func
()
(
seg
Segme
nt
)
{
for
{
for
{
pos
,
tok
,
lit
:=
s
.
Scan
()
pos
,
tok
,
lit
:=
s
.
Scan
()
if
tok
==
token
.
EOF
{
if
tok
==
token
.
EOF
{
...
@@ -242,7 +247,7 @@ func tokenSelection(src []byte, sel token.Token) Selection {
...
@@ -242,7 +247,7 @@ func tokenSelection(src []byte, sel token.Token) Selection {
}
}
offs
:=
file
.
Offset
(
pos
)
offs
:=
file
.
Offset
(
pos
)
if
tok
==
sel
{
if
tok
==
sel
{
seg
=
[]
i
nt
{
offs
,
offs
+
len
(
lit
)}
seg
=
Segme
nt
{
offs
,
offs
+
len
(
lit
)}
break
break
}
}
}
}
...
@@ -251,13 +256,20 @@ func tokenSelection(src []byte, sel token.Token) Selection {
...
@@ -251,13 +256,20 @@ func tokenSelection(src []byte, sel token.Token) Selection {
}
}
// makeSelection is a helper function to make a Selection from a slice of pairs.
// makeSelection is a helper function to make a Selection from a slice of pairs.
// Pairs describing empty segments are ignored.
//
func
makeSelection
(
matches
[][]
int
)
Selection
{
func
makeSelection
(
matches
[][]
int
)
Selection
{
return
func
()
(
seg
[]
int
)
{
i
:=
0
if
len
(
matches
)
>
0
{
return
func
()
Segment
{
seg
=
matches
[
0
]
for
i
<
len
(
matches
)
{
matches
=
matches
[
1
:
]
m
:=
matches
[
i
]
i
++
if
m
[
0
]
<
m
[
1
]
{
// non-empty segment
return
Segment
{
m
[
0
],
m
[
1
]}
}
}
return
}
return
Segment
{}
}
}
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment