| 27 | long utf16_corpus_length = 0; |
| 28 | |
| 29 | uint16_t *randomString(int len) { |
| 30 | if (!utf16_corpus) { |
| 31 | FILE *handle = fopen("utf16_corpus.txt", "rb"); |
| 32 | fseek(handle, 0, SEEK_END); |
| 33 | utf16_corpus_length = ftell(handle)/2; |
| 34 | assert(utf16_corpus_length > 0); |
| 35 | utf16_corpus = malloc(sizeof(uint16_t) * (utf16_corpus_length+1)); |
| 36 | fseek(handle, 0, SEEK_SET); |
| 37 | fread(utf16_corpus, 2, utf16_corpus_length, handle); |
| 38 | fclose(handle); |
| 39 | utf16_corpus[utf16_corpus_length] = 0; |
| 40 | } |
| 41 | int startIdx = rand() % (utf16_corpus_length - len); |
| 42 | while((utf16_corpus[startIdx] & 0xFF00) == 0xDC00) { |
| 43 | ++startIdx; |
| 44 | if (startIdx + len > utf16_corpus_length) len = utf16_corpus_length - startIdx; |
| 45 | } |
| 46 | assert(len > 0); |
| 47 | uint16_t *s = malloc(sizeof(uint16_t) * (len+1)); |
| 48 | memcpy(s, utf16_corpus + startIdx, len*2); |
| 49 | s[len] = 0; |
| 50 | while(((uint16_t)s[len-1] & 0xFF00) == 0xD800) { s[--len] = 0; } |
| 51 | assert(len >= 0); |
| 52 | return s; |
| 53 | } |
| 54 | |
| 55 | int main() { |
| 56 | double t = 0; |