MCPcopy Create free account
hub / github.com/emscripten-core/emscripten / randomString

Function randomString

test/test_utf8_bench.c:29–54  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

27long utf8_corpus_length = 0;
28
29char *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
56int main() {
57 double t = 0;

Callers 1

mainFunction · 0.70

Calls 9

fopenFunction · 0.85
fseekFunction · 0.85
ftellFunction · 0.85
freadFunction · 0.85
fcloseFunction · 0.85
randFunction · 0.85
memcpyFunction · 0.85
assertFunction · 0.50
mallocFunction · 0.50

Tested by

no test coverage detected