Skip to content

Instantly share code, notes, and snippets.

@masters3d
Created December 4, 2015 19:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save masters3d/7b0c6f3653368afe4357 to your computer and use it in GitHub Desktop.
Save masters3d/7b0c6f3653368afe4357 to your computer and use it in GitHub Desktop.
//Filter.swift
public mutating func next() -> Base.Element? {
var n: Base.Element?
for/*ever*/;; {
n = _base.next()
if n != nil ? _predicate(n!) : true {
return n
}
// HeapBuffer.swift
static func _requiredAlignMask() -> Int {
// We can't use max here because it can allocate an array.
let heapAlign = alignof(_HeapObject.self) &- 1
let valueAlign = alignof(Value.self) &- 1
let elementAlign = alignof(Element.self) &- 1
return (heapAlign < valueAlign
? (valueAlign < elementAlign ? elementAlign : valueAlign)
: (heapAlign < elementAlign ? elementAlign : heapAlign))
}
// Index.swift
@warn_unused_result
public func advancedBy(n: Distance, limit: Self) -> Self {
let d = self.distanceTo(limit)
if d == 0 || (d > 0 ? d <= n : d >= n) {
return limit
}
return self.advancedBy(n)
}
// Reflection.swift
let bullet = count == 0 ? "-"
: maxDepth <= 0 ? "▹" : "▿"
print("\(bullet) ", terminator: "", toStream: &targetStream)
// Stride.swift
public mutating func next() -> Element? {
if done {
return nil
}
if stride > 0 ? current >= end : current <= end {
if current == end {
done = true
return current
}
return nil
}
let ret = current
current += stride
return ret
}
}
// StringBridge.swift
self._core = _StringCore(
baseAddress: COpaquePointer(start),
count: length,
elementShift: isUTF16 ? 1 : 0,
hasCocoaBuffer: true,
owner: unsafeBitCast(cfImmutableValue, Optional<AnyObject>.self))
// UnicodeScalar
subscript(position: Int) -> UTF16.CodeUnit {
return position == 0 ? (
endIndex == 1 ? UTF16.CodeUnit(value.value) : UTF16.leadSurrogate(value)
) : UTF16.trailSurrogate(value)
}
}
// StringCore.swift
_StringBuffer(
capacity: newCount,
initialSize: 0,
elementWidth:
width == 1 ? 1
: representableAsASCII() && !newElements.contains { $0 > 0x7f } ? 1
: 2
)
//
let width = elementWidth == 2 || newElements.contains { $0 > 0x7f } ? 2 : 1
//
let minElementWidth
= elementWidth >= rhs.elementWidth
? elementWidth
: rhs.representableAsASCII() ? 1 : 2
//
let newElementWidth =
minElementWidth >= elementWidth
? minElementWidth
: representableAsASCII() ? 1 : 2
//
self._countAndFlags
= (UInt(elementShift) << (UInt._sizeInBits - 1))
| ((hasCocoaBuffer ? 1 : 0) << (UInt._sizeInBits - 2))
| UInt(count)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment