2010-04-26 23:35:12 -06:00
|
|
|
// Copyright 2010 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
// The /doc/codewalk/ tree is synthesized from codewalk descriptions,
|
|
|
|
// files named $GOROOT/doc/codewalk/*.xml.
|
|
|
|
// For an example and a description of the format, see
|
|
|
|
// http://golang.org/doc/codewalk/codewalk or run godoc -http=:6060
|
|
|
|
// and see http://localhost:6060/doc/codewalk/codewalk .
|
|
|
|
// That page is itself a codewalk; the source code for it is
|
|
|
|
// $GOROOT/doc/codewalk/codewalk.xml.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"http"
|
|
|
|
"io"
|
|
|
|
"log"
|
|
|
|
"os"
|
2011-09-26 16:33:13 -06:00
|
|
|
"regexp"
|
2010-04-26 23:35:12 -06:00
|
|
|
"sort"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2011-08-16 22:55:57 -06:00
|
|
|
"template"
|
2010-04-26 23:35:12 -06:00
|
|
|
"utf8"
|
|
|
|
"xml"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Handler for /doc/codewalk/ and below.
|
2010-09-28 22:30:12 -06:00
|
|
|
func codewalk(w http.ResponseWriter, r *http.Request) {
|
2010-04-26 23:35:12 -06:00
|
|
|
relpath := r.URL.Path[len("/doc/codewalk/"):]
|
|
|
|
abspath := absolutePath(r.URL.Path[1:], *goroot)
|
|
|
|
|
|
|
|
r.ParseForm()
|
|
|
|
if f := r.FormValue("fileprint"); f != "" {
|
2010-09-28 22:30:12 -06:00
|
|
|
codewalkFileprint(w, r, f)
|
2010-04-26 23:35:12 -06:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// If directory exists, serve list of code walks.
|
2011-06-15 15:06:35 -06:00
|
|
|
dir, err := fs.Lstat(abspath)
|
2010-04-26 23:35:12 -06:00
|
|
|
if err == nil && dir.IsDirectory() {
|
2010-09-28 22:30:12 -06:00
|
|
|
codewalkDir(w, r, relpath, abspath)
|
2010-04-26 23:35:12 -06:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// If file exists, serve using standard file server.
|
|
|
|
if err == nil {
|
2010-09-28 22:30:12 -06:00
|
|
|
serveFile(w, r)
|
2010-04-26 23:35:12 -06:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise append .xml and hope to find
|
|
|
|
// a codewalk description.
|
|
|
|
cw, err := loadCodewalk(abspath + ".xml")
|
|
|
|
if err != nil {
|
log: new interface
New logging interface simplifies and generalizes.
1) Loggers now have only one output.
2) log.Stdout, Stderr, Crash and friends are gone.
Logging is now always to standard error by default.
3) log.Panic* replaces log.Crash*.
4) Exiting and panicking are not part of the logger's state; instead
the functions Exit* and Panic* simply call Exit or panic after
printing.
5) There is now one 'standard logger'. Instead of calling Stderr,
use Print etc. There are now triples, by analogy with fmt:
Print, Println, Printf
What was log.Stderr is now best represented by log.Println,
since there are now separate Print and Println functions
(and methods).
6) New functions SetOutput, SetFlags, and SetPrefix allow global
editing of the standard logger's properties. This is new
functionality. For instance, one can call
log.SetFlags(log.Lshortfile|log.Ltime|log.Lmicroseconds)
to get all logging output to show file name, line number, and
time stamp.
In short, for most purposes
log.Stderr -> log.Println or log.Print
log.Stderrf -> log.Printf
log.Crash -> log.Panicln or log.Panic
log.Crashf -> log.Panicf
log.Exit -> log.Exitln or log.Exit
log.Exitf -> log.Exitf (no change)
This has a slight breakage: since loggers now write only to one
output, existing calls to log.New() need to delete the second argument.
Also, custom loggers with exit or panic properties will need to be
reworked.
All package code updated to new interface.
The test has been reworked somewhat.
The old interface will be removed after the new release.
For now, its elements are marked 'deprecated' in their comments.
Fixes #1184.
R=rsc
CC=golang-dev
https://golang.org/cl/2419042
2010-10-12 13:59:18 -06:00
|
|
|
log.Print(err)
|
2010-09-28 22:30:12 -06:00
|
|
|
serveError(w, r, relpath, err)
|
2010-04-26 23:35:12 -06:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2010-06-30 01:56:51 -06:00
|
|
|
// Canonicalize the path and redirect if changed
|
2010-09-28 22:30:12 -06:00
|
|
|
if redirect(w, r) {
|
2010-06-30 01:56:51 -06:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2010-04-26 23:35:12 -06:00
|
|
|
b := applyTemplate(codewalkHTML, "codewalk", cw)
|
2010-09-28 22:30:12 -06:00
|
|
|
servePage(w, "Codewalk: "+cw.Title, "", "", b)
|
2010-04-26 23:35:12 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// A Codewalk represents a single codewalk read from an XML file.
|
|
|
|
type Codewalk struct {
|
2011-06-29 07:52:34 -06:00
|
|
|
Title string `xml:"attr"`
|
2010-04-26 23:35:12 -06:00
|
|
|
File []string
|
|
|
|
Step []*Codestep
|
|
|
|
}
|
|
|
|
|
|
|
|
// A Codestep is a single step in a codewalk.
|
|
|
|
type Codestep struct {
|
|
|
|
// Filled in from XML
|
2011-06-29 07:52:34 -06:00
|
|
|
Src string `xml:"attr"`
|
|
|
|
Title string `xml:"attr"`
|
|
|
|
XML string `xml:"innerxml"`
|
2010-04-26 23:35:12 -06:00
|
|
|
|
|
|
|
// Derived from Src; not in XML.
|
|
|
|
Err os.Error
|
|
|
|
File string
|
|
|
|
Lo int
|
|
|
|
LoByte int
|
|
|
|
Hi int
|
|
|
|
HiByte int
|
|
|
|
Data []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
// String method for printing in template.
|
|
|
|
// Formats file address nicely.
|
|
|
|
func (st *Codestep) String() string {
|
|
|
|
s := st.File
|
|
|
|
if st.Lo != 0 || st.Hi != 0 {
|
|
|
|
s += fmt.Sprintf(":%d", st.Lo)
|
|
|
|
if st.Lo != st.Hi {
|
|
|
|
s += fmt.Sprintf(",%d", st.Hi)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
// loadCodewalk reads a codewalk from the named XML file.
|
2011-06-15 15:06:35 -06:00
|
|
|
func loadCodewalk(filename string) (*Codewalk, os.Error) {
|
|
|
|
f, err := fs.Open(filename)
|
2010-04-26 23:35:12 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
cw := new(Codewalk)
|
|
|
|
p := xml.NewParser(f)
|
|
|
|
p.Entity = xml.HTMLEntity
|
|
|
|
err = p.Unmarshal(cw, nil)
|
|
|
|
if err != nil {
|
2011-06-15 15:06:35 -06:00
|
|
|
return nil, &os.PathError{"parsing", filename, err}
|
2010-04-26 23:35:12 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Compute file list, evaluate line numbers for addresses.
|
|
|
|
m := make(map[string]bool)
|
|
|
|
for _, st := range cw.Step {
|
|
|
|
i := strings.Index(st.Src, ":")
|
|
|
|
if i < 0 {
|
|
|
|
i = len(st.Src)
|
|
|
|
}
|
2011-06-15 15:06:35 -06:00
|
|
|
filename := st.Src[0:i]
|
2011-09-21 16:12:06 -06:00
|
|
|
data, err := ReadFile(fs, absolutePath(filename, *goroot))
|
2010-04-26 23:35:12 -06:00
|
|
|
if err != nil {
|
|
|
|
st.Err = err
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if i < len(st.Src) {
|
|
|
|
lo, hi, err := addrToByteRange(st.Src[i+1:], 0, data)
|
|
|
|
if err != nil {
|
|
|
|
st.Err = err
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// Expand match to line boundaries.
|
|
|
|
for lo > 0 && data[lo-1] != '\n' {
|
|
|
|
lo--
|
|
|
|
}
|
|
|
|
for hi < len(data) && (hi == 0 || data[hi-1] != '\n') {
|
|
|
|
hi++
|
|
|
|
}
|
|
|
|
st.Lo = byteToLine(data, lo)
|
|
|
|
st.Hi = byteToLine(data, hi-1)
|
|
|
|
}
|
|
|
|
st.Data = data
|
2011-06-15 15:06:35 -06:00
|
|
|
st.File = filename
|
|
|
|
m[filename] = true
|
2010-04-26 23:35:12 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Make list of files
|
|
|
|
cw.File = make([]string, len(m))
|
|
|
|
i := 0
|
|
|
|
for f := range m {
|
|
|
|
cw.File[i] = f
|
|
|
|
i++
|
|
|
|
}
|
2011-07-07 18:52:50 -06:00
|
|
|
sort.Strings(cw.File)
|
2010-04-26 23:35:12 -06:00
|
|
|
|
|
|
|
return cw, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// codewalkDir serves the codewalk directory listing.
|
|
|
|
// It scans the directory for subdirectories or files named *.xml
|
|
|
|
// and prepares a table.
|
2010-09-28 22:30:12 -06:00
|
|
|
func codewalkDir(w http.ResponseWriter, r *http.Request, relpath, abspath string) {
|
2010-04-26 23:35:12 -06:00
|
|
|
type elem struct {
|
|
|
|
Name string
|
|
|
|
Title string
|
|
|
|
}
|
|
|
|
|
2011-06-15 15:06:35 -06:00
|
|
|
dir, err := fs.ReadDir(abspath)
|
2010-04-26 23:35:12 -06:00
|
|
|
if err != nil {
|
log: new interface
New logging interface simplifies and generalizes.
1) Loggers now have only one output.
2) log.Stdout, Stderr, Crash and friends are gone.
Logging is now always to standard error by default.
3) log.Panic* replaces log.Crash*.
4) Exiting and panicking are not part of the logger's state; instead
the functions Exit* and Panic* simply call Exit or panic after
printing.
5) There is now one 'standard logger'. Instead of calling Stderr,
use Print etc. There are now triples, by analogy with fmt:
Print, Println, Printf
What was log.Stderr is now best represented by log.Println,
since there are now separate Print and Println functions
(and methods).
6) New functions SetOutput, SetFlags, and SetPrefix allow global
editing of the standard logger's properties. This is new
functionality. For instance, one can call
log.SetFlags(log.Lshortfile|log.Ltime|log.Lmicroseconds)
to get all logging output to show file name, line number, and
time stamp.
In short, for most purposes
log.Stderr -> log.Println or log.Print
log.Stderrf -> log.Printf
log.Crash -> log.Panicln or log.Panic
log.Crashf -> log.Panicf
log.Exit -> log.Exitln or log.Exit
log.Exitf -> log.Exitf (no change)
This has a slight breakage: since loggers now write only to one
output, existing calls to log.New() need to delete the second argument.
Also, custom loggers with exit or panic properties will need to be
reworked.
All package code updated to new interface.
The test has been reworked somewhat.
The old interface will be removed after the new release.
For now, its elements are marked 'deprecated' in their comments.
Fixes #1184.
R=rsc
CC=golang-dev
https://golang.org/cl/2419042
2010-10-12 13:59:18 -06:00
|
|
|
log.Print(err)
|
2010-09-28 22:30:12 -06:00
|
|
|
serveError(w, r, relpath, err)
|
2010-04-26 23:35:12 -06:00
|
|
|
return
|
|
|
|
}
|
2011-08-20 13:30:26 -06:00
|
|
|
var v []interface{}
|
2010-04-26 23:35:12 -06:00
|
|
|
for _, fi := range dir {
|
2011-06-15 15:06:35 -06:00
|
|
|
name := fi.Name()
|
2010-04-26 23:35:12 -06:00
|
|
|
if fi.IsDirectory() {
|
2011-08-20 13:30:26 -06:00
|
|
|
v = append(v, &elem{name + "/", ""})
|
2011-06-15 15:06:35 -06:00
|
|
|
} else if strings.HasSuffix(name, ".xml") {
|
|
|
|
cw, err := loadCodewalk(abspath + "/" + name)
|
2010-04-26 23:35:12 -06:00
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
2011-08-20 13:30:26 -06:00
|
|
|
v = append(v, &elem{name[0 : len(name)-len(".xml")], cw.Title})
|
2010-04-26 23:35:12 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
b := applyTemplate(codewalkdirHTML, "codewalkdir", v)
|
2010-09-28 22:30:12 -06:00
|
|
|
servePage(w, "Codewalks", "", "", b)
|
2010-04-26 23:35:12 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// codewalkFileprint serves requests with ?fileprint=f&lo=lo&hi=hi.
|
|
|
|
// The filename f has already been retrieved and is passed as an argument.
|
|
|
|
// Lo and hi are the numbers of the first and last line to highlight
|
|
|
|
// in the response. This format is used for the middle window pane
|
|
|
|
// of the codewalk pages. It is a separate iframe and does not get
|
|
|
|
// the usual godoc HTML wrapper.
|
2010-09-28 22:30:12 -06:00
|
|
|
func codewalkFileprint(w http.ResponseWriter, r *http.Request, f string) {
|
2010-04-26 23:35:12 -06:00
|
|
|
abspath := absolutePath(f, *goroot)
|
2011-09-21 16:12:06 -06:00
|
|
|
data, err := ReadFile(fs, abspath)
|
2010-04-26 23:35:12 -06:00
|
|
|
if err != nil {
|
log: new interface
New logging interface simplifies and generalizes.
1) Loggers now have only one output.
2) log.Stdout, Stderr, Crash and friends are gone.
Logging is now always to standard error by default.
3) log.Panic* replaces log.Crash*.
4) Exiting and panicking are not part of the logger's state; instead
the functions Exit* and Panic* simply call Exit or panic after
printing.
5) There is now one 'standard logger'. Instead of calling Stderr,
use Print etc. There are now triples, by analogy with fmt:
Print, Println, Printf
What was log.Stderr is now best represented by log.Println,
since there are now separate Print and Println functions
(and methods).
6) New functions SetOutput, SetFlags, and SetPrefix allow global
editing of the standard logger's properties. This is new
functionality. For instance, one can call
log.SetFlags(log.Lshortfile|log.Ltime|log.Lmicroseconds)
to get all logging output to show file name, line number, and
time stamp.
In short, for most purposes
log.Stderr -> log.Println or log.Print
log.Stderrf -> log.Printf
log.Crash -> log.Panicln or log.Panic
log.Crashf -> log.Panicf
log.Exit -> log.Exitln or log.Exit
log.Exitf -> log.Exitf (no change)
This has a slight breakage: since loggers now write only to one
output, existing calls to log.New() need to delete the second argument.
Also, custom loggers with exit or panic properties will need to be
reworked.
All package code updated to new interface.
The test has been reworked somewhat.
The old interface will be removed after the new release.
For now, its elements are marked 'deprecated' in their comments.
Fixes #1184.
R=rsc
CC=golang-dev
https://golang.org/cl/2419042
2010-10-12 13:59:18 -06:00
|
|
|
log.Print(err)
|
2010-09-28 22:30:12 -06:00
|
|
|
serveError(w, r, f, err)
|
2010-04-26 23:35:12 -06:00
|
|
|
return
|
|
|
|
}
|
|
|
|
lo, _ := strconv.Atoi(r.FormValue("lo"))
|
|
|
|
hi, _ := strconv.Atoi(r.FormValue("hi"))
|
|
|
|
if hi < lo {
|
|
|
|
hi = lo
|
|
|
|
}
|
|
|
|
lo = lineToByte(data, lo)
|
|
|
|
hi = lineToByte(data, hi+1)
|
|
|
|
|
|
|
|
// Put the mark 4 lines before lo, so that the iframe
|
|
|
|
// shows a few lines of context before the highlighted
|
|
|
|
// section.
|
|
|
|
n := 4
|
|
|
|
mark := lo
|
|
|
|
for ; mark > 0 && n > 0; mark-- {
|
|
|
|
if data[mark-1] == '\n' {
|
|
|
|
if n--; n == 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-09-28 22:30:12 -06:00
|
|
|
io.WriteString(w, `<style type="text/css">@import "/doc/codewalk/codewalk.css";</style><pre>`)
|
|
|
|
template.HTMLEscape(w, data[0:mark])
|
|
|
|
io.WriteString(w, "<a name='mark'></a>")
|
|
|
|
template.HTMLEscape(w, data[mark:lo])
|
2010-04-26 23:35:12 -06:00
|
|
|
if lo < hi {
|
2010-09-28 22:30:12 -06:00
|
|
|
io.WriteString(w, "<div class='codewalkhighlight'>")
|
|
|
|
template.HTMLEscape(w, data[lo:hi])
|
|
|
|
io.WriteString(w, "</div>")
|
2010-04-26 23:35:12 -06:00
|
|
|
}
|
2010-09-28 22:30:12 -06:00
|
|
|
template.HTMLEscape(w, data[hi:])
|
|
|
|
io.WriteString(w, "</pre>")
|
2010-04-26 23:35:12 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// addrToByte evaluates the given address starting at offset start in data.
|
|
|
|
// It returns the lo and hi byte offset of the matched region within data.
|
|
|
|
// See http://plan9.bell-labs.com/sys/doc/sam/sam.html Table II
|
|
|
|
// for details on the syntax.
|
|
|
|
func addrToByteRange(addr string, start int, data []byte) (lo, hi int, err os.Error) {
|
|
|
|
var (
|
|
|
|
dir byte
|
|
|
|
prevc byte
|
|
|
|
charOffset bool
|
|
|
|
)
|
|
|
|
lo = start
|
|
|
|
hi = start
|
|
|
|
for addr != "" && err == nil {
|
|
|
|
c := addr[0]
|
|
|
|
switch c {
|
|
|
|
default:
|
|
|
|
err = os.NewError("invalid address syntax near " + string(c))
|
|
|
|
case ',':
|
|
|
|
if len(addr) == 1 {
|
|
|
|
hi = len(data)
|
|
|
|
} else {
|
|
|
|
_, hi, err = addrToByteRange(addr[1:], hi, data)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
|
|
|
|
case '+', '-':
|
|
|
|
if prevc == '+' || prevc == '-' {
|
|
|
|
lo, hi, err = addrNumber(data, lo, hi, prevc, 1, charOffset)
|
|
|
|
}
|
|
|
|
dir = c
|
|
|
|
|
|
|
|
case '$':
|
|
|
|
lo = len(data)
|
|
|
|
hi = len(data)
|
|
|
|
if len(addr) > 1 {
|
|
|
|
dir = '+'
|
|
|
|
}
|
|
|
|
|
|
|
|
case '#':
|
|
|
|
charOffset = true
|
|
|
|
|
|
|
|
case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
|
|
|
|
var i int
|
|
|
|
for i = 1; i < len(addr); i++ {
|
|
|
|
if addr[i] < '0' || addr[i] > '9' {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var n int
|
|
|
|
n, err = strconv.Atoi(addr[0:i])
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
lo, hi, err = addrNumber(data, lo, hi, dir, n, charOffset)
|
|
|
|
dir = 0
|
|
|
|
charOffset = false
|
|
|
|
prevc = c
|
|
|
|
addr = addr[i:]
|
|
|
|
continue
|
|
|
|
|
|
|
|
case '/':
|
|
|
|
var i, j int
|
|
|
|
Regexp:
|
|
|
|
for i = 1; i < len(addr); i++ {
|
|
|
|
switch addr[i] {
|
|
|
|
case '\\':
|
|
|
|
i++
|
|
|
|
case '/':
|
|
|
|
j = i + 1
|
|
|
|
break Regexp
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if j == 0 {
|
|
|
|
j = i
|
|
|
|
}
|
|
|
|
pattern := addr[1:i]
|
|
|
|
lo, hi, err = addrRegexp(data, lo, hi, dir, pattern)
|
|
|
|
prevc = c
|
|
|
|
addr = addr[j:]
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
prevc = c
|
|
|
|
addr = addr[1:]
|
|
|
|
}
|
|
|
|
|
|
|
|
if err == nil && dir != 0 {
|
|
|
|
lo, hi, err = addrNumber(data, lo, hi, dir, 1, charOffset)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return 0, 0, err
|
|
|
|
}
|
|
|
|
return lo, hi, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// addrNumber applies the given dir, n, and charOffset to the address lo, hi.
|
|
|
|
// dir is '+' or '-', n is the count, and charOffset is true if the syntax
|
|
|
|
// used was #n. Applying +n (or +#n) means to advance n lines
|
|
|
|
// (or characters) after hi. Applying -n (or -#n) means to back up n lines
|
|
|
|
// (or characters) before lo.
|
|
|
|
// The return value is the new lo, hi.
|
|
|
|
func addrNumber(data []byte, lo, hi int, dir byte, n int, charOffset bool) (int, int, os.Error) {
|
|
|
|
switch dir {
|
|
|
|
case 0:
|
|
|
|
lo = 0
|
|
|
|
hi = 0
|
|
|
|
fallthrough
|
|
|
|
|
|
|
|
case '+':
|
|
|
|
if charOffset {
|
|
|
|
pos := hi
|
|
|
|
for ; n > 0 && pos < len(data); n-- {
|
|
|
|
_, size := utf8.DecodeRune(data[pos:])
|
|
|
|
pos += size
|
|
|
|
}
|
|
|
|
if n == 0 {
|
|
|
|
return pos, pos, nil
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
// find next beginning of line
|
|
|
|
if hi > 0 {
|
|
|
|
for hi < len(data) && data[hi-1] != '\n' {
|
|
|
|
hi++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
lo = hi
|
|
|
|
if n == 0 {
|
|
|
|
return lo, hi, nil
|
|
|
|
}
|
|
|
|
for ; hi < len(data); hi++ {
|
|
|
|
if data[hi] != '\n' {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
switch n--; n {
|
|
|
|
case 1:
|
|
|
|
lo = hi + 1
|
|
|
|
case 0:
|
|
|
|
return lo, hi + 1, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
case '-':
|
|
|
|
if charOffset {
|
|
|
|
// Scan backward for bytes that are not UTF-8 continuation bytes.
|
|
|
|
pos := lo
|
|
|
|
for ; pos > 0 && n > 0; pos-- {
|
|
|
|
if data[pos]&0xc0 != 0x80 {
|
|
|
|
n--
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if n == 0 {
|
|
|
|
return pos, pos, nil
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
// find earlier beginning of line
|
|
|
|
for lo > 0 && data[lo-1] != '\n' {
|
|
|
|
lo--
|
|
|
|
}
|
|
|
|
hi = lo
|
|
|
|
if n == 0 {
|
|
|
|
return lo, hi, nil
|
|
|
|
}
|
|
|
|
for ; lo >= 0; lo-- {
|
|
|
|
if lo > 0 && data[lo-1] != '\n' {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
switch n--; n {
|
|
|
|
case 1:
|
|
|
|
hi = lo
|
|
|
|
case 0:
|
|
|
|
return lo, hi, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0, 0, os.NewError("address out of range")
|
|
|
|
}
|
|
|
|
|
|
|
|
// addrRegexp searches for pattern in the given direction starting at lo, hi.
|
|
|
|
// The direction dir is '+' (search forward from hi) or '-' (search backward from lo).
|
|
|
|
// Backward searches are unimplemented.
|
|
|
|
func addrRegexp(data []byte, lo, hi int, dir byte, pattern string) (int, int, os.Error) {
|
|
|
|
re, err := regexp.Compile(pattern)
|
|
|
|
if err != nil {
|
|
|
|
return 0, 0, err
|
|
|
|
}
|
|
|
|
if dir == '-' {
|
|
|
|
// Could implement reverse search using binary search
|
|
|
|
// through file, but that seems like overkill.
|
|
|
|
return 0, 0, os.NewError("reverse search not implemented")
|
|
|
|
}
|
2010-08-12 00:48:41 -06:00
|
|
|
m := re.FindIndex(data[hi:])
|
2010-04-26 23:35:12 -06:00
|
|
|
if len(m) > 0 {
|
|
|
|
m[0] += hi
|
|
|
|
m[1] += hi
|
|
|
|
} else if hi > 0 {
|
|
|
|
// No match. Wrap to beginning of data.
|
2010-08-12 00:48:41 -06:00
|
|
|
m = re.FindIndex(data)
|
2010-04-26 23:35:12 -06:00
|
|
|
}
|
|
|
|
if len(m) == 0 {
|
|
|
|
return 0, 0, os.NewError("no match for " + pattern)
|
|
|
|
}
|
|
|
|
return m[0], m[1], nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// lineToByte returns the byte index of the first byte of line n.
|
|
|
|
// Line numbers begin at 1.
|
|
|
|
func lineToByte(data []byte, n int) int {
|
|
|
|
if n <= 1 {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
n--
|
|
|
|
for i, c := range data {
|
|
|
|
if c == '\n' {
|
|
|
|
if n--; n == 0 {
|
|
|
|
return i + 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return len(data)
|
|
|
|
}
|
|
|
|
|
|
|
|
// byteToLine returns the number of the line containing the byte at index i.
|
|
|
|
func byteToLine(data []byte, i int) int {
|
|
|
|
l := 1
|
|
|
|
for j, c := range data {
|
|
|
|
if j == i {
|
|
|
|
return l
|
|
|
|
}
|
|
|
|
if c == '\n' {
|
|
|
|
l++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return l
|
|
|
|
}
|