2016-05-02 11:26:32 +08:00
|
|
|
// Library for pooling common strings.
|
2016-05-19 06:30:21 +08:00
|
|
|
#include "config.h" // IWYU pragma: keep
|
|
|
|
|
2016-05-02 11:26:32 +08:00
|
|
|
#include <stddef.h>
|
|
|
|
#include <wchar.h>
|
2017-02-14 12:37:27 +08:00
|
|
|
|
2012-03-04 07:20:30 +08:00
|
|
|
#include <algorithm>
|
2016-04-21 14:00:54 +08:00
|
|
|
#include <memory>
|
2016-05-02 11:26:32 +08:00
|
|
|
#include <vector>
|
2005-09-20 21:26:39 +08:00
|
|
|
|
|
|
|
#include "common.h"
|
2016-05-02 11:26:32 +08:00
|
|
|
#include "fallback.h" // IWYU pragma: keep
|
2005-09-20 21:26:39 +08:00
|
|
|
#include "intern.h"
|
|
|
|
|
2017-02-08 13:52:35 +08:00
|
|
|
bool string_less_than_string(const wchar_t *a, const wchar_t *b) { return wcscmp(a, b) < 0; }
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2016-05-02 11:26:32 +08:00
|
|
|
/// The table of intern'd strings.
|
2017-01-30 13:19:39 +08:00
|
|
|
owning_lock<std::vector<const wchar_t *>> string_table;
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2016-05-02 11:26:32 +08:00
|
|
|
static const wchar_t *intern_with_dup(const wchar_t *in, bool dup) {
|
|
|
|
if (!in) return NULL;
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-05-29 13:28:26 +08:00
|
|
|
debug(5, L"intern %ls", in);
|
2017-01-30 13:19:39 +08:00
|
|
|
auto lock_string_table = string_table.acquire();
|
|
|
|
std::vector<const wchar_t *> &string_table = lock_string_table.value;
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2017-01-30 13:19:39 +08:00
|
|
|
const wchar_t *result;
|
2017-02-08 13:52:35 +08:00
|
|
|
auto iter =
|
|
|
|
std::lower_bound(string_table.begin(), string_table.end(), in, string_less_than_string);
|
2016-05-02 11:26:32 +08:00
|
|
|
if (iter != string_table.end() && wcscmp(*iter, in) == 0) {
|
2012-03-04 07:20:30 +08:00
|
|
|
result = *iter;
|
2016-05-02 11:26:32 +08:00
|
|
|
} else {
|
2012-03-04 07:20:30 +08:00
|
|
|
result = dup ? wcsdup(in) : in;
|
|
|
|
string_table.insert(iter, result);
|
|
|
|
}
|
2012-01-24 12:02:15 +08:00
|
|
|
return result;
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
|
2016-05-02 11:26:32 +08:00
|
|
|
const wchar_t *intern(const wchar_t *in) { return intern_with_dup(in, true); }
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2016-05-02 11:26:32 +08:00
|
|
|
const wchar_t *intern_static(const wchar_t *in) { return intern_with_dup(in, false); }
|