Shinichiro Hamaji | b69bf8a | 2015-06-10 14:52:06 +0900 | [diff] [blame] | 1 | // Copyright 2015 Google Inc. All rights reserved |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
Fumitoshi Ukai | 744bb2b | 2015-06-25 00:10:52 +0900 | [diff] [blame] | 15 | package kati |
Fumitoshi Ukai | 0b9e813 | 2015-04-30 10:20:18 +0900 | [diff] [blame] | 16 | |
Fumitoshi Ukai | 9042b99 | 2015-06-23 16:10:27 +0900 | [diff] [blame] | 17 | import "sync" |
| 18 | |
| 19 | type symtabT struct { |
| 20 | mu sync.Mutex |
| 21 | m map[string]string |
| 22 | } |
| 23 | |
| 24 | var symtab = &symtabT{ |
| 25 | m: make(map[string]string), |
| 26 | } |
Fumitoshi Ukai | 0b9e813 | 2015-04-30 10:20:18 +0900 | [diff] [blame] | 27 | |
| 28 | func intern(s string) string { |
Fumitoshi Ukai | 9042b99 | 2015-06-23 16:10:27 +0900 | [diff] [blame] | 29 | symtab.mu.Lock() |
| 30 | v, ok := symtab.m[s] |
| 31 | if ok { |
| 32 | symtab.mu.Unlock() |
Fumitoshi Ukai | 0b9e813 | 2015-04-30 10:20:18 +0900 | [diff] [blame] | 33 | return v |
| 34 | } |
Fumitoshi Ukai | 9042b99 | 2015-06-23 16:10:27 +0900 | [diff] [blame] | 35 | symtab.m[s] = s |
| 36 | symtab.mu.Unlock() |
Fumitoshi Ukai | 0b9e813 | 2015-04-30 10:20:18 +0900 | [diff] [blame] | 37 | return s |
| 38 | } |
| 39 | |
| 40 | func internBytes(s []byte) string { |
Shinichiro Hamaji | 4ed0634 | 2015-05-14 15:52:24 +0900 | [diff] [blame] | 41 | return intern(string(s)) |
Fumitoshi Ukai | 0b9e813 | 2015-04-30 10:20:18 +0900 | [diff] [blame] | 42 | } |