github.com/shogo82148/std@v1.22.1-0.20240327122250-4e474527810c/strings/replace.go (about) 1 // Copyright 2011 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package strings 6 7 import ( 8 "github.com/shogo82148/std/io" 9 "github.com/shogo82148/std/sync" 10 ) 11 12 // Replacerは、置換物のリストで文字列を置換します。 13 // 複数のゴルーチンによる同時使用に対して安全です。 14 type Replacer struct { 15 once sync.Once 16 r replacer 17 oldnew []string 18 } 19 20 // NewReplacerは、古い文字列と新しい文字列のペアのリストから新しい [Replacer] を返します。 21 // 置換は、対象文字列に現れる順序で実行され、重複するマッチングは行われません。 22 // 古い文字列の比較は引数の順序で行われます。 23 // 24 // NewReplacerは、奇数の引数が与えられた場合にパニックを引き起こします。 25 func NewReplacer(oldnew ...string) *Replacer 26 27 // Replaceは、すべての置換を実行したsのコピーを返します。 28 func (r *Replacer) Replace(s string) string 29 30 // WriteStringは、すべての置換を実行したsをwに書き込みます。 31 func (r *Replacer) WriteString(w io.Writer, s string) (n int, err error)