A collection of diverse module source files with interesting solutions to programming challenges.
Table of Contents
- About the Project
-
Contents
- itoa: yet more implementations of integer to string
- PerfTest: records and reports on iterative timings
- read_file_lines: returns a file's contents by line
- commaize: format integer with commas
- columnize: arrange an array of string into columns
- arrayify: break a string of words into an array
- get_keypress: for TUI programs to recognize keystrokes
- init_struct_array: very old file, when I was younger and ignorant
- isJSonNumber: Quick-and-dirty solution that evaluates if a string is a valid JSON number. Developed for a JSON parsing project.
This repository exists for me to gather non-trivial programming solutions for future reference.
git clone https://github.com/cjungmann/c_patterns.gitThe modules are generally independent. Most include some code that can be copied to another project, though some are incomplete ideas that need work (commaize.c, I'm looking at you).
Nearly all of the modules include a Local Variables section with a compile-command that informs Emacs how to generate an executable for demonstration and testing.
Modules that are meant to be directly included into another project have the testing and main function code fenced behind an #ifdef block, which is triggered by the compile-command.
Documentation style is an ongoing project for me. I pay attention to what works and doesn't work and try to improve my meager skills at documentation. Newer modules will usually have better documentation, though I may revisit old modules to improve things if they're too bad.
I won't necessarily fix old modules. Be prepared to dig around a bit for useful stuff.
Going forward, more recent efforts should be at the top in order to prioritize the accumulation of experience in coding, documenting, organizing, etc.
-
Yet-another-itoa
Called itoa to mirror library function atoi, this source file contains several variations of a function that converts an integer value to a string. It includes some testing code that uses snprintf as a benchmark for speed and output. It also uses [PerfTest][README_perftest.md) to compare the performance of the different versions. -
Performance Tester
While considering the value of my new itoa function, it made sense to develop a suite of timing functions to compare the performance different itoa implementations. -
Line-reading Function
Collecting data from various sources usually requires processing files by line. The main function,read_file_linesbuffers the contents of a file and invokes a callback function to present a line to be processed. -
commaize
This function, found in commaize.c, uses recursion to break up a number from the small-end and displays it when the recursion unwinds. I recently found out that you can do the same thing including an apostrophe in the format specifier when using printf when setlocale is used. -
[Columns display][README_columnize.md) Displays a list of strings in column form, according to the screen dimensions.
-
Make an array of strings
This module can break a string into an array of strings, which is then made available through a callback function modeled after main() with a void return type. -
get_keypress
Functions the provide immediate recognition of individual keypresses, including non-printable keys. Source code in get_keypress.c. -
Initializing Struct Arrays
I often iterate over static arrays to perform repetitve tasks like listing program options, simulate user responses, etc. I frequently have trouble initializing these arrays. This guide should help me remember effective coding techniques. -
isJsonNumber Quickly developed code to support JSON parsing. Follows standards set out by RFC-8259 and passes number parsing tests found in Parsing JSON is a Minefield.