1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// Copyright (c) 2015 Baidu, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Author: Ge,Jun (gejun@baidu.com)
// Date: Tue Jun 23 15:03:24 CST 2015
#ifndef BUTIL_FIND_CSTR_H
#define BUTIL_FIND_CSTR_H
#include <string>
#include <map>
#include <algorithm>
#include "butil/thread_local.h"
// Find c-string in maps with std::string as keys without memory allocations.
// Example:
// std::map<std::string, int> string_map;
//
// string_map.find("hello"); // constructed a temporary std::string
// // which needs memory allocation.
//
// find_cstr(string_map, "hello"); // no allocation.
//
// You can specialize find_cstr for other maps.
// Possible prototypes are:
// const_iterator find_cstr(const Map& map, const char* key);
// iterator find_cstr(Map& map, const char* key);
// const_iterator find_cstr(const Map& map, const char* key, size_t length);
// iterator find_cstr(Map& map, const char* key, size_t length);
namespace butil {
struct StringMapThreadLocalTemp {
bool initialized;
char buf[sizeof(std::string)];
static void delete_tls(void* buf) {
StringMapThreadLocalTemp* temp = (StringMapThreadLocalTemp*)buf;
if (temp->initialized) {
temp->initialized = false;
std::string* temp_string = (std::string*)temp->buf;
temp_string->~basic_string();
}
}
inline std::string* get_string(const char* key) {
if (!initialized) {
initialized = true;
std::string* tmp = new (buf) std::string(key);
thread_atexit(delete_tls, this);
return tmp;
} else {
std::string* tmp = (std::string*)buf;
tmp->assign(key);
return tmp;
}
}
inline std::string* get_string(const char* key, size_t length) {
if (!initialized) {
initialized = true;
std::string* tmp = new (buf) std::string(key, length);
thread_atexit(delete_tls, this);
return tmp;
} else {
std::string* tmp = (std::string*)buf;
tmp->assign(key, length);
return tmp;
}
}
inline std::string* get_lowered_string(const char* key) {
std::string* tmp = get_string(key);
std::transform(tmp->begin(), tmp->end(), tmp->begin(), ::tolower);
return tmp;
}
inline std::string* get_lowered_string(const char* key, size_t length) {
std::string* tmp = get_string(key, length);
std::transform(tmp->begin(), tmp->end(), tmp->begin(), ::tolower);
return tmp;
}
};
extern __thread StringMapThreadLocalTemp tls_stringmap_temp;
template <typename T, typename C, typename A>
typename std::map<std::string, T, C, A>::const_iterator
find_cstr(const std::map<std::string, T, C, A>& m, const char* key) {
return m.find(*tls_stringmap_temp.get_string(key));
}
template <typename T, typename C, typename A>
typename std::map<std::string, T, C, A>::iterator
find_cstr(std::map<std::string, T, C, A>& m, const char* key) {
return m.find(*tls_stringmap_temp.get_string(key));
}
template <typename T, typename C, typename A>
typename std::map<std::string, T, C, A>::const_iterator
find_cstr(const std::map<std::string, T, C, A>& m,
const char* key, size_t length) {
return m.find(*tls_stringmap_temp.get_string(key, length));
}
template <typename T, typename C, typename A>
typename std::map<std::string, T, C, A>::iterator
find_cstr(std::map<std::string, T, C, A>& m,
const char* key, size_t length) {
return m.find(*tls_stringmap_temp.get_string(key, length));
}
template <typename T, typename C, typename A>
typename std::map<std::string, T, C, A>::const_iterator
find_lowered_cstr(const std::map<std::string, T, C, A>& m, const char* key) {
return m.find(*tls_stringmap_temp.get_lowered_string(key));
}
template <typename T, typename C, typename A>
typename std::map<std::string, T, C, A>::iterator
find_lowered_cstr(std::map<std::string, T, C, A>& m, const char* key) {
return m.find(*tls_stringmap_temp.get_lowered_string(key));
}
template <typename T, typename C, typename A>
typename std::map<std::string, T, C, A>::const_iterator
find_lowered_cstr(const std::map<std::string, T, C, A>& m,
const char* key, size_t length) {
return m.find(*tls_stringmap_temp.get_lowered_string(key, length));
}
template <typename T, typename C, typename A>
typename std::map<std::string, T, C, A>::iterator
find_lowered_cstr(std::map<std::string, T, C, A>& m,
const char* key, size_t length) {
return m.find(*tls_stringmap_temp.get_lowered_string(key, length));
}
} // namespace butil
#endif // BUTIL_FIND_CSTR_H