MCPcopy Create free account
hub / github.com/apache/impala / ParseNameTokenHelper

Function ParseNameTokenHelper

be/src/runtime/datetime-parser-common.cc:219–265  ·  view source on GitHub ↗

Helper function used by ParseMonthNameToken() and ParseDayNameToken(). Parses 'token_start' string and returns true if it finds a valid short or normal name. The valid name prefixes are stored as keys in 'prefix_to_suffix' map. The valid name prefixes are expected to be equal to the corresponding valid short names. The valid name suffixes and the corresponding int IDs are stored as values in 'pref

Source from the content-addressed store, hash-verified

217// name is found using 'prefix_to_suffix'. If parsing is successful, '*token_end' is
218// adjusted to point to the character right after the end of the name.
219bool ParseNameTokenHelper(
220 bool is_short_name, int short_name_len, int max_name_len,
221 const char* token_start, const char** token_end,
222 bool is_strict_matching,
223 const unordered_map<string, pair<string, int>>& prefix_to_suffix,
224 int* ret_val) {
225 DCHECK(token_start != nullptr);
226 DCHECK(ret_val != nullptr);
227 DCHECK(token_end != nullptr && *token_end != nullptr);
228 DCHECK(token_start <= *token_end);
229 int token_len = *token_end - token_start;
230 if (token_len < short_name_len) return false;
231
232 string buff(token_start, token_len);
233 boost::to_lower(buff);
234 const string& prefix = buff.substr(0, short_name_len);
235 const auto it = prefix_to_suffix.find(prefix);
236 if (UNLIKELY(it == prefix_to_suffix.end())) return false;
237 if (is_short_name) {
238 DCHECK(token_len == short_name_len);
239 *ret_val = it->second.second;
240 return true;
241 }
242
243 if (is_strict_matching) {
244 DCHECK(buff.length() == max_name_len);
245 trim_right_if(buff, is_any_of(" "));
246 }
247
248 // Check if the remaining characters match.
249 const string& expected_suffix = it->second.first;
250 if (buff.length() - short_name_len < expected_suffix.length()) return false;
251 const char* actual_suffix = buff.c_str() + short_name_len;
252 if (strncmp(actual_suffix, expected_suffix.c_str(), expected_suffix.length()) != 0) {
253 return false;
254 }
255 *ret_val = it->second.second;
256
257 // If the end of the name token wasn't identified because it wasn't followed by a
258 // separator then the end of the name token has to be adjusted.
259 if (prefix.length() + expected_suffix.length() < buff.length()) {
260 if (is_strict_matching) return false;
261 *token_end = token_start + prefix.length() + expected_suffix.length();
262 }
263
264 return true;
265}
266
267}
268

Callers 2

ParseMonthNameTokenFunction · 0.85
ParseDayNameTokenFunction · 0.85

Calls 4

substrMethod · 0.80
findMethod · 0.45
endMethod · 0.45
lengthMethod · 0.45

Tested by

no test coverage detected