Skip to content

Instantly share code, notes, and snippets.

View hechen's full-sized avatar
🎯
Focusing

CHEN hechen

🎯
Focusing
View GitHub Profile
func toggleDock2(show: Bool) -> Bool {
 return show ? NSApp.setActivationPolicy(.regular) : NSApp.setActivationPolicy(.accessory)
 }
@hechen
hechen / NSStatusItem.swift
Last active May 4, 2019 11:12
NSStatusItem
let statusItem = NSStatusBar.system.statusItem(withLength:NSStatusItem.squareLength)
if let button = statusItem.button {
 button.image = NSImage(named:NSImage.Name("ic_dock"))
 button.action = #selector(doWhatYouWantToDo(_:))
}
@hechen
hechen / performanceOnMac.html
Last active September 13, 2019 16:53
Performance Test Result
<table>
<tr>
<td>Name</td>
<td>Iterations</td>
<td>Total time (sec)</td>
<td>Time per (ns)</td>
</tr>
<tr>
<td>16 byte memcpy</td>
<td>1000000000</td>
@hechen
hechen / CPU Usage.m
Created September 15, 2019 16:21
How to collect the usage of CPU. Collect the usage of each thread.
- (integer_t)checkCPUUsage {
thread_act_array_t threads;
mach_msg_type_number_t threadCount = 0;
const task_t thisTask = mach_task_self_;
kern_return_t ret = task_threads(thisTask, &threads, &threadCount);
if (ret != KERN_SUCCESS) {
return 0;
}
@hechen
hechen / split string into vector.cpp
Created October 15, 2019 09:04
C++ dose not support split function. Use stringstream.
template<typename T>
void split(const std::string &s, char delim, T output) {
std::istringstream iss(s);
std::string item;
while(getline(iss, item, delim)) {
*output++ = item;
}
}
std::vector<std::string> split(const std::string &s, char delim) {
std::vector<std::string> result;
//
// YTBParser.swift
// SideloadExt
//
// Created by chen he on 2020/7/30.
// Copyright © 2020 chen he. All rights reserved.
//
import Foundation
@hechen
hechen / String's regex extension
Created February 25, 2021 12:26
String's regex matches in Swift
extension String {
func matches(for regexPattern: String) -> [[String]] {
do {
let text = self
let regex = try NSRegularExpression(pattern: regexPattern)
let matches = regex.matches(in: text,
range: NSRange(text.startIndex..., in: text))
return matches.map { match in
return (0..<match.numberOfRanges).map {
let rangeBounds = match.range(at: $0)
@hechen
hechen / string trim in cpp
Created October 14, 2021 19:25
Add trim support for c++ string
const std::string WHITESPACE = " \n\r\t\f\v";
std::string ltrim(const std::string &s) {
size_t start = s.find_first_not_of(WHITESPACE);
return (start == std::string::npos) ? "" : s.substr(start);
}
std::string rtrim(const std::string &s) {
size_t end = s.find_last_not_of(WHITESPACE);
return (end == std::string::npos) ? "" : s.substr(0, end + 1);
}
std::string trim(const std::string &s) {
struct PairHash {
template<class T1, class T2>
std::size_t operator()(std::pair<T1, T2> const & p) const noexcept {
std::size_t hash1 = std::hash<T1>{}(p.first);
std::size_t hash2 = std::hash<T2>{}(p.second);
return hash1 ^ (hash2 << 1);
};
};
// MARK - Heap
public struct Heap<Element: Equatable> {
var elements: [Element]
let sort: (Element, Element) -> Bool
public init(sort: @escaping (Element, Element) -> Bool, elements: [Element] = []) {
self.sort = sort
self.elements = elements
// heapify