Skip to content

Commit 039c4d6

Browse files
jmschonfeldkarwa
andauthored
Efficient buffer size calculation for String file system representation
* Use smaller buffer for file system representation * Reword scalars -> code-units Co-authored-by: Karl <5254025+karwa@users.noreply.github.com> --------- Co-authored-by: Karl <5254025+karwa@users.noreply.github.com>
1 parent f99560e commit 039c4d6

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

Sources/FoundationEssentials/String/String+Internals.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,11 @@ extension String {
9090
}
9191

9292
private var maxFileSystemRepresentationSize: Int {
93-
// TODO: Likely an over estimate and slow for non-UTF16 strings, we should investigate to see if it can be smaller and computed more quickly, especially if we know the string is ASCII
94-
self.utf16.count * 9 + 1
93+
// The Darwin file system representation expands the UTF-8 contents to decomposed UTF-8 contents (only decomposing specific scalars)
94+
// For any given scalar that we decompose, we will increase its UTF-8 length by at most a factor of 3 during decomposition
95+
// (ex. U+0390 expands from 2 to 6 UTF-8 code-units, U+1D160 expands from 4 to 12 UTF-8 code-units)
96+
// Therefore in the worst case scenario, the result will be the UTF-8 length multiplied by a factor of 3 plus an additional byte for the null byte
97+
self.utf8.count * 3 + 1
9598
}
9699
#endif
97100

Tests/FoundationEssentialsTests/StringTests.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,13 @@ final class StringTests : XCTestCase {
370370
Array(repeating: "A", count: Int(PATH_MAX)).joined().withFileSystemRepresentation { ptr in
371371
XCTAssertNotNil(ptr)
372372
}
373+
374+
// The buffer should fit the scalars that expand the most during decomposition
375+
for string in ["\u{1D160}", "\u{0CCB}", "\u{0390}"] {
376+
string.withFileSystemRepresentation { ptr in
377+
XCTAssertNotNil(ptr, "Could not create file system representation for \(string.debugDescription)")
378+
}
379+
}
373380
#endif
374381
}
375382

0 commit comments

Comments
 (0)