-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
82 lines (72 loc) · 2.53 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <cstdio>
#include <string>
#include <windows.h>
#include "utf8.h"
bool LoadFile(const std::string &path, std::string *contents_ptr){
FILE *file;
file = fopen(path.c_str(), "rb");
if(!file){
printf("Error! Could not load file: %s\n", path.c_str());
return false;
}
// Determine the size of the file
fseek(file, 0, SEEK_END);
int len = ftell(file);
fseek(file, 0, SEEK_SET);
// Read the entire file
std::string& contents = *contents_ptr;
contents.resize(len);
int c = fread(&contents[0], len, 1, file);
fclose(file);
return true;
}
int main(int argc, char* argv[]){
std::string str_ascii;
if(!LoadFile("alice.txt",&str_ascii)){
getchar();
return 1;
}
printf("ASCII file loaded: %d bytes\n",str_ascii.length()*sizeof(str_ascii[0]));
std::string str_utf8 = str_ascii;
printf("UTF8 file loaded: %d bytes\n",str_utf8.length()*sizeof(str_utf8[0]));
std::basic_string<int> str_utf32;
int str_len = str_ascii.length();
str_utf32.resize(str_len);
for(unsigned i=0; i<str_len; ++i){
str_utf32[i] = str_ascii[i];
}
printf("UTF-32 file loaded: %d bytes\n",str_utf32.length()*sizeof(str_utf32[0]));
LARGE_INTEGER time_start, time_end;
QueryPerformanceCounter(&time_start);
for(int i=0; i<str_len; ++i){
if(str_ascii[i] == '.'){
str_ascii[i] = ',';
}
}
QueryPerformanceCounter(&time_end);
printf("Find/replace from '.' to ',' took %lld microseconds using ASCII\n", time_end.QuadPart - time_start.QuadPart);
QueryPerformanceCounter(&time_start);
for(int i=0; i<str_len; ++i){
if(str_utf32[i] == '.'){
str_utf32[i] = ',';
}
}
QueryPerformanceCounter(&time_end);
printf("Find/replace from '.' to ',' took %lld microseconds using UTF-32\n", time_end.QuadPart - time_start.QuadPart);
QueryPerformanceCounter(&time_start);
std::string processed_str_utf8;
std::string::iterator iter = str_utf8.begin();
int code_point;
while(iter != str_utf8.end()){
code_point = utf8::next(iter, str_utf8.end());
if(code_point == '.'){
utf8::append(',', std::back_inserter(processed_str_utf8));
} else {
utf8::append(code_point, std::back_inserter(processed_str_utf8));
}
}
QueryPerformanceCounter(&time_end);
printf("Find/replace from '.' to ',' took %lld microseconds using UTF-8\n", time_end.QuadPart - time_start.QuadPart);
getchar();
return 0;
}