You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

71 lines
1.7 KiB

  1. // Copyright 2013 The Go Authors. All rights reserved.
  2. //
  3. // Use of this source code is governed by a BSD-style
  4. // license that can be found in the LICENSE file or at
  5. // https://developers.google.com/open-source/licenses/bsd.
  6. package gosrc
  7. import (
  8. "regexp"
  9. "strconv"
  10. "strings"
  11. )
  12. var defaultTags = map[string]string{"git": "master", "hg": "default"}
  13. func bestTag(tags map[string]string, defaultTag string) (string, string, error) {
  14. if commit, ok := tags["go1"]; ok {
  15. return "go1", commit, nil
  16. }
  17. if commit, ok := tags[defaultTag]; ok {
  18. return defaultTag, commit, nil
  19. }
  20. return "", "", NotFoundError{Message: "Tag or branch not found."}
  21. }
  22. // expand replaces {k} in template with match[k] or subs[atoi(k)] if k is not in match.
  23. func expand(template string, match map[string]string, subs ...string) string {
  24. var p []byte
  25. var i int
  26. for {
  27. i = strings.Index(template, "{")
  28. if i < 0 {
  29. break
  30. }
  31. p = append(p, template[:i]...)
  32. template = template[i+1:]
  33. i = strings.Index(template, "}")
  34. if s, ok := match[template[:i]]; ok {
  35. p = append(p, s...)
  36. } else {
  37. j, _ := strconv.Atoi(template[:i])
  38. p = append(p, subs[j]...)
  39. }
  40. template = template[i+1:]
  41. }
  42. p = append(p, template...)
  43. return string(p)
  44. }
  45. var readmePat = regexp.MustCompile(`(?i)^readme(?:$|\.)`)
  46. // isDocFile returns true if a file with name n should be included in the
  47. // documentation.
  48. func isDocFile(n string) bool {
  49. if strings.HasSuffix(n, ".go") && n[0] != '_' && n[0] != '.' {
  50. return true
  51. }
  52. return readmePat.MatchString(n)
  53. }
  54. var linePat = regexp.MustCompile(`(?m)^//line .*$`)
  55. func OverwriteLineComments(p []byte) {
  56. for _, m := range linePat.FindAllIndex(p, -1) {
  57. for i := m[0] + 2; i < m[1]; i++ {
  58. p[i] = ' '
  59. }
  60. }
  61. }