Function to perform casefolding replacement
| 38 | |
| 39 | // Function to perform casefolding replacement |
| 40 | static std::string casefold(const std::string& input) { |
| 41 | std::string output; |
| 42 | size_t i = 0; |
| 43 | |
| 44 | while (i < input.size()) { |
| 45 | uint8_t current_byte = static_cast<uint8_t>(input[i]); |
| 46 | bool replaced = false; |
| 47 | |
| 48 | if( current_byte >= 'A' && current_byte <= 'Z' ) { |
| 49 | // Casefold uppercase ASCII characters |
| 50 | output.push_back( static_cast<char>(current_byte + 32) ); // Convert to lowercase |
| 51 | i++; |
| 52 | continue; |
| 53 | } |
| 54 | // Check if the current byte is in the _casefold_table |
| 55 | auto it = _casefold_table.find(current_byte); |
| 56 | if (it != _casefold_table.end()) { |
| 57 | const auto& replacements = it->second; |
| 58 | |
| 59 | // Check if any of the stored sequences match |
| 60 | for (const auto& [from, to] : replacements) { |
| 61 | if (input.compare(i, from.size(), from) == 0) { // Directly compare substring |
| 62 | output.append(to); |
| 63 | i += from.size(); // Move past the matched sequence |
| 64 | replaced = true; |
| 65 | break; |
| 66 | } |
| 67 | } |
| 68 | } |
| 69 | // If no replacement occurred, just copy the current byte |
| 70 | if (!replaced) { |
| 71 | output.push_back(input[i]); |
| 72 | i++; |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | return output; |
| 77 | } |
| 78 | |
| 79 | |
| 80 | // Not working – do we really need it!? Is it faster than casefolding and then comparing!? |