diff --git a/Swifternalization.xcodeproj/project.pbxproj b/Swifternalization.xcodeproj/project.pbxproj index 33fdf79..e8927bd 100644 --- a/Swifternalization.xcodeproj/project.pbxproj +++ b/Swifternalization.xcodeproj/project.pbxproj @@ -388,10 +388,14 @@ TargetAttributes = { 6D5004471B3EF91600A54B36 = { CreatedOnToolsVersion = 6.3.2; + DevelopmentTeam = NPGLBT5EYF; + DevelopmentTeamName = "Snupps Limited"; LastSwiftMigration = 0800; }; 6D5004521B3EF91600A54B36 = { CreatedOnToolsVersion = 6.3.2; + DevelopmentTeam = NPGLBT5EYF; + DevelopmentTeamName = "Snupps Limited"; LastSwiftMigration = 0800; }; }; diff --git a/Swifternalization/JSONFileLoader.swift b/Swifternalization/JSONFileLoader.swift index 4470815..b419ae2 100644 --- a/Swifternalization/JSONFileLoader.swift +++ b/Swifternalization/JSONFileLoader.swift @@ -3,7 +3,7 @@ import Foundation /** Represents json content. */ -internal typealias JSONDictionary = Dictionary +internal typealias JSONDictionary = [String : Any] /** Simple JSON file loader. @@ -28,8 +28,8 @@ final class JSONFileLoader { :param: bundle A bundle when file is located. :returns: dictionary with expressions or empty dictionary if cannot load a file. */ - class func loadExpressions(_ countryCode: CountryCode, bundle: Bundle) -> Dictionary { - return self.load("expressions", bundle: bundle)?[countryCode] as? Dictionary ?? [:] + class func loadExpressions(_ countryCode: CountryCode, bundle: Bundle) -> [String : String] { + return self.load("expressions", bundle: bundle)?[countryCode] as? [String : String] ?? [:] } /** @@ -41,9 +41,10 @@ final class JSONFileLoader { :returns: JSON or nil if file cannot be loaded. */ private class func load(_ fileName: String, bundle: Bundle) -> JSONDictionary? { - if let fileURL = bundle.urlForResource(fileName, withExtension: "json") { + if let fileURL = bundle.url(forResource: fileName, withExtension: "json") { return load(fileURL) } + print("Cannot find file \(fileName).json.") return nil } diff --git a/Swifternalization/LoadedTranslation.swift b/Swifternalization/LoadedTranslation.swift index 0853f4e..de0feaa 100644 --- a/Swifternalization/LoadedTranslation.swift +++ b/Swifternalization/LoadedTranslation.swift @@ -26,5 +26,5 @@ struct LoadedTranslation { /** A content of translation just loaded from a file user in future processing. */ - let content: Dictionary + let content: [String : Any] } diff --git a/Swifternalization/LoadedTranslationsProcessor.swift b/Swifternalization/LoadedTranslationsProcessor.swift index 083771f..95fe3ec 100644 --- a/Swifternalization/LoadedTranslationsProcessor.swift +++ b/Swifternalization/LoadedTranslationsProcessor.swift @@ -65,7 +65,7 @@ class LoadedTranslationsProcessor { if there is a key it is replaced with real expression pattern. */ var expressions = [Expression]() - for (key, value) in $0.content as! Dictionary { + for (key, value) in $0.content as! [String : String] { let pattern = sharedExpressions.filter({$0.identifier == key}).first?.pattern ?? key expressions.append(Expression(pattern: pattern, value: value)) } @@ -74,7 +74,7 @@ class LoadedTranslationsProcessor { case .withLengthVariations: // Translation contains length expressions like @100, @200, etc. var lengthVariations = [LengthVariation]() - for (key, value) in $0.content as! Dictionary { + for (key, value) in $0.content as! [String : String] { lengthVariations.append(LengthVariation(width: self.parseNumberFromLengthVariation(key), value: value)) } return Translation(key: $0.key, expressions: [Expression(pattern: $0.key, value: lengthVariations.last!.value, lengthVariations: lengthVariations)]) @@ -91,9 +91,9 @@ class LoadedTranslationsProcessor { var expressions = [Expression]() for (key, value) in $0.content { let pattern = sharedExpressions.filter({$0.identifier == key}).first?.pattern ?? key - if value is Dictionary { + if value is [String : String] { var lengthVariations = [LengthVariation]() - for (lvKey, lvValue) in value as! Dictionary { + for (lvKey, lvValue) in value as! [String : String] { lengthVariations.append(LengthVariation(width: self.parseNumberFromLengthVariation(lvKey), value: lvValue)) } expressions.append(Expression(pattern: pattern, value: lengthVariations.last!.value, lengthVariations: lengthVariations)) @@ -112,7 +112,7 @@ class LoadedTranslationsProcessor { :param: string A string that contains length variation string like @100. :returns: A number parsed from the string. */ - private class func parseNumberFromLengthVariation(_ string: String) -> Int { + fileprivate class func parseNumberFromLengthVariation(_ string: String) -> Int { return (Regex.matchInString(string, pattern: "@(\\d+)", capturingGroupIdx: 1)! as NSString).integerValue } } diff --git a/Swifternalization/Regex.swift b/Swifternalization/Regex.swift index 33eba62..150e957 100644 --- a/Swifternalization/Regex.swift +++ b/Swifternalization/Regex.swift @@ -24,10 +24,10 @@ final class Regex { var resultString: String? let range = NSMakeRange(0, str.characters.distance(from: str.startIndex, to: str.endIndex)) - regexp(pattern)?.enumerateMatches(in: str, options: RegularExpression.MatchingOptions.reportCompletion, range: range, using: { result, flags, stop in + regexp(pattern)?.enumerateMatches(in: str, options: NSRegularExpression.MatchingOptions.reportCompletion, range: range, using: { result, flags, stop in if let result = result { - if let capturingGroupIdx = capturingGroupIdx where result.numberOfRanges > capturingGroupIdx { - resultString = self.substring(str, range: result.range(at: capturingGroupIdx)) + if let capturingGroupIdx = capturingGroupIdx, result.numberOfRanges > capturingGroupIdx { + resultString = self.substring(str, range: result.rangeAt(capturingGroupIdx)) } else { resultString = self.substring(str, range: result.range) } @@ -76,9 +76,9 @@ final class Regex { :param: pattern A regexp pattern. :returns: `NSRegularExpression` object or nil if it cannot be created. */ - private class func regexp(_ pattern: String) -> RegularExpression? { + private class func regexp(_ pattern: String) -> NSRegularExpression? { do { - return try RegularExpression(pattern: pattern, options: RegularExpression.Options.caseInsensitive) + return try NSRegularExpression(pattern: pattern, options: NSRegularExpression.Options.caseInsensitive) } catch let error as NSError { print(error) } diff --git a/Swifternalization/RegexExpressionParser.swift b/Swifternalization/RegexExpressionParser.swift index 4a5b050..6d9d515 100644 --- a/Swifternalization/RegexExpressionParser.swift +++ b/Swifternalization/RegexExpressionParser.swift @@ -46,7 +46,7 @@ class RegexExpressionParser: ExpressionParser { :returns: `RegexPattern` or nil when there is no regular expression in the pattern. */ - private func regexPattern() -> RegexPattern? { + fileprivate func regexPattern() -> RegexPattern? { if let regex = Regex.firstMatchInString(pattern, pattern: "(?<=^\(ExpressionPatternType.Regex.rawValue):).*") { return regex } else { @@ -54,4 +54,4 @@ class RegexExpressionParser: ExpressionParser { return nil } } -} \ No newline at end of file +} diff --git a/Swifternalization/SharedExpressionLoader.swift b/Swifternalization/SharedExpressionLoader.swift index 4084e0e..7dcd542 100644 --- a/Swifternalization/SharedExpressionLoader.swift +++ b/Swifternalization/SharedExpressionLoader.swift @@ -11,7 +11,7 @@ final class SharedExpressionsLoader { :param: countryCode A country code :returns: array of loaded expressions. */ - class func loadExpressions(_ json: Dictionary) -> [SharedExpression] { + class func loadExpressions(_ json: [String : String]) -> [SharedExpression] { var expressions = [SharedExpression]() for (identifier, pattern) in json { expressions.append(SharedExpression(identifier: identifier, pattern: pattern)) diff --git a/Swifternalization/SharedExpressionsProcessor.swift b/Swifternalization/SharedExpressionsProcessor.swift index 58db81a..d99af77 100644 --- a/Swifternalization/SharedExpressionsProcessor.swift +++ b/Swifternalization/SharedExpressionsProcessor.swift @@ -87,7 +87,11 @@ class SharedExpressionsProcessor { } } -infix operator ) -> [LoadedTranslation] { + class func loadTranslations(_ json: [String : Any]) -> [LoadedTranslation] { var loadedTranslations = [LoadedTranslation]() for (key, value) in json { if value is String { @@ -45,7 +45,7 @@ final class TranslationsLoader { for (_, value) in element { if value is String { strings += 1 - } else if value is Dictionary { + } else if value is [String : Any] { dicts += 1 } } diff --git a/SwifternalizationTests/ExpressionJSONs.swift b/SwifternalizationTests/ExpressionJSONs.swift index 926945c..f11a16b 100644 --- a/SwifternalizationTests/ExpressionJSONs.swift +++ b/SwifternalizationTests/ExpressionJSONs.swift @@ -10,7 +10,7 @@ import Foundation class ExpressionJSONs { - class func base() -> Dictionary { + class func base() -> [String : String] { return [ "key1": "val1", "key2": "val2", @@ -18,11 +18,11 @@ class ExpressionJSONs { ] } - class func en() -> Dictionary { + class func en() -> [String : String] { return [ "key3": "val3", "key4": "val4", "key5": "val5" ] } -} \ No newline at end of file +} diff --git a/SwifternalizationTests/InequalityExpressionMatcherTests.swift b/SwifternalizationTests/InequalityExpressionMatcherTests.swift index 15ee0b2..40f628f 100644 --- a/SwifternalizationTests/InequalityExpressionMatcherTests.swift +++ b/SwifternalizationTests/InequalityExpressionMatcherTests.swift @@ -22,43 +22,43 @@ class InequalityExpressionMatcherTests: XCTestCase { let m = createMatcher("ie:x=3") XCTAssertTrue(m.validate("3"), "") XCTAssertTrue(m.validate("3.0"), "") - for n in Float.randomNumbersStrings(lower: -100.33, upper: 100.99, count: steps) { XCTAssertFalse(m.validate(n), "") } + for n in Float.randomNumbersStrings( -100.33, upper: 100.99, count: steps) { XCTAssertFalse(m.validate(n), "") } } func testIE2() { let m = createMatcher("ie:x<=3") - for n in Float.randomNumbersStrings(lower: -9999, upper: 3.0, count: steps) { XCTAssertTrue(m.validate(n), "") } - for n in Float.randomNumbersStrings(lower: 3.00001, upper: 1000000, count: steps) { XCTAssertFalse(m.validate(n), "") } + for n in Float.randomNumbersStrings( -9999, upper: 3.0, count: steps) { XCTAssertTrue(m.validate(n), "") } + for n in Float.randomNumbersStrings( 3.00001, upper: 1000000, count: steps) { XCTAssertFalse(m.validate(n), "") } } func testIE3() { let m = createMatcher("ie:x>=3") - for n in Float.randomNumbersStrings(lower: 3.0, upper: 999999, count: steps) { XCTAssertTrue(m.validate(n), "") } - for n in Float.randomNumbersStrings(lower: -999999, upper: 2.9999999, count: steps) { XCTAssertFalse(m.validate(n), "") } + for n in Float.randomNumbersStrings( 3.0, upper: 999999, count: steps) { XCTAssertTrue(m.validate(n), "") } + for n in Float.randomNumbersStrings( -999999, upper: 2.9999999, count: steps) { XCTAssertFalse(m.validate(n), "") } } func testIE4() { let m = createMatcher("ie:x>3") - for n in Float.randomNumbersStrings(lower: 3.0001, upper: 999999, count: steps) { XCTAssertTrue(m.validate(n), "") } - for n in Float.randomNumbersStrings(lower: -999999, upper: 3.000000, count: steps) { XCTAssertFalse(m.validate(n), "") } + for n in Float.randomNumbersStrings( 3.0001, upper: 999999, count: steps) { XCTAssertTrue(m.validate(n), "") } + for n in Float.randomNumbersStrings( -999999, upper: 3.000000, count: steps) { XCTAssertFalse(m.validate(n), "") } } func testIE5() { let m = createMatcher("ie:x<3") - for n in Float.randomNumbersStrings(lower: -99999, upper: 2.9999, count: steps) { XCTAssertTrue(m.validate(n), "") } - for n in Float.randomNumbersStrings(lower: 3.0000, upper: 999999, count: steps) { XCTAssertFalse(m.validate(n), "") } + for n in Float.randomNumbersStrings( -99999, upper: 2.9999, count: steps) { XCTAssertTrue(m.validate(n), "") } + for n in Float.randomNumbersStrings( 3.0000, upper: 999999, count: steps) { XCTAssertFalse(m.validate(n), "") } } // MARK: Float func testIE6() { let m = createMatcher("ie:x<=4.3") - for n in Float.randomNumbersStrings(lower: -99999, upper: 4.3, count: steps) { XCTAssertTrue(m.validate(n), "") } - for n in Float.randomNumbersStrings(lower: 4.31, upper: 10000000, count: steps) { XCTAssertFalse(m.validate(n), "") } + for n in Float.randomNumbersStrings( -99999, upper: 4.3, count: steps) { XCTAssertTrue(m.validate(n), "") } + for n in Float.randomNumbersStrings( 4.31, upper: 10000000, count: steps) { XCTAssertFalse(m.validate(n), "") } } func testIE7() { let m = createMatcher("ie:x>-199") - for n in Float.randomNumbersStrings(lower: -198, upper: 99999, count: steps) { XCTAssertTrue(m.validate(n), "") } - for n in Float.randomNumbersStrings(lower: -99999, upper: -199, count: steps) { XCTAssertFalse(m.validate(n), "") } + for n in Float.randomNumbersStrings( -198, upper: 99999, count: steps) { XCTAssertTrue(m.validate(n), "") } + for n in Float.randomNumbersStrings( -99999, upper: -199, count: steps) { XCTAssertFalse(m.validate(n), "") } } } diff --git a/SwifternalizationTests/InequalityExtendedExpressionMatcherTests.swift b/SwifternalizationTests/InequalityExtendedExpressionMatcherTests.swift index bd80ced..a1cf4f9 100644 --- a/SwifternalizationTests/InequalityExtendedExpressionMatcherTests.swift +++ b/SwifternalizationTests/InequalityExtendedExpressionMatcherTests.swift @@ -21,30 +21,30 @@ class InequalityExtendedExpressionMatcherTests: XCTestCase { // MARK: - Int func testIEX1() { let m = createMatcher("iex:4 Dictionary { + func loadExpressions(_ cc: CountryCode) -> [String : String] { return JSONFileLoader.loadExpressions(cc, bundle: Bundle.testBundle()) } diff --git a/SwifternalizationTests/LoadedTranslationsProcessorTests.swift b/SwifternalizationTests/LoadedTranslationsProcessorTests.swift index c8c1c3d..49e4e94 100644 --- a/SwifternalizationTests/LoadedTranslationsProcessorTests.swift +++ b/SwifternalizationTests/LoadedTranslationsProcessorTests.swift @@ -11,14 +11,14 @@ import XCTest class LoadedTranslationsProcessorTests: XCTestCase { - private func _baseExpressions() -> Dictionary { + private func _baseExpressions() -> [String : String] { return [ "e1": "ie:x=1", "e2": "ie:x=2" ] } - private func _enExpressions() -> Dictionary { + private func _enExpressions() -> [String : String] { return [ "e1": "ie:x=1", "e2": "ie:x=2", @@ -27,7 +27,7 @@ class LoadedTranslationsProcessorTests: XCTestCase { ] } - private func _baseTranslations() -> Dictionary { + private func _baseTranslations() -> [String : Any] { return [ "k1": "v1", "k2": "v2", @@ -36,7 +36,7 @@ class LoadedTranslationsProcessorTests: XCTestCase { ] } - private func _enTranslations() -> Dictionary { + private func _enTranslations() -> [String : Any] { return [ "k1": "v1", "k2": [ @@ -72,7 +72,7 @@ class LoadedTranslationsProcessorTests: XCTestCase { let enTranslations = TranslationsLoader.loadTranslations(_enTranslations()) var translations = LoadedTranslationsProcessor.processTranslations(baseTranslations, preferedLanguageTranslations: enTranslations, sharedExpressions: expressions) - translations.sort(isOrderedBefore: {$0.key < $1.key}) + translations.sort(by: {$0.key < $1.key}) XCTAssertEqual(translations.count, 4, "") @@ -99,10 +99,10 @@ class LoadedTranslationsProcessorTests: XCTestCase { // Get patterns of expressions var k3ExpressionPatterns: [String] = k3Translation.expressions.map({ $0.pattern }) - k3ExpressionPatterns.sort(isOrderedBefore: {$0 < $1}) + k3ExpressionPatterns.sort(by: {$0 < $1}) var k3ExpressionsToMatch: [String] = [_enExpressions()["e1"]!, _enExpressions()["e2"]!] - k3ExpressionsToMatch.sort(isOrderedBefore: {$0 < $1}) + k3ExpressionsToMatch.sort(by: {$0 < $1}) XCTAssertEqual(k3ExpressionPatterns, k3ExpressionsToMatch, "") @@ -115,10 +115,10 @@ class LoadedTranslationsProcessorTests: XCTestCase { // Get patterns of expressions var k4ExpressionPatterns: [String] = k4Translation.expressions.map({ $0.pattern }) - k4ExpressionPatterns.sort(isOrderedBefore: {$0 < $1}) + k4ExpressionPatterns.sort(by: {$0 < $1}) var k4ExpressionsToMatch: [String] = [_enExpressions()["e2"]!, _enExpressions()["e3"]!, _enExpressions()["e4"]!] - k4ExpressionsToMatch.sort(isOrderedBefore: {$0 < $1}) + k4ExpressionsToMatch.sort(by: {$0 < $1}) XCTAssertEqual(k4ExpressionPatterns, k4ExpressionsToMatch, "") } diff --git a/SwifternalizationTests/RandomNumbers.swift b/SwifternalizationTests/RandomNumbers.swift index 2f58928..26d2f1d 100644 --- a/SwifternalizationTests/RandomNumbers.swift +++ b/SwifternalizationTests/RandomNumbers.swift @@ -9,29 +9,29 @@ import Foundation /// http://stackoverflow.com/a/28075271/1046965 -func arc4random (_ type: T.Type) -> T { +func arc4random (_ type: T.Type) -> T { var r: T = 0 - arc4random_buf(&r, Int(sizeof(T.self))) + arc4random_buf(&r, Int(MemoryLayout.size)) return r } extension Float { - static func random(lower: Float, upper: Float) -> Float { + static func random(_ lower: Float, upper: Float) -> Float { let r = Float(arc4random(UInt32.self)) / Float(UInt32.max) return (r * (upper - lower)) + lower } - static func randomNumbers(lower: Float, upper: Float, count: Int) -> [Float] { + static func randomNumbers(_ lower: Float, upper: Float, count: Int) -> [Float] { var nums = [Float]() for _ in 0.. [String] { + static func randomNumbersStrings(_ lower: Float, upper: Float, count: Int) -> [String] { var numStr = [String]() - for i in randomNumbers(lower: lower, upper: upper, count: count) { + for i in randomNumbers(lower, upper: upper, count: count) { numStr.append(String(format: "%f", i)) } return numStr diff --git a/SwifternalizationTests/TranslationJSONs.swift b/SwifternalizationTests/TranslationJSONs.swift index 3ce4744..a918ffb 100644 --- a/SwifternalizationTests/TranslationJSONs.swift +++ b/SwifternalizationTests/TranslationJSONs.swift @@ -10,7 +10,7 @@ import Foundation class TranslationJSONs { - class func base() -> Dictionary { + class func base() -> [String : Any] { return [ "key-1": "value-1", "key-2": [ @@ -32,7 +32,7 @@ class TranslationJSONs { ] } - class func en() -> Dictionary { + class func en() -> [String : Any] { return [ "key-1": "en-value-1", "key-2": [ @@ -53,4 +53,4 @@ class TranslationJSONs { ] ] } -} \ No newline at end of file +}