Overpass API regression suite coverage (Saxony) / overpass_api/frontend/hash_request.cc

overpass_api/frontend/hash_request.cc

83 of 84 instrumented lines run by the suite (98.8%) at 8edf11b0419c (this file on GitHub).

runnever runcolumns: line, executions, queries (click for the queries that run the line)

1/** Copyright 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018 Roland Olbricht et al.
2 *
3 * This file is part of Overpass_API.
4 *
5 * Overpass_API is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Affero General Public License as
7 * published by the Free Software Foundation, either version 3 of the
8 * License, or (at your option) any later version.
9 *
10 * Overpass_API 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 Affero General Public License
16 * along with Overpass_API. If not, see <http://www.gnu.org/licenses/>.
17 */
19#include "hash_request.h"
22std::string sanitize_string(const std::string& input, bool anonymize)
231346673 q{
241346673 q std::string result = input;
251346673 q std::string::size_type src_pos = 0;
261346673 q std::string::size_type target_pos = 0;
27330008673 q while (src_pos < input.size())
28328662673 q {
29328662673 q if (isspace(input[src_pos]))
3036359673 q {
3136359673 q ++src_pos;
3236989673 q while (src_pos < input.size() && isspace(input[src_pos]))
33630124 q ++src_pos;
3436359673 q if (target_pos > 0 && src_pos < input.size()
3536359673 q && (isalpha(result[target_pos-1]) || isdigit(result[target_pos-1]) || result[target_pos-1] == '_')
3636359673 q && (isalpha(input[src_pos]) || isdigit(input[src_pos]) || input[src_pos] == '_' || input[src_pos] == '-'))
3722621650 q result[target_pos++] = ' ';
3836359673 q }
39292303673 q else if (anonymize && (isdigit(input[src_pos])
40119516673 q || (input[src_pos] == '-' && src_pos+1 < input.size() && isdigit(input[src_pos+1]))))
4113060660 q {
4213060660 q result[target_pos++] = '0';
4313060660 q ++src_pos;
4436046660 q while (src_pos < input.size() && (isdigit(input[src_pos]) || input[src_pos] == '.'))
4522986658 q ++src_pos;
4613060660 q }
47279243673 q else if (anonymize && input[src_pos] == '\"' && target_pos >= 5 && result.substr(target_pos-5,5) == "poly:")
482020 q {
492020 q ++src_pos;
501660020 q while (src_pos < input.size() && input[src_pos] != '\"')
511658020 q ++src_pos;
522020 q result[target_pos++] = '\"';
532020 q result[target_pos++] = '\"';
542020 q ++src_pos;
552020 q }
56279223673 q else if (anonymize && input[src_pos] == '\'' && target_pos >= 5 && result.substr(target_pos-5,5) == "poly:")
572525 q {
582525 q ++src_pos;
591700025 q while (src_pos < input.size() && input[src_pos] != '\'')
601697525 q ++src_pos;
612525 q result[target_pos++] = '\'';
622525 q result[target_pos++] = '\'';
632525 q ++src_pos;
642525 q }
65279198673 q else if (input[src_pos] == '/' && src_pos+1 < input.size())
6616636 q {
6716636 q if (input[src_pos+1] == '/')
68147 q {
69147 q src_pos += 2;
707267 q while (src_pos < input.size() && input[src_pos] != '\n')
717127 q ++src_pos;
72147 q }
7315229 q else if (input[src_pos+1] == '*')
7521 q src_pos += 2;
76981 q while (src_pos < input.size() && (input[src_pos] != '/' || input[src_pos-1] != '*'))
77961 q ++src_pos;
7821 q if (src_pos < input.size())
7921 q ++src_pos;
8115028 q else
8215028 q result[target_pos++] = input[src_pos++];
8316636 q }
84279032673 q else if ((input[src_pos] & 0xe0) == 0)
850 ++src_pos;
86279032673 q else
87279032673 q result[target_pos++] = input[src_pos++];
88328662673 q }
891346673 q result.resize(target_pos);
901346673 q return result;
911346673 q}
94uint64_t hash(const std::string& input)
951346673 q{
961346673 q uint64_t result = (input.size()<<32) ^ (input.size()<<48);
971346673 q int pos = 0;
98316299673 q for (decltype(input.size()) i = 0; i < input.size(); ++i)
99314953673 q {
100314953673 q int spread0 = ((5*i)%17)+8;
101314953673 q int spread1 = ((5*i)%23)+33;
102314953673 q uint64_t val = (unsigned char)input[i];
103314953673 q val = val ^ (val<<spread0);
104314953673 q val = val ^ (val<<spread1);
105314953673 q result ^= ((val << pos)|(val >> (64-pos)));
106314953673 q pos = (pos + 19) & 63;
107314953673 q }
1081346673 q return result;