Skip to content

Instantly share code, notes, and snippets.

@bobergj
bobergj / ToolbarItemWithControlValidating.swift
Created December 4, 2020 13:13
ToolbarItemWithControlValidating
// View-based toolbar items do not auto-validate by default, since the toolbar item view may
// be of any type. This subclass validates a toolbar item NSControl view against its action target.
class ToolbarItemWithControlValidating: NSToolbarItem {
override func validate() {
// validate with views view
if let action = self.action {
let validator = NSApp.target(forAction: action, to: self.target, from: self) as AnyObject?
switch validator {
case let validator as NSToolbarItemValidation:
isEnabled = validator.validateToolbarItem(self)

Proposed solution

Add a Unicode.Scalar.ASCII type (another alternative naming/namespace is Unicode.ASCIIScalar). This type represents the ASCII compatible subset of Unicode.Scalar.

Until something like compile time constant expressions and compile time asserts (static asserts) lands in the language, the compiler would have a special range check for this type:

let s: Unicode.Scalar.ASCII = 'a' // OK
let s: Unicode.Scalar.ASCII = 'ÿ' // error: ÿ is not in the ASCII compatible subset of unicode 
@bobergj
bobergj / main.cpp
Created November 8, 2017 14:05
Undefined behaviour in CGAL::internal::vector
// Compile and run as follows:
// $ clang++ main.cpp -I<cgal-and-boost-include-path> -L<cgal-library-path> -lCGAL -fsanitize=undefined
// $ ./a.out
// CGAL/vector.h:75:57: runtime error: reference binding to null pointer of type 'int'
//
#include <CGAL/vector.h>
int main() {
CGAL::internal::vector<int> foo;
std::cout << foo.size() << std::endl;
@bobergj
bobergj / main.cpp
Created November 8, 2017 12:28
CGAL::random_polygon_2 infinite loop
#define CGAL_DONT_SHUFFLE_IN_RANDOM_POLYGON_2
#include <CGAL/Exact_predicates_exact_constructions_kernel.h>
#include <CGAL/Polygon_2.h>
#include <CGAL/point_generators_2.h>
#include <CGAL/random_polygon_2.h>
#include <vector>
typedef CGAL::Exact_predicates_exact_constructions_kernel K;