MCPcopy Create free account
hub / github.com/Moddable-OpenSource/moddable / fxCacheUTF8ToUnicodeOffset

Function fxCacheUTF8ToUnicodeOffset

xs/sources/xsString.c:140–197  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

138}
139
140txSize 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}

Callers 4

fxAdvanceStringIndexFunction · 0.85
fx_RegExp_prototype_execFunction · 0.85

Calls 2

fxCacheStringInfoFunction · 0.85
fxUTF8ToUnicodeOffsetFunction · 0.85

Tested by

no test coverage detected