Skip to content

Instantly share code, notes, and snippets.

@stevedoyle
stevedoyle / Ripgrep cheatsheet
Last active June 26, 2024 09:37
Ripgrep Cheatsheet
# Search only within a single file
rg <pattern> <filename>
rg <pattern> -g <filename pattern>
rg <pattern> --glob <filename pattern>
# Use ripgrep to extract unique 4 digit numbers from a file
`rg -o '\b\d{4}\b' <file_name> | sort -u`
@stevedoyle
stevedoyle / goexec.go
Created May 9, 2024 13:42
Execute a shell command from a Golang program
package main
import (
"fmt"
"os/exec"
)
func main() {
app := "echo"
arg0 := "-e"
@stevedoyle
stevedoyle / read_text_file.rs
Last active December 28, 2023 22:29
Read text file line by line using Rust.
// From: https://doc.rust-lang.org/rust-by-example/std_misc/file/read_lines.html
use std::fs::File;
use std::io::{self, BufRead};
use std::path::Path;
fn main() {
// File hosts.txt must exist in the current path
if let Ok(lines) = read_lines("./hosts.txt") {
// Consumes the iterator, returns an (Optional) String
@stevedoyle
stevedoyle / scanner.go
Last active March 5, 2023 21:35
Go: Reading a string with spaces from stdin using bufio
package main
import (
"bufio"
"fmt"
"os"
"strings"
)
func main() {
@stevedoyle
stevedoyle / CMakeLists.txt
Created February 20, 2023 20:43
Example CMakeLists.txt template
project(cpp_condition)
set(CMAKE_CXX_STANDARD 17)
add_executable(cpp_condition cpp-condition.cpp)
@stevedoyle
stevedoyle / cpp-condition.cpp
Created February 20, 2023 20:39
Example of using a condition variable with a mutex using std C++
#include <iostream>
#include <string>
#include <thread>
#include <mutex>
#include <condition_variable>
std::mutex m;
std::condition_variable cv;
std::string data;
bool ready = false;
@stevedoyle
stevedoyle / RustCallingCCodeWithOpaquePointers.rs
Created February 9, 2023 16:45
Rust code for calling C APIs that use opaque pointers
#![allow(non_upper_case_globals)]
#![allow(non_camel_case_types)]
#![allow(non_snake_case)]
include!("../bindings.rs");
fn main() {
unsafe {
let size = fooGetCtxSize();
println!("Ctx Size: {}", size);
@stevedoyle
stevedoyle / ipp_sha_hash_example.cpp
Created January 25, 2023 15:09
Example of using IPP Crypto Primitives for SHA hashing
#include <iostream>
#include <stdint.h>
#include <x86intrin.h>
#include "ippcp.h"
#include "examples_common.h"
#define DATA_SIZE 1024*1024
#define DIGEST_SIZE 32
#define ITERATIONS 10000
@stevedoyle
stevedoyle / rust-snippets.md
Last active October 20, 2022 09:01
Rust code snippets

Rust Snippets

Pass a Vec<> by reference to a function

let data: vec![0u8; 1024];
foo(data.as_ref()); // Passes an immutable slice

fn foo(data: &[u8]) {
    ...
}
# Read N lines of input, splitting each line and storing the result in a numpy array.
import numpy as np
a = np.array([input().split() for _ in range(N)], int)