Returns the soundex code for a given string The soundex function evaluates expression and returns the most significant letter in the input string followed by a phonetic code. Characters that are not alphabetic are ignored. If expression evaluates to the null value, null is returned. The soundex algorithm works with the following steps: 1. Retain the first letter of the string and drop all other
| 3100 | // numbers, append with zeros until there are three numbers. If you have four or more |
| 3101 | // numbers, retain only the first three. |
| 3102 | FORCE_INLINE |
| 3103 | const char* soundex_utf8(gdv_int64 context, const char* in, gdv_int32 in_len, |
| 3104 | bool in_validity, bool* out_valid, int32_t* out_len) { |
| 3105 | if (in_len <= 0) { |
| 3106 | *out_valid = true; |
| 3107 | *out_len = 0; |
| 3108 | return ""; |
| 3109 | } |
| 3110 | |
| 3111 | // The soundex code is composed by one letter and three numbers |
| 3112 | char* soundex = reinterpret_cast<char*>(gdv_fn_context_arena_malloc(context, in_len)); |
| 3113 | char* ret = reinterpret_cast<char*>(gdv_fn_context_arena_malloc(context, 4)); |
| 3114 | |
| 3115 | if (soundex == nullptr || ret == nullptr) { |
| 3116 | gdv_fn_context_set_error_msg(context, "Could not allocate memory for output string"); |
| 3117 | *out_valid = false; |
| 3118 | *out_len = 0; |
| 3119 | return ""; |
| 3120 | } |
| 3121 | |
| 3122 | int si = 1; |
| 3123 | int ret_len = 1; |
| 3124 | unsigned char c; |
| 3125 | |
| 3126 | int start_idx = 0; |
| 3127 | for (int i = 0; i < in_len; ++i) { |
| 3128 | if (isalpha(static_cast<unsigned char>(in[i])) > 0) { |
| 3129 | // Retain the first letter |
| 3130 | ret[0] = toupper(static_cast<unsigned char>(in[i])); |
| 3131 | start_idx = i + 1; |
| 3132 | break; |
| 3133 | } |
| 3134 | } |
| 3135 | |
| 3136 | // If ret[0] is not initialised, return validity false |
| 3137 | if (start_idx == 0) { |
| 3138 | *out_valid = false; |
| 3139 | *out_len = 0; |
| 3140 | return ""; |
| 3141 | } |
| 3142 | |
| 3143 | soundex[0] = '\0'; |
| 3144 | // Replace consonants with digits and special letters with 0 |
| 3145 | for (int i = start_idx; i < in_len; i++) { |
| 3146 | if (isalpha(static_cast<unsigned char>(in[i])) > 0) { |
| 3147 | c = toupper(static_cast<unsigned char>(in[i])) - 65; |
| 3148 | if (mappings[c] != soundex[si - 1]) { |
| 3149 | soundex[si] = mappings[c]; |
| 3150 | si++; |
| 3151 | } |
| 3152 | } |
| 3153 | } |
| 3154 | |
| 3155 | int i = 1; |
| 3156 | // If the saved letter's digit is the same as the resulting first digit, skip it |
| 3157 | if (si > 1) { |
| 3158 | if (soundex[1] == mappings[ret[0] - 65]) { |
| 3159 | i = 2; |