What is this?

I just found this source in the office. I probably copied it from someone's post here to test it. Can anyone figure out what it does?
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
#include <iostream>
#include <string>
#include <ctime>
#include <cstring>

#if 1
typedef char char_t;
#else
typedef wchar_t char_t;
#endif

typedef std::basic_string<char_t> str_t;

const size_t size = 1 << 21;

std::string replace(const std::string &s, const char_t *what, const char_t *with){
	std::string ret;
	ret.reserve(s.capacity());
	size_t len1 = strlen(what);
	size_t len2 = strlen(with);
	ret.resize(ret.size() * len2 / len1);
}

int main(){
	clock_t t0 = clock();
	str_t string;
	size_t size2 = size * 8 / 7;
	size2 = size2 + size2 / 90 * 2;
	string.reserve(size2);
	const unsigned N = 1000;
	for (int n = 0; n < N; n++){
		string.resize(size, '-');
		char_t *p = &string[0];
		size_t length = string.size();
		for (size_t i = 6; i < length; i += 7)
			p[i] = 'P';
		length = length * 8 / 7;
		string.resize(length);
		p = &string[0];
		for (size_t i = length; --i;)
			p[i] = p[i - i / 8];
		for (size_t i = 7; i < length; i += 8)
			p[i] = 'U';
		
		length += length / 90 * 2;
		string.resize(length);
		p = &string[0];
		for (size_t i = length; --i;)
			p[i] = p[i - i / 92 * 2];
		
		for (size_t i = 90; i < length; i += 92)
			p[i] = 13;
		for (size_t i = 91; i < length; i += 92)
			p[i] = 10;
	}
	clock_t t1 = clock();
	std::cout <<double(t1 - t0) / (double)CLOCKS_PER_SEC * 1000.0 / N<<std::endl;
	std::cout << string << std::endl;
}
closed account (28poGNh0)
I think we cure cancer with this code !
closed account (28poGNh0)
1
2
3
4
for (size_t i = 90; i < length; i += 92)
	p[i] = 13;
for (size_t i = 91; i < length; i += 92)
	p[i] = 10;


Doesn't make any sence

First loop :
Well lets fill some of p array element with the number 13

second loop :
Sorry lets fill them with 10 ,except the p[90] !!!!!
Last edited on
Read it more carefully. It's setting to 13 every p[i] where i % 92 == 90, and to 10 every p[i] where i % 92 == 91. In other words, it makes sure that there's a CRLF sequence at least once every 92 characters (including the sequence itself).
closed account (28poGNh0)
I searched in google and I didnt found anything ,how did you do that?
Yeah. I don't see anything particularly distinctive about that code.
Just picked random part which is probably written specially for this code and not pasted from elsewhere and then run search on this site (I wanted to check this statement: "I probably copied it from someone's post here to test it."). So, here is the search request:
		string.resize(size, '-'); 		char_t *p = &string[0]; 		size_t length = string.size(); site:cplusplus.com/forum
https://www.google.com/search?q=%09%09string.resize%28size%2C+%27-%27%29%3B+%09%09char_t+*p+%3D+%26string[0]%3B+%09%09size_t+length+%3D+string.size%28%29%3B+site%3Acplusplus.com%2Fforum&ie=utf-8&oe=utf-8
Topic archived. No new replies allowed.