on master help sign in
Fix line endings
a81254esharkdp
tests/syntax-tests/highlighted/Swift/test.swift+268−268
@@ -1,268 +1,268 @@
1−class Person {
1+class Person {
2−// We can define class property here 
2+// We can define class property here
3−var age = 25
3+var age = 25
4−// Implement Class initializer. Initializers are called when a new object of this class is created 
4+// Implement Class initializer. Initializers are called when a new object of this class is created
5−init() { 
5+init() { 
6− print(“A new instance of this class Person is created.”) 
6+ print(“A new instance of this class Person is created.”) 
7− } 
7+ } 
8−} 
8+} 
9−// We can now create an instance of class Person - an object - by putting parentheses after the class name 
9+// We can now create an instance of class Person - an object - by putting parentheses after the class name
10−let personObj = Person()
10+let personObj = Person()
11−// Once an instance of Person class is created we can access its properties using the dot “.” syntax. 
11+// Once an instance of Person class is created we can access its properties using the dot “.” syntax.
12−print(“This person age is \(personObj.age)”)
12+print(“This person age is \(personObj.age)”)
13−
13+
14−import Foundation
14+import Foundation
15−class Friend : Comparable {
15+class Friend : Comparable {
16− let name : String
16+ let name : String
17− let age : Int
17+ let age : Int
18− 
18+ 
19− init(name : String, age: Int) {
19+ init(name : String, age: Int) {
20− self.name = name
20+ self.name = name
21− self.age = age
21+ self.age = age
22− }
22+ }
23−}
23+}
24−func < (lhs: Friend, rhs: Friend) -> Bool {
24+func < (lhs: Friend, rhs: Friend) -> Bool {
25− return lhs.age < rhs.age } func > (lhs: Friend, rhs: Friend) -> Bool {
25+ return lhs.age < rhs.age }; func > (lhs: Friend, rhs: Friend) -> Bool {
26− return lhs.age > rhs.age
26+ return lhs.age > rhs.age
27−}
27+}
28−func == (lhs: Friend, rhs: Friend) -> Bool {
28+func == (lhs: Friend, rhs: Friend) -> Bool {
29− var returnValue = false
29+ var returnValue = false
30− if (lhs.name == rhs.name) && (lhs.age == rhs.age)
30+ if (lhs.name == rhs.name) && (lhs.age == rhs.age)
31− {
31+ {
32− returnValue = true
32+ returnValue = true
33− }
33+ }
34− return returnValue
34+ return returnValue
35−}
35+}
36−
36+
37− let friend1 = Friend(name: "Sergey", age: 35)
37+ let friend1 = Friend(name: "Sergey", age: 35)
38− let friend2 = Friend(name: "Sergey", age: 30)
38+ let friend2 = Friend(name: "Sergey", age: 30)
39− 
39+ 
40− print("\Compare Friend object. Same person? (friend1 == friend2)")
40+ print("Compare Friend object. Same person? (friend1 == friend2)")
41−
41+
42−func sayHelloWorld() {
42+func sayHelloWorld() {
43− print("Hello World")
43+ print("Hello World")
44−}
44+}
45−// Call function 
45+// Call function
46−sayHelloWorld()
46+sayHelloWorld()
47−
47+
48−func printOutFriendNames(names: String...) {
48+func printOutFriendNames(names: String...) {
49− 
49+ 
50− for name in names {
50+ for name in names {
51− 
51+ 
52− print(name)
52+ print(name)
53− }
53+ }
54− 
54+ 
55−}
55+}
56−// Call the printOutFriendNames with two parameters 
56+// Call the printOutFriendNames with two parameters
57−printOutFriendNames("Sergey", "Bill")
57+printOutFriendNames("Sergey", "Bill")
58−// Call the function with more parameters 
58+// Call the function with more parameters
59−printOutFriendNames("Sergey", "Bill", "Max")
59+printOutFriendNames("Sergey", "Bill", "Max")
60−
60+
61−let simpleClosure = {
61+let simpleClosure = {
62− print("From a simpleClosure")
62+ print("From a simpleClosure")
63−}
63+}
64−// Call closure 
64+// Call closure
65−simpleClosure() 
65+simpleClosure() 
66−
66+
67−let fullName = { (firstName:String, lastName:String)->String in
67+let fullName = { (firstName:String, lastName:String)->String in
68− return firstName + " " + lastName
68+ return firstName + " " + lastName
69−}
69+}
70−// Call Closure 
70+// Call Closure
71−let myFullName = fullName("Sergey", "Kargopolov")
71+let myFullName = fullName("Sergey", "Kargopolov")
72−print("My full name is \(myFullName)")
72+print("My full name is \(myFullName)")
73−
73+
74−let myDictionary = [String:String]()
74+let myDictionary = [String:String]()
75−// Another way to create an empty dictionary 
75+// Another way to create an empty dictionary
76−let myDictionary2:[String:String] = [:]
76+let myDictionary2:[String:String] = [:]
77−// Keys in dictionary can also be of type Int 
77+// Keys in dictionary can also be of type Int
78−let myDictionary3 = [Int:String]()
78+let myDictionary3 = [Int:String]()
79−
79+
80−var myDictionary = ["first_name": "Sergey", "last_name": "Kargopolov"]
80+var myDictionary = ["first_name": "Sergey", "last_name": "Kargopolov"]
81−// print to preview 
81+// print to preview
82−print(myDictionary)
82+print(myDictionary)
83−// Add a new key with a value 
83+// Add a new key with a value
84−myDictionary["user_id"] = "f5h7ru0tJurY8f7g5s6fd"
84+myDictionary["user_id"] = "f5h7ru0tJurY8f7g5s6fd"
85−// We should now have 3 key value pairs printed 
85+// We should now have 3 key value pairs printed
86−print(myDictionary)
86+print(myDictionary)
87−
87+
88−var myDictionary = ["first_name": "Sergey", "last_name": "Kargopolov"]
88+var myDictionary = ["first_name": "Sergey", "last_name": "Kargopolov"]
89−// Loop through dictionary keys and print values 
89+// Loop through dictionary keys and print values
90−for (key,value) in myDictionary {
90+for (key,value) in myDictionary {
91− print("\(key) = \(value)")
91+ print("\(key) = \(value)")
92−}
92+}
93− class Friend {
93+ class Friend {
94− let name : String
94+ let name : String
95− let age : Int
95+ let age : Int
96− 
96+ 
97− init(name : String, age: Int) {
97+ init(name : String, age: Int) {
98− self.name = name
98+ self.name = name
99− self.age = age
99+ self.age = age
100− }
100+ }
101−}
101+}
102−
102+
103− var friends:[Friend] = []
103+ var friends:[Friend] = []
104− 
104+ 
105− let friend1 = Friend(name: "Sergey", age: 30)
105+ let friend1 = Friend(name: "Sergey", age: 30)
106− let friend2 = Friend(name: "Bill", age: 35)
106+ let friend2 = Friend(name: "Bill", age: 35)
107− let friend3 = Friend(name: "Michael", age: 21)
107+ let friend3 = Friend(name: "Michael", age: 21)
108− 
108+ 
109− friends.append(friend1)
109+ friends.append(friend1)
110− friends.append(friend2)
110+ friends.append(friend2)
111− friends.append(friend3)
111+ friends.append(friend3)
112− 
112+ 
113− printFriends(friends: friends)
113+ printFriends(friends: friends)
114− 
114+ 
115− // Get sorted array in descending order (largest to the smallest number) 
115+ // Get sorted array in descending order (largest to the smallest number)
116− let sortedFriends = friends.sorted(by: { $0.age > $1.age })
116+ let sortedFriends = friends.sorted(by: { $0.age > $1.age })
117− printFriends(friends: sortedFriends)
117+ printFriends(friends: sortedFriends)
118− 
118+ 
119− // Get sorted array in ascending order (smallest to the largest number) 
119+ // Get sorted array in ascending order (smallest to the largest number)
120− let sortedFriendsAscendingOrder = friends.sorted(by: { $0.age < $1.age })
120+ let sortedFriendsAscendingOrder = friends.sorted(by: { $0.age < $1.age })
121− printFriends(friends: sortedFriendsAscendingOrder)
121+ printFriends(friends: sortedFriendsAscendingOrder)
122−
122+
123−
123+
124− func printFriends(friends: [Friend])
124+ func printFriends(friends: [Friend])
125− {
125+ {
126− for friendEntry in friends {
126+ for friendEntry in friends {
127− print("Name: \(friendEntry.name), age: \(friendEntry.age)")
127+ print("Name: \(friendEntry.name), age: \(friendEntry.age)")
128− }
128+ }
129− }
129+ }
130−
130+
131−import UIKit
131+import UIKit
132−class ViewController: UIViewController {
132+class ViewController: UIViewController {
133−override func viewDidLoad() {
133+override func viewDidLoad() {
134− super.viewDidLoad()
134+ super.viewDidLoad()
135−}
135+}
136−override func viewWillAppear(_ animated: Bool) {
136+override func viewWillAppear(_ animated: Bool) {
137− super.viewWillAppear(animated)
137+ super.viewWillAppear(animated)
138− 
138+ 
139− // Create destination URL  
139+ // Create destination URL 
140− let documentsUrl:URL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first as URL!
140+ let documentsUrl:URL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first as URL!
141− let destinationFileUrl = documentsUrl.appendingPathComponent("downloadedFile.jpg")
141+ let destinationFileUrl = documentsUrl.appendingPathComponent("downloadedFile.jpg")
142− 
142+ 
143− //Create URL to the source file you want to download 
143+ //Create URL to the source file you want to download
144− let fileURL = URL(string: "https://s3.amazonaws.com/learn-swift/IMG_0001.JPG")
144+ let fileURL = URL(string: "https://s3.amazonaws.com/learn-swift/IMG_0001.JPG")
145− 
145+ 
146− let sessionConfig = URLSessionConfiguration.default
146+ let sessionConfig = URLSessionConfiguration.default
147− let session = URLSession(configuration: sessionConfig)
147+ let session = URLSession(configuration: sessionConfig)
148− 
148+ 
149− let request = URLRequest(url:fileURL!)
149+ let request = URLRequest(url:fileURL!)
150− 
150+ 
151− let task = session.downloadTask(with: request) { (tempLocalUrl, response, error) in
151+ let task = session.downloadTask(with: request) { (tempLocalUrl, response, error) in
152− if let tempLocalUrl = tempLocalUrl, error == nil {
152+ if let tempLocalUrl = tempLocalUrl, error == nil {
153− // Success 
153+ // Success
154− if let statusCode = (response as? HTTPURLResponse)?.statusCode {
154+ if let statusCode = (response as? HTTPURLResponse)?.statusCode {
155− print("Successfully downloaded. Status code: \(statusCode)")
155+ print("Successfully downloaded. Status code: \(statusCode)")
156− }
156+ }
157− 
157+ 
158− do {
158+ do {
159− try FileManager.default.copyItem(at: tempLocalUrl, to: destinationFileUrl)
159+ try FileManager.default.copyItem(at: tempLocalUrl, to: destinationFileUrl)
160− } catch (let writeError) {
160+ } catch (let writeError) {
161− print("Error creating a file \(destinationFileUrl) : \(writeError)")
161+ print("Error creating a file \(destinationFileUrl) : \(writeError)")
162− }
162+ }
163− 
163+ 
164− } else {
164+ } else {
165− print("Error took place while downloading a file. Error description: %@", error?.localizedDescription);
165+ print("Error took place while downloading a file. Error description: %@", error?.localizedDescription);
166− }
166+ }
167− }
167+ }
168− task.resume()
168+ task.resume()
169− 
169+ 
170− }
170+ }
171−}
171+}
172−
172+
173− do {
173+ do {
174− 
174+ 
175− // Convert JSON Object received from server side into Swift NSArray. 
175+ // Convert JSON Object received from server side into Swift NSArray.
176− // Note the use "try" 
176+ // Note the use "try"
177− if let convertedJsonIntoArray = try JSONSerialization.JSONObjectWithData(data!, options: []) as? NSArray {
177+ if let convertedJsonIntoArray = try JSONSerialization.JSONObjectWithData(data!, options: []) as? NSArray {
178− }
178+ }
179− 
179+ 
180− } catch let error as NSError {
180+ } catch let error as NSError {
181− print(error.localizedDescription)
181+ print(error.localizedDescription)
182− }
182+ }
183−
183+
184−DispatchQueue.global(qos: .userInitiated).async {
184+DispatchQueue.global(qos: .userInitiated).async {
185− // Do some time consuming task in this background thread 
185+ // Do some time consuming task in this background thread
186− // Mobile app will remain to be responsive to user actions 
186+ // Mobile app will remain to be responsive to user actions
187− 
187+ 
188− print("Performing time consuming task in this background thread")
188+ print("Performing time consuming task in this background thread")
189− 
189+ 
190− DispatchQueue.main.async {
190+ DispatchQueue.main.async {
191− // Task consuming task has completed 
191+ // Task consuming task has completed
192− // Update UI from this block of code 
192+ // Update UI from this block of code
193− print("Time consuming task has completed. From here we are allowed to update user interface.")
193+ print("Time consuming task has completed. From here we are allowed to update user interface.")
194− }
194+ }
195− }
195+ }
196−
196+
197−import UIKit
197+import UIKit
198−class ViewController: UIViewController {
198+class ViewController: UIViewController {
199− 
199+ 
200− override func viewDidLoad() {
200+ override func viewDidLoad() {
201− super.viewDidLoad()
201+ super.viewDidLoad()
202− // Do any additional setup after loading the view, typically from a nib. 
202+ // Do any additional setup after loading the view, typically from a nib.
203− 
203+ 
204− let button = UIButton(type: UIButtonType.system) as UIButton
204+ let button = UIButton(type: UIButtonType.system) as UIButton
205− 
205+ 
206− let xPostion:CGFloat = 50
206+ let xPostion:CGFloat = 50
207− let yPostion:CGFloat = 100
207+ let yPostion:CGFloat = 100
208− let buttonWidth:CGFloat = 150
208+ let buttonWidth:CGFloat = 150
209− let buttonHeight:CGFloat = 45
209+ let buttonHeight:CGFloat = 45
210− 
210+ 
211− button.frame = CGRect(x:xPostion, y:yPostion, width:buttonWidth, height:buttonHeight)
211+ button.frame = CGRect(x:xPostion, y:yPostion, width:buttonWidth, height:buttonHeight)
212− 
212+ 
213− button.backgroundColor = UIColor.lightGray
213+ button.backgroundColor = UIColor.lightGray
214− button.setTitle("Tap me", for: UIControlState.normal)
214+ button.setTitle("Tap me", for: UIControlState.normal)
215− button.tintColor = UIColor.black
215+ button.tintColor = UIColor.black
216− button.addTarget(self, action: #selector(ViewController.buttonAction(_:)), for: .touchUpInside)
216+ button.addTarget(self, action: #selector(ViewController.buttonAction(_:)), for: .touchUpInside)
217− 
217+ 
218− self.view.addSubview(button)
218+ self.view.addSubview(button)
219− }
219+ }
220− 
220+ 
221− func buttonAction(_ sender:UIButton!)
221+ func buttonAction(_ sender:UIButton!)
222− {
222+ {
223− print("Button tapped")
223+ print("Button tapped")
224− }
224+ }
225− 
225+ 
226− override func didReceiveMemoryWarning() {
226+ override func didReceiveMemoryWarning() {
227− super.didReceiveMemoryWarning()
227+ super.didReceiveMemoryWarning()
228− // Dispose of any resources that can be recreated. 
228+ // Dispose of any resources that can be recreated.
229− }
229+ }
230− 
230+ 
231− 
231+ 
232−}
232+}
233−
233+
234−import UIKit
234+import UIKit
235−class ViewController: UIViewController {
235+class ViewController: UIViewController {
236− 
236+ 
237− override func viewDidLoad() {
237+ override func viewDidLoad() {
238− super.viewDidLoad()
238+ super.viewDidLoad()
239− 
239+ 
240− //Create Activity Indicator 
240+ //Create Activity Indicator
241− let myActivityIndicator = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.gray)
241+ let myActivityIndicator = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.gray)
242− 
242+ 
243− // Position Activity Indicator in the center of the main view 
243+ // Position Activity Indicator in the center of the main view
244− myActivityIndicator.center = view.center
244+ myActivityIndicator.center = view.center
245− 
245+ 
246− // If needed, you can prevent Acivity Indicator from hiding when stopAnimating() is called 
246+ // If needed, you can prevent Acivity Indicator from hiding when stopAnimating() is called
247− myActivityIndicator.hidesWhenStopped = false
247+ myActivityIndicator.hidesWhenStopped = false
248− 
248+ 
249− // Start Activity Indicator 
249+ // Start Activity Indicator
250− myActivityIndicator.startAnimating()
250+ myActivityIndicator.startAnimating()
251− 
251+ 
252− // Call stopAnimating() when need to stop activity indicator 
252+ // Call stopAnimating() when need to stop activity indicator
253− //myActivityIndicator.stopAnimating() 
253+ //myActivityIndicator.stopAnimating()
254− 
254+ 
255− 
255+ 
256− view.addSubview(myActivityIndicator)
256+ view.addSubview(myActivityIndicator)
257− }
257+ }
258− 
258+ 
259− override func didReceiveMemoryWarning() {
259+ override func didReceiveMemoryWarning() {
260− super.didReceiveMemoryWarning()
260+ super.didReceiveMemoryWarning()
261− }
261+ }
262− 
262+ 
263−}
263+}
264−
264+
265−
265+
266−
266+
267−
267+
268−
268+