+ /* Make sure the UTF8ness of the string and locale match */
+ if (utf8 != PL_in_utf8_COLLATE_locale) {
+ const char * const t = s; /* Temporary so we can later find where the
+ input was */
+
+ /* Here they don't match. Change the string's to be what the locale is
+ * expecting */
+
+ if (! utf8) { /* locale is UTF-8, but input isn't; upgrade the input */
+ s = (char *) bytes_to_utf8((const U8 *) s, &len);
+ utf8 = TRUE;
+ }
+ else { /* locale is not UTF-8; but input is; downgrade the input */
+
+ s = (char *) bytes_from_utf8((const U8 *) s, &len, &utf8);
+
+ /* If the downgrade was successful we are done, but if the input
+ * contains things that require UTF-8 to represent, have to do
+ * damage control ... */
+ if (UNLIKELY(utf8)) {
+
+ /* What we do is construct a non-UTF-8 string with
+ * 1) the characters representable by a single byte converted
+ * to be so (if necessary);
+ * 2) and the rest converted to collate the same as the
+ * highest collating representable character. That makes
+ * them collate at the end. This is similar to how we
+ * handle embedded NULs, but we use the highest collating
+ * code point instead of the smallest. Like the NUL case,
+ * this isn't perfect, but is the best we can reasonably
+ * do. Every above-255 code point will sort the same as
+ * the highest-sorting 0-255 code point. If that code
+ * point can combine in a sequence with some other code
+ * points for weight calculations, us changing something to
+ * be it can adversely affect the results. But in most
+ * cases, it should work reasonably. And note that this is
+ * really an illegal situation: using code points above 255
+ * on a locale where only 0-255 are valid. If two strings
+ * sort entirely equal, then the sort order for the
+ * above-255 code points will be in code point order. */
+
+ utf8 = FALSE;
+
+ /* If we haven't calculated the code point with the maximum
+ * collating order for this locale, do so now */
+ if (! PL_strxfrm_max_cp) {
+ int j;
+
+ /* The current transformed string that collates the
+ * highest (except it also includes the prefixed collation
+ * index. */
+ char * cur_max_x = NULL;
+
+ /* Look through all legal code points (NUL isn't) */
+ for (j = 1; j < 256; j++) {
+ char * x;
+ STRLEN x_len;
+
+ /* Create a 1-char string of the current code point. */
+ char cur_source[] = { (char) j, '\0' };
+
+ /* Then transform it */
+ x = _mem_collxfrm(cur_source, 1, &x_len, FALSE);
+
+ /* If something went wrong (which it shouldn't), just
+ * ignore this code point */
+ if (! x) {
+ continue;
+ }
+
+ /* If this character's transformation is higher than
+ * the current highest, this one becomes the highest */
+ if ( cur_max_x == NULL
+ || strGT(x + COLLXFRM_HDR_LEN,
+ cur_max_x + COLLXFRM_HDR_LEN))
+ {
+ PL_strxfrm_max_cp = j;
+ cur_max_x = x;
+ }
+ else {
+ Safefree(x);
+ }
+ }
+
+ if (! cur_max_x) {
+ DEBUG_L(PerlIO_printf(Perl_debug_log,
+ "_mem_collxfrm: Couldn't find any character to"
+ " replace above-Latin1 chars in locale %s with",
+ PL_collation_name));
+ goto bad;
+ }
+
+ DEBUG_L(PerlIO_printf(Perl_debug_log,
+ "_mem_collxfrm: highest 1-byte collating character"
+ " in locale %s is 0x%02X\n",
+ PL_collation_name,
+ PL_strxfrm_max_cp));
+
+ Safefree(cur_max_x);
+ }
+
+ /* Here we know which legal code point collates the highest.
+ * We are ready to construct the non-UTF-8 string. The length
+ * will be at least 1 byte smaller than the input string
+ * (because we changed at least one 2-byte character into a
+ * single byte), but that is eaten up by the trailing NUL */
+ Newx(s, len, char);
+
+ {
+ STRLEN i;
+ STRLEN d= 0;
+
+ for (i = 0; i < len; i+= UTF8SKIP(t + i)) {
+ U8 cur_char = t[i];
+ if (UTF8_IS_INVARIANT(cur_char)) {
+ s[d++] = cur_char;
+ }
+ else if (UTF8_IS_DOWNGRADEABLE_START(cur_char)) {
+ s[d++] = EIGHT_BIT_UTF8_TO_NATIVE(cur_char, t[i+1]);
+ }
+ else { /* Replace illegal cp with highest collating
+ one */
+ s[d++] = PL_strxfrm_max_cp;
+ }
+ }
+ s[d++] = '\0';
+ Renew(s, d, char); /* Free up unused space */
+ }
+ }
+ }
+
+ /* Here, we have constructed a modified version of the input. It could
+ * be that we already had a modified copy before we did this version.
+ * If so, that copy is no longer needed */
+ if (t != input_string) {
+ Safefree(t);
+ }
+ }
+
+ length_in_chars = (utf8)
+ ? utf8_length((U8 *) s, (U8 *) s + len)
+ : len;
+