Thanks for using Compiler Explorer
Sponsors
Jakt
C++
Ada
Analysis
Android Java
Android Kotlin
Assembly
C
C3
Carbon
C++ (Circle)
CIRCT
Clean
CMake
CMakeScript
COBOL
C++ for OpenCL
MLIR
Cppx
Cppx-Blue
Cppx-Gold
Cpp2-cppfront
Crystal
C#
CUDA C++
D
Dart
Elixir
Erlang
Fortran
F#
GLSL
Go
Haskell
HLSL
Hook
Hylo
IL
ispc
Java
Julia
Kotlin
LLVM IR
LLVM MIR
Modula-2
Nim
Objective-C
Objective-C++
OCaml
Odin
OpenCL C
Pascal
Pony
Python
Racket
Ruby
Rust
Snowball
Scala
Slang
Solidity
Spice
SPIR-V
Swift
LLVM TableGen
Toit
TypeScript Native
V
Vala
Visual Basic
Vyper
WASM
Zig
Javascript
GIMPLE
Ygen
swift source #1
Output
Compile to binary object
Link to binary
Execute the code
Intel asm syntax
Demangle identifiers
Verbose demangling
Filters
Unused labels
Library functions
Directives
Comments
Horizontal whitespace
Debug intrinsics
Compiler
x86-64 swiftc 3.1.1
x86-64 swiftc 4.0.2
x86-64 swiftc 4.0.3
x86-64 swiftc 4.1
x86-64 swiftc 4.1.1
x86-64 swiftc 4.1.2
x86-64 swiftc 4.2
x86-64 swiftc 5.0
x86-64 swiftc 5.1
x86-64 swiftc 5.10
x86-64 swiftc 5.2
x86-64 swiftc 5.3
x86-64 swiftc 5.4
x86-64 swiftc 5.5
x86-64 swiftc 5.6
x86-64 swiftc 5.7
x86-64 swiftc 5.8
x86-64 swiftc 5.9
x86-64 swiftc nightly
Options
Source code
typealias StringBuffer = UInt16 enum StringGuts { case empty case inline1(payload: UInt8, flags: UInt8) case inline2(payload: (UInt8, UInt8), flags: UInt8) case inline3(payload: (UInt8, UInt8, UInt8), flags: UInt8) case inline4(payload: (UInt8, UInt8, UInt8, UInt8), flags: UInt8) // inline5 has no room for flags // Whether or not that’s a good tradeoff probably goes to someone else to answer. // Making it specifically asciiInline5 might be a good compromise. // On the other hand, if you omit it, you can move the flags out of the enum. case inline5(payload: (UInt8, UInt8, UInt8, UInt8, UInt8)) case owned(count: UInt16, buffer: StringBuffer, flags: UInt8) case unowned(count: UInt16, adjustedAddress: UInt16, flags: UInt8) var flags: UInt8 { switch self { case .empty: return 0b1100_0000 // ASCII and NFC or whatever case .inline1(payload: _, flags: let flags), .inline2(payload: _, flags: let flags), .inline3(payload: _, flags: let flags), .inline4(payload: _, flags: let flags), .owned(count: _, buffer: _, flags: let flags), .unowned(count: _, adjustedAddress: _, flags: let flags): return flags case .inline5: return 0 } } var count: UInt16 { switch self { case .empty: 0 case .inline1: 1 case .inline2: 2 case .inline3: 3 case .inline4: 4 case .inline5: 5 case .owned(count: let count, buffer: _, flags: _): count case .unowned(count: let count, adjustedAddress: _, flags: _): count } } func withUnsafeBytes<T>(do body: (UnsafeRawBufferPointer) throws -> T) rethrows -> T { switch self { case .empty: return try body(UnsafeRawBufferPointer(start: nil, count: 0)) case .inline1(payload: let payload, flags: _): return try Swift.withUnsafeBytes(of: payload, body) case .inline2(payload: let payload, flags: _): return try Swift.withUnsafeBytes(of: payload, body) case .inline3(payload: let payload, flags: _): return try Swift.withUnsafeBytes(of: payload, body) case .inline4(payload: let payload, flags: _): return try Swift.withUnsafeBytes(of: payload, body) case .inline5(payload: let payload): return try Swift.withUnsafeBytes(of: payload, body) case .owned(count: let count, buffer: let buffer, flags: _): _ = buffer return try body(UnsafeRawBufferPointer(start: ("abcdefg" as StaticString).utf8Start, count: Int(count))) case .unowned(count: let count, adjustedAddress: let adjustedAddress, flags: _): _ = adjustedAddress return try body(UnsafeRawBufferPointer(start: ("abcdefg" as StaticString).utf8Start, count: Int(count))) } } } enum StringGutsPadded { case empty(padding: (UInt16, UInt16), flags: UInt8) case inline1(payload: UInt8, padding: (UInt8, UInt8, UInt8), flags: UInt8) case inline2(payload: (UInt8, UInt8), padding: (UInt8, UInt8), flags: UInt8) case inline3(payload: (UInt8, UInt8, UInt8), padding: UInt8, flags: UInt8) case inline4(payload: (UInt8, UInt8, UInt8, UInt8), flags: UInt8) // inline5 has no room for flags // Whether or not that’s a good tradeoff probably goes to someone else to answer. // Making it specifically asciiInline5 might be a good compromise. // On the other hand, if you omit it, you can move the flags out of the enum. case inline5(payload: (UInt8, UInt8, UInt8, UInt8, UInt8)) case owned(count: UInt16, buffer: StringBuffer, flags: UInt8) case unowned(count: UInt16, adjustedAddress: UInt16, flags: UInt8) var flags: UInt8 { switch self { case .empty(padding: _, flags: let flags), .inline1(payload: _, padding: _, flags: let flags), .inline2(payload: _, padding: _, flags: let flags), .inline3(payload: _, padding: _, flags: let flags), .inline4(payload: _, flags: let flags), .owned(count: _, buffer: _, flags: let flags), .unowned(count: _, adjustedAddress: _, flags: let flags): return flags case .inline5: return 0 } } var count: UInt16 { switch self { case .empty: 0 case .inline1: 1 case .inline2: 2 case .inline3: 3 case .inline4: 4 case .inline5: 5 case .owned(count: let count, buffer: _, flags: _): count case .unowned(count: let count, adjustedAddress: _, flags: _): count } } func withUnsafeBytes<T>(do body: (UnsafeRawBufferPointer) throws -> T) rethrows -> T { switch self { case .empty: return try body(UnsafeRawBufferPointer(start: nil, count: 0)) case .inline1(payload: let payload, padding: _, flags: _): return try Swift.withUnsafeBytes(of: payload, body) case .inline2(payload: let payload, padding: _, flags: _): return try Swift.withUnsafeBytes(of: payload, body) case .inline3(payload: let payload, padding: _, flags: _): return try Swift.withUnsafeBytes(of: payload, body) case .inline4(payload: let payload, flags: _): return try Swift.withUnsafeBytes(of: payload, body) case .inline5(payload: let payload): return try Swift.withUnsafeBytes(of: payload, body) case .owned(count: let count, buffer: let buffer, flags: _): _ = buffer return try body(UnsafeRawBufferPointer(start: ("abcdefg" as StaticString).utf8Start, count: Int(count))) case .unowned(count: let count, adjustedAddress: let adjustedAddress, flags: _): _ = adjustedAddress return try body(UnsafeRawBufferPointer(start: ("abcdefg" as StaticString).utf8Start, count: Int(count))) } } } var sizeOfOriginal: Int { MemoryLayout<StringGuts>.size } var sizeOfPadded: Int { MemoryLayout<StringGutsPadded>.size }
Become a Patron
Sponsor on GitHub
Donate via PayPal
Source on GitHub
Mailing list
Installed libraries
Wiki
Report an issue
How it works
Contact the author
CE on Mastodon
CE on Bluesky
About the author
Statistics
Changelog
Version tree