1 /**
2  * This file is part of DCD, a development tool for the D programming language.
3  * Copyright (C) 2014 Brian Schott
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 module dsymbol.string_interning;
20 
21 import dparse.lexer;
22 
23 /**
24  * Interns the given string and returns the interned version.
25  */
26 istring internString(string s) nothrow @safe @nogc
27 {
28 	return istring(stringCache.intern(s));
29 }
30 
31 static this()
32 {
33 	stringCache = StringCache(StringCache.defaultBucketCount);
34 }
35 
36 alias istring = InternedString;
37 
38 private StringCache stringCache = void;
39 
40 private struct InternedString
41 {
42 	void opAssign(T)(T other) if (is(Unqual!T == istring))
43 	{
44 		this.data = other.data;
45 	}
46 
47 	size_t toHash() const nothrow @safe
48 	{
49 		return typeid(string).getHash(&data);
50 	}
51 
52 	string data;
53 	alias data this;
54 private:
55 	import std.traits : Unqual;
56 }