Skip to content

Instantly share code, notes, and snippets.

View hamza-cskn's full-sized avatar
🎯
Focusing

Hamza Coşkun hamza-cskn

🎯
Focusing
  • Türkiye/Kocaeli
  • 06:32 (UTC +03:00)
View GitHub Profile
@hamza-cskn
hamza-cskn / output_capturing.c
Created February 15, 2024 17:56
Output capturing for tests in c.
#include <unistd.h>
#include <fcntl.h>
#define OUTPUT_BUFFER_SIZE 1024
struct listen_data
{
int fd_copy;
int fd;
int *pipe_fd;
@hamza-cskn
hamza-cskn / sum-and-multiply.c
Last active December 2, 2023 16:55
Multiply and sum implementations using only bitwise operators. (negative numbers are not supported.)
int get_bit(int val, int bit) {
return (val >> bit) & 1;
}
/* sets ith bit to zero*/
int set_zero_bit(int val, int i) {
return val & (~(1 << i));
}
/* sets ith bit to one*/
@hamza-cskn
hamza-cskn / delimiter-validation.js
Created November 7, 2023 19:26
Finite State Machine Delimiter Validation
const State = {
next: undefined,
isLookingFor(char) {
throw new Error('Not implemented');
}
};
const assert = (ensureTrue, message="Validation failed.") => {
if (!ensureTrue)
throw new Error(message);
@hamza-cskn
hamza-cskn / binarytest.c
Created June 25, 2023 07:08
bit to string, string to bit serialization.
#include <stdio.h>
void right_shift_bit(char *c, int last_bit) {
if (last_bit)
*c = (*c >> 1) | 0b10000000;
else
*c = (*c >> 1) & 0b01111111;
}
int get_bit(char c, int bit_index) {
@hamza-cskn
hamza-cskn / CoordinateRounder.java
Last active March 24, 2023 12:24
Coordinate rounder for arenas of Bedwars1058. (mostly written by ChatGPT)
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class CoordinateRounder {
@hamza-cskn
hamza-cskn / ft_split.c
Created February 19, 2023 07:15
split exercise for école thirty four
#include <stdlib.h>
#include <printf.h>
int is_seperator(char c) {
return (c == '\0' || c == ' ' || c == '\n');
}
int word_length(char *str) {
int i;
@hamza-cskn
hamza-cskn / itoa.c
Last active February 12, 2023 21:25
converts integers to string.
int power(int n, int pow) {
int res = n;
if (pow == 0) return 1;
while (--pow > 0) res *= n;
return res;
}
// increases 'pow' until find the smallest power of 10
// which is bigger than the number. and its gives the digit count.
int count_digits(int nbr) {
@hamza-cskn
hamza-cskn / ChatEntry.java
Created January 22, 2023 10:31
Provides taking text input from players using chat. Extremely lightweight.
public final class ChatEntry implements Listener {
private static final Map<UUID, ChatEntry> entryMap = new HashMap<>();
public Consumer<AsyncPlayerChatEvent> action;
public ChatEntry(UUID uuid) {
entryMap.put(uuid, this);
Bukkit.getPluginManager().registerEvents(this, SeniorRegions.getInstance());
}
@hamza-cskn
hamza-cskn / TimeFormatUtil.java
Created December 25, 2022 10:27
Formats durations to relative times
public static Duration getRelativeDuration(Date date) {
return Duration.ofMillis(Objects.requireNonNull(date).toInstant().toEpochMilli() - System.currentTimeMillis());
}
public static String formatRelativeDuration(Duration duration, boolean verbose) {
if (duration.isZero()) return "now";
String direction = duration.isNegative() ? "ago" : "later";
duration = duration.abs();
StringBuilder builder = new StringBuilder();
if (!applyTimeFormat(builder, duration.toDaysPart(), "day") || verbose)
@hamza-cskn
hamza-cskn / ChainExecutor.java
Last active December 5, 2022 22:27
Simple Task Chain for Bukkit
import org.bukkit.Bukkit;
import java.util.function.Function;
public class ChainExecutor {
private ChainTask first;
private static Function<ChainTask, Boolean> toBooleanFunction(Runnable runnable) {
return chain -> {