Skip to content

Commit 5be350a

Browse files
committed
stdlib: add a semantic attribute on the String.utf8CString and define it as "readonly"
Needed for StringOptimization.
1 parent 78a7887 commit 5be350a

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

include/swift/AST/SemanticAttrs.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
SEMANTICS_ATTR(STRING_EQUALS, "string.equals")
2828
SEMANTICS_ATTR(STRING_MAKE_UTF8, "string.makeUTF8")
29+
SEMANTICS_ATTR(STRING_GET_UTF8_CSTRING, "string.getUTF8CString")
2930
SEMANTICS_ATTR(STRING_ESCAPE_PERCENT_GET, "string.escapePercent.get")
3031
SEMANTICS_ATTR(STRING_CONCAT, "string.concat")
3132
SEMANTICS_ATTR(STRING_APPEND, "string.append")

stdlib/public/core/StringUTF8View.swift

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -251,13 +251,16 @@ extension String {
251251
/// }
252252
/// // Prints "6"
253253
public var utf8CString: ContiguousArray<CChar> {
254-
if _fastPath(_guts.isFastUTF8) {
255-
var result = _guts.withFastCChar { ContiguousArray($0) }
256-
result.append(0)
257-
return result
258-
}
254+
@_effects(readonly) @_semantics("string.getUTF8CString")
255+
get {
256+
if _fastPath(_guts.isFastUTF8) {
257+
var result = _guts.withFastCChar { ContiguousArray($0) }
258+
result.append(0)
259+
return result
260+
}
259261

260-
return _slowUTF8CString()
262+
return _slowUTF8CString()
263+
}
261264
}
262265

263266
@usableFromInline @inline(never) // slow-path

0 commit comments

Comments
 (0)