mirror of
https://github.com/fish-shell/fish-shell
synced 2024-12-31 23:28:45 +00:00
common: Pass in length for wcs2str with wcstring
Removes _two_ wcslen calls.
This commit is contained in:
parent
d4db4f40a4
commit
6eccf6557f
1 changed files with 5 additions and 4 deletions
|
@ -330,9 +330,9 @@ wcstring str2wcstring(const std::string &in, size_t len) {
|
||||||
return str2wcs_internal(in.data(), len);
|
return str2wcs_internal(in.data(), len);
|
||||||
}
|
}
|
||||||
|
|
||||||
char *wcs2str(const wchar_t *in) {
|
char *wcs2str(const wchar_t *in, size_t len) {
|
||||||
if (!in) return NULL;
|
if (!in) return NULL;
|
||||||
size_t desired_size = MAX_UTF8_BYTES * wcslen(in) + 1;
|
size_t desired_size = MAX_UTF8_BYTES * len + 1;
|
||||||
char local_buff[512];
|
char local_buff[512];
|
||||||
if (desired_size <= sizeof local_buff / sizeof *local_buff) {
|
if (desired_size <= sizeof local_buff / sizeof *local_buff) {
|
||||||
// Convert into local buff, then use strdup() so we don't waste malloc'd space.
|
// Convert into local buff, then use strdup() so we don't waste malloc'd space.
|
||||||
|
@ -346,7 +346,7 @@ char *wcs2str(const wchar_t *in) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Here we probably allocate a buffer probably much larger than necessary.
|
// Here we probably allocate a buffer probably much larger than necessary.
|
||||||
char *out = (char *)malloc(MAX_UTF8_BYTES * wcslen(in) + 1);
|
char *out = (char *)malloc(MAX_UTF8_BYTES * len + 1);
|
||||||
assert(out);
|
assert(out);
|
||||||
// Instead of returning the return value of wcs2str_internal, return `out` directly.
|
// Instead of returning the return value of wcs2str_internal, return `out` directly.
|
||||||
// This eliminates false warnings in coverity about resource leaks.
|
// This eliminates false warnings in coverity about resource leaks.
|
||||||
|
@ -354,7 +354,8 @@ char *wcs2str(const wchar_t *in) {
|
||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
|
|
||||||
char *wcs2str(const wcstring &in) { return wcs2str(in.c_str()); }
|
char *wcs2str(const wchar_t *in) { return wcs2str(in, wcslen(in)); }
|
||||||
|
char *wcs2str(const wcstring &in) { return wcs2str(in.c_str(), in.length()); }
|
||||||
|
|
||||||
/// This function is distinguished from wcs2str_internal in that it allows embedded null bytes.
|
/// This function is distinguished from wcs2str_internal in that it allows embedded null bytes.
|
||||||
std::string wcs2string(const wcstring &input) {
|
std::string wcs2string(const wcstring &input) {
|
||||||
|
|
Loading…
Reference in a new issue