{"id":22249835,"url":"https://github.com/slackadays/superb-snippets","last_synced_at":"2026-03-19T22:11:41.664Z","repository":{"id":264977328,"uuid":"875436345","full_name":"Slackadays/superb-snippets","owner":"Slackadays","description":"🥾 Kickass C++ snippets for very common tasks","archived":false,"fork":false,"pushed_at":"2024-10-20T01:55:36.000Z","size":14,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-11-22T15:03:47.913Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"unlicense","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Slackadays.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-10-20T01:08:59.000Z","updated_at":"2024-10-21T13:42:56.000Z","dependencies_parsed_at":"2024-11-27T06:35:10.362Z","dependency_job_id":null,"html_url":"https://github.com/Slackadays/superb-snippets","commit_stats":null,"previous_names":["slackadays/superb-snippets"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/Slackadays/superb-snippets","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Slackadays%2Fsuperb-snippets","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Slackadays%2Fsuperb-snippets/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Slackadays%2Fsuperb-snippets/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Slackadays%2Fsuperb-snippets/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Slackadays","download_url":"https://codeload.github.com/Slackadays/superb-snippets/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Slackadays%2Fsuperb-snippets/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28221441,"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","status":"online","status_checked_at":"2026-01-06T02:00:07.049Z","response_time":56,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":[],"created_at":"2024-12-03T06:29:18.906Z","updated_at":"2026-01-06T03:49:21.001Z","avatar_url":"https://github.com/Slackadays.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# ✂️ Welcome to Superb Snippets\n\nSuper Snippets is my collection of C++ snippets that solve common tasks most programs of yours probably need to do but for which lack an obvious and/or optimized solution. \n\nTo use these snippets, just copy and paste them into your own code. And if they don't fit your needs perfectly, feel free to customize them.\n\nSuperb Snippets are better than whatever AI you use for coding because these have been verified to work, and work well they do!\n\n## Where these came from\n\nMost of these snippets are from my projects like [Clipboard](https://github.com/Slackadays/Clipboard), so they're already optimized, tested, and used in a production setting.\n\nIf these snippets change upstream, then I may update them here.\n\n## Licensing?\n\nAlthough I pulled these snippets from my projects which have licenses like the GPL, all the snippets here fall under The Unlicense which means you can use, modify, and redistribute them without attribution or other requirements or limitations, because I wrote them myself. Why? I did this to make it super duper easy to hit the ground running when it comes to the tasks these snippets solve!\n\n# The Snippets\n\n## Are we running on a Unix-like (Linux, Haiku) or Unix (BSD, macOS) system?\n\n```cpp\n#if defined(__linux__) || defined(__unix__) || defined(__APPLE__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) || defined(__HAIKU__) || defined(__FreeBSD__) \\\n        || defined(__posix__)\n#define UNIX_OR_UNIX_LIKE\n#endif\n```\n\nThis checks if we're running on a variety of systems considered Unix-like or downright Unix.\n\n### Requirements\n\nNone\n\n### Example\n\n```cpp\n#if defined(UNIX_OR_UNIX_LIKE)\nstd::cout \u003c\u003c \"Not running on Windows!\" \u003c\u003c std::endl;\n#endif\n```\n\n## Just get everything contained in a file\n\n```cpp\nstd::optional\u003cstd::string\u003e fileContents(const fs::path\u0026 path) {\n#if defined(UNIX_OR_UNIX_LIKE)\n    errno = 0;\n    int fd = open(path.string().data(), O_RDONLY);\n    if (fd == -1) {\n        if (errno == ENOENT)\n            return std::nullopt;\n        else\n            throw std::runtime_error(\"Couldn't open file \" + path.string() + \": \" + std::strerror(errno));\n    }\n    std::string contents;\n#if defined(__linux__) || defined(__FreeBSD__)\n    std::array\u003cchar, 65536\u003e buffer;\n#elif defined(__APPLE__) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__DragonFly__)\n    std::array\u003cchar, 16384\u003e buffer;\n#else\n    std::array\u003cchar, PIPE_BUF\u003e buffer;\n#endif\n    ssize_t bytes_read;\n    errno = 0;\n    while ((bytes_read = read(fd, buffer.data(), buffer.size())) \u003e 0) {\n        contents.append(buffer.data(), bytes_read);\n        if (bytes_read \u003c buffer.size() \u0026\u0026 errno == 0) break; // check if we reached EOF early and not due to an error\n    }\n    close(fd);\n    return contents;\n#else\n    std::stringstream buffer;\n    std::ifstream file(path, std::ios::binary);\n    if (!file.is_open()) return std::nullopt;\n    buffer \u003c\u003c file.rdbuf();\n    return buffer.str();\n#endif\n}\n```\n\nThis reads everything from a file if it exists and returns its raw content in a `std::optional\u003cstd::string\u003e`. If the file doesn't exist, this returns a blank value provided by `std::optional`.\n\n### Requirements\n\n`\u003coptional\u003e`, `\u003carray\u003e`, `\u003cstring\u003e`, `\u003cfilesystem\u003e`, and `\u003cunistd.h\u003e` on Unix or Unix-like platforms, or `\u003cfstream\u003e` on Windows.\n\n### Example\n\n```cpp\nauto content = fileContents(\"foobar.txt\");\nif (content.has_value())\n  std::cout \u003c\u003c content \u003c\u003c std::endl;\nelse\n  std::cout \u003c\u003c \"This file does not exist\" \u003c\u003c std::endl;\n```\n\n## Just write something to a file\n\n```cpp\nsize_t writeToFile(const fs::path\u0026 path, const std::string\u0026 content, bool append = false) {\n    std::ofstream file(path, append ? std::ios::app : std::ios::trunc | std::ios::binary);\n    file \u003c\u003c content;\n    return content.size();\n}\n```\n\nThis writes arbitrary content to a specified file and returns how many bytes it wrote.\n\n### Requirements\n\n`\u003cfilesystem\u003e`, `\u003cstring\u003e`, `\u003cfstream\u003e`\n\n### Example\n\n```cpp\nauto content = \"Your mother\";\nwriteToFile(\"Gigantic_Objects.txt\", content);\n```\n\n## Is an environment variable set to a \"true\" value?\n\n```cpp\nbool envVarIsTrue(const std::string_view\u0026 name) {\n    auto temp = getenv(name.data());\n    if (temp == nullptr) return false;\n    std::string result(temp);\n    std::transform(result.begin(), result.end(), result.begin(), [](unsigned char c) { return std::tolower(c); });\n    if (result == \"1\" || result == \"true\" || result == \"yes\" || result == \"y\" || result == \"on\" || result == \"enabled\") return true;\n    return false;\n}\n```\n\nThis checks if an environment variable is set to a value that you can interpret as \"true.\"\n\n### Requirements\n\n`\u003cstring_view\u003e`, `\u003cstring\u003e`\n\n### Example\n\n```cpp\nbool skip_some_process = envVarIsTrue(\"SKIP_SOME_PROCESS\");\nstd::cout \u003c\u003c skip_some_process \u003c\u003c std::endl;\n```\n\n## Find Levenshtein distance between any two strings\n\n```cpp\nsize_t levenshteinDistance(const std::string_view\u0026 one, const std::string_view\u0026 two) {\n    if (one == two) return 0;\n\n    if (one.empty()) return two.size();\n    if (two.empty()) return one.size();\n\n    std::vector\u003cstd::vector\u003csize_t\u003e\u003e matrix(one.size() + 1, std::vector\u003csize_t\u003e(two.size() + 1));\n\n    for (size_t i = 0; i \u003c= one.size(); i++)\n        matrix.at(i).at(0) = i;\n\n    for (size_t j = 0; j \u003c= two.size(); j++)\n        matrix.at(0).at(j) = j;\n\n    for (size_t i = 1; i \u003c= one.size(); i++) {\n        for (size_t j = 1; j \u003c= two.size(); j++) {\n            if (one.at(i - 1) == two.at(j - 1))\n                matrix.at(i).at(j) = matrix.at(i - 1).at(j - 1);\n            else\n                matrix.at(i).at(j) = std::min({matrix.at(i - 1).at(j - 1), matrix.at(i - 1).at(j), matrix.at(i).at(j - 1)}) + 1;\n        }\n    }\n\n    return matrix.at(one.size()).at(two.size());\n};\n```\n\nThis calculates the Levenshtein distance between two strings using any character as a valid difference.\n\n### Requirements\n\n`\u003cstring_view\u003e`, `\u003cvector\u003e`\n\n### Example\n\n```cpp\nauto difference = levenshteinDistance(\"hello\", \"hallo\");\n// difference = 1\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fslackadays%2Fsuperb-snippets","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fslackadays%2Fsuperb-snippets","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fslackadays%2Fsuperb-snippets/lists"}