Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
G
golang
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
go
golang
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
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
41 additions
and
29 deletions
+41
-29
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 (
// ----------------------------------------------------------------------------
// Implementation of FormatSelections
// A Selection is a function returning offset pairs []int{a, b}
// describing consecutive non-overlapping text segments [a, b).
// If there are no more segments, a Selection must return nil.
// A Segment describes a text segment [start, end).
// The zero value of a Segment is a ready-to-use empty segment.
//
// TODO It's more efficient to return a pair (a, b int) instead
// of creating lots of slices. Need to determine how to
// indicate the end of a Selection.
type
Segment
struct
{
start
,
end
int
}
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.
// 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,
//
type
merger
struct
{
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
func
newMerger
(
selections
[]
Selection
)
*
merger
{
segments
:=
make
([]
[]
i
nt
,
len
(
selections
))
segments
:=
make
([]
Segme
nt
,
len
(
selections
))
for
i
,
sel
:=
range
selections
{
segments
[
i
]
=
[]
i
nt
{
infinity
,
infinity
}
segments
[
i
]
=
Segme
nt
{
infinity
,
infinity
}
if
sel
!=
nil
{
if
seg
:=
sel
();
seg
!=
nil
{
if
seg
:=
sel
();
!
seg
.
isEmpty
()
{
segments
[
i
]
=
seg
}
}
...
...
@@ -170,12 +176,12 @@ func (m *merger) next() (index, offs int, start bool) {
index
=
-
1
for
i
,
seg
:=
range
m
.
segments
{
switch
{
case
seg
[
0
]
<
offs
:
offs
=
seg
[
0
]
case
seg
.
start
<
offs
:
offs
=
seg
.
start
index
=
i
start
=
true
case
seg
[
1
]
<
offs
:
offs
=
seg
[
1
]
case
seg
.
end
<
offs
:
offs
=
seg
.
end
index
=
i
start
=
false
}
...
...
@@ -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
// to infinity so it won't be considered in the following
// next call
m
.
segments
[
index
]
[
0
]
=
infinity
m
.
segments
[
index
]
.
start
=
infinity
if
start
{
return
}
// end offset found - consume it
m
.
segments
[
index
]
[
1
]
=
infinity
m
.
segments
[
index
]
.
end
=
infinity
// advance to the next segment for that selection
seg
:=
m
.
selections
[
index
]()
if
seg
==
nil
{
return
if
!
seg
.
isEmpty
()
{
m
.
segments
[
index
]
=
seg
}
m
.
segments
[
index
]
=
seg
return
}
...
...
@@ -209,7 +214,7 @@ func (m *merger) next() (index, offs int, start bool) {
// lineSelection returns the line segments for text as a Selection.
func
lineSelection
(
text
[]
byte
)
Selection
{
i
,
j
:=
0
,
0
return
func
()
(
seg
[]
i
nt
)
{
return
func
()
(
seg
Segme
nt
)
{
// find next newline, if any
for
j
<
len
(
text
)
{
j
++
...
...
@@ -219,7 +224,7 @@ func lineSelection(text []byte) Selection {
}
if
i
<
j
{
// text[i:j] constitutes a line
seg
=
[]
i
nt
{
i
,
j
}
seg
=
Segme
nt
{
i
,
j
}
i
=
j
}
return
...
...
@@ -234,7 +239,7 @@ func tokenSelection(src []byte, sel token.Token) Selection {
fset
:=
token
.
NewFileSet
()
file
:=
fset
.
AddFile
(
""
,
fset
.
Base
(),
len
(
src
))
s
.
Init
(
file
,
src
,
nil
,
scanner
.
ScanComments
)
return
func
()
(
seg
[]
i
nt
)
{
return
func
()
(
seg
Segme
nt
)
{
for
{
pos
,
tok
,
lit
:=
s
.
Scan
()
if
tok
==
token
.
EOF
{
...
...
@@ -242,7 +247,7 @@ func tokenSelection(src []byte, sel token.Token) Selection {
}
offs
:=
file
.
Offset
(
pos
)
if
tok
==
sel
{
seg
=
[]
i
nt
{
offs
,
offs
+
len
(
lit
)}
seg
=
Segme
nt
{
offs
,
offs
+
len
(
lit
)}
break
}
}
...
...
@@ -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.
// Pairs describing empty segments are ignored.
//
func
makeSelection
(
matches
[][]
int
)
Selection
{
return
func
()
(
seg
[]
int
)
{
if
len
(
matches
)
>
0
{
seg
=
matches
[
0
]
matches
=
matches
[
1
:
]
i
:=
0
return
func
()
Segment
{
for
i
<
len
(
matches
)
{
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