-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.swift
73 lines (54 loc) · 1.47 KB
/
main.swift
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
//
// gen.swift
// CompilationBenchmarks
//
// Created by Sergey Rakov on 14/04/2019.
// Copyright © 2019 Gimungagap. All rights reserved.
//
import Foundation
import Darwin
guard CommandLine.arguments.count > 1 else {
print("Error: template not specified")
exit(1)
}
let input = CommandLine.arguments[1]
let kRepeatMark = "{r}"
let kIndexMark = "{i}"
let kDefaultCount = 1000
var result = ""
var lines = input.components(separatedBy: .newlines)
var count = kDefaultCount
if let firstLine = lines.first,
let templateCount = Int(firstLine),
templateCount > 0 {
count = templateCount
lines.remove(at: 0)
}
let repeatMarksCount = lines.filter { $0.trimmingCharacters(in: .whitespaces) == kRepeatMark }.count
guard repeatMarksCount % 2 == 0 else {
print("Error: repeat scope not closed")
exit(1)
}
var i = 0
while i < lines.count {
let line = lines[i]
if line.trimmingCharacters(in: .whitespaces) == kRepeatMark {
var repeatedLines = ""
var j = i + 1
while j < lines.count && lines[j].trimmingCharacters(in: .whitespaces) != kRepeatMark {
repeatedLines += lines[j] + "\n"
j += 1
}
for n in 0..<count {
result += repeatedLines.replacingOccurrences(of: kIndexMark, with: String(n))
}
i = j + 1
} else {
result += line
if i < lines.count - 1 {
result += "\n"
}
i += 1
}
}
print(String(describing: result))