Skip to content

Instantly share code, notes, and snippets.

View hnakamur's full-sized avatar

Hiroaki Nakamura hnakamur

View GitHub Profile
@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,
@hnakamur
hnakamur / create_union_enum.zig
Last active July 14, 2022 05:03
Crate a binary tree
const std = @import("std");
const expect = std.testing.expect;
const cell = struct {
v1: ?*Variant,
v2: ?*Variant,
};
const Variant = union(enum) {
int: i32,
@hnakamur
hnakamur / create_union_enum.zig
Created July 14, 2022 03:37
create union enum in Zig
const std = @import("std");
const expect = std.testing.expect;
const Variant = union(enum) {
int: i32,
boolean: bool,
// void can be omitted when inferring enum tag type.
none,
@hnakamur
hnakamur / build.log
Created July 10, 2022 06:23
Non-comptime switch cases not allowed
$ zig test src/main.zig
./src/main.zig:11:28: error: cannot store runtime value in compile time variable
const d: u32 = c;
^
@hnakamur
hnakamur / mytask.yaml
Created May 15, 2022 06:28
trim spaces in ansible vairable using jinja2 if
---
- name: My playbook
hosts: localhost
vars:
zabbix_agentd_d_dir: >-
{% if ansible_facts['os_family'] == 'RedHat' -%}
/etc/zabbix/zabbix_agentd.d
{%- else -%}
/etc/zabbix/zabbix_agentd.conf.d
{%- endif %}
atob(`DES9N7bxsOmHupY4JsjDg6fZ7vaFIZaWDBASiCj6vN+S
VYuCa9Bo5LdJHmeo+kpmK2PTvlShVkxpOwt59hGX6sdlT
apaRgEGCB8FZt3iSkE9EdmShv5vmSv3oMrCoSFlqnLeGY
9Wh6hNCNx4nUfxtzjoExo494fUr+hZebjFTo5ow//oy22
fW8fuwieImoEm7y28eFSmN5ITVpjzDabYQBjYPgRpLStG
jRMcsilxGH6Ud3nweSyqjimsCs6f2OL4JuoIfPTSVAP9/
hiab9VKmyBM3WbOVwAi+wLjoS6k1FcAcyjQo8HUM3v
GALSnPn7w+wnD5YNKRdXPVpQ8tq+stidQzFdESSzajS7
rPC81pzrIjW3tXOkrDmusp/mEzfTEHOsFRq9eq3kOJr+
CXXSOhjXuSSPVNH1rt8JIDUts529LqAb5pPfYta1L4bD
@hnakamur
hnakamur / main.rs
Last active April 14, 2022 01:35
Read a byte from stdin in rust
use std::io;
use std::io::prelude::*;
fn main() -> io::Result<()> {
let mut buffer = [0; 1];
loop {
let n = io::stdin().read(&mut buffer)?;
if n == 0 {
break;
}