New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[feature] Add utf8ndup #41
Comments
What's the point in size_t bytes = utf8size(src);
bytes = 0; ? |
I think originally I intended to check to see if the new string will be smaller than the requested size. But this is literally the |
Also, you don't need two iterators ( |
void*
utf8ndup(const void* src, size_t n)
{
const char* s = (const char*)src;
char* c = 0;
// figure out how many bytes (including the terminator) we need to copy first
size_t bytes = utf8size(src);
if (n < bytes) {
c = (char*)malloc(n + 1);
} else {
c = (char*)malloc(bytes);
n = bytes;
}
if (!c) {
// out of memory so we bail
return 0;
}
bytes = 0;
// copy src byte-by-byte into our new utf8 string
while ('\0' != s[bytes] && bytes < n) {
c[bytes] = s[bytes];
bytes++;
}
// append null terminating byte
c[bytes] = '\0';
return c;
} |
Anyways, this could probably be better and probably share the code used in utf8dup if the string is shorter than the requested |
Thanks for looking at this! Two options:
I'm happy to do the work, but some people would rather there name was on the commit if they did the work! |
@sheredom I would be more than happy to submit a PR for this. Just wanted to test the waters here first. |
This is my current implementation. I am using it to replace all of my
strndup
sI don't know if this is desirable. I am almost just half tempted to
calloc
anmemcpy
the results.The text was updated successfully, but these errors were encountered: