当前位置: 首页>>代码示例>>Golang>>正文


Golang Replacer.Replace方法代码示例

本文整理汇总了Golang中strings.Replacer.Replace方法的典型用法代码示例。如果您正苦于以下问题:Golang Replacer.Replace方法的具体用法?Golang Replacer.Replace怎么用?Golang Replacer.Replace使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在strings.Replacer的用法示例。


在下文中一共展示了Replacer.Replace方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: insertLicense

// Inserts license header to file represented by filename
func insertLicense(filename string, replacer *strings.Replacer, config *Config) error {
	licensedFile := bytes.NewBuffer(nil)

	lcopyright, err := Asset(filepath.Join("licenses", string(config.LicenseType)+".copyright"))

	if err == nil {
		err = prependEOLComment(licensedFile, config, lcopyright)
		if err != nil {
			return err
		}
	}

	lheader, err := Asset(filepath.Join("licenses", string(config.LicenseType)+".header"))
	if err == nil {
		err := prependEOLComment(licensedFile, config, lheader)
		if err != nil {
			return err
		}
	}

	data, err := ioutil.ReadFile(filename)
	_, err = licensedFile.WriteString(string(data))
	if err != nil {
		return err
	}

	filedata := replacer.Replace(licensedFile.String())

	return ioutil.WriteFile(filename, []byte(filedata), 0640)
}
开发者ID:germanescobar,项目名称:licentia,代码行数:31,代码来源:licentia.go

示例2: writeTo

// writeTo serializes the character data entity to the writer.
func (c *CharData) writeTo(w *bufio.Writer, s *WriteSettings) {
	var r *strings.Replacer
	if s.CanonicalText {
		r = xmlReplacerCanonicalText
	} else {
		r = xmlReplacerNormal
	}
	w.WriteString(r.Replace(c.Data))
}
开发者ID:ma314smith,项目名称:etree,代码行数:10,代码来源:etree.go

示例3: ValueWithEscaper

func (n *ninjaString) ValueWithEscaper(pkgNames map[*packageContext]string,
	escaper *strings.Replacer) string {

	str := escaper.Replace(n.strings[0])
	for i, v := range n.variables {
		str += "${" + v.fullName(pkgNames) + "}"
		str += escaper.Replace(n.strings[i+1])
	}
	return str
}
开发者ID:robinivanka,项目名称:blueprint,代码行数:10,代码来源:ninja_strings.go

示例4: ParseMemoryMap

// ParseMemoryMap parses a memory map in the format of
// /proc/self/maps, and overrides the mappings in the current profile.
// It renumbers the samples and locations in the profile correspondingly.
func (p *Profile) ParseMemoryMap(rd io.Reader) error {
	b := bufio.NewReader(rd)

	var attrs []string
	var r *strings.Replacer
	const delimiter = "="
	for {
		l, err := b.ReadString('\n')
		if err != nil {
			if err != io.EOF {
				return err
			}
			if l == "" {
				break
			}
		}
		if l = strings.TrimSpace(l); l == "" {
			continue
		}

		if r != nil {
			l = r.Replace(l)
		}
		m, err := parseMappingEntry(l)
		if err != nil {
			if err == errUnrecognized {
				// Recognize assignments of the form: attr=value, and replace
				// $attr with value on subsequent mappings.
				if attr := strings.SplitN(l, delimiter, 2); len(attr) == 2 {
					attrs = append(attrs, "$"+strings.TrimSpace(attr[0]), strings.TrimSpace(attr[1]))
					r = strings.NewReplacer(attrs...)
				}
				// Ignore any unrecognized entries
				continue
			}
			return err
		}
		if m == nil || (m.File == "" && len(p.Mapping) != 0) {
			// In some cases the first entry may include the address range
			// but not the name of the file. It should be followed by
			// another entry with the name.
			continue
		}
		if len(p.Mapping) == 1 && p.Mapping[0].File == "" {
			// Update the name if this is the entry following that empty one.
			p.Mapping[0].File = m.File
			continue
		}
		p.Mapping = append(p.Mapping, m)
	}
	p.remapLocationIDs()
	p.remapFunctionIDs()
	p.remapMappingIDs()
	return nil
}
开发者ID:danny8002,项目名称:go,代码行数:58,代码来源:legacy_profile.go

示例5: ParseProcMaps

// ParseProcMaps parses a memory map in the format of /proc/self/maps.
// ParseMemoryMap should be called after setting on a profile to
// associate locations to the corresponding mapping based on their
// address.
func ParseProcMaps(rd io.Reader) ([]*Mapping, error) {
	var mapping []*Mapping

	b := bufio.NewReader(rd)

	var attrs []string
	var r *strings.Replacer
	const delimiter = "="
	for {
		l, err := b.ReadString('\n')
		if err != nil {
			if err != io.EOF {
				return nil, err
			}
			if l == "" {
				break
			}
		}
		if l = strings.TrimSpace(l); l == "" {
			continue
		}

		if r != nil {
			l = r.Replace(l)
		}
		m, err := parseMappingEntry(l)
		if err != nil {
			if err == errUnrecognized {
				// Recognize assignments of the form: attr=value, and replace
				// $attr with value on subsequent mappings.
				if attr := strings.SplitN(l, delimiter, 2); len(attr) == 2 {
					attrs = append(attrs, "$"+strings.TrimSpace(attr[0]), strings.TrimSpace(attr[1]))
					r = strings.NewReplacer(attrs...)
				}
				// Ignore any unrecognized entries
				continue
			}
			return nil, err
		}
		if m == nil {
			continue
		}
		mapping = append(mapping, m)
	}
	return mapping, nil
}
开发者ID:achanda,项目名称:go,代码行数:50,代码来源:profile_memmap.go

