Normalizes `src` with `chars_map` and returns normalized Chars. `max_len` specifies the maximum length of the key in `chars_map`.
| 122 | // Normalizes `src` with `chars_map` and returns normalized Chars. |
| 123 | // `max_len` specifies the maximum length of the key in `chars_map`. |
| 124 | Builder::Chars Normalize(const Builder::CharsMap &chars_map, |
| 125 | const Builder::Chars &src, int max_len) { |
| 126 | CHECK_GE(max_len, 1); |
| 127 | Builder::Chars normalized; |
| 128 | |
| 129 | for (size_t i = 0; i < src.size();) { |
| 130 | Builder::CharsMap::const_iterator it = chars_map.end(); |
| 131 | const size_t slice = std::min<size_t>(i + max_len, src.size()); |
| 132 | // starts with the longest prefix. |
| 133 | Builder::Chars key(src.begin() + i, src.begin() + slice); |
| 134 | while (!key.empty()) { |
| 135 | it = chars_map.find(key); |
| 136 | if (it != chars_map.end()) { |
| 137 | break; |
| 138 | } |
| 139 | key.pop_back(); // remove the last character. |
| 140 | } |
| 141 | |
| 142 | // Consumes one character when no rule is found. |
| 143 | if (it == chars_map.end()) { |
| 144 | normalized.push_back(src[i]); |
| 145 | ++i; |
| 146 | } else { |
| 147 | std::copy(it->second.begin(), it->second.end(), |
| 148 | std::back_inserter(normalized)); |
| 149 | i += it->first.size(); |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | return normalized; |
| 154 | } |
| 155 | |
| 156 | bool IsValidNormalizerData(absl::string_view blob_data) { |
| 157 | NormalizerSpec spec; |