{"id":27867604,"url":"https://github.com/eteran/cxx17_printf","last_synced_at":"2025-05-04T22:50:30.722Z","repository":{"id":145240913,"uuid":"107591929","full_name":"eteran/cxx17_printf","owner":"eteran","description":null,"archived":false,"fork":false,"pushed_at":"2024-05-22T17:52:39.000Z","size":34,"stargazers_count":20,"open_issues_count":0,"forks_count":3,"subscribers_count":6,"default_branch":"master","last_synced_at":"2024-05-22T18:58:24.090Z","etag":null,"topics":["c-plus-plus","c-plus-plus-17","templates"],"latest_commit_sha":null,"homepage":null,"language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/eteran.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":"2017-10-19T19:55:24.000Z","updated_at":"2024-05-22T17:52:42.000Z","dependencies_parsed_at":null,"dependency_job_id":"aeebaa9d-162c-4151-8eb4-bb228321dc17","html_url":"https://github.com/eteran/cxx17_printf","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eteran%2Fcxx17_printf","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eteran%2Fcxx17_printf/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eteran%2Fcxx17_printf/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eteran%2Fcxx17_printf/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/eteran","download_url":"https://codeload.github.com/eteran/cxx17_printf/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252411807,"owners_count":21743604,"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-plus-plus","c-plus-plus-17","templates"],"created_at":"2025-05-04T22:50:29.649Z","updated_at":"2025-05-04T22:50:30.711Z","avatar_url":"https://github.com/eteran.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"# cxx17_printf\n\nAn implementation of `printf` using c++17's variadic templates. It was inspired \nby some of the \"safe printf\" examples that I've seen. But none of them attempted\nto actually implement the `printf` fully with all of its quirks. This code is \n(an attempt) to do that.\n\nAn advantage of this, is that we can support ANY type being passed to a format, \neven complex objects. For example, when `CXX17_PRINTF_EXTENSIONS` is enabled\n`\"%?\"` means \"call `to_string` on the argument and print the result\" This uses\nADL, so it will find the version that you specify in your namespaces, or use \n`std::to_string` as a fallback. If no `to_string` is found, it uses the internal\none which asserts.\n\nNOTE: floating point is not implemented, as it is complex to do correctly, but \n      is on the TODO list.\n\t  \nUsage is similar to `snprintf`, but more robust. Instead of a buffer/size pair\nbeing passed as a parameter, you pass a context object which has 3 functions\nand a data member:\n\n\t// writes a single character to the output of your choosing\n\tvoid write(char ch);\n\t\n\t// writes a string of characters to the output of your choosing\n\tvoid write(const char *p, size_t n);\n\t\n\t// called when formatting is complete, useful for ensuring NUL termination\n\tvoid done();\n\t\n\t// increment this every time we write is called, this is what printf will\n\t// return.\n\tsize_t written = 0;\n\t\n\t\nA simple example of the usage of the library is as follows:\n\n\tchar buf[256];\n\tcxx17::buffer_writer ctx(buf, sizeof(buf));\n\tcxx17::Printf(ctx, \"[hello %*s %d]\", 10, \"world\", 123);\n\t\n\t// buffer now contains \"[hello      world 123]\"\n\t\nThe context uses duck typing, so any object that meets the critera will suffice,\nbut there are several examples in Formatters.h\n\n--------\n\nAdditionally, while the context based interface is very flexible and can \naccomidate essentially any destination stream or buffer. This implementation\nalso includes more familiar interfaces:\n\n* `int cxx17::sprintf(std::ostream \u0026os, const char *format, const Ts \u0026... ts);`\n* `int cxx17::sprintf(char *str, size_t size, const char *format, const Ts \u0026... ts);`\n* `int cxx17::printf(const char *format, const Ts \u0026... ts);`\n\nAll of which work in the expected ways without the need to manually manage the \nconcept of \"contexts\".\n\n\n--------\n\nPerformance so far, when optimizations are at -O3, slightly edges out glibc's \nprintf. Here is the included test program's output on my machine:\n\n\thello      world, A, -123, 00001234 0x7ffff03cb7c8 0000004294967292 ffffffff 0000000000000100\n\thello      world, A, -123, 00001234 0x7ffff03cb7c8 0000004294967292 ffffffff 0000000000000100\n\tcxx17 Took:  366344 µs to execute.\n\tprintf Took: 384387 µs to execute.\n\n\nWhen profile guided optimizations are used, it performs even better. I am sure \nhowever, that there is room for some optimizations too :-)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Feteran%2Fcxx17_printf","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Feteran%2Fcxx17_printf","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Feteran%2Fcxx17_printf/lists"}