Skip to content

Commit 966b1cc

Browse files
authored
SE-0498: clarify state of output span after truncated demangle (#3051)
1 parent 85fb937 commit 966b1cc

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

proposals/0498-runtime-demangle.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,9 @@ The span accepting API is necessary for performance sensitive use-cases, which a
5555

5656
The output from this API is an `OutputSpan` of `UTF8.CodeUnit`s, and it may not necessarily be well-formed UTF8, because of the potential of truncation happening between two code units which would render the UTF8 invalid.
5757

58-
If the demangled representation does not fit the preallocated buffer, the demangle method will return `truncated(actualSize)` such that developers can determine by how much the buffer might need to be increased to handle the complete demangling.
58+
If the demangled representation does not fit the preallocated buffer, the demangle method will return `truncated(actualSize)` such that developers can determine by how much the buffer might need to be increased to handle the complete demangling. When `.truncated` is returned, the `OutputSpan` will contain a _partial result_, of however many characters were able to fit into it before truncation ocurred. This also means that a truncated output may not be entirely valid UTF8.
5959

60-
To construct an `UTF8Span` or valid `String` from the `OutputSpan` you can do the following:
60+
Converting the outputSpan to a `String`, which is guarateed to be valid UTF8, follows the below pattern, where creating an `UTF8Span` _will_ perform validation of the UTF8 String contents, and fail if the output wasn't correct.
6161

6262
```swift
6363
var demangledOutputSpan: OutputSpan<UTF8.CodeUnit> = ...
@@ -69,6 +69,8 @@ if demangle("$sSG", into: &demangledOutputSpan) == .success {
6969
}
7070
```
7171

72+
As this example shows, the path to constructing a String is therefore safe, and we don't want to perform validation earlier during `demangle`, because you may want to store the exact bytes that were returned for some future processing.
73+
7274
### Demangling format
7375

7476
While the mangled strings are part of Swift ABI and can therefore not really change on platforms with stable ABI, the demangled representation returned by the `demangle` functions is _not guaranteed to be stable in any way_.

0 commit comments

Comments
 (0)