Skip to content

Instantly share code, notes, and snippets.

@lstomberg
Last active September 8, 2018 16:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lstomberg/a6d919f91760a904a8b32f0890c7b091 to your computer and use it in GitHub Desktop.
Save lstomberg/a6d919f91760a904a8b32f0890c7b091 to your computer and use it in GitHub Desktop.
Find all protocols defined in Swift source from apple/swift
# Get the source
git clone https://github.com/apple/swift.git
cd swift
# 1.
# find all lines that have protocol as one of the first 2 words, strip characters '{' and '}' to clean up the line
grep -rh -E "^[^\/ ]* ?protocol " --exclude-dir={.git,.github,unittests,validation-test,test,docs,cmake,apinotes,benchmark} . | sed 's/[{}]//g'
# 2.
# To get just the protocol name and inheritance, additionally strip all characters preceeding and including 'protocol '
# <regex1> | sed 's/^.*protocol //' | sort
grep -rh -E "^[^\/ ]* ?protocol " --exclude-dir={.git,.github,unittests,validation-test,test,docs,cmake,apinotes,benchmark} . | sed 's/[{}]//g;s/^.*protocol //' | sort
# 3.
# List just the protocols' name. Strip any inheritance and instances of :
# <regex2> | awk '{ print $1 }'
grep -rh -E "^[^\/ ]* ?protocol " --exclude-dir={.git,.github,unittests,validation-test,test,docs,cmake,apinotes,benchmark} . | sed 's/[{}]//g;s/^.*protocol //;s/://' | sort | awk '{ print $1 }'
# 4.
# Strip protocols beginning with _ using grep -v "^_"
grep -rh -E "^[^\/ ]* ?protocol " --exclude-dir={.git,.github,unittests,validation-test,test,docs,cmake,apinotes,benchmark} . | sed 's/[{}]//g;s/^.*protocol //;s/://' | grep -v "^_" | sort | awk '{ print $1 }'
# 4.
# Sort the protocols by length of name, from biggest to smallest. This will allow us to search the sourcecode for instances of each string without finding duplicates.
# Similar to regex 3 but print out "length(name) name" and move sort after awk, then output column 2 (the name) again
grep -rh -E "^[^\/ @]* ?protocol " --exclude-dir={.git,.github,unittests,validation-test,test,docs,cmake,apinotes,benchmark} . | sed 's/[{}]//g;s/^.*protocol //;s/://' | grep -v "^_" | awk '{print length($1), $1}' | sort -nrk 1 | awk '{print $2}'
# Output: 75
ExpressibleByExtendedGraphemeClusterLiteral
CustomPlaygroundDisplayConvertible
ExpressibleByUnicodeScalarLiteral
NSKeyValueObservingCustomization
KeyedEncodingContainerProtocol
KeyedDecodingContainerProtocol
ExpressibleByDictionaryLiteral
SingleValueEncodingContainer
SingleValueDecodingContainer
CustomDebugStringConvertible
CKObjCRecordValueConvertible
ExpressibleByIntegerLiteral
ExpressibleByBooleanLiteral
RangeReplaceableCollection
ExpressibleByStringLiteral
LosslessStringConvertible
ExpressibleByFloatLiteral
ExpressibleByArrayLiteral
UnkeyedEncodingContainer
UnkeyedDecodingContainer
RaceTestWithPerTrialData
CKRecordValueConvertible
ExpressibleByNilLiteral
CustomStringConvertible
BidirectionalCollection
RandomAccessCollection
LazyCollectionProtocol
RandomNumberGenerator
CustomLeafReflectable
CKRecordValueProtocol
TextOutputStreamable
ReferenceConvertible
LazySequenceProtocol
BinaryFloatingPoint
MutableCollection
FixedWidthInteger
CustomReflectable
TextOutputStream
RecoverableError
RawRepresentable
IteratorProtocol
UnsignedInteger
RangeExpression
StringProtocol
LocalizedError
SignedNumeric
SignedInteger
FloatingPoint
CustomNSError
BinaryInteger
ArrayProtocol
UnicodeCodec
CaseIterable
LoggingType
Strideable
SetAlgebra
Resettable
MirrorPath
Comparable
Collection
OptionSet
IPAddress
Equatable
Encodable
Decodable
CodingKey
Sequence
Hashable
Wrapper
Numeric
Encoder
Decoder
CVarArg
Error
P
# Output beginning with _: 79
_ExpressibleByBuiltinUTF16ExtendedGraphemeClusterLiteral
_ExpressibleByBuiltinExtendedGraphemeClusterLiteral
_ExpressibleByBuiltinConstUTF16StringLiteral
_ExpressibleByBuiltinUnicodeScalarLiteral
_ExpressibleByBuiltinUTF16StringLiteral
_ExpressibleByBuiltinConstStringLiteral
__DefaultCustomPlaygroundQuickLookable
_JSONStringDictionaryEncodableMarker
_JSONStringDictionaryEncodableMarker
_JSONStringDictionaryDecodableMarker
_JSONStringDictionaryDecodableMarker
_HasCustomAnyHashableRepresentation
_ExpressibleByBuiltinIntegerLiteral
_ExpressibleByBuiltinBooleanLiteral
_ExpressibleByFileReferenceLiteral
_ExpressibleByBuiltinStringLiteral
_ExpressibleByStringInterpolation
_ExpressibleByBuiltinFloatLiteral
_AppKitKitNumericRawRepresentable
_CustomPlaygroundQuickLookable
_UIKitNumericRawRepresentable
_RuntimeFunctionCountersStats
_KeyValueCodingAndObserving
_ObjectiveCBridgeableError
_ExpressibleByImageLiteral
_ExpressibleByColorLiteral
_ArrayAnyHashableProtocol
_SwiftGKStateMachineLike
_INIntentSetImageKeyPath
_DestructorSafeContainer
_CVarArgPassedAsDouble
_CGColorInitTrampoline
_AnyCollectionProtocol
_ObjectiveCBridgeable
_BridgedStoredNSError
_SwiftNewtypeWrapper
_ArrayBufferProtocol
_HasContiguousBytes
_NSFastEnumeration
_HeapBufferHeader_
_ErrorCodeProtocol
_BitwiseOperations
_NSDictionaryCore
_NSDictionaryCore
_DictionaryBuffer
_UnicodeEncoding
_SwiftStringView
_SequenceWrapper
_MinimalIterator
_ShadowProtocol
_CVarArgAligned
_BridgedNSError
_AnyHashableBox
_UnicodeParser
_StringVariant
_StringElement
_SegmentSource
_MutableBoxing
_AppendKeyPath
_OpaqueString
_NSSwiftValue
_NSStringCore
_NSStringCore
_NSEnumerator
_NSDictionary
_Unwrappable
_NSArrayCore
_NSArrayCore
_AnyIndexBox
_HasherCore
_UTFParser
_SetBuffer
_NSSetCore
_NSSetCore
_NSCopying
_NSNumber
_CFObject
_Pointer
_NSSet
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment