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