If a post contains source code that is too long (vertically) to be visible without being scrolled, I can't select (and copy) the entire source code.
Perhaps I'm missing something obvious?
(Ie, for example, how do I select and copy the following 102 lines of code:
import AppKit
extension String {
enum PaddingMethod { case left, right }
func padded(_ m: PaddingMethod, to w: Int, with c: Character) -> String {
let n = w - count
guard n > 0 else { return self }
switch m {
case .left: return String(repeating: c, count: n) + self
case .right: return self + String(repeating: c, count: n)
}
}
}
extension FixedWidthInteger where Self: UnsignedInteger {
var binaryString : String {
return String(self, radix: 2)
.padded(.left, to: MemoryLayout<Self>.stride * 8, with: "0")
}
var hexString : String {
return String(self, radix: 16)
.padded(.left, to: MemoryLayout<Self>.stride * 2, with: "0")
}
}
extension BinaryFloatingPoint where
RawExponent: FixedWidthInteger,
RawSignificand: FixedWidthInteger
{
var binaryStringInSections : String {
let signBit: String
switch self.sign {
case .plus: signBit = "0"
case .minus: signBit = "1"
}
let exponentBits = String(exponentBitPattern, radix: 2)
.padded(.left, to: Self.exponentBitCount, with: "0")
let significandBits = String(significandBitPattern, radix: 2)
.padded(.left, to: Self.significandBitCount, with: "0")
return signBit + "_" + exponentBits + "_" + significandBits
}
}
extension Array {
mutating func unsafeSetElementsToRandomBitPatterns() {
let byteCount = count * MemoryLayout<Element>.stride
let r = SecRandomCopyBytes(nil, byteCount, &self)
precondition(r == errSecSuccess)
}
}
extension NSBitmapImageRep {
struct Pixel { var a, r, g, b: UInt8 }
convenience init(width: Int, height: Int) {
self.init(bitmapDataPlanes: nil, pixelsWide: width, pixelsHigh: height,
bitsPerSample: 8, samplesPerPixel: 4, hasAlpha: true,
isPlanar: false, colorSpaceName: .deviceRGB,
bitmapFormat: .alphaFirst, bytesPerRow: 0, bitsPerPixel: 0)!
}
func withEachPixel(_ op: (_ x: Int, _ y: Int, _ px: inout Pixel) -> ()) {
let data = UnsafeMutableRawPointer(self.bitmapData!)
for y in 0 ..< pixelsHigh {
for x in 0 ..< pixelsWide {
let offset = x &* samplesPerPixel &+ y &* bytesPerRow
var px = data.load(fromByteOffset: offset, as: Pixel.self)
op(x, y, &px)
data.storeBytes(of: px, toByteOffset: offset, as: Pixel.self)
}
}
}
func withCurrentGraphicsContext(_ draw: (_ bounds: NSRect) -> ()) {
NSGraphicsContext.saveGraphicsState()
let ctx = NSGraphicsContext.init(bitmapImageRep: self)
NSGraphicsContext.current = ctx
draw(NSRect(origin: .zero, size:
NSSize(width: pixelsWide, height: pixelsHigh)))
NSGraphicsContext.restoreGraphicsState()
}
func save(url: URL, fileType: FileType, properties: [PropertyKey : Any]) {
let data = representation(using: fileType, properties: properties)!
try! data.write(to: url, options: .atomic)
}
}
extension Collection where Element == (NSBitmapImageRep, Double) {
func saveAsAnimatedGIF(url: URL) {
let fileProperties = [kCGImagePropertyGIFDictionary as String:
[kCGImagePropertyGIFLoopCount as String: 0]] as CFDictionary?
guard let destination = CGImageDestinationCreateWithURL(
url as CFURL, kUTTypeGIF, count, nil)
else { preconditionFailure("Can't save animated GIF as \(url)") }
CGImageDestinationSetProperties(destination, fileProperties)
for (bitmap, delay) in self {
let gifProps = [
kCGImagePropertyGIFDictionary as String:
[kCGImagePropertyGIFDelayTime as String: delay]
] as CFDictionary?
CGImageDestinationAddImage(destination, bitmap.cgImage!, gifProps)
}
precondition(CGImageDestinationFinalize(destination))
}
}
)