-
Notifications
You must be signed in to change notification settings - Fork 3
/
lightning.cpp
52 lines (45 loc) · 1.2 KB
/
lightning.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
#include <algorithm>
#include <assert.h>
#include <stdio.h>
#include <string>
#include <unistd.h>
struct Buffer {
char data[BUFSIZ];
size_t index = 0;
void extend(const char *str, size_t size) {
std::copy(str, str + size, data + index);
index += size;
}
void push(char c) { data[index++] = c; }
void clear() { index = 0; }
size_t size() const { return index; }
};
int increase_str_num(char *input, int input_size) {
int i = input_size - 1;
for (; i >= 0; --i) {
++input[i];
if (input[i] - '0' < 10)
return input_size - i;
input[i] = '0';
}
assert(!"overflow input");
return 0;
}
int main() {
Buffer buffer;
char num[12] = "00000000000";
int num_len = 1;
const char prefix[] = "Hello, ";
const size_t LINE_MAX_LEN = 17;
for (unsigned int i = 0; i < 100'000'000; ++i) {
buffer.extend(prefix, sizeof(prefix) - 1);
buffer.extend(num + sizeof(num) - 1 - num_len, num_len);
num_len = std::max(num_len, increase_str_num(num, sizeof(num) - 1));
buffer.push('\t');
if (buffer.size() + LINE_MAX_LEN > BUFSIZ) {
write(STDOUT_FILENO, buffer.data, buffer.size());
buffer.clear();
}
}
write(STDOUT_FILENO, buffer.data, buffer.size());
}