Skip to content

Instantly share code, notes, and snippets.

View hnakamur's full-sized avatar

Hiroaki Nakamura hnakamur

View GitHub Profile
@hnakamur
hnakamur / main.go
Last active February 12, 2023 00:15
A go example to stop a worker goroutine when Ctrl-C is pressed (MIT License)
package main
import (
"fmt"
"os"
"os/signal"
"time"
"golang.org/x/net/context"
)
@hnakamur
hnakamur / ilog2_test.c
Created January 4, 2023 12:52
Test ilog2
#include <math.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <gmp.h>
#include <mpfr.h>
int ilog2(uint64_t x) { return 63 - __builtin_clzll(x); }
@hnakamur
hnakamur / mpfr_log2.c
Last active January 4, 2023 12:51
Calculate log2(1<<49 - 1) in a higher precision than double.
#include <stdio.h>
#include <stdlib.h>
#include <gmp.h>
#include <mpfr.h>
int main (void)
{
mpfr_t x, y;
@hnakamur
hnakamur / modify_private_field.go
Created September 21, 2013 07:42
unsafeでポインタを使って構造体のプライベートフィールドの値を書き換える例。 unsafeを使っているので http://play.golang.org/ では試せず、ローカルで実行する必要があります。
package main
import (
"fmt"
"unsafe"
)
/*
void modifyN(void* p, int n) {
*(int *)p = n;
@hnakamur
hnakamur / main.go
Last active October 11, 2022 04:18
Validate certificate key pair and hostname in Go
package main
import (
"crypto/tls"
"crypto/x509"
"flag"
"fmt"
"log"
"golang.org/x/net/idna"
@hnakamur
hnakamur / apt-key-add
Created May 12, 2021 04:59
Alternative script for "apt-key add" which is deprecated in Ubuntu 20.10
#!/bin/bash
# apt-key-add - Alternative script for "apt-key add" which is deprecated in Ubuntu 20.10
#
# example:
# curl -fsSL https://deb.nodesource.com/gpgkey/nodesource.gpg.key | sudo apt-key-add - nodesource
# instead of
# curl -fsSL https://deb.nodesource.com/gpgkey/nodesource.gpg.key | sudo apt-key add -
#
set -e
if [ $# -ne 2 -o $UID -ne 0 ]; then
@hnakamur
hnakamur / index.html
Created August 1, 2022 05:57
show JavaScirpt alert from window.onerror
<script>
window.onerror = () => alert("Hello, World!");
function hello(x) {
if(typeof x === 'undefined') {
alert(x.f());
}
}
hello();
</script>
@hnakamur
hnakamur / bigint.zig
Last active July 18, 2022 18:26
An idea of redesign Zig's std.math.big.int
const std = @import("std");
const testing = std.testing;
pub const Limb = u64;
pub const Metadata = packed struct {
// positive is set as true for n >= 0, false for n < 0.
positive: bool,
len: u63,
};
@hnakamur
hnakamur / dynamic_error_msg.zig
Created July 17, 2022 19:25
An example of returning a dynamic error message in Zig
const std = @import("std");
const Context = struct {
canceled: bool = false,
err: [4096]u8 = undefined,
pub fn formatErr(self: *Context, comptime fmt: []const u8, args: anytype) error{NoSpaceLeft}!void {
var fbs = std.io.fixedBufferStream(self.err[0..]);
var writer = fbs.writer();
try std.fmt.format(writer, fmt, args);
@hnakamur
hnakamur / create_union_enum.zig
Created July 14, 2022 05:17
debug failed attempt of creating union enum in Zig
const std = @import("std");
const expect = std.testing.expect;
const cell = struct {
v1: ?*Variant,
v2: ?*Variant,
};
const Variant = union(enum) {
int: i32,