LEFT | RIGHT |
1 // Copyright 2010 The Go Authors. All rights reserved. | 1 // Copyright 2010 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 // The /doc/codewalk/ tree is synthesized from codewalk descriptions, | 5 // The /doc/codewalk/ tree is synthesized from codewalk descriptions, |
6 // files named $GOROOT/doc/codewalk/*.xml. | 6 // files named $GOROOT/doc/codewalk/*.xml. |
7 // For an example and a description of the format, see | 7 // For an example and a description of the format, see |
8 // http://golang.org/doc/codewalk/codewalk or run godoc -http=:6060 | 8 // http://golang.org/doc/codewalk/codewalk or run godoc -http=:6060 |
9 // and see http://localhost:6060/doc/codewalk/codewalk . | 9 // and see http://localhost:6060/doc/codewalk/codewalk . |
10 // That page is itself a codewalk; the source code for it is | 10 // That page is itself a codewalk; the source code for it is |
(...skipping 255 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
266 dir byte | 266 dir byte |
267 prevc byte | 267 prevc byte |
268 charOffset bool | 268 charOffset bool |
269 ) | 269 ) |
270 lo = start | 270 lo = start |
271 hi = start | 271 hi = start |
272 for addr != "" && err == nil { | 272 for addr != "" && err == nil { |
273 c := addr[0] | 273 c := addr[0] |
274 switch c { | 274 switch c { |
275 default: | 275 default: |
276 » » » err = os.ErrorString("invalid address syntax near " + st
ring(c)) | 276 » » » err = os.NewError("invalid address syntax near " + strin
g(c)) |
277 case ',': | 277 case ',': |
278 if len(addr) == 1 { | 278 if len(addr) == 1 { |
279 hi = len(data) | 279 hi = len(data) |
280 } else { | 280 } else { |
281 _, hi, err = addrToByteRange(addr[1:], hi, data) | 281 _, hi, err = addrToByteRange(addr[1:], hi, data) |
282 } | 282 } |
283 return | 283 return |
284 | 284 |
285 case '+', '-': | 285 case '+', '-': |
286 if prevc == '+' || prevc == '-' { | 286 if prevc == '+' || prevc == '-' { |
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
427 } | 427 } |
428 switch n--; n { | 428 switch n--; n { |
429 case 1: | 429 case 1: |
430 hi = lo | 430 hi = lo |
431 case 0: | 431 case 0: |
432 return lo, hi, nil | 432 return lo, hi, nil |
433 } | 433 } |
434 } | 434 } |
435 } | 435 } |
436 | 436 |
437 » return 0, 0, os.ErrorString("address out of range") | 437 » return 0, 0, os.NewError("address out of range") |
438 } | 438 } |
439 | 439 |
440 | 440 |
441 // addrRegexp searches for pattern in the given direction starting at lo, hi. | 441 // addrRegexp searches for pattern in the given direction starting at lo, hi. |
442 // The direction dir is '+' (search forward from hi) or '-' (search backward fro
m lo). | 442 // The direction dir is '+' (search forward from hi) or '-' (search backward fro
m lo). |
443 // Backward searches are unimplemented. | 443 // Backward searches are unimplemented. |
444 func addrRegexp(data []byte, lo, hi int, dir byte, pattern string) (int, int, os
.Error) { | 444 func addrRegexp(data []byte, lo, hi int, dir byte, pattern string) (int, int, os
.Error) { |
445 re, err := regexp.Compile(pattern) | 445 re, err := regexp.Compile(pattern) |
446 if err != nil { | 446 if err != nil { |
447 return 0, 0, err | 447 return 0, 0, err |
448 } | 448 } |
449 if dir == '-' { | 449 if dir == '-' { |
450 // Could implement reverse search using binary search | 450 // Could implement reverse search using binary search |
451 // through file, but that seems like overkill. | 451 // through file, but that seems like overkill. |
452 » » return 0, 0, os.ErrorString("reverse search not implemented") | 452 » » return 0, 0, os.NewError("reverse search not implemented") |
453 } | 453 } |
454 m := re.FindIndex(data[hi:]) | 454 m := re.FindIndex(data[hi:]) |
455 if len(m) > 0 { | 455 if len(m) > 0 { |
456 m[0] += hi | 456 m[0] += hi |
457 m[1] += hi | 457 m[1] += hi |
458 } else if hi > 0 { | 458 } else if hi > 0 { |
459 // No match. Wrap to beginning of data. | 459 // No match. Wrap to beginning of data. |
460 m = re.FindIndex(data) | 460 m = re.FindIndex(data) |
461 } | 461 } |
462 if len(m) == 0 { | 462 if len(m) == 0 { |
463 » » return 0, 0, os.ErrorString("no match for " + pattern) | 463 » » return 0, 0, os.NewError("no match for " + pattern) |
464 } | 464 } |
465 return m[0], m[1], nil | 465 return m[0], m[1], nil |
466 } | 466 } |
467 | 467 |
468 | 468 |
469 // lineToByte returns the byte index of the first byte of line n. | 469 // lineToByte returns the byte index of the first byte of line n. |
470 // Line numbers begin at 1. | 470 // Line numbers begin at 1. |
471 func lineToByte(data []byte, n int) int { | 471 func lineToByte(data []byte, n int) int { |
472 if n <= 1 { | 472 if n <= 1 { |
473 return 0 | 473 return 0 |
(...skipping 16 matching lines...) Expand all Loading... |
490 for j, c := range data { | 490 for j, c := range data { |
491 if j == i { | 491 if j == i { |
492 return l | 492 return l |
493 } | 493 } |
494 if c == '\n' { | 494 if c == '\n' { |
495 l++ | 495 l++ |
496 } | 496 } |
497 } | 497 } |
498 return l | 498 return l |
499 } | 499 } |
LEFT | RIGHT |