{"id":24617917,"url":"https://github.com/jacoblincool/cimple-lib","last_synced_at":"2025-03-18T21:28:03.000Z","repository":{"id":38444552,"uuid":"446883891","full_name":"JacobLinCool/Cimple-Lib","owner":"JacobLinCool","description":"A delightful library for C learners.","archived":false,"fork":false,"pushed_at":"2023-12-15T17:28:18.000Z","size":422,"stargazers_count":1,"open_issues_count":1,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-01-24T23:41:31.005Z","etag":null,"topics":["c","computer-programming"],"latest_commit_sha":null,"homepage":"https://jacoblincool.github.io/Cimple-Lib/","language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/JacobLinCool.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-01-11T15:47:43.000Z","updated_at":"2022-06-25T10:24:09.000Z","dependencies_parsed_at":"2022-08-18T23:10:09.694Z","dependency_job_id":null,"html_url":"https://github.com/JacobLinCool/Cimple-Lib","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JacobLinCool%2FCimple-Lib","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JacobLinCool%2FCimple-Lib/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JacobLinCool%2FCimple-Lib/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JacobLinCool%2FCimple-Lib/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/JacobLinCool","download_url":"https://codeload.github.com/JacobLinCool/Cimple-Lib/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244308989,"owners_count":20432297,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["c","computer-programming"],"created_at":"2025-01-24T23:41:39.872Z","updated_at":"2025-03-18T21:28:02.979Z","avatar_url":"https://github.com/JacobLinCool.png","language":"C","readme":"# Cimple Lib\n\nA delightful helper library for C learners.\n\nGood for someone who needs to do C programming homework.\n\n\u003c!-- It helps me to learn how to use (fancy) macros in C. --\u003e\n\n## Usage\n\nAll libraries are _header files_, so you can directly include them in your C code.\n\nSince the libraries are all lightweight, we don't have to worry about the compilation time.\n\n```c\n#include \"src/all.h\"\n\nint main() {\n    char* label = \"print 1 to 1000\";\n    Timing.start(label);\n    for (int i = 1; i \u003c= 1000; i++) {\n        Console.rainbow(\"%d\", i);\n    }\n    Console.log(\"%s: %Lf ms\", label, Timing.check(label));\n\n    return 0;\n}\n```\n\nI recommend you to use VS Code because of the great language server support.\n\n![language server](img/language-server.png)\n\n## Features\n\n### `all.h`\n\nIncludes all the libraries below.\n\n### `base.h`\n\nThis header file includes:\n\n- Standard libraries that are useful in most cases.\n- Type alias for standard types.\n  - `i8`, `i16`, `i32`, `i64`, `u8`, `u16`, `u32`, `u64`, `f32`, `f64`, `f128`\n\n### `bucket.h`\n\nPointer manager. It can help you to elegantly code with function chaining.\n\n`$()`, `$clear()`, ...\n\n**Example**\n\n```c\nfor (int i = 0; i \u003c 4096; i++) {\n    char* str = $(calloc(1024, sizeof(char)));\n    sprintf(str, \"%d\", i);\n}\n\nassert($size() == 4096);\nassert($capacity() == 4096);\n$free(); // free all pointers at once\n```\n\n```c\n// With $\nchar* str = String.upper($(String.trim($(String.format(\"%s %s!\", \"  Hello\", \"World\")), \" \")));\n$free(); // free the intermediates\nassert(strcmp(str, \"HELLO WORLD!\") == 0);\ndo_something(str);\n\n// Without $\nchar* formatted = String.format(\"%s %s!\", \"  Hello\", \"World\");\nchar* trimmed = String.trim(formatted, \" \");\nfree(formatted);\nchar* str = String.upper(trimmed);\nfree(trimmed);\nassert(strcmp(str, \"HELLO WORLD!\") == 0);\ndo_something(str);\n```\n\n### `buffer.h`\n\nBit operations and endianness conversion.\n\n```c\n/**\n * @brief Buffer utility functions.\n */\nstruct {\n    /**\n     * @brief Get bit from buffer\n     * @param buffer The buffer\n     * @param bit The bit to be get\n     */\n    bool (*get_bit)(const void* buffer, size_t bit);\n    /**\n     * @brief Set bit in buffer\n     * @param buffer The buffer\n     * @param bit The bit to be set\n     * @param value The value to be set\n     */\n    void (*set_bit)(void* buffer, size_t bit, bool value);\n    /**\n     * @brief Stringify the buffer\n     * @param buffer The buffer\n     * @param size The size of the buffer, in bits\n     * @param col_size The size of each column, in bits\n     * @param row_size The size of each row, in columns\n     */\n    char* (*stringify)(const void* buffer, size_t size, size_t col_size, size_t row_size);\n    /**\n     * @brief Parse a stringified buffer into a buffer\n     * @param buffer The buffer\n     * @param str The string to be parsed\n     * @param size The size of the buffer, in bits\n     */\n    void (*parse)(void* buffer, const char* str, size_t size);\n    /**\n     * @brief Get the endian of the system\n     * @return true if the system is little endian, false otherwise\n     */\n    bool (*endian)();\n    /**\n     * @brief Reverse the buffer\n     * @param buffer The buffer\n     * @param size The size of the buffer, in bytes\n     */\n    uint8_t* (*reverse)(const void* buffer, size_t size);\n    /**\n     * @brief Convert a little endian buffer to big endian\n     * @param buffer The buffer\n     * @param size The size of the buffer, in bits\n     */\n    void* (*to_big)(const void* buffer, size_t size);\n    /**\n     * @brief Convert a big endian buffer to little endian\n     * @param buffer The buffer\n     * @param size The size of the buffer, in bits\n     */\n    void* (*to_little)(const void* buffer, size_t size);\n} Buffer;\n```\n\n**Example**\n\n```c\nuint8_t* buffer = calloc(16 / 8, sizeof(uint8_t));\nfor (int i = 0; i \u003c 16; i++) {\n    Buffer.set_bit(buffer, i, i % 2);\n}\nfor (int i = 0; i \u003c 16; i++) {\n    assert(Buffer.get_bit(buffer, i) == i % 2);\n}\n\nchar* str = Buffer.stringify(buffer, 16, 8, 8);\nprintf(\"%s\\n\", str); // \"01010101 01010101\"\n```\n\n### `console.h`\n\n`Console` utilities.\n\n```c\nstruct {\n    size_t (*gray)(const char* str, ...);\n    size_t (*red)(const char* str, ...);\n    size_t (*green)(const char* str, ...);\n    size_t (*blue)(const char* str, ...);\n    size_t (*yellow)(const char* str, ...);\n    size_t (*magenta)(const char* str, ...);\n    size_t (*cyan)(const char* str, ...);\n\n    size_t (*success)(const char* str, ...);\n    size_t (*info)(const char* str, ...);\n    size_t (*warn)(const char* str, ...);\n    size_t (*error)(const char* str, ...);\n\n    /** Logs a message to the console, with time prefix */\n    void (*log)(const char* str, ...);\n\n    /** Print in rainbow colors */\n    size_t (*rainbow)(const char* str, ...);\n\n    /** Color constants */\n    struct {\n        const char* GRAY;\n        const char* RED;\n        const char* GREEN;\n        const char* BLUE;\n        const char* YELLOW;\n        const char* MAGENTA;\n        const char* CYAN;\n    } C;\n} Console;\n```\n\n### `debug.h`\n\nDebug utilities.\n\n`THROW` `THROW_IF`\n\n### `options.h`\n\nParse command line options.\n\n```c\nstruct {\n    /**\n     * @brief Parse command line arguments\n     * @param argc number of arguments\n     * @param argv array of arguments\n     * @return parsed options\n     */\n    ParsedOptions* (*parse)(size_t argc, char* argv[]);\n    /**\n     * @brief Get an option from the parsed options\n     * @param options parsed options\n     * @param name name of the option\n     * @return value of the option\n     */\n    char* (*get)(ParsedOptions* options, char* name);\n    /**\n     * @brief Check if an option is present\n     * @param options parsed options\n     * @param name name of the option\n     * @return true if the option is present, false otherwise\n     */\n    bool (*has)(ParsedOptions* options, char* name);\n    /**\n     * @brief Free the parsed options\n     * @param options parsed options\n     */\n    void (*free)(ParsedOptions* options);\n} Options;\n```\n\n**Example**\n\n```c\nParsedOptions* options = Options.parse(argc, argv);\n\nif (Options.has(options, \"help\") || Options.has(options, \"h\")) {\n    printf(\"Usage: %s --name \u003csomething\u003e\\n\", argv[0]);\n    return 0;\n}\n\nprintf(\"Hello, %s!\\n\", Options.get(options, \"name\") || \"XYZ\");\nOptions.free(options);\n```\n\n### `string.h`\n\n`String` utilities.\n\n```c\nstruct {\n    /**\n     * Construct a new string with IO formatting.\n     */\n    char* (*format)(const char* format, ...);\n    /**\n     * Trim the chars from the beginning and end of a string and returns the new string.\n     * If charset is NULL, the default charset ( \\\\t\\\\n\\\\r\\\\f\\\\v ) is used.\n     */\n    char* (*trim)(const char* string, const char* charset);\n    /**\n     * Extract a substring from a string and returns the new string.\n     */\n    char* (*substring)(const char* string, size_t start, size_t end);\n    /**\n     * Search for a substring in a string and returns the index of occurrences, with size `count`.\n     */\n    size_t* (*search)(const char* string, const char* pattern, size_t* count);\n    /**\n     * Repeat a string with separator for a given number of times and returns the new string.\n     */\n    char* (*repeat)(const char* string, size_t times, const char* separator);\n    /**\n     * Replace a substring in a string with another substring and returns the new string.\n     */\n    char* (*replace)(const char* string, const char* old, const char* new);\n    /**\n     * Make a string uppercase and returns the new string.\n     */\n    char* (*upper)(const char* string);\n    /**\n     * Make a string lowercase and returns the new string.\n     */\n    char* (*lower)(const char* string);\n    /**\n     * Reverse a string and returns the new string.\n     */\n    char* (*reverse)(const char* string);\n    /**\n     * Pad a string with a given string and returns the new string.\n     */\n    char* (*pad)(const char* string, size_t length, const char* pad);\n    /**\n     * Split a string with a given separator and returns the new string array.\n     */\n    char** (*split)(const char* string, const char* delimiter, size_t* count);\n} String;\n```\n\n**Example**\n\n```c\nchar* my_string = String.format(\"There are %d monkeys in the %s\", 10, \"tree\");\n\nsize_t size;\nchar** words = String.split(my_string, \" \", \u0026size);\n```\n\n### `timing.h`\n\n`Timing` utilities.\n\n```c\nstruct {\n    /**\n     * @brief Start a timer with a label.\n     * @param label The label of the timer.\n     */\n    bool (*start)(const char* label);\n    /**\n     * @brief Get the time elapsed since the last timing_start() call.\n     * @param label The label of the timer.\n     * @return long double The time elapsed since the last timing_start() call in milliseconds.\n     */\n    long double (*check)(const char* label);\n    /**\n     * @brief Remove a timer with a label.\n     * @param label The label of the timer.\n     * @return bool True if the timer is removed successfully.\n     */\n    bool (*remove)(const char* label);\n    /**\n     * @brief Clear all the timers.\n     * @return size_t The number of timers cleared.\n     */\n    size_t (*clear)();\n    /**\n     * @brief Sleep for a certain amount of milliseconds.\n     * @param ms The amount of milliseconds to sleep.\n     */\n    void (*sleep)(const uint64_t ms);\n} Timing;\n```\n\n**Example**\n\n```c\nTiming.start(\"test\");\nTiming.sleep(500);\nConsole.cyan(\"%Lf ms\", Timing.check(\"test\"));\nTiming.sleep(500);\nConsole.yellow(\"%Lf ms\", Timing.check(\"test\"));\n```\n\n## Run Tests\n\n1. Clone the repository and cd into the directory.\n2. Run `make` to compile the C code.\n\nOr just open in Gitpod:\n\n[![Open in Gitpod](https://gitpod.io/button/open-in-gitpod.svg)](https://gitpod.io/#https://github.com/JacobLinCool/Cimple-Lib)\n\n## Some Concepts\n\n### Why using `String.lower` instead of `strlower`?\n\nNamespace is a good idea, although it is not exisiting in C, but we can still simulate it by using a global struct.\n\nBenefits:\n\n- Avoid naming conflicts (pollution)\n- Provide library description through VS Code intellisense\n\nThe only downside is that dereferring a function pointer may have a small cost. (Maybe compiler will optimize it away?)\n\n## Development\n\n1. build the ubuntu environment image `scripts/build.sh`\n2. enter the container `scripts/start.sh`\n3. do anything you want\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjacoblincool%2Fcimple-lib","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjacoblincool%2Fcimple-lib","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjacoblincool%2Fcimple-lib/lists"}