Skip to content

Instantly share code, notes, and snippets.

@x-yuri
x-yuri / perl: internal representation of numbers.md
Created November 13, 2024 11:18
perl: internal representation of numbers

perl: internal representation of numbers

Perl can internally represent numbers in 3 different ways: as native integers, as native floating point numbers, and as decimal strings.

https://perldoc.perl.org/perlnumber

Small integer numbers are stored as signed integers. When that is not possible perl switches to unsigned integers. And then to floating point numbers:

a.pl:

@x-yuri
x-yuri / 01. perl: pack().md
Last active November 14, 2024 16:39
perl: pack()

perl: pack()

I'm going to ignore the j, J, f, F, d, D, p, P, u, w template characters.

I'm running this on a system with x86_64 architecture (little endian) with perl-5.38-2, sizeof(int) == 4.

pack TEMPLATE, LIST takes LIST and packs it into a string according to TEMPLATE. E.g. pack('ac', 'a', 1) returns "a\x01". There are two template charaters here: [a][aa] and [c][ac]. Each template character tells pack() what to do with the next argument: [a][aa] to take one character from the first argument ('a'), and put it into the resulting string, [c][ac] to take the next argument (the 8-bit signed integer 1), and add chr(1) to the resulting string.

Template characters differ in what values they take:

@x-yuri
x-yuri / Building mecab (with its perl module) with debug symbols.md
Created October 31, 2024 06:22
Building mecab (with its perl module) with debug symbols

Building mecab (with its perl module) with debug symbols

Dockerfile:

FROM alpine:3.20.3
RUN set -x && apk add git build-base autoconf gdb automake libtool gettext-dev perl-dev \
    && git clone https://github.com/taku910/mecab \
    && cd mecab \
    && git checkout 05481e751dd5aa536a2bace46715ce54568b972a \
@x-yuri
x-yuri / Creating a shared library in C, C++.md
Last active November 1, 2024 20:10
Creating a shared library in C/C++

Creating a shared library in C/C++

a.c:

#include <stdio.h>
#include "a.h"

void f(void)
{
@x-yuri
x-yuri / Making a STUN request in C.md
Created October 28, 2024 17:58
Making a STUN request in C

Making a STUN request in C

a.c:

#include <stdlib.h>
#include <sys/socket.h>
#include <err.h>
#include <stdio.h>
#include <sys/random.h>
@x-yuri
x-yuri / c, c++: jumping over declarations.md
Last active October 28, 2024 13:03
c/c++: jumping over declarations

c/c++: jumping over declarations

In C one can't have a labeled variable declaration. This quirk probably appeared in C99. Before that variable declarations had to be at the beginnings of blocks, so there could never be a variable declaration after a label. C99 allowed intermixing variable declarations and statements, but the grammar [remained the same][a], allowing only statements after labels.

cases
$ gcc --version
gcc (Alpine 13.2.1_git20240309) 13.2.1 20240309
@x-yuri
x-yuri / A poor man's http server.md
Created October 26, 2024 20:00
A poor man's http server

A poor man's http server

The server process handles a request and exits. But the server is running in a docker container with --restart always, so after each request docker restarts the server.

Dockerfile:

FROM alpine:3.20
COPY a.c a.c
RUN apk add gcc musl-dev \
@x-yuri
x-yuri / Reading an HTTP request without blocking.md
Created October 26, 2024 18:57
Reading an HTTP request without blocking

Reading an HTTP request without blocking

It's assumed that only valid requests are received. And that read() doesn't block until \r\n\r\n is received.

a.c:

#include <unistd.h>
#include <err.h>
#include <string.h>
@x-yuri
x-yuri / a. mecab.md
Last active October 31, 2024 12:25
mecab

mecab

Dockerfile (git):

FROM alpine:3.20
RUN apk add git build-base curl \
    && git clone https://github.com/taku910/mecab \
    && cd mecab/mecab \
    && ./configure \
@x-yuri
x-yuri / BRE|ERE: escape special chars.md
Created October 20, 2024 08:32
BRE/ERE: escape special chars

BRE/ERE: escape special chars

a.bats:

setup() {
    load bats-support/load
    load bats-assert/load
}