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