Skip to content

Commit d5cc9fb

Browse files
authored
Merge pull request #16 from presto95/feature/slicing2
Add slicing using characters
2 parents d213340 + 6762976 commit d5cc9fb

File tree

2 files changed

+79
-0
lines changed

2 files changed

+79
-0
lines changed

Sources/String/Utility.swift

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,3 +142,52 @@ extension String {
142142
return self.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)
143143
}
144144
}
145+
146+
// MARK: - Slicing using characters
147+
148+
extension String {
149+
150+
/// Returns a sliced string of given string
151+
/// from the first `start` character to the last `end` character.
152+
///
153+
/// If the lower bound is greater than the upper bound, it returns an empty string.
154+
/// If the given string doesn't contain given characters, it also returns an empty string.
155+
///
156+
/// - Parameters:
157+
/// - start: The character value being the lower bound of the given string.
158+
/// - end: The character value being the upper bound of the given string.
159+
/// - inversed: A boolean value that indicates whether the sliced string is inversed.
160+
/// The Default value is `false`.
161+
///
162+
/// - Returns: A sliced string of given string.
163+
public func sliced(from start: Character, to end: Character, inversed: Bool = false) -> String {
164+
guard let startCharacterIndex = firstIndex(of: start),
165+
let endCharacterIndex = lastIndex(of: end),
166+
startCharacterIndex <= endCharacterIndex
167+
else { return "" }
168+
let slicedSubstring = self[startCharacterIndex ... endCharacterIndex]
169+
return !inversed ? String(slicedSubstring) : String(slicedSubstring.reversed())
170+
}
171+
172+
/// Slices a given string
173+
/// from the first `start` character to the last `end` character.
174+
///
175+
/// If the lower bound is greater than the upper bound, it becomes an empty string.
176+
/// If the given string doesn't contain given characters, it also becomes an emtpy string.
177+
///
178+
/// - Parameters:
179+
/// - start: The character value being the lower bound of the given string.
180+
/// - end: The character value being the upper bound of the given string.
181+
/// - inversed: A boolean value that indicates whether the sliced string is inversed.
182+
/// The Default value is `false`.
183+
public mutating func slice(from start: Character, to end: Character, inversed: Bool = false) {
184+
guard let startCharacterIndex = firstIndex(of: start),
185+
let endCharacterIndex = lastIndex(of: end),
186+
startCharacterIndex <= endCharacterIndex else {
187+
self = ""
188+
return
189+
}
190+
let slicedSubstring = self[startCharacterIndex ... endCharacterIndex]
191+
self = !inversed ? String(slicedSubstring) : String(slicedSubstring.reversed())
192+
}
193+
}

Tests/AGStringTests.swift

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,4 +108,34 @@ class AGStringTests: XCTestCase {
108108
str.trim()
109109
}
110110
}
111+
112+
func testSliced() {
113+
XCTAssertEqual("abcdef".sliced(from: "a", to: "d"), "abcd")
114+
XCTAssertEqual("abcdef".sliced(from: "a", to: "a"), "a")
115+
XCTAssertEqual("abcdef".sliced(from: "a", to: "d", inversed: true), "dcba")
116+
XCTAssertEqual("abcdef".sliced(from: "a", to: "a", inversed: true), "a")
117+
XCTAssertEqual("abcdef".sliced(from: "a", to: "g"), "")
118+
XCTAssertEqual("abcdef".sliced(from: "g", to: "g"), "")
119+
}
120+
121+
func testSlice() {
122+
var string = "abcdef"
123+
string.slice(from: "a", to: "d")
124+
XCTAssertEqual(string, "abcd")
125+
string = "abcdef"
126+
string.slice(from: "a", to: "a")
127+
XCTAssertEqual(string, "a")
128+
string = "abcdef"
129+
string.slice(from: "a", to: "d", inversed: true)
130+
XCTAssertEqual(string, "dcba")
131+
string = "abcdef"
132+
string.slice(from: "a", to: "a", inversed: true)
133+
XCTAssertEqual(string, "a")
134+
string = "abcdef"
135+
string.slice(from: "a", to: "g")
136+
XCTAssertEqual(string, "")
137+
string = "abcdef"
138+
string.slice(from: "g", to: "g")
139+
XCTAssertEqual(string, "")
140+
}
111141
}

0 commit comments

Comments
 (0)