Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Swift 4.1 support to SwiftString #15

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .swift-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.0
4.0
32 changes: 26 additions & 6 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -1,9 +1,29 @@
// Generated automatically by Perfect Assistant Application
// Date: 2017-11-11 21:12:58 +0000
// swift-tools-version:4.0

import PackageDescription

let package = Package(
name: "SwiftString",
targets: [],
dependencies: [
]
name: "SwiftString",
products: [
// Products define the executables and libraries produced by a package, and make them visible to other packages.
.library(
name: "SwiftString",
targets: ["SwiftString"]),
],
dependencies: [
// Dependencies declare other packages that this package depends on.
// .package(url: /* package url */, from: "1.0.0"),
],
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
// Targets can depend on other targets in this package, and on products in packages which this package depends on.
.target(
name: "SwiftString",
dependencies: []),
.testTarget(
name: "SwiftStringTests",
dependencies: ["SwiftString"]),
],

swiftLanguageVersions: [4]
)
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public extension String {
left != right && leftRange.upperBound != rightRange.lowerBound
else { return nil }

return self[leftRange.upperBound...index(before: rightRange.lowerBound)]
return String(self[leftRange.upperBound...index(before: rightRange.lowerBound)])

}

Expand Down Expand Up @@ -52,9 +52,9 @@ public extension String {
func chompLeft(_ prefix: String) -> String {
if let prefixRange = range(of: prefix) {
if prefixRange.upperBound >= endIndex {
return self[startIndex..<prefixRange.lowerBound]
return String(self[startIndex..<prefixRange.lowerBound])
} else {
return self[prefixRange.upperBound..<endIndex]
return String(self[prefixRange.upperBound..<endIndex])
}
}
return self
Expand All @@ -63,9 +63,9 @@ public extension String {
func chompRight(_ suffix: String) -> String {
if let suffixRange = range(of: suffix, options: .backwards) {
if suffixRange.upperBound >= endIndex {
return self[startIndex..<suffixRange.lowerBound]
return String(self[startIndex..<suffixRange.lowerBound])
} else {
return self[suffixRange.upperBound..<endIndex]
return String(self[suffixRange.upperBound..<endIndex])
}
}
return self
Expand Down Expand Up @@ -125,7 +125,7 @@ public extension String {

func initialsFirstAndLast() -> String {
let words = self.components(separatedBy: " ")
return words.reduce("") { ($0 == "" ? "" : $0[startIndex...startIndex]) + $1[startIndex...startIndex]}
return words.reduce("") { ($0 == "" ? "" : String($0[startIndex...startIndex])) + $1[startIndex...startIndex]}
}

func isAlpha() -> Bool {
Expand Down Expand Up @@ -221,8 +221,11 @@ public extension String {
.joined(separator: " ")
}

@available(*, deprecated:7.0,message: "Use String(repeating,count)")
func times(_ n: Int) -> String {
return (0..<n).reduce("") { $0.0 + self }
//return (0..<n).reduce("") { $0 + self }

return String(repeating: self, count: n)
}

func toFloat() -> Float? {
Expand Down Expand Up @@ -264,14 +267,14 @@ public extension String {

func trimmedLeft() -> String {
if let range = rangeOfCharacter(from: NSCharacterSet.whitespacesAndNewlines.inverted) {
return self[range.lowerBound..<endIndex]
return String(self[range.lowerBound..<endIndex])
}
return self
}

func trimmedRight() -> String {
if let range = rangeOfCharacter(from: NSCharacterSet.whitespacesAndNewlines.inverted, options: NSString.CompareOptions.backwards) {
return self[startIndex..<range.upperBound]
return String(self[startIndex..<range.upperBound])
}
return self
}
Expand All @@ -284,7 +287,7 @@ public extension String {
get {
let startIndex = self.index(self.startIndex, offsetBy: r.lowerBound)
let endIndex = self.index(self.startIndex, offsetBy: r.upperBound)
return self[startIndex..<endIndex]
return String(self[startIndex..<endIndex])
}
}

Expand All @@ -301,7 +304,7 @@ public extension String {
let upper = range.upperBound < 0 ? 0 : range.upperBound
let s = index(startIndex, offsetBy: lower, limitedBy: endIndex) ?? endIndex
let e = index(startIndex, offsetBy: upper, limitedBy: endIndex) ?? endIndex
return self[s..<e]
return String(self[s..<e])
}
}

Expand All @@ -313,14 +316,14 @@ public extension String {
let upper = range.upperBound < 0 ? 0 : range.upperBound
let s = index(startIndex, offsetBy: lower, limitedBy: closedEndIndex) ?? closedEndIndex
let e = index(startIndex, offsetBy: upper, limitedBy: closedEndIndex) ?? closedEndIndex
return self[s...e]
return String(self[s...e])
}
}

func substring(_ startIndex: Int, length: Int) -> String {
let start = self.index(self.startIndex, offsetBy: startIndex)
let end = self.index(self.startIndex, offsetBy: startIndex + length)
return self[start..<end]
return String(self[start..<end])
}

subscript(i: Int) -> Character {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -301,28 +301,28 @@ public extension String {

// Find the next '&' and copy the characters preceding it to `result`:
while let ampRange = self.range(of: "&", range: position ..< endIndex) {
result.append(self[position ..< ampRange.lowerBound])
result.append(String(self[position ..< ampRange.lowerBound]))
position = ampRange.lowerBound

// Find the next ';' and copy everything from '&' to ';' into `entity`
if let semiRange = self.range(of: ";", range: position ..< endIndex) {
let entity = self[position ..< semiRange.upperBound]
position = semiRange.upperBound

if let decoded = decode(entity) {
if let decoded = decode(String(entity)) {
// Replace by decoded character:
result.append(decoded)
} else {
// Invalid entity, copy verbatim:
result.append(entity)
result.append(String(entity))
}
} else {
// No matching ';'.
break
}
}
// Copy remaining characters to `result`:
result.append(self[position ..< endIndex])
result.append(String(self[position ..< endIndex]))
return result
}
}
File renamed without changes.
18 changes: 17 additions & 1 deletion SwiftString.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
OBJ_10 /* StringHTML.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StringHTML.swift; sourceTree = "<group>"; };
OBJ_13 /* SwiftStringTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SwiftStringTests.swift; sourceTree = "<group>"; };
OBJ_15 /* SwiftString.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; path = SwiftString.framework; sourceTree = BUILT_PRODUCTS_DIR; };
OBJ_16 /* SwiftStringTests.xctest */ = {isa = PBXFileReference; lastKnownFileType = file; path = SwiftStringTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
OBJ_16 /* SwiftStringTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; path = SwiftStringTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
OBJ_6 /* Package.swift */ = {isa = PBXFileReference; explicitFileType = sourcecode.swift; path = Package.swift; sourceTree = "<group>"; };
OBJ_9 /* StringExtensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StringExtensions.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */
Expand Down Expand Up @@ -154,6 +154,14 @@
isa = PBXProject;
attributes = {
LastUpgradeCheck = 9999;
TargetAttributes = {
OBJ_17 = {
LastSwiftMigration = 0940;
};
OBJ_25 = {
LastSwiftMigration = 0940;
};
};
};
buildConfigurationList = OBJ_2 /* Build configuration list for PBXProject "SwiftString" */;
compatibilityVersion = "Xcode 3.2";
Expand Down Expand Up @@ -220,6 +228,8 @@
PRODUCT_BUNDLE_IDENTIFIER = SwiftString;
PRODUCT_MODULE_NAME = "$(TARGET_NAME:c99extidentifier)";
PRODUCT_NAME = "$(TARGET_NAME:c99extidentifier)";
SWIFT_SWIFT3_OBJC_INFERENCE = On;
SWIFT_VERSION = 4.0;
TARGET_NAME = SwiftString;
};
name = Debug;
Expand All @@ -240,6 +250,8 @@
PRODUCT_BUNDLE_IDENTIFIER = SwiftString;
PRODUCT_MODULE_NAME = "$(TARGET_NAME:c99extidentifier)";
PRODUCT_NAME = "$(TARGET_NAME:c99extidentifier)";
SWIFT_SWIFT3_OBJC_INFERENCE = On;
SWIFT_VERSION = 4.0;
TARGET_NAME = SwiftString;
};
name = Release;
Expand All @@ -257,6 +269,8 @@
LD_RUNPATH_SEARCH_PATHS = "@loader_path/../Frameworks";
OTHER_LDFLAGS = "$(inherited)";
OTHER_SWIFT_FLAGS = "$(inherited)";
SWIFT_SWIFT3_OBJC_INFERENCE = On;
SWIFT_VERSION = 4.0;
TARGET_NAME = SwiftStringTests;
};
name = Debug;
Expand All @@ -274,6 +288,8 @@
LD_RUNPATH_SEARCH_PATHS = "@loader_path/../Frameworks";
OTHER_LDFLAGS = "$(inherited)";
OTHER_SWIFT_FLAGS = "$(inherited)";
SWIFT_SWIFT3_OBJC_INFERENCE = On;
SWIFT_VERSION = 4.0;
TARGET_NAME = SwiftStringTests;
};
name = Release;
Expand Down
7 changes: 7 additions & 0 deletions Tests/SwiftStringTests/SwiftStringTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,12 @@ class SwiftStringTests: XCTestCase {
let decoded = encoded.fromBase64()
XCTAssertEqual(str, decoded, "Base64 does not match")
}

func testtimes() {
let fivetimesc = "ccccc"
let fivetimescComputed = "c".times(5)
XCTAssertEqual(fivetimesc, fivetimescComputed, "times doesnt work properly")
}

static var allTests : [(String, (SwiftStringTests) -> () throws -> Void)] {
return [
Expand Down Expand Up @@ -313,6 +319,7 @@ class SwiftStringTests: XCTestCase {
("testsubstring", testsubstring),
("testsubscript", testsubscript),
("testsafesubscript", testsafesubscript),
("testtimes", testtimes)
]
}

Expand Down