Skip to content

Instantly share code, notes, and snippets.

View MightyPork's full-sized avatar
🐶
meow

Ondřej Hruška MightyPork

🐶
meow
View GitHub Profile
@MightyPork
MightyPork / usb_hid_keys.h
Last active May 1, 2024 05:11
USB HID Keyboard scan codes
/**
* USB HID Keyboard scan codes as per USB spec 1.11
* plus some additional codes
*
* Created by MightyPork, 2016
* Public domain
*
* Adapted from:
* https://source.android.com/devices/input/keyboard-devices.html
*/
@MightyPork
MightyPork / custom-serde.rs
Created June 11, 2019 20:34
example of custom serialize and deserialize in serde
use serde::ser::SerializeMap;
use serde::{Serialize, Serializer, de::Visitor, de::MapAccess, Deserialize, Deserializer};
use std::fmt;
#[derive(Debug)]
struct Custom(String, u32);
impl Serialize for Custom {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
@MightyPork
MightyPork / urldecode.c
Created January 28, 2016 17:39
C routine to decode url-encoded string in place
/**
* Decode URL-encoded string in place.
*/
void urldecode(char *str)
{
unsigned int x;
for (int i = 0; str[i] != 0; i++) {
char c = str[i];
if (c == '+') {
@MightyPork
MightyPork / utf8_encode.c
Last active February 28, 2024 08:07
C function to encode a Unicode code point as UTF-8 byte array
#include <stdint.h>
/**
* Encode a code point using UTF-8
*
* @author Ondřej Hruška <ondra@ondrovo.com>
* @license MIT
*
* @param out - output buffer (min 5 characters), will be 0-terminated
* @param utf - code point 0-0x10FFFF
@MightyPork
MightyPork / elements_json.php
Last active January 3, 2024 14:43
Generate JSON of PT elements
<?php
$table = [
["Hydrogen", "H", "1", "1.00794", "0.0708 (@ -253°C)", "14.01", "20.28", "79", "32", "154 (-1e)", "14.1", "14.267 (H-H)", "0.117 (H-H)", "0.904 (H-H)", "0.1815", "110.00", "2.20", "1311.3", "1, -1", "1s<sup>1</sup>", "HEX", "3.750", "1.731", "Colorless, odorless, tasteless gas", "1766 (England)", "Henry Cavendish", "Greek: hydro (water) and genes (generate)", 1, 1, 0x7FFFFFFF, 0xAAAA, 0xFFFF, 0xAAAA],
["Helium", "He", "2", "4.002602", "0.147 (@ -270°C)", "0.95", "4.216", "0.0", "n/a", "93", "31.8", "5.188", "n/a", "0.08", "0.152", "n/a", "n/a", "2361.3", "n/a", "1s<sup>2</sup>", "HEX", "3.570", "1.633", "Inert, colorless, odorless, tasteless gas", "1895 (Scotland/Sweden)", "Sir William Ramsey, Nils Langet, P.T.Cleve", "Greek: helios (sun).", 18, 1, 0x7FFFFFFF, 0xC000, 0x7000, 0x0],
["Lithium", "Li", "3", "6.941", "0.534", "553.69", "1118.15", "155", "163", "68 (+1e)", "13.1", "3.489", "2.89", "148", "84.8", "400.00", "0.98", "519.9", "1", "[He] 2s<sup>1</sup>", "BCC", "3.490", "n/a", "So
@MightyPork
MightyPork / ansi_rgb.js
Last active October 4, 2023 08:55
Convert ANSI color 0-255 to RGB
const low_rgb = [
'#000000', '#800000', '#008000', '#808000', '#000080', '#800080', '#008080', '#c0c0c0',
'#808080', '#ff0000', '#00ff00', '#ffff00', '#0000ff', '#ff00ff', '#00ffff', '#ffffff'
]
function ansi_rgb(ansi) {
if (ansi < 0 || ansi > 255) return '#000'
if (ansi < 16) return low_rgb[ansi]
if (ansi > 231) {
@MightyPork
MightyPork / ascii.txt
Created April 14, 2020 11:28
good ASCII table with hex
Dec Hex Char Dec Hex Char Dec Hex Char Dec Hex Char
-------------- -------------- -------------- --------------
0 00h NUL (null) 32 20h SP 64 40h @ 96 60h `
1 01h SOH (start of heading) 33 21h ! 65 41h A 97 61h a
2 02h STX (start of text) 34 22h " 66 42h B 98 62h b
3 03h ETX (end of text) 35 23h # 67 43h C 99 63h c
4 04h EOT (end of transmission) 36 24h $ 68 44h D 100 64h d
5 05h ENQ (enquiry) 37 25h % 69 45h E 101 65h e
6 06h ACK (acknowledge) 38 26h & 70 46h F 102 66h f
7 07h BEL (bell) 39 27h ' 71 47h G 103 67h g
use std::io;
use std::io::Write;
fn fibo(n:u32) -> u32 {
if n <= 2 {
1
} else {
let mut prev1 = 1;
let mut prev2 = 1;
for _n in 2..n {
@MightyPork
MightyPork / animals_game.txt
Created December 29, 2018 09:12
Rust animals game sample output
----- NEW GAME -----
Secret animal is: duck
What animal is it?
> duck
Game ended.
----- NEW GAME -----
Secret animal is: dog
Is it a duck?
> no
@MightyPork
MightyPork / weekday.c
Last active May 2, 2023 11:52
Get weekday from a date, C version
/**
* Weekday calculation utils.
*
* Month and weekday are 1-based, 1 is Monday.
* The enums may be replaced by ints at the expense of clarity.
*
* Released to the public domain
*/
#include <stdint.h>