{"id":21719880,"url":"https://github.com/adielmtz/str","last_synced_at":"2025-07-21T09:04:43.660Z","repository":{"id":51680361,"uuid":"411413681","full_name":"adielmtz/str","owner":"adielmtz","description":"Mutable string library for C","archived":false,"fork":false,"pushed_at":"2023-06-25T23:00:24.000Z","size":179,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-20T21:11:50.896Z","etag":null,"topics":["c","c-library","c-string","string","string-manipulation"],"latest_commit_sha":null,"homepage":"","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/adielmtz.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":"2021-09-28T19:28:55.000Z","updated_at":"2023-07-10T23:03:48.000Z","dependencies_parsed_at":"2024-11-26T02:00:53.331Z","dependency_job_id":null,"html_url":"https://github.com/adielmtz/str","commit_stats":null,"previous_names":["adielmtz/str"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/adielmtz/str","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/adielmtz%2Fstr","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/adielmtz%2Fstr/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/adielmtz%2Fstr/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/adielmtz%2Fstr/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/adielmtz","download_url":"https://codeload.github.com/adielmtz/str/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/adielmtz%2Fstr/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":266270409,"owners_count":23902734,"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","c-library","c-string","string","string-manipulation"],"created_at":"2024-11-26T01:42:51.479Z","updated_at":"2025-07-21T09:04:43.644Z","avatar_url":"https://github.com/adielmtz.png","language":"C","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Str\n\nA string library for C.\n\n## Usage\n\nCopy 'str.h' \u0026 'str.c' to your project directory and include it:\n\n```c\n#include \"str.h\"\n```\n\n## Initialization\n\nUse the functions `str_init()` or `str_init_size()` to initialize a Str object.\n\n```c\nStr str;\n\nstr_init(\u0026str);\n// or\nstr_init_size(\u0026str, 32);\n```\n\nAfter you're done working with your string, call `str_finalize()` to clean up resources:\n\n```c\nstr_finalize(\u0026str);\n```\n\nAnd lastly, with `str_copy()` you can make a copy of an existing Str object:\n\n```c\nStr first;\n\nstr_init(\u0026first);\nstr_append_str(\u0026first, \"Copy me!\", -1);\n\nStr second; // Must not be initialized otherwise a memory leak may occur!\nstr_copy(\u0026first, \u0026second);\n\n// Finalize both objects!\nstr_finalize(\u0026first);\nstr_finalize(\u0026second);\n```\n\n## String Properties\n\n### Get/Set the length the string\n\nStr exposes its `length` and `size` properties for read-only purposes. However, altering their values may result in\nunpredictable behaviour of the API which can cause a segfault.\n\nThe length of the string counts how many bytes represent the actual string contents in memory, and it does not count\nthe NULL terminator. To change the length of the string you must use the `str_set_length()` function:\n\n* If the value is greater than the original length, the gap/extra space will be filled with `\\0`.\n* If the value is less than the original length, the string will be truncated.\n\n```c\nstr_append_str(\u0026str, \"Hello world!\", -1);\n\nint64_t old_length = str.length; // 12\nstr_set_length(\u0026str, 5);         // Truncates to: \"Hello\"\nstr_set_length(\u0026str, 10);        // Expands to:   \"Hello\\0\\0\\0\\0\\0\"\n```\n\n### Get/Set the size of the string\n\nThe size of the Str object refers to the amount of allocated memory. This value is always larger than the\nlength of the string: `size \u003e= length + 1`.\n\nThe API keeps track of the allocated memory using the `size` field. To change it, you must use the `str_set_size()`\nfunction:\n\n* If the size is less than or equal to the length of the string, the length will be truncated to `size - 1`.\n\n```c\nstr_append_str(\u0026str, \"Watame is best sheep\", -1);\nint64_t old_size = str.size; // size=64\nstr_set_size(\u0026str, 4);       // Truncates to: \"Wat\" (length = size - 1)\n```\n\n## String comparison\n\n`str_compare`:\n\n* Returns a negative integer if the first string is less than the second string.\n* Returns a positive integer if the first string is greater than the second string.\n* Returns zero if both strings are equal.\n\n```c\nStr a, b;\nstr_init(\u0026a);\nstr_init(\u0026b);\n\nstr_append_char(\u0026a, 'A');\nstr_append_char(\u0026b, 'B');\n\nint64_t result = str_compare(\u0026a, \u0026b); // result = -1 : A \u003c B\n\nstr_finalize(\u0026a);\nstr_finalize(\u0026b);\n```\n\n`str_equals`:\n\n* Returns true if both strings are equal.\n\n```c\nStr a, b;\nstr_init(\u0026a);\nstr_init(\u0026b);\n\nstr_append_str(\u0026a, \"XYZ\", 3);\nstr_append_str(\u0026b, \"XYZ\", 3);\n\nbool result = str_equals(\u0026a, \u0026b); // result: true\n\nstr_finalize(\u0026a);\nstr_finalize(\u0026b);\n```\n\n## Concatenation\n\nYou can concatenate strings (and other types of values) using the `str_append_*()` functions:\n\n```c\n// Concatenate another Str object:\nstr_concat(\u0026str, \u0026other);\n\n// Concatenate a character:\nstr_append_char(\u0026str, 'C');\n\n// Concatenate a const string literal\nstr_append_str(\u0026str, \"Hello world!\", -1);\n\n// Concatenate a signed integer:\nstr_append_int(\u0026str, 2022);\nstr_append_int(\u0026str, -128);\n\n// Concatenate an unsigned integer:\nstr_append_uint(\u0026str, 4096);\n\n// Concatenate a float value:\nstr_append_float(\u0026str, M_PI, 4);\nstr_append_float(\u0026str, 100.30, 2);\n\n// Concatenate a formatted string:\nstr_append_format(\u0026str, \"Formatted %s are the %s!\", \"strings\", \"best\");\n```\n\nThis API is binary safe, you can append a string that contains NULL chars:\n\n```c\nstr_append_char(\u0026sb, '\\0'); // OK!\nstr_append_str(\u0026sb, \"Contains\\0NULL\\0chars!\", 20); // Works as long as you know the length\n```\n\n## Trim\n\nUse `str_trim()` function to trim whitespace off the string:\n\n```c\nstr str;\nstr_init(\u0026str);\n\nstr_append_str(\u0026str, \"    Padded String      \");\n\n// Trim left side\nstr_trim(\u0026str, STR_TRIM_LEFT); // \"Padded String      \"\n\n// Trim right side\nstr_trim(\u0026str, STR_TRIM_RIGHT); // \"    Padded String\"\n\n// Trim both sides\nstr_trim(\u0026str, STR_TRIM_BOTH); // \"Padded String\"\n\nstr_finalize(\u0026str);\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fadielmtz%2Fstr","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fadielmtz%2Fstr","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fadielmtz%2Fstr/lists"}