| 1 | /* |
| 2 | Copyright 2013 The Perkeep Authors |
| 3 | |
| 4 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | you may not use this file except in compliance with the License. |
| 6 | You may obtain a copy of the License at |
| 7 | |
| 8 | http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | Unless required by applicable law or agreed to in writing, software |
| 11 | distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | See the License for the specific language governing permissions and |
| 14 | limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package strutilscip-go gomod go4.org . `go4.org/strutil`/ |
| 18 | |
| 19 | var internStrscip-go gomod go4.org . `go4.org/strutil`/internStr. = map[string]string{} |
| 20 | |
| 21 | // RegisterCommonString adds common strings to the interned string |
| 22 | // table. This should be called during init from the main |
| 23 | // goroutine, not later at runtime. |
| 24 | func RegisterCommonStringscip-go gomod go4.org . `go4.org/strutil`/RegisterCommonString().(s ...string) { |
| 25 | for _, v := range s { |
| 26 | internStrscip-go gomod go4.org . `go4.org/strutil`/internStr.[v] = v |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | // StringFromBytes returns string(v), minimizing copies for common values of v |
| 31 | // as previously registered with RegisterCommonString. |
| 32 | func StringFromBytesscip-go gomod go4.org . `go4.org/strutil`/StringFromBytes().(v []byte) string { |
| 33 | // In Go 1.3, this string conversion in the map lookup does not allocate |
| 34 | // to make a new string. We depend on Go 1.3, so this is always free: |
| 35 | if s, ok := internStrscip-go gomod go4.org . `go4.org/strutil`/internStr.[string(v)]; ok { |
| 36 | return s |
| 37 | } |
| 38 | return string(v) |
| 39 | } |