Skip to content

Instantly share code, notes, and snippets.

View TheRolfFR's full-sized avatar
💼
Working

TheRolf TheRolfFR

💼
Working
View GitHub Profile
@TheRolfFR
TheRolfFR / str_rstrip.c
Created February 22, 2024 16:07
Simple C rstrip implementation
/**
* @brief Strips all the characters at the end
* @author TheRolf
*
* @param str target string
* @param chars chars to find and remove
*/
void str_rstrip(char* str, const char* chars)
{
size_t size_str = strlen(str);
@TheRolfFR
TheRolfFR / save_restore_set.md
Last active July 25, 2023 13:28
Quick save, restore and set with multiline lambda

Create temporary setup for pytest with context managers

My need

To create a temporary setup for pytest, you may think fixtures. But my case isn't like yours.
I needed to change tempolarly a value in the middle of my test so I could get faster results.

The clean solution

We will use the with statement. It will make a dedicated "scope" to that temporary setup. I'm sure that you already used them to open files. It is particularly powerful as it creates a context manager, which increases readibility, and protects developers from themselves.

Why do clean? Because it's an addiction since I discovered C++ destructors, but above all, Rust drop trait.

@TheRolfFR
TheRolfFR / install.md
Created December 30, 2022 18:52
Modify Apache2::Imager::Resize to fix iCCP: known incorrect sRGB profile error

Modify Apache2::Imager::Resize to fix iCCP: known incorrect sRGB profile error

First this error is not your module or your library in fault, it's the libpng library fault.

Since libpng 1.6 release, the library became more aggresive about incorrect iCCP data chunks. To fix that problem, you could go to a prior version or modify the images so that the iCCP chunk is removed.


Install ImageMagick with your packet manager:

@TheRolfFR
TheRolfFR / dark_theme_store.js
Last active December 13, 2022 09:01
Dark theme color scheme store with watch
/**
* Dark theme color scheme store with watch
* full demo at https://codepen.io/TheRolf/pen/xxzNreP
* @author TheRolf
*/
window.DarkThemeStore = {
VALUES: {
AUTO: 'auto',
DARK: 'dark',
LIGHT: 'light'
@TheRolfFR
TheRolfFR / SmartPointer.h
Created November 23, 2022 08:50
Smart Pointer implementation
template <class Type>
class SmartPointer
{
private:
Type * pointer;
public:
SmartPointer() : pointer(nullptr) {}
SmartPointer(Type * p) : pointer(p) {}
Type * set(Type * p) { pointer = p; return p; }
Type * get() { return pointer; }
@TheRolfFR
TheRolfFR / tiny_string.cpp
Created October 20, 2022 13:43
C++ convert int to string
std::string from_int(int value) {
int tmp = value;
std::string valueAsString = tmp < 0 ? "-" : "";
size_t pos = valueAsString.length();
if(tmp < 0) tmp = -tmp;
else if(tmp == 0) valueAsString = "0";
while(tmp != 0) {
int digit = tmp%10;
valueAsString.insert(pos,1,'0' + digit);
@TheRolfFR
TheRolfFR / xml_ie_format.js
Created September 22, 2022 15:23
XML ie format
let value;
let spaces;
spaces = 0;
console.log(value.split('\n').filter(f => f.trim().length !== 0).map(cur => {
if(cur.startsWith('-')) spaces ++;
else if(cur.startsWith('</')) spaces --;
else if(cur.startsWith('<')) spaces++;
let sp = '';
@TheRolfFR
TheRolfFR / string_extensions.js
Created September 19, 2022 08:03
JS String extension with codes and chars
/**
* Gives char codes of the string
* @author TheRolf
*/
String.prototype.codes = function() {
return this.split('').map(c => {
let code = c.charCodeAt(0);
return code;
}).flat()
}
@TheRolfFR
TheRolfFR / wordle.py
Created February 17, 2022 21:44
Mini offline wordle with Python and rich library
import random
from rich.console import Console
from rich.panel import Panel
console = Console()
pathway = "wordlepy.txt"
def unused(letter):
return "[white on #2c3032]" + letter + "[/]"
@TheRolfFR
TheRolfFR / number_to_roman_numeral.py
Created February 14, 2022 17:01
Number to roman numeral converter
romlist = ["M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"]
numlist = [1000,900,500,400,100,90,50,40,10,9,5,4,1]
number = int(input("Enter integer > 0: "))
rn = []
numlist_index = 0
# a number should
while number > 0: