Skip to content

Commit 6f240e0

Browse files
committed
cr feedback: refactor switch statement
1 parent 568b24b commit 6f240e0

File tree

1 file changed

+34
-60
lines changed

1 file changed

+34
-60
lines changed

stdlib/public/core/KeyPath.swift

Lines changed: 34 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -3744,10 +3744,10 @@ fileprivate func dynamicLibraryAddress<Base, Leaf>(
37443744

37453745
@available(SwiftStdLib 5.7, *)
37463746
extension AnyKeyPath: CustomDebugStringConvertible {
3747-
3747+
37483748
@available(SwiftStdLib 5.7, *)
37493749
public var debugDescription: String {
3750-
#if SWIFT_ENABLE_REFLECTION
3750+
#if SWIFT_ENABLE_REFLECTION
37513751
var description = "\\\(String(describing: Self.rootType))"
37523752
return withBuffer {
37533753
var buffer = $0
@@ -3759,83 +3759,57 @@ extension AnyKeyPath: CustomDebugStringConvertible {
37593759
let (rawComponent, optNextType) = buffer.next()
37603760
let hasEnded = optNextType == nil
37613761
let nextType = optNextType ?? Self.valueType
3762-
func name(for offset: Int) -> String {
3762+
switch rawComponent.value {
3763+
case .optionalForce, .optionalWrap, .optionalChain:
3764+
break
3765+
default:
3766+
description.append(".")
3767+
}
3768+
switch rawComponent.value {
3769+
case .class(let offset),
3770+
.struct(let offset):
37633771
let count = _getRecursiveChildCount(valueType)
3764-
for i in 0..<count {
3765-
if _getChildOffset(
3766-
valueType,
3767-
index: i
3768-
) == offset {
3769-
var field = _FieldReflectionMetadata()
3770-
_ = _getChildMetadata(
3772+
let index = (0..<count)
3773+
.first(where: { i in
3774+
_getChildOffset(
37713775
valueType,
3772-
index: i,
3773-
fieldMetadata: &field
3774-
)
3775-
defer {
3776-
field.freeFunc?(field.name)
3777-
}
3778-
return String(cString: field.name)
3776+
index: i
3777+
) == offset
3778+
})
3779+
if let index = index {
3780+
var field = _FieldReflectionMetadata()
3781+
_ = _getChildMetadata(
3782+
valueType,
3783+
index: index,
3784+
fieldMetadata: &field
3785+
)
3786+
defer {
3787+
field.freeFunc?(field.name)
37793788
}
3789+
description.append(String(cString: field.name))
3790+
} else {
3791+
description.append("<offset \(offset) (\(nextType))>")
37803792
}
3781-
return "<offset \(offset) (\(nextType))>"
3782-
}
3783-
func name(
3784-
for pointer: ComputedAccessorsPtr,
3785-
base: Any.Type,
3786-
leaf: Any.Type
3787-
) -> String {
3793+
case .get(_, let accessors, _),
3794+
.nonmutatingGetSet(_, let accessors, _),
3795+
.mutatingGetSet(_, let accessors, _):
37883796
func project<Base>(base: Base.Type) -> String {
37893797
func project2<Leaf>(leaf: Leaf.Type) -> String {
37903798
dynamicLibraryAddress(
3791-
of: pointer,
3799+
of: accessors,
37923800
base,
37933801
leaf
37943802
)
37953803
}
37963804
return _openExistential(nextType, do: project2)
37973805
}
3798-
return _openExistential(base, do: project)
3799-
}
3800-
switch rawComponent.value {
3801-
case .optionalForce, .optionalWrap, .optionalChain:
3802-
break
3803-
default:
3804-
description.append(".")
3805-
}
3806-
switch rawComponent.value {
3807-
case .class(let offset):
3808-
description.append(name(for: offset))
3809-
case .get(_, let accessors, _):
38103806
description.append(
3811-
name(
3812-
for: accessors,
3813-
base: valueType,
3814-
leaf: nextType
3815-
)
3816-
)
3817-
case .mutatingGetSet(_, let accessors, _):
3818-
description.append(
3819-
name(
3820-
for: accessors,
3821-
base: valueType,
3822-
leaf: nextType
3823-
)
3824-
)
3825-
case .nonmutatingGetSet(_, let accessors, _):
3826-
description.append(
3827-
name(
3828-
for: accessors,
3829-
base: valueType,
3830-
leaf: nextType
3831-
)
3807+
_openExistential(valueType, do: project)
38323808
)
38333809
case .optionalChain, .optionalWrap:
38343810
description.append("?")
38353811
case .optionalForce:
38363812
description.append("!")
3837-
case .struct(let offset):
3838-
description.append(name(for: offset))
38393813
}
38403814
if hasEnded {
38413815
break

0 commit comments

Comments
 (0)