| 138 | } |
| 139 | |
| 140 | txSize fxCacheUTF8ToUnicodeOffset(txMachine* the, txString string, txSize offset) |
| 141 | { |
| 142 | txStringInfo* info; |
| 143 | if (offset <= 0) |
| 144 | return offset ? -1 : 0; |
| 145 | info = fxCacheStringInfo(the, string); |
| 146 | if (info) { |
| 147 | txSize delta, sign, unicodeOffset, utf8Offset; |
| 148 | txU1* p; |
| 149 | if (/* (offset < 0) || */ (info->utf8Length < offset)) |
| 150 | return -1; |
| 151 | if ((info->ascii) /* || (offset == 0) */) |
| 152 | return offset; |
| 153 | if (offset == info->utf8Length) |
| 154 | return info->unicodeLength; |
| 155 | delta = offset - info->utf8Offset; |
| 156 | if (delta == 0) |
| 157 | return info->unicodeOffset; |
| 158 | if (delta > 0) { |
| 159 | if (delta < info->utf8Length - offset) { |
| 160 | sign = 1; |
| 161 | unicodeOffset = info->unicodeOffset + 1; |
| 162 | utf8Offset = info->utf8Offset + 1; |
| 163 | } |
| 164 | else { |
| 165 | sign = -1; |
| 166 | unicodeOffset = info->unicodeLength - 1; |
| 167 | utf8Offset = info->utf8Length - 1; |
| 168 | } |
| 169 | } |
| 170 | else { |
| 171 | if (0 - delta < offset) { |
| 172 | sign = -1; |
| 173 | unicodeOffset = info->unicodeOffset - 1; |
| 174 | utf8Offset = info->utf8Offset - 1; |
| 175 | } |
| 176 | else { |
| 177 | sign = 1; |
| 178 | unicodeOffset = 1; |
| 179 | utf8Offset = 1; |
| 180 | } |
| 181 | } |
| 182 | p = (txU1*)string + utf8Offset; |
| 183 | for (;;) { |
| 184 | if ((*p & 0xC0) != 0x80) { |
| 185 | if (utf8Offset == offset) |
| 186 | break; |
| 187 | unicodeOffset += sign; |
| 188 | } |
| 189 | p += sign; |
| 190 | utf8Offset += sign; |
| 191 | } |
| 192 | info->utf8Offset = utf8Offset; |
| 193 | info->unicodeOffset = unicodeOffset; |
| 194 | return unicodeOffset; |
| 195 | } |
| 196 | return fxUTF8ToUnicodeOffset(string, offset); |
| 197 | } |
no test coverage detected