示例6: insertLicense

// Inserts license header to file represented by filename
func insertLicense(filename string, replacer *strings.Replacer, config *Config) error {
	licensedFile := bytes.NewBuffer(nil)

	lcopyright, err := Asset(filepath.Join("licenses", string(config.LicenseType)+".copyright"))

	cr := false
	if err == nil {
		err = prependEOLComment(licensedFile, config.EOLCommentStyle,
			[]byte(replacer.Replace(string(lcopyright))))
		if err != nil {
			return err
		}
		cr = true
	}

	lheader, err := Asset(filepath.Join("licenses", string(config.LicenseType)+".header"))
	if err == nil {
		plus := ""
		if cr {
			plus = "\n"
		}
		err := prependEOLComment(licensedFile, config.EOLCommentStyle,
			[]byte(replacer.Replace(plus+string(lheader))))
		if err != nil {
			return err
		}
	}
	// Extra newline for separating license code from package docs.
	licensedFile.WriteByte('\n')

	// Only use the replacer for the license, not the whole file.

	fh, err := os.Open(filename)
	if err != nil {
		return err
	}
	_, err = io.Copy(licensedFile, fh)
	fh.Close()
	if err != nil {
		return err
	}

	return ioutil.WriteFile(filename, licensedFile.Bytes(), 0640)
}
开发者ID:tgulacsi,项目名称:licentia,代码行数:45,代码来源:licentia.go

示例7: parseProcMapsFromScanner

func parseProcMapsFromScanner(s *bufio.Scanner) ([]*Mapping, error) {
	var mapping []*Mapping

	var attrs []string
	var r *strings.Replacer
	const delimiter = "="
	for s.Scan() {
		line := strings.TrimSpace(s.Text())
		if line == "" {
			continue
		}

		if r != nil {
			line = r.Replace(line)
		}
		m, err := parseMappingEntry(line)
		if err != nil {
			if err == errUnrecognized {
				// Recognize assignments of the form: attr=value, and replace
				// $attr with value on subsequent mappings.
				if attr := strings.SplitN(line, delimiter, 2); len(attr) == 2 {
					attrs = append(attrs, "$"+strings.TrimSpace(attr[0]), strings.TrimSpace(attr[1]))
					r = strings.NewReplacer(attrs...)
				}
				// Ignore any unrecognized entries
				continue
			}
			return nil, err
		}
		if m == nil {
			continue
		}
		mapping = append(mapping, m)
	}
	if err := s.Err(); err != nil {
		return nil, err
	}
	return mapping, nil
}
开发者ID:google,项目名称:pprof,代码行数:39,代码来源:legacy_profile.go

示例8: pageRenderLinks

func pageRenderLinks(content string) (res string) {
	re := regexp.MustCompile("\\[\\[.*\\]\\]")
	links := re.FindAllString(content, -1)

	for _, include := range links {
		title := strings.Trim(include, "[] ")

		renderAsLink := false
		if title[0] == '@' {
			renderAsLink = true
			title = title[1:]
		}

		isFile := false
		if len(title) > 5 && title[0:5] == "file:" {
			isFile = true
			title = title[5:]
		}

		var r *strings.Replacer
		if renderAsLink {
			if isFile {
				r = strings.NewReplacer(include, "/file/get/"+title)
			} else {
				r = strings.NewReplacer(include, "/page/"+title)
			}
		} else {
			if isFile {
				r = strings.NewReplacer(include, "<a href=\"/file/get/"+title+"\" >"+title+"</a>")
			} else {
				r = strings.NewReplacer(include, "<a href=\"/page/"+title+"\" >"+title+"</a>")
			}
		}
		content = r.Replace(content)
	}
	res = content
	return
}
开发者ID:manageryzy,项目名称:go-wikiiii,代码行数:38,代码来源:page.go

示例9: SetPlaceholder

// SetPlaceholder fix @placeholder in post values
func (p *Page) SetPlaceholder(htmlReplacer *strings.Replacer) {
	p.contentBytes = []byte(htmlReplacer.Replace(string(p.contentBytes)))
}
开发者ID:go-xiaohei,项目名称:pugo,代码行数:4,代码来源:page.go

示例10: SetPlaceholder

// SetPlaceholder fix @placeholder in post values
func (p *Post) SetPlaceholder(stringReplacer, htmlReplacer *strings.Replacer) {
	p.Thumb = stringReplacer.Replace(p.Thumb)
	p.contentBytes = []byte(htmlReplacer.Replace(string(p.contentBytes)))
	p.briefBytes = []byte(htmlReplacer.Replace(string(p.briefBytes)))
}
开发者ID:go-xiaohei,项目名称:pugo,代码行数:6,代码来源:post.go


注:本文中的strings.Replacer.Replace方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。