| 1100 | } |
| 1101 | |
| 1102 | int utf8makevalid(utf8_int8_t *str, const utf8_int32_t replacement) { |
| 1103 | utf8_int8_t *read = str; |
| 1104 | utf8_int8_t *write = read; |
| 1105 | const utf8_int8_t r = (utf8_int8_t)replacement; |
| 1106 | utf8_int32_t codepoint = 0; |
| 1107 | |
| 1108 | if (replacement > 0x7f) { |
| 1109 | return -1; |
| 1110 | } |
| 1111 | |
| 1112 | while ('\0' != *read) { |
| 1113 | if (0xf0 == (0xf8 & *read)) { |
| 1114 | // ensure each of the 3 following bytes in this 4-byte |
| 1115 | // utf8 codepoint began with 0b10xxxxxx |
| 1116 | if ((0x80 != (0xc0 & read[1])) || (0x80 != (0xc0 & read[2])) || |
| 1117 | (0x80 != (0xc0 & read[3]))) { |
| 1118 | *write++ = r; |
| 1119 | read++; |
| 1120 | continue; |
| 1121 | } |
| 1122 | |
| 1123 | // 4-byte utf8 code point (began with 0b11110xxx) |
| 1124 | read = utf8codepoint(read, &codepoint); |
| 1125 | write = utf8catcodepoint(write, codepoint, 4); |
| 1126 | } else if (0xe0 == (0xf0 & *read)) { |
| 1127 | // ensure each of the 2 following bytes in this 3-byte |
| 1128 | // utf8 codepoint began with 0b10xxxxxx |
| 1129 | if ((0x80 != (0xc0 & read[1])) || (0x80 != (0xc0 & read[2]))) { |
| 1130 | *write++ = r; |
| 1131 | read++; |
| 1132 | continue; |
| 1133 | } |
| 1134 | |
| 1135 | // 3-byte utf8 code point (began with 0b1110xxxx) |
| 1136 | read = utf8codepoint(read, &codepoint); |
| 1137 | write = utf8catcodepoint(write, codepoint, 3); |
| 1138 | } else if (0xc0 == (0xe0 & *read)) { |
| 1139 | // ensure the 1 following byte in this 2-byte |
| 1140 | // utf8 codepoint began with 0b10xxxxxx |
| 1141 | if (0x80 != (0xc0 & read[1])) { |
| 1142 | *write++ = r; |
| 1143 | read++; |
| 1144 | continue; |
| 1145 | } |
| 1146 | |
| 1147 | // 2-byte utf8 code point (began with 0b110xxxxx) |
| 1148 | read = utf8codepoint(read, &codepoint); |
| 1149 | write = utf8catcodepoint(write, codepoint, 2); |
| 1150 | } else if (0x00 == (0x80 & *read)) { |
| 1151 | // 1-byte ascii (began with 0b0xxxxxxx) |
| 1152 | read = utf8codepoint(read, &codepoint); |
| 1153 | write = utf8catcodepoint(write, codepoint, 1); |
| 1154 | } else { |
| 1155 | // if we got here then we've got a dangling continuation (0b10xxxxxx) |
| 1156 | *write++ = r; |
| 1157 | read++; |
| 1158 | continue; |
| 1159 | } |
nothing calls this directly
no test coverage detected