Skip to content

Instantly share code, notes, and snippets.

View krzyzanowskim's full-sized avatar

Marcin Krzyzanowski krzyzanowskim

View GitHub Profile
@krzyzanowskim
krzyzanowskim / newline.swift
Last active July 10, 2021 20:51
In Swift \r\n is a single Character, just like \n
let str = "dupa\r\nblada"
for i in str.indices {
switch str[i] {
case "\n":
print("\\n found") // \n not found
case "\r":
print("\\r found") // \r not found
case "\r\n":
print("\\r\\n found") // found
default:
@krzyzanowskim
krzyzanowskim / TrieAutocomplete.swift
Last active June 29, 2021 17:39
Trie autocomplete
import Foundation
/*
D
O
O
G R
S
@krzyzanowskim
krzyzanowskim / BOM.swift
Created May 22, 2021 18:29
one bom to rule them all
let bom: String = "\u{feff}" // The character \uFEFF is the BOM character for all UTFs (8, 16 LE and 16 BE)
let str = "abc😂"
str.count // 4
str.utf16.count // 5
str.utf8.count // 7
import Foundation
(str as NSString).length // 5
(str as NSString).lengthOfBytes(using: String.Encoding.utf8.rawValue) // 7
@krzyzanowskim
krzyzanowskim / class_Equatable.swift
Created January 14, 2021 19:24
Swift class Equatable need check parent class
class A: Equatable {
let name: String
init(name: String) {
self.name = name
}
static func ==(lhs: A, rhs: A) -> Bool {
return lhs.name == rhs.name
}
import Foundation
class Node: Equatable, CustomStringConvertible {
let value: Int
var children: [Node]
init(value: Int, children: [Node]) {
self.value = value
self.children = children
}
@krzyzanowskim
krzyzanowskim / myview.swift
Last active October 24, 2020 10:16
FB8820682: Since when NSView.setNeedsDisplayInRect does not affects drawRect rectangle
// macOS 11.0 (20A5395g)
// Xcode 12.2 beta 3 (12B5035g)
class MyView: NSView {
override init(frame frameRect: NSRect) {
super.init(frame: frameRect)
DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(1)) {
let rect = NSRect(x: 10, y: 10, width: 10, height: 10)
// https://github.com/apple/swift-algorithms
import Algorithms
extension String {
func lineChunks() -> [Self.SubSequence] {
chunked {
!(
($0.isNewline && !$1.isNewline) || ($0.isNewline && $1.isNewline) && !($0 == "\r" && $1 == "\n")
)
}
@krzyzanowskim
krzyzanowskim / .zshrc
Created July 31, 2020 11:37
Close Xcode on git checkout
# .zshrc
# Close Xcode on git checkout
git() {
if [[ $1 == "checkout" ]]; then
osascript -e 'quit app "Xcode"'
command git "$@";
else
command git "$@";
fi;
}
private struct PopUpButton: NSViewRepresentable {
@Binding var selection: String
var content: [String]
func makeNSView(context: Context) -> NSPopUpButton {
let button = NSPopUpButton()
button.imagePosition = .imageLeading
button.usesSingleLineMode = true
button.autoenablesItems = false