Skip to content

Instantly share code, notes, and snippets.

@fnky
fnky / ANSI.md
Last active July 18, 2024 18:21
ANSI Escape Codes

ANSI Escape Sequences

Standard escape codes are prefixed with Escape:

  • Ctrl-Key: ^[
  • Octal: \033
  • Unicode: \u001b
  • Hexadecimal: \x1B
  • Decimal: 27
@lhorie
lhorie / longest-keyword-sequence.md
Last active November 14, 2022 23:21
What's the longest keyword sequence in Javascript?
@louis-langholtz
louis-langholtz / CppNoStdEndl.md
Last active October 24, 2023 02:40
C++: More Reasons To Avoid std::endl

C++: More Reasons To Avoid std::endl

In the realm of death by a thousand cuts, please stop using std::endl and remove it from code. Per cppreference.com, std::endl "inserts a newline character into the output sequence... and flushes it". Okay, so why exactly shouldn't it be used???

The Issues

There have been numerous articles written about endl that echo the sentiment against using it. Here's some:

@Eisenwave
Eisenwave / divison_proposal.md
Last active June 22, 2024 11:42
C++26 Proposal Draft - <intdiv> Header for Integer Divisions

<intdiv> Header for Integer Divisions

Introduction

C++ currently offers only truncating integer division. As a consequence, the remainder operator's sign is same as the sign of the dividend. Alternative rounding modes and remainder sign behaviour are useful.

This proposal adds an <intdiv> header containing free functions for obtaining the quotient and remainder for different rounding modes, such as rounding towards the nearest integer, towards the infinities, etc.

@STCollier
STCollier / main.c
Created May 2, 2023 00:58
Golfed code that converts a 10 digit number to a phone number. e.g., 1234567890 -> (123) 456-7890
main(){char s[10];scanf("%[^\n]",s);printf("(%.*s) %.*s-%.*s",3,s,3,s+3,4,s+6);}
@m1lkweed
m1lkweed / un_builtin.c
Last active May 24, 2024 06:31
Replacements for various gcc builtins using c23's `typeof` and c11's `_Generic`
// Compares types of both parameters. Returns true if the types are compatible, otherwise false.
// Parameter y may not be a variably-modified type.
// Designed to be a drop-in replacement for gcc's __builtin_types_compatible_p
#define types_compatible_p(x, y) _Generic(((typeof_unqual(x)*){}), typeof_unqual(y)*:1, default:0)
// Cast the bits of arg to type. Parameter type must be a type and must not be a literal or object with that type.
// Designed to be compatible with g++'s __builtin_bit_cast
#define bit_cast(type, ...) ( \
union{typeof(__VA_ARGS__) in; typeof(type) out; \
int enforce_type:_Generic((int(*)(int(type)))0, \
@m1lkweed
m1lkweed / py_else.c
Last active May 24, 2024 06:31
Porting python's `for`/`else` to C with no `setjmp`.
#include <stdio.h>
#define py_for(...) for(int loop_flag = 2, break_called = 0; loop_flag && !break_called; --loop_flag)if((loop_flag == 2) && !break_called)for(__VA_ARGS__, break_called = 0)if((break_called = 1), 0){}else
#define py_whl(...) py_for(;__VA_ARGS__;(void)0)
int main(){
py_for(int i = 0; i < 5; ++i){
printf("%d\n", i);
}else{
puts("egg");