Skip to content

Commit 316b9d8

Browse files
authored
Make a micro optimization for whitespace trimming of strings (#945)
1 parent 9e212e5 commit 316b9d8

File tree

2 files changed

+32
-14
lines changed

2 files changed

+32
-14
lines changed

Sources/SwiftDocC/Model/Rendering/Content/Extensions/RenderTermLists.swift

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
This source file is part of the Swift.org open source project
33

4-
Copyright (c) 2021 Apple Inc. and the Swift project authors
4+
Copyright (c) 2021-2024 Apple Inc. and the Swift project authors
55
Licensed under Apache License v2.0 with Runtime Library Exception
66

77
See https://swift.org/LICENSE.txt for license information
@@ -286,22 +286,20 @@ extension RenderInlineContent {
286286
}
287287

288288
extension String {
289-
290-
/// The result of removing whitespace from the beginning of the string.
289+
/// Returns a new string made by removing whitespace from the beginning of the string.
291290
func removingLeadingWhitespace() -> String {
292-
var trimmedString = self
293-
while trimmedString.first?.isWhitespace == true {
294-
trimmedString = String(trimmedString.dropFirst())
291+
guard let index = self.firstIndex(where: { !$0.isWhitespace }) else { return "" }
292+
if index == startIndex {
293+
// Avoid a potential copy if the string doesn't have any leading whitespace.
294+
return self
295295
}
296-
return trimmedString
296+
return String(self[index...])
297297
}
298298

299-
/// The result of removing whitespace from the end of the string.
299+
/// Returns a new string made by removing whitespace from the end of the string.
300300
func removingTrailingWhitespace() -> String {
301-
var trimmedString = self
302-
while trimmedString.last?.isWhitespace == true {
303-
trimmedString = String(trimmedString.dropLast())
304-
}
305-
return trimmedString
301+
guard let index = self.lastIndex(where: { !$0.isWhitespace }) else { return "" }
302+
// It's not worth checking if the index is at the end because `index(before: endIndex)` is slower than not checking.
303+
return String(self[...index])
306304
}
307305
}

Tests/SwiftDocCTests/Utility/String+WhitespaceTests.swift

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
This source file is part of the Swift.org open source project
33

4-
Copyright (c) 2021 Apple Inc. and the Swift project authors
4+
Copyright (c) 2021-2024 Apple Inc. and the Swift project authors
55
Licensed under Apache License v2.0 with Runtime Library Exception
66

77
See https://swift.org/LICENSE.txt for license information
@@ -37,5 +37,25 @@ class String_WhitespaceTests: XCTestCase {
3737
XCTAssertEqual("Words: with, various. punctuation! as - separators".replacingWhitespaceAndPunctuation(with: "-"),
3838
"Words-with-various-punctuation-as-separators")
3939
}
40+
41+
func testTrimmingWhitespace() {
42+
XCTAssertEqual("".removingLeadingWhitespace(), "")
43+
XCTAssertEqual("".removingTrailingWhitespace(), "")
44+
45+
XCTAssertEqual("ABC".removingLeadingWhitespace(), "ABC")
46+
XCTAssertEqual("ABC".removingTrailingWhitespace(), "ABC")
47+
48+
XCTAssertEqual("\t ABC\t ".removingLeadingWhitespace(), "ABC\t ", "Removing leading whitespace doesn't remove trailing whitespace")
49+
XCTAssertEqual("\t ABC\t ".removingTrailingWhitespace(), "\t ABC", "Removing trailing whitespace doesn't remove leading whitespace")
50+
51+
XCTAssertEqual("\t ".removingLeadingWhitespace(), "", "All characters are whitespace")
52+
XCTAssertEqual("\t ".removingTrailingWhitespace(), "", "All characters are whitespace")
53+
54+
XCTAssertEqual("\n ABC \n".removingLeadingWhitespace(), "ABC \n", "Newline is considered a whitespace character")
55+
XCTAssertEqual("\n ABC \n".removingTrailingWhitespace(), "\n ABC", "Newline is considered a whitespace character")
56+
57+
XCTAssertEqual("start ABC end".removingLeadingWhitespace(), "start ABC end", "Only removes whitespace at the start and end of the string")
58+
XCTAssertEqual("start ABC end".removingTrailingWhitespace(), "start ABC end", "Only removes whitespace at the start and end of the string")
59+
}
4060
}
4161

0 commit comments

Comments
 (0)